-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathDatabricksTelemetryExporter.ts
More file actions
392 lines (352 loc) · 14.1 KB
/
DatabricksTelemetryExporter.ts
File metadata and controls
392 lines (352 loc) · 14.1 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* Copyright (c) 2025 Databricks Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { v4 as uuidv4 } from 'uuid';
import fetch, { RequestInit, Response, Request } from 'node-fetch';
import IClientContext from '../contracts/IClientContext';
import { LogLevel } from '../contracts/IDBSQLLogger';
import IAuthentication from '../connection/contracts/IAuthentication';
import AuthenticationError from '../errors/AuthenticationError';
import HiveDriverError from '../errors/HiveDriverError';
import { TelemetryMetric, DEFAULT_TELEMETRY_CONFIG } from './types';
import { CircuitBreaker, CircuitBreakerOpenError, CircuitBreakerRegistry } from './CircuitBreaker';
import ExceptionClassifier from './ExceptionClassifier';
import {
buildTelemetryUrl,
hasAuthorization,
normalizeHeaders,
redactSensitive,
sanitizeProcessName,
} from './telemetryUtils';
import buildUserAgentString from '../utils/buildUserAgentString';
interface DatabricksTelemetryLog {
workspace_id?: string;
frontend_log_event_id: string;
context: {
client_context: {
timestamp_millis: number;
user_agent: string;
};
};
entry: {
sql_driver_log: {
session_id?: string;
sql_statement_id?: string;
system_configuration?: {
driver_version?: string;
runtime_name?: string;
runtime_version?: string;
runtime_vendor?: string;
os_name?: string;
os_version?: string;
os_arch?: string;
driver_name?: string;
client_app_name?: string;
locale_name?: string;
char_set_encoding?: string;
process_name?: string;
};
operation_latency_ms?: number;
sql_operation?: {
execution_result?: string;
chunk_details?: {
total_chunks_present?: number;
total_chunks_iterated?: number;
initial_chunk_latency_millis?: number;
slowest_chunk_latency_millis?: number;
sum_chunks_download_time_millis?: number;
};
};
error_info?: {
error_name: string;
stack_trace: string;
};
};
};
}
/**
* Thrown for non-credential terminal telemetry failures (e.g. refusal to
* export to an invalid host). Separate from `AuthenticationError` so the
* classifier can keep the "short-circuit, don't retry, count as breaker
* failure" contract without muddying the auth taxonomy used by the rest of
* the driver.
*/
export class TelemetryTerminalError extends HiveDriverError {
readonly terminal = true as const;
}
/**
* Exports telemetry metrics to the Databricks telemetry service.
*
* CRITICAL: export() never throws — all errors are swallowed and logged at
* LogLevel.debug (the one exception is a single warn on the first observed
* auth-missing, re-armed on recovery).
*/
export default class DatabricksTelemetryExporter {
private readonly circuitBreaker: CircuitBreaker;
private readonly authenticatedUserAgent: string;
/** User-Agent used for the unauthenticated endpoint; strips any
* caller-supplied `userAgentEntry` that could identify the customer. */
private readonly unauthenticatedUserAgent: string;
private authMissingWarned = false;
constructor(
private context: IClientContext,
private host: string,
private circuitBreakerRegistry: CircuitBreakerRegistry,
private authProvider?: IAuthentication,
) {
this.circuitBreaker = circuitBreakerRegistry.getCircuitBreaker(host);
const config = this.context.getConfig();
this.authenticatedUserAgent = buildUserAgentString(config.userAgentEntry);
this.unauthenticatedUserAgent = buildUserAgentString(undefined);
}
/**
* Release the per-host circuit breaker. Intended for the owning client's
* close() path.
*
* NOTE: `CircuitBreakerRegistry` currently shares one breaker per host
* across consumers; calling this while another consumer is active will
* reset their failure-count memory. The owning-client is expected to be
* the last consumer on its host; multi-consumer refcounting on the
* registry will land in the consumer-wiring PR.
*/
dispose(): void {
this.circuitBreakerRegistry.removeCircuitBreaker(this.host);
}
async export(metrics: TelemetryMetric[]): Promise<void> {
if (!metrics || metrics.length === 0) {
return;
}
const logger = this.context.getLogger();
try {
await this.circuitBreaker.execute(() => this.exportWithRetry(metrics));
} catch (error: any) {
if (error instanceof CircuitBreakerOpenError) {
logger.log(LogLevel.debug, 'Circuit breaker OPEN - dropping telemetry');
} else if (error instanceof AuthenticationError) {
logger.log(LogLevel.debug, `Telemetry export auth failure: ${error.message}`);
} else if (error instanceof TelemetryTerminalError) {
logger.log(LogLevel.debug, `Telemetry export refused: ${error.message}`);
} else {
logger.log(LogLevel.debug, `Telemetry export error: ${error?.message ?? error}`);
}
}
}
/**
* Retry wrapper shaped after HttpRetryPolicy: retries only on errors
* classified as retryable by ExceptionClassifier, stops on terminal ones,
* surfaces the last error to the circuit breaker.
*
* `maxRetries` is the number of retries *after* the first attempt (i.e.
* attempts = maxRetries + 1), matching HttpRetryPolicy's semantics.
*/
private async exportWithRetry(metrics: TelemetryMetric[]): Promise<void> {
const config = this.context.getConfig();
const logger = this.context.getLogger();
const rawMaxRetries = config.telemetryMaxRetries ?? DEFAULT_TELEMETRY_CONFIG.maxRetries;
const maxRetries =
Number.isFinite(rawMaxRetries) && rawMaxRetries >= 0 ? rawMaxRetries : DEFAULT_TELEMETRY_CONFIG.maxRetries;
const baseMs = config.telemetryBackoffBaseMs ?? DEFAULT_TELEMETRY_CONFIG.backoffBaseMs;
const maxMs = config.telemetryBackoffMaxMs ?? DEFAULT_TELEMETRY_CONFIG.backoffMaxMs;
const jitterMs = config.telemetryBackoffJitterMs ?? DEFAULT_TELEMETRY_CONFIG.backoffJitterMs;
const totalAttempts = maxRetries + 1;
let lastError: Error | null = null;
/* eslint-disable no-await-in-loop */
for (let attempt = 0; attempt < totalAttempts; attempt += 1) {
try {
await this.exportInternal(metrics);
return;
} catch (error: any) {
lastError = error;
if (
error instanceof AuthenticationError ||
error instanceof TelemetryTerminalError ||
ExceptionClassifier.isTerminal(error)
) {
throw error;
}
if (!ExceptionClassifier.isRetryable(error)) {
throw error;
}
if (attempt >= totalAttempts - 1) {
throw error;
}
const base = Math.min(baseMs * 2 ** attempt, maxMs);
const jitter = Math.random() * jitterMs;
const delay = Math.min(base + jitter, maxMs);
// Include the failing error so ops can see what's being retried,
// not just the cadence.
logger.log(
LogLevel.debug,
`Retrying telemetry export (attempt ${attempt + 1}/${totalAttempts}) after ${Math.round(delay)}ms: ${
error?.statusCode ?? ''
} ${redactSensitive(error?.message ?? '')}`,
);
await this.sleep(delay);
}
}
/* eslint-enable no-await-in-loop */
if (lastError) {
throw lastError;
}
}
private async exportInternal(metrics: TelemetryMetric[]): Promise<void> {
const config = this.context.getConfig();
const logger = this.context.getLogger();
const authenticatedExport = config.telemetryAuthenticatedExport ?? DEFAULT_TELEMETRY_CONFIG.authenticatedExport;
const endpoint = buildTelemetryUrl(this.host, authenticatedExport ? '/telemetry-ext' : '/telemetry-unauth');
if (!endpoint) {
// Malformed / deny-listed host — drop the batch rather than letting
// it target an attacker-controlled destination.
throw new TelemetryTerminalError('Refusing telemetry export: host failed validation');
}
const userAgent = authenticatedExport ? this.authenticatedUserAgent : this.unauthenticatedUserAgent;
let headers: Record<string, string> = {
'Content-Type': 'application/json',
'User-Agent': userAgent,
};
if (authenticatedExport) {
headers = { ...headers, ...(await this.getAuthHeaders()) };
if (!hasAuthorization(headers)) {
if (!this.authMissingWarned) {
this.authMissingWarned = true;
logger.log(LogLevel.warn, 'Telemetry: Authorization header missing — metrics will be dropped');
}
throw new AuthenticationError('Telemetry export: missing Authorization header');
}
}
const protoLogs = metrics.map((m) => this.toTelemetryLog(m, authenticatedExport, userAgent));
const body = JSON.stringify({
uploadTime: Date.now(),
items: [],
protoLogs: protoLogs.map((log) => JSON.stringify(log)),
});
logger.log(
LogLevel.debug,
`Exporting ${metrics.length} telemetry metrics to ${
authenticatedExport ? 'authenticated' : 'unauthenticated'
} endpoint`,
);
const response = await this.sendRequest(endpoint, {
method: 'POST',
headers,
body,
timeout: 10000,
});
if (!response.ok) {
await response.text().catch(() => {});
const error: any = new Error(`Telemetry export failed: ${response.status} ${response.statusText}`);
error.statusCode = response.status;
throw error;
}
await response.text().catch(() => {});
// Successful round-trip re-arms the "auth missing" warn so operators see
// a fresh signal the next time auth breaks.
this.authMissingWarned = false;
logger.log(LogLevel.debug, `Successfully exported ${metrics.length} telemetry metrics`);
}
private async getAuthHeaders(): Promise<Record<string, string>> {
if (!this.authProvider) {
return {};
}
const logger = this.context.getLogger();
try {
return normalizeHeaders(await this.authProvider.authenticate());
} catch (error: any) {
logger.log(LogLevel.debug, `Telemetry: auth provider threw: ${error?.message ?? error}`);
return {};
}
}
private async sendRequest(url: string, init: RequestInit): Promise<Response> {
const connectionProvider = await this.context.getConnectionProvider();
const agent = await connectionProvider.getAgent();
const retryPolicy = await connectionProvider.getRetryPolicy();
const requestConfig: RequestInit = { agent, ...init };
const result = await retryPolicy.invokeWithRetry(() => {
const request = new Request(url, requestConfig);
return fetch(request).then((response) => ({ request, response }));
});
return result.response;
}
private toTelemetryLog(
metric: TelemetryMetric,
authenticatedExport: boolean,
userAgent: string,
): DatabricksTelemetryLog {
// Unauthenticated export must not ship correlation IDs, fingerprint
// data, or raw error detail — an on-path observer could otherwise link
// sessions → workspaces → user activity without any auth.
const includeCorrelation = authenticatedExport;
const log: DatabricksTelemetryLog = {
workspace_id: includeCorrelation ? metric.workspaceId : undefined,
frontend_log_event_id: uuidv4(),
context: {
client_context: {
timestamp_millis: metric.timestamp,
user_agent: userAgent,
},
},
entry: {
sql_driver_log: {
session_id: includeCorrelation ? metric.sessionId : undefined,
sql_statement_id: includeCorrelation ? metric.statementId : undefined,
},
},
};
if (metric.metricType === 'connection' && metric.driverConfig && includeCorrelation) {
// system_configuration is a high-entropy client fingerprint (OS, arch,
// locale, process, runtime). Only ship on the authenticated path.
log.entry.sql_driver_log.system_configuration = {
driver_version: metric.driverConfig.driverVersion,
driver_name: metric.driverConfig.driverName,
runtime_name: 'Node.js',
runtime_version: metric.driverConfig.nodeVersion,
runtime_vendor: metric.driverConfig.runtimeVendor,
os_name: metric.driverConfig.platform,
os_version: metric.driverConfig.osVersion,
os_arch: metric.driverConfig.osArch,
locale_name: metric.driverConfig.localeName,
char_set_encoding: metric.driverConfig.charSetEncoding,
process_name: sanitizeProcessName(metric.driverConfig.processName) || undefined,
};
} else if (metric.metricType === 'statement') {
log.entry.sql_driver_log.operation_latency_ms = metric.latencyMs;
if (metric.resultFormat || metric.chunkCount) {
log.entry.sql_driver_log.sql_operation = {
execution_result: metric.resultFormat,
};
if (metric.chunkCount && metric.chunkCount > 0) {
log.entry.sql_driver_log.sql_operation.chunk_details = {
total_chunks_present: metric.chunkCount,
total_chunks_iterated: metric.chunkCount,
};
}
}
} else if (metric.metricType === 'error') {
const stackOrMessage = metric.errorStack ?? metric.errorMessage ?? '';
log.entry.sql_driver_log.error_info = {
error_name: metric.errorName || 'UnknownError',
// Redact common secret shapes and cap length. On the unauth path we
// keep only the error class — no message body.
stack_trace: includeCorrelation ? redactSensitive(stackOrMessage) : '',
};
}
return log;
}
private sleep(ms: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
}