|
2 | 2 |
|
3 | 3 | import com.databricks.jdbc.client.DatabricksHttpException; |
4 | 4 | import com.databricks.jdbc.client.http.DatabricksHttpClient; |
| 5 | +import com.databricks.jdbc.commons.LogLevel; |
| 6 | +import com.databricks.jdbc.commons.util.LoggingUtil; |
5 | 7 | import com.databricks.jdbc.core.DatabricksSQLException; |
6 | 8 | import com.databricks.jdbc.driver.IDatabricksConnectionContext; |
7 | 9 | import com.fasterxml.jackson.databind.ObjectMapper; |
|
17 | 19 | import org.apache.http.client.utils.URIBuilder; |
18 | 20 | import org.apache.http.util.EntityUtils; |
19 | 21 |
|
20 | | -// TODO (Bhuvan) Flush out metrics once the driver connection is closed |
21 | | -public class DatabricksMetrics { |
| 22 | +public class DatabricksMetrics implements AutoCloseable { |
22 | 23 | private final String URL = |
23 | 24 | "https://aa87314c1e33d4c1f91a919f8cf9c4ba-387609431.us-west-2.elb.amazonaws.com:443/api/2.0/oss-sql-driver-telemetry/metrics"; |
24 | | - public final Map<String, Double> gaugeMetrics = new HashMap<>(); |
25 | | - public final Map<String, Double> counterMetrics = new HashMap<>(); |
| 25 | + private final Map<String, Double> gaugeMetrics = new HashMap<>(); |
| 26 | + private final Map<String, Double> counterMetrics = new HashMap<>(); |
26 | 27 | private final long intervalDurationForSendingReq = |
27 | 28 | TimeUnit.SECONDS.toMillis(10 * 60); // 10 minutes |
28 | 29 | private final ObjectMapper objectMapper = new ObjectMapper(); |
@@ -156,4 +157,19 @@ public void increment(String name, double value) { |
156 | 157 | incCounterMetrics(name + "_" + workspaceId, value); |
157 | 158 | if (!hasInitialExportOccurred) initialExport(counterMetrics, MetricsType.COUNTER); |
158 | 159 | } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void close() { |
| 163 | + // Flush out metrics when connection is closed |
| 164 | + if (telemetryClient != null) { |
| 165 | + try { |
| 166 | + sendRequest(gaugeMetrics, DatabricksMetrics.MetricsType.GAUGE); |
| 167 | + sendRequest(counterMetrics, DatabricksMetrics.MetricsType.COUNTER); |
| 168 | + } catch (Exception e) { |
| 169 | + LoggingUtil.log( |
| 170 | + LogLevel.DEBUG, |
| 171 | + "Failed to export metrics when connection is closed. Error: " + e.getMessage()); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
159 | 175 | } |
0 commit comments