-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathMetricsAggregator.ts
More file actions
434 lines (372 loc) · 14.3 KB
/
MetricsAggregator.ts
File metadata and controls
434 lines (372 loc) · 14.3 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/**
* 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 IClientContext from '../contracts/IClientContext';
import { LogLevel } from '../contracts/IDBSQLLogger';
import { TelemetryEvent, TelemetryEventType, TelemetryMetric, DEFAULT_TELEMETRY_CONFIG } from './types';
import DatabricksTelemetryExporter from './DatabricksTelemetryExporter';
interface StatementTelemetryDetails {
statementId: string;
sessionId: string;
workspaceId?: string;
operationType?: string;
startTime: number;
executionLatencyMs?: number;
resultFormat?: string;
chunkCount: number;
bytesDownloaded: number;
pollCount: number;
compressionEnabled?: boolean;
errors: TelemetryEvent[];
}
/**
* Aggregates telemetry events by statement_id and manages batching/flushing.
*
* Overflow policy — when the pending buffer hits `maxPendingMetrics`, error
* metrics are preserved preferentially over connection/statement metrics.
* The first-failure error is usually the most valuable signal in post-mortem
* debugging; dropping it FIFO would defeat the purpose of capture.
*/
export default class MetricsAggregator {
private statementMetrics: Map<string, StatementTelemetryDetails> = new Map();
private pendingMetrics: TelemetryMetric[] = [];
private flushTimer: NodeJS.Timeout | null = null;
private closed = false;
private closing = false;
private batchSize: number;
private flushIntervalMs: number;
private maxPendingMetrics: number;
private maxErrorsPerStatement: number;
private statementTtlMs: number;
constructor(private context: IClientContext, private exporter: DatabricksTelemetryExporter) {
try {
const config = context.getConfig();
this.batchSize = config.telemetryBatchSize ?? DEFAULT_TELEMETRY_CONFIG.batchSize;
this.flushIntervalMs = config.telemetryFlushIntervalMs ?? DEFAULT_TELEMETRY_CONFIG.flushIntervalMs;
this.maxPendingMetrics = config.telemetryMaxPendingMetrics ?? DEFAULT_TELEMETRY_CONFIG.maxPendingMetrics;
this.maxErrorsPerStatement =
config.telemetryMaxErrorsPerStatement ?? DEFAULT_TELEMETRY_CONFIG.maxErrorsPerStatement;
this.statementTtlMs = config.telemetryStatementTtlMs ?? DEFAULT_TELEMETRY_CONFIG.statementTtlMs;
this.startFlushTimer();
} catch (error: any) {
const logger = this.context.getLogger();
logger.log(LogLevel.debug, `MetricsAggregator constructor error: ${error.message}`);
this.batchSize = DEFAULT_TELEMETRY_CONFIG.batchSize;
this.flushIntervalMs = DEFAULT_TELEMETRY_CONFIG.flushIntervalMs;
this.maxPendingMetrics = DEFAULT_TELEMETRY_CONFIG.maxPendingMetrics;
this.maxErrorsPerStatement = DEFAULT_TELEMETRY_CONFIG.maxErrorsPerStatement;
this.statementTtlMs = DEFAULT_TELEMETRY_CONFIG.statementTtlMs;
}
}
processEvent(event: TelemetryEvent): void {
if (this.closed) return;
const logger = this.context.getLogger();
try {
if (event.eventType === TelemetryEventType.CONNECTION_OPEN) {
this.processConnectionEvent(event);
return;
}
if (event.eventType === TelemetryEventType.ERROR) {
this.processErrorEvent(event);
return;
}
if (event.statementId) {
this.processStatementEvent(event);
}
} catch (error: any) {
logger.log(LogLevel.debug, `MetricsAggregator.processEvent error: ${error.message}`);
}
}
private processConnectionEvent(event: TelemetryEvent): void {
const metric: TelemetryMetric = {
metricType: 'connection',
timestamp: event.timestamp,
sessionId: event.sessionId,
workspaceId: event.workspaceId,
driverConfig: event.driverConfig,
};
this.addPendingMetric(metric);
}
private processErrorEvent(event: TelemetryEvent): void {
const logger = this.context.getLogger();
// `isTerminal` is carried on the event by the emitter (it knows the
// call site's taxonomy). If callers ever drop it we default to
// retryable — buffering by statement is the safer choice.
const isTerminal = event.isTerminal === true;
if (isTerminal) {
logger.log(LogLevel.debug, 'Terminal error detected - flushing immediately');
if (event.statementId && this.statementMetrics.has(event.statementId)) {
const details = this.statementMetrics.get(event.statementId)!;
this.pushBoundedError(details, event);
this.completeStatement(event.statementId);
} else {
const metric: TelemetryMetric = {
metricType: 'error',
timestamp: event.timestamp,
sessionId: event.sessionId,
statementId: event.statementId,
workspaceId: event.workspaceId,
errorName: event.errorName,
errorMessage: event.errorMessage,
errorStack: event.errorStack,
};
this.addPendingMetric(metric);
}
// Fire-and-forget on the terminal-error path so customer code doesn't
// stall on telemetry HTTP. Do NOT reset the periodic flush timer:
// under burst failures that would keep the tail-drain timer from
// ever firing.
Promise.resolve(this.flush(false)).catch((err: any) => {
logger.log(LogLevel.debug, `Terminal-error flush failed: ${err?.message ?? err}`);
});
} else if (event.statementId) {
const details = this.getOrCreateStatementDetails(event);
this.pushBoundedError(details, event);
}
}
private pushBoundedError(details: StatementTelemetryDetails, event: TelemetryEvent): void {
if (details.errors.length >= this.maxErrorsPerStatement) {
details.errors.shift();
}
details.errors.push(event);
}
private processStatementEvent(event: TelemetryEvent): void {
const details = this.getOrCreateStatementDetails(event);
switch (event.eventType) {
case TelemetryEventType.STATEMENT_START:
details.operationType = event.operationType;
details.startTime = event.timestamp;
break;
case TelemetryEventType.STATEMENT_COMPLETE:
details.executionLatencyMs = event.latencyMs;
details.resultFormat = event.resultFormat;
details.chunkCount = event.chunkCount ?? 0;
details.bytesDownloaded = event.bytesDownloaded ?? 0;
details.pollCount = event.pollCount ?? 0;
break;
case TelemetryEventType.CLOUDFETCH_CHUNK:
details.chunkCount += 1;
details.bytesDownloaded += event.bytes ?? 0;
if (event.compressed !== undefined) {
details.compressionEnabled = event.compressed;
}
break;
default:
break;
}
}
private getOrCreateStatementDetails(event: TelemetryEvent): StatementTelemetryDetails {
const statementId = event.statementId!;
if (!this.statementMetrics.has(statementId)) {
this.statementMetrics.set(statementId, {
statementId,
sessionId: event.sessionId!,
workspaceId: event.workspaceId,
startTime: event.timestamp,
chunkCount: 0,
bytesDownloaded: 0,
pollCount: 0,
errors: [],
});
}
return this.statementMetrics.get(statementId)!;
}
/**
* Drop entries older than `statementTtlMs`, emitting their buffered error
* events as standalone metrics first so the first-failure signal survives
* the eviction. Called from the periodic flush timer so idle clients
* don't leak orphan entries.
*/
private evictExpiredStatements(): void {
const cutoff = Date.now() - this.statementTtlMs;
let evicted = 0;
for (const [id, details] of this.statementMetrics) {
if (details.startTime < cutoff) {
for (const errorEvent of details.errors) {
this.addPendingMetric({
metricType: 'error',
timestamp: errorEvent.timestamp,
sessionId: details.sessionId,
statementId: details.statementId,
workspaceId: details.workspaceId,
errorName: errorEvent.errorName,
errorMessage: errorEvent.errorMessage,
errorStack: errorEvent.errorStack,
});
}
this.statementMetrics.delete(id);
evicted += 1;
}
}
if (evicted > 0) {
this.context
.getLogger()
.log(LogLevel.debug, `Evicted ${evicted} abandoned statement(s) past ${this.statementTtlMs}ms TTL`);
}
}
completeStatement(statementId: string): void {
if (this.closed) return;
const logger = this.context.getLogger();
try {
const details = this.statementMetrics.get(statementId);
if (!details) {
return;
}
const metric: TelemetryMetric = {
metricType: 'statement',
timestamp: details.startTime,
sessionId: details.sessionId,
statementId: details.statementId,
workspaceId: details.workspaceId,
latencyMs: details.executionLatencyMs,
resultFormat: details.resultFormat,
chunkCount: details.chunkCount,
bytesDownloaded: details.bytesDownloaded,
pollCount: details.pollCount,
};
this.addPendingMetric(metric);
for (const errorEvent of details.errors) {
const errorMetric: TelemetryMetric = {
metricType: 'error',
timestamp: errorEvent.timestamp,
sessionId: details.sessionId,
statementId: details.statementId,
workspaceId: details.workspaceId,
errorName: errorEvent.errorName,
errorMessage: errorEvent.errorMessage,
errorStack: errorEvent.errorStack,
};
this.addPendingMetric(errorMetric);
}
this.statementMetrics.delete(statementId);
} catch (error: any) {
logger.log(LogLevel.debug, `MetricsAggregator.completeStatement error: ${error.message}`);
}
}
/**
* Append `metric` to the pending buffer, enforcing `maxPendingMetrics`.
*
* Overflow drops the oldest non-error entry (single `splice` — no new
* allocation). Under an all-error buffer it falls back to dropping the
* oldest entry at index 0.
*/
private addPendingMetric(metric: TelemetryMetric): void {
if (this.closed) return;
this.pendingMetrics.push(metric);
if (this.pendingMetrics.length > this.maxPendingMetrics) {
const dropIndex = this.findDropIndex();
this.pendingMetrics.splice(dropIndex, 1);
const logger = this.context.getLogger();
logger.log(
LogLevel.debug,
`Dropped 1 oldest non-error telemetry metric (buffer full at ${this.maxPendingMetrics})`,
);
}
if (this.pendingMetrics.length >= this.batchSize && !this.closing) {
// resetTimer=false so the periodic tail-drain keeps its cadence even
// under sustained batch-size bursts.
const logger = this.context.getLogger();
Promise.resolve(this.flush(false)).catch((err: any) => {
logger.log(LogLevel.debug, `Batch-trigger flush failed: ${err?.message ?? err}`);
});
}
}
private findDropIndex(): number {
for (let i = 0; i < this.pendingMetrics.length; i += 1) {
if (this.pendingMetrics[i].metricType !== 'error') {
return i;
}
}
return 0;
}
/**
* Drain the pending buffer and return a promise that resolves when the
* exporter finishes with the drained batch. `close()` awaits this so
* `process.exit()` after `client.close()` doesn't truncate the POST.
*/
async flush(resetTimer: boolean = true): Promise<void> {
const logger = this.context.getLogger();
let exportPromise: Promise<void> | null = null;
try {
if (this.pendingMetrics.length === 0) {
if (resetTimer && !this.closed) {
this.startFlushTimer();
}
return;
}
const metricsToExport = this.pendingMetrics;
this.pendingMetrics = [];
logger.log(LogLevel.debug, `Flushing ${metricsToExport.length} telemetry metrics`);
exportPromise = this.exporter.export(metricsToExport);
if (resetTimer && !this.closed) {
this.startFlushTimer();
}
} catch (error: any) {
logger.log(LogLevel.debug, `MetricsAggregator.flush error: ${error.message}`);
}
if (exportPromise) {
try {
await exportPromise;
} catch (err: any) {
logger.log(LogLevel.debug, `Unexpected export error: ${err?.message ?? err}`);
}
}
}
private startFlushTimer(): void {
if (this.closed) return;
const logger = this.context.getLogger();
try {
if (this.flushTimer) {
clearInterval(this.flushTimer);
}
this.flushTimer = setInterval(() => {
// Idle eviction: run before the flush so orphan-error metrics have
// a chance to batch into this drain rather than wait for the next.
try {
this.evictExpiredStatements();
} catch (err: any) {
logger.log(LogLevel.debug, `evictExpiredStatements error: ${err?.message ?? err}`);
}
Promise.resolve(this.flush(false)).catch((err: any) => {
logger.log(LogLevel.debug, `Periodic flush failed: ${err?.message ?? err}`);
});
}, this.flushIntervalMs);
this.flushTimer.unref();
} catch (error: any) {
logger.log(LogLevel.debug, `MetricsAggregator.startFlushTimer error: ${error.message}`);
}
}
async close(): Promise<void> {
const logger = this.context.getLogger();
try {
// Suppress batch-triggered fire-and-forget flushes from addPendingMetric
// so no promises escape past the single awaited flush below.
this.closing = true;
if (this.flushTimer) {
clearInterval(this.flushTimer);
this.flushTimer = null;
}
// closed is still false here so completeStatement → addPendingMetric works normally.
const remainingStatements = [...this.statementMetrics.keys()];
for (const statementId of remainingStatements) {
this.completeStatement(statementId);
}
this.closed = true;
await this.flush(false);
} catch (error: any) {
logger.log(LogLevel.debug, `MetricsAggregator.close error: ${error.message}`);
}
}
}