forked from databricks/databricks-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelemetryHelper.java
More file actions
361 lines (332 loc) · 15.7 KB
/
TelemetryHelper.java
File metadata and controls
361 lines (332 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package com.databricks.jdbc.telemetry;
import static com.databricks.jdbc.common.util.WildcardUtil.isNullOrEmpty;
import com.databricks.jdbc.api.internal.IDatabricksConnectionContext;
import com.databricks.jdbc.common.DatabricksClientConfiguratorManager;
import com.databricks.jdbc.common.safe.DatabricksDriverFeatureFlagsContextFactory;
import com.databricks.jdbc.common.util.DatabricksThreadContextHolder;
import com.databricks.jdbc.common.util.DriverUtil;
import com.databricks.jdbc.common.util.StringUtil;
import com.databricks.jdbc.exception.DatabricksParsingException;
import com.databricks.jdbc.log.JdbcLogger;
import com.databricks.jdbc.log.JdbcLoggerFactory;
import com.databricks.jdbc.model.telemetry.*;
import com.databricks.jdbc.model.telemetry.latency.ChunkDetails;
import com.databricks.sdk.core.DatabricksConfig;
import com.databricks.sdk.core.ProxyConfig;
import com.databricks.sdk.core.UserAgent;
import com.google.common.annotations.VisibleForTesting;
import java.nio.charset.Charset;
import java.time.Instant;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public class TelemetryHelper {
private static final JdbcLogger LOGGER = JdbcLoggerFactory.getLogger(TelemetryHelper.class);
// Cache to store unique DriverConnectionParameters for each connectionUuid
private static final ConcurrentHashMap<String, DriverConnectionParameters>
connectionParameterCache = new ConcurrentHashMap<>();
@VisibleForTesting
static final String TELEMETRY_FEATURE_FLAG_NAME =
"databricks.partnerplatform.clientConfigsFeatureFlags.enableTelemetry";
private static final DriverSystemConfiguration DRIVER_SYSTEM_CONFIGURATION =
new DriverSystemConfiguration()
.setCharSetEncoding(Charset.defaultCharset().displayName())
.setDriverName(DriverUtil.getDriverName())
.setDriverVersion(DriverUtil.getDriverVersion())
.setLocaleName(
System.getProperty("user.language") + '_' + System.getProperty("user.country"))
.setRuntimeVendor(System.getProperty("java.vendor"))
.setRuntimeVersion(System.getProperty("java.version"))
.setRuntimeName(System.getProperty("java.vm.name"))
.setOsArch(System.getProperty("os.arch"))
.setOsVersion(System.getProperty("os.version"))
.setOsName(System.getProperty("os.name"))
.setClientAppName(null);
public static DriverSystemConfiguration getDriverSystemConfiguration() {
return DRIVER_SYSTEM_CONFIGURATION;
}
public static void updateClientAppName(String clientAppName) {
if (!isNullOrEmpty(clientAppName)) {
DRIVER_SYSTEM_CONFIGURATION.setClientAppName(clientAppName);
}
}
public static boolean isTelemetryAllowedForConnection(IDatabricksConnectionContext context) {
if (context.forceEnableTelemetry()) {
return true;
}
return context != null
&& context.isTelemetryEnabled()
&& DatabricksDriverFeatureFlagsContextFactory.getInstance(context)
.isFeatureEnabled(TELEMETRY_FEATURE_FLAG_NAME);
}
public static void exportInitialTelemetryLog(IDatabricksConnectionContext connectionContext) {
if (connectionContext == null) {
return;
}
TelemetryFrontendLog telemetryFrontendLog =
new TelemetryFrontendLog()
.setFrontendLogEventId(getEventUUID())
.setContext(getLogContext())
.setEntry(
new FrontendLogEntry()
.setSqlDriverLog(
new TelemetryEvent()
.setDriverConnectionParameters(
getDriverConnectionParameter(connectionContext))
.setDriverSystemConfiguration(getDriverSystemConfiguration())));
TelemetryClientFactory.getInstance()
.getTelemetryClient(connectionContext)
.exportEvent(telemetryFrontendLog);
}
public static void exportFailureLog(
IDatabricksConnectionContext connectionContext, String errorName, String errorMessage) {
exportFailureLog(
connectionContext,
errorName,
errorMessage,
null,
DatabricksThreadContextHolder.getStatementId());
}
public static void exportFailureLog(
IDatabricksConnectionContext connectionContext,
String errorName,
String errorMessage,
Long chunkIndex,
String statementId) {
// Connection context is not set in following scenarios:
// a. Unit tests
// b. When Url parsing has failed
// In either of these scenarios, we don't export logs
if (connectionContext != null) {
DriverErrorInfo errorInfo =
new DriverErrorInfo().setErrorName(errorName).setStackTrace(errorMessage);
TelemetryFrontendLog telemetryFrontendLog =
new TelemetryFrontendLog()
.setFrontendLogEventId(getEventUUID())
.setContext(getLogContext())
.setEntry(
new FrontendLogEntry()
.setSqlDriverLog(
new TelemetryEvent()
.setSqlStatementId(statementId)
.setDriverConnectionParameters(
getDriverConnectionParameter(connectionContext))
.setDriverErrorInfo(errorInfo)
.setDriverSystemConfiguration(getDriverSystemConfiguration())));
if (chunkIndex != null) {
// When chunkIndex is provided, we are exporting a chunk download failure log
telemetryFrontendLog
.getEntry()
.getSqlDriverLog()
.setSqlOperation(new SqlExecutionEvent().setChunkId(chunkIndex));
}
ITelemetryClient client =
TelemetryClientFactory.getInstance().getTelemetryClient(connectionContext);
client.exportEvent(telemetryFrontendLog);
}
}
public static void exportLatencyLog(long executionTime) {
SqlExecutionEvent executionEvent =
new SqlExecutionEvent()
.setDriverStatementType(DatabricksThreadContextHolder.getStatementType())
.setRetryCount(DatabricksThreadContextHolder.getRetryCount());
exportLatencyLog(
DatabricksThreadContextHolder.getConnectionContext(),
executionTime,
executionEvent,
DatabricksThreadContextHolder.getStatementId(),
DatabricksThreadContextHolder.getSessionId());
}
public static void exportChunkLatencyTelemetry(ChunkDetails chunkDetails, String statementId) {
if (chunkDetails == null) {
return;
}
IDatabricksConnectionContext connectionContext =
DatabricksThreadContextHolder.getConnectionContext();
if (connectionContext == null) {
return;
}
SqlExecutionEvent sqlExecutionEvent = new SqlExecutionEvent().setChunkDetails(chunkDetails);
TelemetryEvent telemetryEvent =
new TelemetryEvent()
.setSqlOperation(sqlExecutionEvent)
.setDriverConnectionParameters(getDriverConnectionParameter(connectionContext));
TelemetryFrontendLog telemetryFrontendLog =
new TelemetryFrontendLog()
.setFrontendLogEventId(getEventUUID())
.setContext(getLogContext())
.setEntry(new FrontendLogEntry().setSqlDriverLog(telemetryEvent));
TelemetryClientFactory.getInstance()
.getTelemetryClient(connectionContext)
.exportEvent(telemetryFrontendLog);
}
@VisibleForTesting
static void exportLatencyLog(
IDatabricksConnectionContext connectionContext,
long latencyMilliseconds,
SqlExecutionEvent executionEvent,
String statementId,
String sessionId) {
// Though we already handle null connectionContext in the downstream implementation,
// we are adding this check for extra sanity
if (connectionContext != null) {
TelemetryEvent telemetryEvent =
new TelemetryEvent()
.setLatency(latencyMilliseconds)
.setSqlOperation(executionEvent)
.setDriverConnectionParameters(getDriverConnectionParameter(connectionContext))
.setSqlStatementId(statementId)
.setSessionId(sessionId);
TelemetryFrontendLog telemetryFrontendLog =
new TelemetryFrontendLog()
.setFrontendLogEventId(getEventUUID())
.setContext(getLogContext())
.setEntry(new FrontendLogEntry().setSqlDriverLog(telemetryEvent));
TelemetryClientFactory.getInstance()
.getTelemetryClient(connectionContext)
.exportEvent(telemetryFrontendLog);
}
}
public static void exportLatencyLog(
IDatabricksConnectionContext connectionContext,
long latencyMilliseconds,
DriverVolumeOperation volumeOperationEvent) {
// Though we already handle null connectionContext in the downstream implementation,
// we are adding this check for extra sanity
if (connectionContext != null) {
TelemetryFrontendLog telemetryFrontendLog =
new TelemetryFrontendLog()
.setFrontendLogEventId(getEventUUID())
.setContext(getLogContext())
.setEntry(
new FrontendLogEntry()
.setSqlDriverLog(
new TelemetryEvent()
.setLatency(latencyMilliseconds)
.setVolumeOperation(volumeOperationEvent)
.setDriverConnectionParameters(
getDriverConnectionParameter(connectionContext))));
TelemetryClientFactory.getInstance()
.getTelemetryClient(connectionContext)
.exportEvent(telemetryFrontendLog);
}
}
private static DriverConnectionParameters getDriverConnectionParameter(
IDatabricksConnectionContext connectionContext) {
if (connectionContext == null) {
return null;
}
return connectionParameterCache.computeIfAbsent(
connectionContext.getConnectionUuid(),
uuid -> buildDriverConnectionParameters(connectionContext));
}
private static DriverConnectionParameters buildDriverConnectionParameters(
IDatabricksConnectionContext connectionContext) {
String hostUrl;
try {
hostUrl = connectionContext.getHostUrl();
} catch (DatabricksParsingException e) {
hostUrl = "Error in parsing host url";
}
DriverConnectionParameters connectionParameters =
new DriverConnectionParameters()
.setHostDetails(getHostDetails(hostUrl))
.setUseProxy(connectionContext.getUseProxy())
.setAuthMech(connectionContext.getAuthMech())
.setAuthScope(connectionContext.getAuthScope())
.setUseSystemProxy(connectionContext.getUseSystemProxy())
.setUseCfProxy(connectionContext.getUseCloudFetchProxy())
.setDriverAuthFlow(connectionContext.getAuthFlow())
.setDiscoveryModeEnabled(connectionContext.isOAuthDiscoveryModeEnabled())
.setDiscoveryUrl(connectionContext.getOAuthDiscoveryURL())
.setIdentityFederationClientId(connectionContext.getIdentityFederationClientId())
.setUseEmptyMetadata(connectionContext.getUseEmptyMetadata())
.setSupportManyParameters(connectionContext.supportManyParameters())
.setGoogleCredentialFilePath(connectionContext.getGoogleCredentials())
.setGoogleServiceAccount(connectionContext.getGoogleServiceAccount())
.setAllowedVolumeIngestionPaths(connectionContext.getVolumeOperationAllowedPaths())
.setSocketTimeout(connectionContext.getSocketTimeout())
.setStringColumnLength(connectionContext.getDefaultStringColumnLength())
.setEnableComplexDatatypeSupport(connectionContext.isComplexDatatypeSupportEnabled())
.setAzureWorkspaceResourceId(connectionContext.getAzureWorkspaceResourceId())
.setAzureTenantId(connectionContext.getAzureTenantId())
.setSslTrustStoreType(connectionContext.getSSLTrustStoreType())
.setEnableArrow(connectionContext.shouldEnableArrow())
.setEnableDirectResults(connectionContext.getDirectResultMode())
.setCheckCertificateRevocation(connectionContext.checkCertificateRevocation())
.setAcceptUndeterminedCertificateRevocation(
connectionContext.acceptUndeterminedCertificateRevocation())
.setDriverMode(connectionContext.getClientType().toString())
.setAuthEndpoint(connectionContext.getAuthEndpoint())
.setTokenEndpoint(connectionContext.getTokenEndpoint())
.setNonProxyHosts(StringUtil.split(connectionContext.getNonProxyHosts()))
.setHttpConnectionPoolSize(connectionContext.getHttpConnectionPoolSize())
.setEnableSeaHybridResults(connectionContext.isSqlExecHybridResultsEnabled())
.setAllowSelfSignedSupport(connectionContext.allowSelfSignedCerts())
.setUseSystemTrustStore(connectionContext.useSystemTrustStore())
.setRowsFetchedPerBlock(connectionContext.getRowsFetchedPerBlock())
.setAsyncPollIntervalMillis(connectionContext.getAsyncExecPollInterval())
.setEnableTokenCache(connectionContext.isTokenCacheEnabled())
.setHttpPath(connectionContext.getHttpPath());
if (connectionContext.useJWTAssertion()) {
connectionParameters
.setEnableJwtAssertion(true)
.setJwtAlgorithm(connectionContext.getJWTAlgorithm())
.setJwtKeyFile(connectionContext.getJWTKeyFile());
}
if (connectionContext.getUseCloudFetchProxy()) {
connectionParameters.setCfProxyHostDetails(
getHostDetails(
connectionContext.getCloudFetchProxyHost(),
connectionContext.getCloudFetchProxyPort(),
connectionContext.getCloudFetchProxyAuthType()));
}
if (connectionContext.getUseProxy()) {
HostDetails hostDetails =
getHostDetails(
connectionContext.getProxyHost(),
connectionContext.getProxyPort(),
connectionContext.getProxyAuthType());
hostDetails.setNonProxyHosts(connectionContext.getNonProxyHosts());
connectionParameters.setProxyHostDetails(hostDetails);
} else if (connectionContext.getUseSystemProxy()) {
String protocol = System.getProperty("https.proxyHost") != null ? "https" : "http";
connectionParameters.setProxyHostDetails(
getHostDetails(
System.getProperty(protocol + ".proxyHost"),
Integer.parseInt(System.getProperty(protocol + ".proxyPort")),
connectionContext.getProxyAuthType()));
}
return connectionParameters;
}
private static String getEventUUID() {
return UUID.randomUUID().toString();
}
private static FrontendLogContext getLogContext() {
return new FrontendLogContext()
.setClientContext(
new TelemetryClientContext()
.setTimestampMillis(Instant.now().toEpochMilli())
.setUserAgent(UserAgent.asString()));
}
private static HostDetails getHostDetails(
String host, int port, ProxyConfig.ProxyAuthType proxyAuthType) {
return new HostDetails().setHostUrl(host).setPort(port).setProxyType(proxyAuthType);
}
private static HostDetails getHostDetails(String host) {
return new HostDetails().setHostUrl(host);
}
public static DatabricksConfig getDatabricksConfigSafely(IDatabricksConnectionContext context) {
try {
return DatabricksClientConfiguratorManager.getInstance()
.getConfigurator(context)
.getDatabricksConfig();
} catch (Exception e) {
String errorMessage =
String.format(
"Unable to get databricks config for telemetry helper; falling back to no-auth. Error: %s; Context: %s",
e.getMessage(), context);
LOGGER.debug(errorMessage);
return null;
}
}
}