-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathDatabricksTelemetryExporter.test.ts
More file actions
583 lines (476 loc) · 25.7 KB
/
Copy pathDatabricksTelemetryExporter.test.ts
File metadata and controls
583 lines (476 loc) · 25.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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/**
* 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 { expect } from 'chai';
import sinon from 'sinon';
import DatabricksTelemetryExporter from '../../../lib/telemetry/DatabricksTelemetryExporter';
import { CircuitBreakerRegistry } from '../../../lib/telemetry/CircuitBreaker';
import { TelemetryMetric } from '../../../lib/telemetry/types';
import ClientContextStub from '../.stubs/ClientContextStub';
import { LogLevel } from '../../../lib/contracts/IDBSQLLogger';
import IAuthentication from '../../../lib/connection/contracts/IAuthentication';
const fakeAuthProvider: IAuthentication = {
authenticate: async () => ({ Authorization: 'Bearer test-token' }),
};
function makeMetric(overrides: Partial<TelemetryMetric> = {}): TelemetryMetric {
return {
metricType: 'connection',
timestamp: Date.now(),
sessionId: 'session-1',
...overrides,
};
}
function makeOkResponse() {
return Promise.resolve({ ok: true, status: 200, statusText: 'OK', text: () => Promise.resolve('') });
}
function makeErrorResponse(status: number, statusText: string) {
return Promise.resolve({ ok: false, status, statusText, text: () => Promise.resolve('') });
}
describe('DatabricksTelemetryExporter', () => {
let clock: sinon.SinonFakeTimers;
beforeEach(() => {
clock = sinon.useFakeTimers();
});
afterEach(() => {
clock.restore();
sinon.restore();
});
describe('export() - basic', () => {
it('should return immediately for empty metrics array', async () => {
const context = new ClientContextStub();
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([]);
expect(sendRequestStub.called).to.be.false;
});
it('should call sendRequest with correct endpoint for authenticated export', async () => {
const context = new ClientContextStub({ telemetryAuthenticatedExport: true } as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeMetric()]);
expect(sendRequestStub.calledOnce).to.be.true;
const url = sendRequestStub.firstCall.args[0] as string;
expect(url).to.include('telemetry-ext');
expect(url).to.include('https://');
});
it('should call sendRequest with unauthenticated endpoint when configured', async () => {
const context = new ClientContextStub({ telemetryAuthenticatedExport: false } as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeMetric()]);
const url = sendRequestStub.firstCall.args[0] as string;
expect(url).to.include('telemetry-unauth');
});
it('should preserve host protocol if already set', async () => {
const context = new ClientContextStub({ telemetryAuthenticatedExport: true } as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'https://host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeMetric()]);
const url = sendRequestStub.firstCall.args[0] as string;
expect(url).to.equal('https://host.example.com/telemetry-ext');
});
it('should never throw even when sendRequest fails', async () => {
const context = new ClientContextStub();
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
sinon.stub(exporter as any, 'sendRequest').rejects(new Error('network error'));
let threw = false;
try {
await exporter.export([makeMetric()]);
} catch {
threw = true;
}
expect(threw).to.be.false;
});
it('should attach config.customHeaders to the POST (SPOG)', async () => {
const context = new ClientContextStub({
customHeaders: { 'x-databricks-org-id': '12345678901234' },
} as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeMetric()]);
const init = sendRequestStub.firstCall.args[1] as { headers: Record<string, string> };
expect(init.headers['x-databricks-org-id']).to.equal('12345678901234');
});
it('auth headers win over customHeaders on key collision', async () => {
const context = new ClientContextStub({
customHeaders: { Authorization: 'Bearer not-the-real-token' },
} as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeMetric()]);
const init = sendRequestStub.firstCall.args[1] as { headers: Record<string, string> };
expect(init.headers.Authorization).to.equal('Bearer test-token');
});
it('does not attach customHeaders when none are configured', async () => {
const context = new ClientContextStub();
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeMetric()]);
const init = sendRequestStub.firstCall.args[1] as { headers: Record<string, string> };
expect(init.headers).to.not.have.property('x-databricks-org-id');
});
});
describe('export() - driver_connection_params', () => {
// The driver tracks socketTimeout in milliseconds, but the receiver proto
// defines `socket_timeout` in seconds. Lock in the ms -> s conversion so a
// 15-minute (900000ms) timeout is reported as 900s, not 900000s.
function getConnectionParams(sendRequestStub: sinon.SinonStub): any {
const init = sendRequestStub.firstCall.args[1] as { body: string };
const body = JSON.parse(init.body);
const log = JSON.parse(body.protoLogs[0]);
return log.entry.sql_driver_log.driver_connection_params;
}
function makeConnectionMetric(socketTimeout: number): TelemetryMetric {
return makeMetric({
metricType: 'connection',
driverConfig: {
driverName: 'nodejs-sql-driver',
driverVersion: '1.14.0',
socketTimeout,
} as any,
});
}
it('converts socketTimeout from milliseconds to seconds', async () => {
const context = new ClientContextStub({ telemetryAuthenticatedExport: true } as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeConnectionMetric(900000)]);
expect(getConnectionParams(sendRequestStub).socket_timeout).to.equal(900);
});
it('rounds sub-second socketTimeout values', async () => {
const context = new ClientContextStub({ telemetryAuthenticatedExport: true } as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeConnectionMetric(1500)]);
expect(getConnectionParams(sendRequestStub).socket_timeout).to.equal(2);
});
});
describe('export() - retry logic', () => {
it('should retry on retryable HTTP errors (503)', async () => {
const context = new ClientContextStub({ telemetryMaxRetries: 2 } as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
// Fail twice with 503, then succeed
const sendRequestStub = sinon
.stub(exporter as any, 'sendRequest')
.onFirstCall()
.returns(makeErrorResponse(503, 'Service Unavailable'))
.onSecondCall()
.returns(makeErrorResponse(503, 'Service Unavailable'))
.onThirdCall()
.returns(makeOkResponse());
// Advance fake timers automatically for sleep calls
const exportPromise = exporter.export([makeMetric()]);
await clock.runAllAsync();
await exportPromise;
expect(sendRequestStub.callCount).to.equal(3);
});
it('should not retry on terminal HTTP errors (400)', async () => {
const context = new ClientContextStub({ telemetryMaxRetries: 3 } as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeErrorResponse(400, 'Bad Request'));
await exporter.export([makeMetric()]);
// Only one call — no retry on terminal error
expect(sendRequestStub.callCount).to.equal(1);
});
it('should not retry on terminal HTTP errors (401)', async () => {
const context = new ClientContextStub({ telemetryMaxRetries: 3 } as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon
.stub(exporter as any, 'sendRequest')
.returns(makeErrorResponse(401, 'Unauthorized'));
await exporter.export([makeMetric()]);
expect(sendRequestStub.callCount).to.equal(1);
});
it('should give up after maxRetries are exhausted', async () => {
const context = new ClientContextStub({ telemetryMaxRetries: 2 } as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon
.stub(exporter as any, 'sendRequest')
.returns(makeErrorResponse(503, 'Service Unavailable'));
const exportPromise = exporter.export([makeMetric()]);
await clock.runAllAsync();
await exportPromise;
// 1 initial + 2 retries = 3 total calls
expect(sendRequestStub.callCount).to.equal(3);
});
});
describe('export() - circuit breaker integration', () => {
it('should drop telemetry when circuit breaker is OPEN', async () => {
// maxRetries: 0 avoids sleep delays; failureThreshold: 1 trips the breaker on first failure
const context = new ClientContextStub({ telemetryMaxRetries: 0 } as any);
const registry = new CircuitBreakerRegistry(context);
registry.getCircuitBreaker('host.example.com', { failureThreshold: 1 });
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon
.stub(exporter as any, 'sendRequest')
.returns(makeErrorResponse(503, 'Service Unavailable'));
// Trip the circuit breaker (1 non-retryable-pathway failure is enough)
await exporter.export([makeMetric()]);
sendRequestStub.reset();
// Now circuit is OPEN, export should be dropped without calling sendRequest
await exporter.export([makeMetric()]);
expect(sendRequestStub.called).to.be.false;
});
it('should log at debug level when circuit is OPEN', async () => {
const context = new ClientContextStub({ telemetryMaxRetries: 0 } as any);
const logSpy = sinon.spy((context as any).logger, 'log');
const registry = new CircuitBreakerRegistry(context);
registry.getCircuitBreaker('host.example.com', { failureThreshold: 1 });
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
sinon.stub(exporter as any, 'sendRequest').returns(makeErrorResponse(503, 'Service Unavailable'));
await exporter.export([makeMetric()]);
logSpy.resetHistory();
await exporter.export([makeMetric()]);
expect(logSpy.calledWith(LogLevel.debug, sinon.match(/Circuit breaker OPEN/))).to.be.true;
});
});
describe('export() - payload format', () => {
it('should send POST request with JSON content-type', async () => {
const context = new ClientContextStub();
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeMetric()]);
const options = sendRequestStub.firstCall.args[1] as any;
expect(options.method).to.equal('POST');
expect(options.headers['Content-Type']).to.equal('application/json');
});
it('should include protoLogs in payload body', async () => {
const context = new ClientContextStub();
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeMetric(), makeMetric()]);
const body = JSON.parse((sendRequestStub.firstCall.args[1] as any).body);
expect(body.protoLogs).to.be.an('array').with.length(2);
expect(body.items).to.be.an('array').that.is.empty;
expect(body.uploadTime).to.be.a('number');
});
});
describe('logging level compliance', () => {
it('should only log at debug level', async () => {
const context = new ClientContextStub();
const logSpy = sinon.spy((context as any).logger, 'log');
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
sinon.stub(exporter as any, 'sendRequest').rejects(new Error('something went wrong'));
const exportPromise = exporter.export([makeMetric()]);
await clock.runAllAsync();
await exportPromise;
expect(logSpy.neverCalledWith(LogLevel.error, sinon.match.any)).to.be.true;
// Note: circuit breaker logs at warn level when transitioning to OPEN, which is expected
});
});
describe('Authorization header flow', () => {
it('sends Authorization header returned by the auth provider on authenticated export', async () => {
const context = new ClientContextStub({ telemetryAuthenticatedExport: true } as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeMetric()]);
const init = sendRequestStub.firstCall.args[1] as any;
expect(init.headers.Authorization).to.equal('Bearer test-token');
});
it('drops the batch when authenticated export is requested but auth returns no header', async () => {
const context = new ClientContextStub({ telemetryAuthenticatedExport: true, telemetryMaxRetries: 0 } as any);
const registry = new CircuitBreakerRegistry(context);
const emptyAuth = { authenticate: async () => ({}) };
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, emptyAuth as any);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeMetric()]);
expect(sendRequestStub.called).to.be.false;
});
it('warns exactly once across consecutive auth-missing drops', async () => {
const context = new ClientContextStub({ telemetryAuthenticatedExport: true, telemetryMaxRetries: 0 } as any);
const logSpy = sinon.spy((context as any).logger, 'log');
const registry = new CircuitBreakerRegistry(context);
const emptyAuth = { authenticate: async () => ({}) };
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, emptyAuth as any);
sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeMetric()]);
await exporter.export([makeMetric()]);
await exporter.export([makeMetric()]);
const warnCalls = logSpy
.getCalls()
.filter((c) => c.args[0] === LogLevel.warn && /Authorization/.test(String(c.args[1])));
expect(warnCalls.length).to.equal(1);
});
it('re-arms the auth-missing warn after a successful export', async () => {
const context = new ClientContextStub({ telemetryAuthenticatedExport: true, telemetryMaxRetries: 0 } as any);
const logSpy = sinon.spy((context as any).logger, 'log');
const registry = new CircuitBreakerRegistry(context);
let headers: Record<string, string> = {};
const toggleAuth = { authenticate: async () => headers };
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, toggleAuth as any);
sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeMetric()]); // warns once
headers = { Authorization: 'Bearer recovered' };
await exporter.export([makeMetric()]); // success → re-arms
headers = {};
await exporter.export([makeMetric()]); // warns again
const warnCalls = logSpy
.getCalls()
.filter((c) => c.args[0] === LogLevel.warn && /Authorization/.test(String(c.args[1])));
expect(warnCalls.length).to.equal(2);
});
});
describe('unauthenticated endpoint privacy', () => {
it('omits workspace_id, session_id, statement_id from unauth payload', async () => {
const context = new ClientContextStub({ telemetryAuthenticatedExport: false } as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([
makeMetric({
metricType: 'connection',
sessionId: 'session-xyz',
statementId: 'stmt-abc',
workspaceId: 'ws-123',
} as any),
]);
const body = JSON.parse((sendRequestStub.firstCall.args[1] as any).body);
const log = JSON.parse(body.protoLogs[0]);
expect(log.workspace_id).to.be.undefined;
expect(log.entry.sql_driver_log.session_id).to.be.undefined;
expect(log.entry.sql_driver_log.sql_statement_id).to.be.undefined;
});
it('omits system_configuration from unauth payload', async () => {
const context = new ClientContextStub({ telemetryAuthenticatedExport: false } as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([
makeMetric({
metricType: 'connection',
driverConfig: {
driverVersion: '1.x',
driverName: 'nodejs-sql-driver',
nodeVersion: '20.0',
platform: 'linux',
osVersion: '5.0',
osArch: 'x64',
runtimeVendor: 'v8',
localeName: 'en_US',
charSetEncoding: 'UTF-8',
processName: '/home/alice/worker.js',
},
} as any),
]);
const body = JSON.parse((sendRequestStub.firstCall.args[1] as any).body);
const log = JSON.parse(body.protoLogs[0]);
expect(log.entry.sql_driver_log.system_configuration).to.be.undefined;
});
it('strips userAgentEntry from User-Agent on unauth path', async () => {
const context = new ClientContextStub({
telemetryAuthenticatedExport: false,
userAgentEntry: 'MyTenantApp/1.2.3',
} as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeMetric()]);
const ua = (sendRequestStub.firstCall.args[1] as any).headers['User-Agent'];
expect(ua).to.not.include('MyTenantApp');
});
it('blanks stack_trace on unauth error metrics', async () => {
const context = new ClientContextStub({ telemetryAuthenticatedExport: false } as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([
makeMetric({
metricType: 'error',
errorName: 'SomeError',
errorMessage: 'Bearer leaked-token in the message',
errorStack: 'Error: leak\n at fn (dapi0123456789abcdef)',
} as any),
]);
const body = JSON.parse((sendRequestStub.firstCall.args[1] as any).body);
const log = JSON.parse(body.protoLogs[0]);
expect(log.entry.sql_driver_log.error_info.stack_trace).to.equal('');
expect(log.entry.sql_driver_log.error_info.error_name).to.equal('SomeError');
});
});
describe('errorStack flow (authenticated)', () => {
it('redacts Bearer tokens in stack_trace before export', async () => {
const context = new ClientContextStub({ telemetryAuthenticatedExport: true } as any);
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([
makeMetric({
metricType: 'error',
errorName: 'AuthError',
errorMessage: 'ignored because errorStack is preferred',
errorStack: 'Error: boom\n at Bearer leaked-bearer-token',
} as any),
]);
const body = JSON.parse((sendRequestStub.firstCall.args[1] as any).body);
const log = JSON.parse(body.protoLogs[0]);
const stack = log.entry.sql_driver_log.stack_trace ?? log.entry.sql_driver_log.error_info?.stack_trace;
expect(stack).to.include('<REDACTED>');
expect(stack).to.not.include('leaked-bearer-token');
});
});
describe('host validation', () => {
it('drops the batch when host fails validation (malformed)', async () => {
const context = new ClientContextStub();
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, '//attacker.com', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeMetric()]);
expect(sendRequestStub.called).to.be.false;
});
it('drops the batch when host is loopback', async () => {
const context = new ClientContextStub();
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, '127.0.0.1', registry, fakeAuthProvider);
const sendRequestStub = sinon.stub(exporter as any, 'sendRequest').returns(makeOkResponse());
await exporter.export([makeMetric()]);
expect(sendRequestStub.called).to.be.false;
});
});
describe('dispose()', () => {
it('removes the per-host circuit breaker from the registry', () => {
const context = new ClientContextStub();
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
expect(registry.getAllBreakers().has('host.example.com')).to.be.true;
exporter.dispose();
expect(registry.getAllBreakers().has('host.example.com')).to.be.false;
});
it('is idempotent', () => {
const context = new ClientContextStub();
const registry = new CircuitBreakerRegistry(context);
const exporter = new DatabricksTelemetryExporter(context, 'host.example.com', registry, fakeAuthProvider);
exporter.dispose();
expect(() => exporter.dispose()).to.not.throw();
});
});
});