11package com .databricks .jdbc .telemetry ;
22
3+ import static com .databricks .jdbc .driver .DatabricksJdbcConstants .TELEMETRY_LOG_LEVEL ;
4+
35import com .databricks .jdbc .client .http .DatabricksHttpClient ;
4- import com .databricks .jdbc .commons .LogLevel ;
56import com .databricks .jdbc .commons .util .LoggingUtil ;
67import com .databricks .jdbc .core .DatabricksSQLException ;
78import com .databricks .jdbc .driver .IDatabricksConnectionContext ;
@@ -36,7 +37,7 @@ private void setWorkspaceId(String workspaceId) {
3637 this .workspaceId = workspaceId ;
3738 }
3839
39- public enum MetricsType {
40+ private enum MetricsType {
4041 GAUGE ,
4142 COUNTER
4243 }
@@ -62,8 +63,8 @@ public void run() {
6263 }
6364
6465 public DatabricksMetrics (IDatabricksConnectionContext context ) throws DatabricksSQLException {
65- if (context != null && context .enableTelemetry ()) {
66- enableTelemetry = true ;
66+ enableTelemetry = (context != null && context .enableTelemetry ());
67+ if ( enableTelemetry ) {
6768 String resourceId = context .getComputeResource ().getWorkspaceId ();
6869 setWorkspaceId (resourceId );
6970 this .telemetryClient = DatabricksHttpClient .getInstance (context );
@@ -72,10 +73,13 @@ public DatabricksMetrics(IDatabricksConnectionContext context) throws Databricks
7273 }
7374
7475 private void sendRequest (Map <String , Double > map , MetricsType metricsType ) {
76+ if (!enableTelemetry ) {
77+ return ;
78+ }
7579 // Check if the telemetry client is set
7680 if (telemetryClient == null ) {
7781 LoggingUtil .log (
78- LogLevel . DEBUG ,
82+ TELEMETRY_LOG_LEVEL ,
7983 "Telemetry client is not set for resource Id: "
8084 + workspaceId
8185 + ". Initialize the Driver first." );
@@ -99,10 +103,10 @@ private void sendRequest(Map<String, Double> map, MetricsType metricsType) {
99103
100104 // Error handling
101105 if (response == null ) {
102- LoggingUtil .log (LogLevel . DEBUG , "Response is null for metrics export." );
106+ LoggingUtil .log (TELEMETRY_LOG_LEVEL , "Response is null for metrics export." );
103107 } else if (response .getStatusLine ().getStatusCode () != 200 ) {
104108 LoggingUtil .log (
105- LogLevel . DEBUG ,
109+ TELEMETRY_LOG_LEVEL ,
106110 "Response code for metrics export: "
107111 + response .getStatusLine ().getStatusCode ()
108112 + " Response: "
@@ -112,11 +116,11 @@ private void sendRequest(Map<String, Double> map, MetricsType metricsType) {
112116 map .clear ();
113117
114118 // Get the response string
115- LoggingUtil .log (LogLevel . DEBUG , EntityUtils .toString (response .getEntity ()));
119+ LoggingUtil .log (TELEMETRY_LOG_LEVEL , EntityUtils .toString (response .getEntity ()));
116120 response .close ();
117121 }
118122 } catch (Exception e ) {
119- LoggingUtil .log (LogLevel . DEBUG , "Failed to export metrics. Error: " + e .getMessage ());
123+ LoggingUtil .log (TELEMETRY_LOG_LEVEL , "Failed to export metrics. Error: " + e .getMessage ());
120124 }
121125 }
122126 }
@@ -144,7 +148,7 @@ private void initialExport(Map<String, Double> map, MetricsType metricsType) {
144148 sendRequest (map , metricsType );
145149 } catch (Exception e ) {
146150 // Commenting out the exception for now - failing silently
147- // System.out.println( e.getMessage());
151+ LoggingUtil . log ( TELEMETRY_LOG_LEVEL , "Initial export failed. Error: " + e .getMessage ());
148152 }
149153 });
150154 }
@@ -168,13 +172,13 @@ public void increment(String name, double value) {
168172 @ Override
169173 public void close () {
170174 // Flush out metrics when connection is closed
171- if (telemetryClient != null ) {
175+ if (enableTelemetry && telemetryClient != null ) {
172176 try {
173177 sendRequest (gaugeMetrics , DatabricksMetrics .MetricsType .GAUGE );
174178 sendRequest (counterMetrics , DatabricksMetrics .MetricsType .COUNTER );
175179 } catch (Exception e ) {
176180 LoggingUtil .log (
177- LogLevel . DEBUG ,
181+ TELEMETRY_LOG_LEVEL ,
178182 "Failed to export metrics when connection is closed. Error: " + e .getMessage ());
179183 }
180184 }
0 commit comments