-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathapi.spec.ts
More file actions
1164 lines (1007 loc) · 37.7 KB
/
Copy pathapi.spec.ts
File metadata and controls
1164 lines (1007 loc) · 37.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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { display } from '@datadog/browser-core'
import { mockClock, registerCleanupTask } from '@datadog/browser-core/test'
import { onEntry, onReturn, onThrow, initDebuggerTransport, resetDebuggerTransport } from './api'
import { addProbe, removeProbe, getProbes, clearProbes } from './probes'
import type { Probe } from './probes'
describe('api', () => {
let mockBatchAdd: jasmine.Spy
let warnSpy: jasmine.Spy
function initTransport(overrides: Record<string, unknown> = {}) {
resetDebuggerTransport()
initDebuggerTransport(
{ service: 'test-service', env: 'test-env', ...overrides } as any,
{
add: mockBatchAdd,
} as any
)
}
beforeEach(() => {
clearProbes()
warnSpy = spyOn(display, 'warn')
mockBatchAdd = jasmine.createSpy('batchAdd')
initTransport()
;(window as any).DD_DEBUGGER = {
version: '0.0.1',
}
registerCleanupTask(() => {
delete (window as any).DD_DEBUGGER
resetDebuggerTransport()
clearProbes()
})
})
describe('onEntry and onReturn', () => {
it('should capture this inside arguments.fields', () => {
const probe: Probe = {
id: 'test-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'testMethod' },
template: 'Test message',
captureSnapshot: true,
capture: { maxReferenceDepth: 1 },
sampling: { snapshotsPerSecond: 5000 },
evaluateAt: 'ENTRY',
}
addProbe(probe)
const self = { name: 'testObj' }
const args = { a: 1, b: 2 }
const probes = getProbes('TestClass;testMethod')!
onEntry(probes, self, args)
onReturn(probes, 'result', self, args, {})
const payload = mockBatchAdd.calls.mostRecent().args[0]
const snapshot = payload.debugger.snapshot
// Verify entry.arguments structure - now flat
expect(snapshot.captures.entry.arguments).toEqual({
a: { type: 'number', value: '1' },
b: { type: 'number', value: '2' },
this: {
type: 'Object',
fields: {
name: { type: 'string', value: 'testObj' },
},
},
})
// Verify return.arguments structure - now flat
expect(snapshot.captures.return.arguments).toEqual({
a: { type: 'number', value: '1' },
b: { type: 'number', value: '2' },
this: {
type: 'Object',
fields: {
name: { type: 'string', value: 'testObj' },
},
},
})
// Verify return.locals structure - also flat
expect(snapshot.captures.return.locals['@return']).toEqual({
type: 'string',
value: 'result',
})
})
it('should capture entry and return for simple probe', () => {
const probe: Probe = {
id: 'test-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'testMethod' },
template: 'Test message',
captureSnapshot: true,
capture: { maxReferenceDepth: 1 },
sampling: { snapshotsPerSecond: 5000 },
evaluateAt: 'ENTRY',
}
addProbe(probe)
const self = { name: 'test' }
const args = { arg1: 'value1', arg2: 42 }
const probes = getProbes('TestClass;testMethod')!
onEntry(probes, self, args)
const result = onReturn(probes, 'returnValue', self, args, {})
expect(result).toBe('returnValue')
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
const payload = mockBatchAdd.calls.mostRecent().args[0]
expect(payload.message).toBe('Test message')
expect(payload.debugger.snapshot).toEqual(
jasmine.objectContaining({ id: jasmine.any(String), captures: jasmine.any(Object) })
)
})
it('should skip probe if sampling budget exceeded', () => {
// Use a very low sampling rate to ensure budget is exceeded
const probe: Probe = {
id: 'test-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'budgetTest' },
template: 'Test',
captureSnapshot: true,
capture: { maxReferenceDepth: 1 },
sampling: { snapshotsPerSecond: 0.5 }, // 0.5 per second = 2000ms between samples
evaluateAt: 'ENTRY',
}
addProbe(probe)
const probes = getProbes('TestClass;budgetTest')!
// First call should work
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
// Second immediate call should be skipped (less than 2000ms passed)
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
// Still only one call because sampling budget not refreshed
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
})
it('should evaluate condition at ENTRY', () => {
const probe: Probe = {
id: 'test-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'conditionEntry' },
when: {
dsl: 'x > 5',
json: { gt: [{ ref: 'x' }, 5] },
},
template: 'Condition passed',
captureSnapshot: false,
capture: {},
sampling: {},
evaluateAt: 'ENTRY',
}
addProbe(probe)
let probes = getProbes('TestClass;conditionEntry')!
// Should fire when condition passes
onEntry(probes, {}, { x: 10 })
onReturn(probes, null, {}, { x: 10 }, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
clearProbes()
addProbe(probe)
mockBatchAdd.calls.reset()
probes = getProbes('TestClass;conditionEntry')!
// Should not fire when condition fails
onEntry(probes, {}, { x: 3 })
onReturn(probes, null, {}, { x: 3 }, {})
expect(mockBatchAdd).not.toHaveBeenCalled()
})
it('should evaluate condition at EXIT with @return', () => {
const probe: Probe = {
id: 'test-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'conditionExit' },
when: {
dsl: '@return > 10',
json: { gt: [{ ref: '@return' }, 10] },
},
template: 'Return value check',
captureSnapshot: false,
capture: {},
sampling: {},
evaluateAt: 'EXIT',
}
addProbe(probe)
let probes = getProbes('TestClass;conditionExit')!
// Should fire when return value > 10
onEntry(probes, {}, {})
onReturn(probes, 15, {}, {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
clearProbes()
addProbe(probe)
mockBatchAdd.calls.reset()
probes = getProbes('TestClass;conditionExit')!
// Should not fire when return value <= 10
onEntry(probes, {}, {})
onReturn(probes, 5, {}, {}, {})
expect(mockBatchAdd).not.toHaveBeenCalled()
})
// TODO: Validate that this test is actually correct
it('should capture entry snapshot only for ENTRY evaluation with no condition', () => {
const probe: Probe = {
id: 'test-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'entrySnapshot' },
template: 'Test',
captureSnapshot: true,
capture: { maxReferenceDepth: 1 },
sampling: {},
evaluateAt: 'ENTRY',
}
addProbe(probe)
const probes = getProbes('TestClass;entrySnapshot')!
onEntry(probes, { name: 'obj' }, { arg: 'value' })
onReturn(probes, 'result', { name: 'obj' }, { arg: 'value' }, { local: 'data' })
const payload = mockBatchAdd.calls.mostRecent().args[0]
const snapshot = payload.debugger.snapshot
expect(snapshot.captures).toEqual({
entry: {
arguments: {
arg: { type: 'string', value: 'value' },
this: { type: 'Object', fields: { name: { type: 'string', value: 'obj' } } },
},
},
return: {
arguments: {
arg: { type: 'string', value: 'value' },
this: { type: 'Object', fields: { name: { type: 'string', value: 'obj' } } },
},
locals: {
local: { type: 'string', value: 'data' },
'@return': { type: 'string', value: 'result' },
},
},
})
})
it('should only capture return snapshot for EXIT evaluation with condition', () => {
const probe: Probe = {
id: 'test-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'exitSnapshot' },
when: {
dsl: '@return === true',
json: { eq: [{ ref: '@return' }, true] },
},
template: 'Test',
captureSnapshot: true,
capture: { maxReferenceDepth: 1 },
sampling: {},
evaluateAt: 'EXIT',
}
addProbe(probe)
const probes = getProbes('TestClass;exitSnapshot')!
onEntry(probes, {}, { arg: 'value' })
onReturn(probes, true, {}, { arg: 'value' }, {})
const payload = mockBatchAdd.calls.mostRecent().args[0]
const snapshot = payload.debugger.snapshot
expect(snapshot.captures.entry).toBeUndefined()
expect(snapshot.captures.return).toBeDefined()
})
it('should include duration in snapshot', () => {
const clock = mockClock()
const probe: Probe = {
id: 'test-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'durationTest' },
template: 'Test',
captureSnapshot: true,
capture: { maxReferenceDepth: 1 },
sampling: {},
evaluateAt: 'ENTRY',
}
addProbe(probe)
const probes = getProbes('TestClass;durationTest')!
onEntry(probes, {}, {})
clock.tick(10)
onReturn(probes, null, {}, {}, {})
const payload = mockBatchAdd.calls.mostRecent().args[0]
const snapshot = payload.debugger.snapshot
expect(snapshot.duration).toBe(10_000_000) // Should be in nanoseconds (10ms)
})
})
describe('onThrow', () => {
it('should capture this inside arguments.fields for exceptions', () => {
const probe: Probe = {
id: 'test-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'throwTest' },
template: 'Test',
captureSnapshot: true,
capture: { maxReferenceDepth: 1 },
sampling: {},
evaluateAt: 'ENTRY',
}
addProbe(probe)
const self = { name: 'testObj' }
const args = { a: 1, b: 2 }
const error = new Error('Test error')
const probes = getProbes('TestClass;throwTest')!
onEntry(probes, self, args)
onThrow(probes, error, self, args)
const payload = mockBatchAdd.calls.mostRecent().args[0]
const snapshot = payload.debugger.snapshot
// Verify return.arguments structure - now flat
expect(snapshot.captures.return.arguments).toEqual({
a: { type: 'number', value: '1' },
b: { type: 'number', value: '2' },
this: {
type: 'Object',
fields: {
name: { type: 'string', value: 'testObj' },
},
},
})
// Verify throwable is still present
expect(snapshot.captures.return.throwable).toEqual({
message: 'Test error',
stacktrace: jasmine.any(Array),
})
for (const frame of snapshot.captures.return.throwable.stacktrace) {
expect(frame).toEqual(
jasmine.objectContaining({
fileName: jasmine.any(String),
function: jasmine.any(String),
lineNumber: jasmine.any(Number),
columnNumber: jasmine.any(Number),
})
)
}
})
it('should capture exception details', () => {
const probe: Probe = {
id: 'test-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'throwTest' },
template: 'Test',
captureSnapshot: true,
capture: { maxReferenceDepth: 1 },
sampling: {},
evaluateAt: 'ENTRY',
}
addProbe(probe)
const probes = getProbes('TestClass;throwTest')!
const error = new Error('Test error')
onEntry(probes, {}, { arg: 'value' })
onThrow(probes, error, {}, { arg: 'value' })
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
const payload = mockBatchAdd.calls.mostRecent().args[0]
const snapshot = payload.debugger.snapshot
expect(snapshot.captures.return.throwable).toEqual({
message: 'Test error',
stacktrace: jasmine.any(Array),
})
for (const frame of snapshot.captures.return.throwable.stacktrace) {
expect(frame).toEqual(
jasmine.objectContaining({
fileName: jasmine.any(String),
function: jasmine.any(String),
lineNumber: jasmine.any(Number),
columnNumber: jasmine.any(Number),
})
)
}
})
it('should evaluate EXIT condition with @exception', () => {
const probe: Probe = {
id: 'test-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'exceptionCondition' },
when: {
dsl: '@exception.message',
json: { getmember: [{ ref: '@exception' }, 'message'] },
},
template: 'Exception captured',
captureSnapshot: false,
capture: { maxReferenceDepth: 1 },
sampling: {},
evaluateAt: 'EXIT',
}
addProbe(probe)
const probes = getProbes('TestClass;exceptionCondition')!
const error = new Error('Test error')
onEntry(probes, {}, {})
onThrow(probes, error, {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
})
it('should handle onThrow without preceding onEntry', () => {
const probe: Probe = {
id: 'test-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'throwWithoutEntry' },
template: 'Test',
captureSnapshot: true,
capture: { maxReferenceDepth: 1 },
sampling: {},
evaluateAt: 'ENTRY',
}
addProbe(probe)
const probes = getProbes('TestClass;throwWithoutEntry')!
const error = new Error('Test error')
onThrow(probes, error, {}, {})
expect(mockBatchAdd).not.toHaveBeenCalled()
})
})
describe('global snapshot budget', () => {
it('should respect global snapshot rate limit', () => {
const probes: Probe[] = []
for (let i = 0; i < 30; i++) {
const probe: Probe = {
id: `probe-${i}`,
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: `method${i}` },
template: 'Test',
captureSnapshot: true,
capture: {},
sampling: { snapshotsPerSecond: 5000 },
evaluateAt: 'ENTRY',
}
addProbe(probe)
probes.push(probe)
}
// Try to fire 30 probes rapidly
for (let i = 0; i < 30; i++) {
const probes = getProbes(`TestClass;method${i}`)!
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
}
// Should only get 25 calls (global limit)
expect(mockBatchAdd).toHaveBeenCalledTimes(25)
})
it('should respect configured global snapshot rate limit', () => {
initTransport({ maxSnapshotsPerSecondGlobally: 2 })
for (let i = 0; i < 3; i++) {
const probe: Probe = {
id: `configured-global-probe-${i}`,
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: `configuredGlobal${i}` },
template: 'Test',
captureSnapshot: true,
capture: {},
sampling: { snapshotsPerSecond: 5000 },
evaluateAt: 'ENTRY',
}
addProbe(probe)
}
for (let i = 0; i < 3; i++) {
const probes = getProbes(`TestClass;configuredGlobal${i}`)!
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
}
expect(mockBatchAdd).toHaveBeenCalledTimes(2)
})
})
describe('configured per-second budgets', () => {
it('should respect configured default snapshot per-probe rate limit', () => {
initTransport({ maxSnapshotsPerSecondPerProbe: 0.5 })
const probe: Probe = {
id: 'configured-snapshot-rate-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'configuredSnapshotRate' },
template: 'Test',
captureSnapshot: true,
capture: { maxReferenceDepth: 1 },
sampling: {},
evaluateAt: 'ENTRY',
}
addProbe(probe)
const probes = getProbes('TestClass;configuredSnapshotRate')!
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
})
it('should respect configured default non-snapshot per-probe rate limit', () => {
initTransport({ maxNonSnapshotsPerSecondPerProbe: 1 })
const probe: Probe = {
id: 'configured-non-snapshot-rate-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'configuredNonSnapshotRate' },
template: 'Test',
captureSnapshot: false,
capture: {},
sampling: {},
evaluateAt: 'ENTRY',
}
addProbe(probe)
const probes = getProbes('TestClass;configuredNonSnapshotRate')!
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
})
})
describe('probe lifetime budgets', () => {
it('should stop sending snapshot events after maxSnapshotsPerProbeLifetime', () => {
initTransport({ maxSnapshotsPerProbeLifetime: 1 })
const probe: Probe = {
id: 'snapshot-lifetime-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'snapshotLifetime' },
template: 'Test',
captureSnapshot: true,
capture: { maxReferenceDepth: 1 },
// Disable per-probe rate limiting so the second invocation exercises the
// lifetime cap rather than the per-second cap.
sampling: { snapshotsPerSecond: Infinity },
evaluateAt: 'ENTRY',
}
addProbe(probe)
// First invocation: probe sends its single allowed event.
const probes = getProbes('TestClass;snapshotLifetime')!
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
// Second invocation: the lifetime budget is now exhausted. No new event should
// be queued, and the probe should be auto-unregistered.
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
expect(getProbes('TestClass;snapshotLifetime')).toBeUndefined()
})
it('should skip snapshot collection once the lifetime budget is exhausted', () => {
initTransport({ maxSnapshotsPerProbeLifetime: 1 })
const getterSpy = jasmine.createSpy('argGetter').and.returnValue('value')
const args = {}
Object.defineProperty(args, 'arg', {
enumerable: true,
get: getterSpy,
})
const probe: Probe = {
id: 'snapshot-lifetime-collection-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'snapshotLifetimeCollection' },
template: 'Test',
captureSnapshot: true,
capture: { maxReferenceDepth: 1 },
// Disable per-probe rate limiting so the second invocation isn't sampled out
// by it — we want to exercise the lifetime cap, not the rate cap.
sampling: { snapshotsPerSecond: Infinity },
evaluateAt: 'ENTRY',
}
addProbe(probe)
// First invocation does the full pipeline: 2 reads from entry capture
// (context spread + captureFields) + 1 read from return capture = 3 reads.
// This exhausts the lifetime budget.
const probes = getProbes('TestClass;snapshotLifetimeCollection')!
onEntry(probes, {}, args)
onReturn(probes, null, {}, args, {})
// Second invocation: both onEntry and onReturn detect the exhausted budget
// up front and skip all capture work — no further reads from args.
onEntry(probes, {}, args)
onReturn(probes, null, {}, args, {})
expect(getterSpy).toHaveBeenCalledTimes(3)
})
it('should stop sending non-snapshot events after maxNonSnapshotsPerProbeLifetime', () => {
initTransport({ maxNonSnapshotsPerProbeLifetime: 1 })
const probe: Probe = {
id: 'non-snapshot-lifetime-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'nonSnapshotLifetime' },
template: 'Test',
captureSnapshot: false,
capture: {},
// Disable per-probe rate limiting so the second invocation exercises the
// lifetime cap rather than the per-second cap.
sampling: { snapshotsPerSecond: Infinity },
evaluateAt: 'ENTRY',
}
addProbe(probe)
// First invocation: probe sends its single allowed event.
const probes = getProbes('TestClass;nonSnapshotLifetime')!
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
// Second invocation: the lifetime budget is now exhausted. No new event should
// be queued, and the probe should be auto-unregistered.
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
expect(getProbes('TestClass;nonSnapshotLifetime')).toBeUndefined()
})
it('should reset the lifetime budget when a new probe version is delivered', () => {
initTransport({ maxSnapshotsPerProbeLifetime: 1 })
const probe: Probe = {
id: 'versioned-lifetime-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'versionedLifetime' },
template: 'Test',
captureSnapshot: true,
capture: { maxReferenceDepth: 1 },
sampling: { snapshotsPerSecond: 5000 },
evaluateAt: 'ENTRY',
}
addProbe(probe)
let probes = getProbes('TestClass;versionedLifetime')!
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
// A Remote Config delivery for an existing probe id replaces the old probe with
// the new version. After re-add, the new version should have a fresh budget.
removeProbe(probe.id)
addProbe({ ...probe, version: 1 })
probes = getProbes('TestClass;versionedLifetime')!
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(2)
})
it('should not emit any event when the lifetime budget is zero', () => {
initTransport({ maxSnapshotsPerProbeLifetime: 0 })
const probe: Probe = {
id: 'zero-budget-probe',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'zeroBudget' },
template: 'Test',
captureSnapshot: true,
capture: { maxReferenceDepth: 1 },
sampling: { snapshotsPerSecond: 5000 },
evaluateAt: 'ENTRY',
}
addProbe(probe)
const probes = getProbes('TestClass;zeroBudget')!
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
expect(mockBatchAdd).not.toHaveBeenCalled()
expect(getProbes('TestClass;zeroBudget')).toBeUndefined()
})
it('should still process sibling probes when one is removed mid-iteration', () => {
// Use distinct snapshot/non-snapshot lifetime caps so probeA hits its cap after
// one event while probeB still has plenty of budget. On the second invocation,
// probeA's pre-call budget check fails and it gets removed from the shared
// probes array. This exposes the array mutation hazard: removing probeA
// mid-iteration must not cause probeB to be skipped.
initTransport({ maxSnapshotsPerProbeLifetime: 1, maxNonSnapshotsPerProbeLifetime: 1000 })
// Disable per-probe rate limiting on both probes so the second invocation
// exercises the lifetime cap rather than the per-second cap.
const probeA: Probe = {
id: 'sibling-probe-a',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'sibling' },
template: 'A',
captureSnapshot: true,
capture: { maxReferenceDepth: 1 },
sampling: { snapshotsPerSecond: Infinity },
evaluateAt: 'ENTRY',
}
const probeB: Probe = {
id: 'sibling-probe-b',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'sibling' },
template: 'B',
captureSnapshot: false,
capture: {},
sampling: { snapshotsPerSecond: Infinity },
evaluateAt: 'ENTRY',
}
addProbe(probeA)
addProbe(probeB)
// First invocation: both probes emit one event. probeA hits its cap (eventsSent=1,
// max=1) but is not removed yet — the pre-call budget check still passed.
const probes = getProbes('TestClass;sibling')!
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(2)
// Second invocation: probeA's pre-call check now fails and it is queued for
// removal. probeB must still be processed in the same iteration even though
// probeA gets spliced out of the probes array.
mockBatchAdd.calls.reset()
const probesAfterFirst = getProbes('TestClass;sibling')!
onEntry(probesAfterFirst, {}, {})
onReturn(probesAfterFirst, null, {}, {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
expect(getProbes('TestClass;sibling')).toEqual([jasmine.objectContaining({ id: 'sibling-probe-b' })])
// probeB's stack entry must not leak: a third onReturn without onEntry is a no-op.
mockBatchAdd.calls.reset()
const remainingProbes = getProbes('TestClass;sibling')!
onReturn(remainingProbes, null, {}, {}, {})
expect(mockBatchAdd).not.toHaveBeenCalled()
})
})
describe('active entries cleanup', () => {
function createProbe(id: string, methodName: string): Probe {
return {
id,
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName },
template: 'Test',
captureSnapshot: false,
capture: {},
sampling: { snapshotsPerSecond: 5000 },
evaluateAt: 'ENTRY',
}
}
it('should discard in-flight entries when a probe is removed', () => {
const probe = createProbe('cleanup-probe', 'cleanupTest')
addProbe(probe)
const probes = getProbes('TestClass;cleanupTest')!
onEntry(probes, {}, {})
removeProbe('cleanup-probe')
addProbe(probe)
const newProbes = getProbes('TestClass;cleanupTest')!
onReturn(newProbes, null, {}, {}, {})
expect(mockBatchAdd).not.toHaveBeenCalled()
})
it('should discard in-flight entries when all probes are cleared', () => {
const probe = createProbe('cleanup-probe', 'clearAllTest')
addProbe(probe)
const probes = getProbes('TestClass;clearAllTest')!
onEntry(probes, {}, {})
clearProbes()
addProbe(probe)
const newProbes = getProbes('TestClass;clearAllTest')!
onReturn(newProbes, null, {}, {}, {})
expect(mockBatchAdd).not.toHaveBeenCalled()
})
it('should not leak active entries after onReturn completes', () => {
const probe = createProbe('leak-probe', 'leakTest')
addProbe(probe)
const probes = getProbes('TestClass;leakTest')!
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
mockBatchAdd.calls.reset()
// A second onReturn without onEntry should not produce a snapshot
onReturn(probes, null, {}, {}, {})
expect(mockBatchAdd).not.toHaveBeenCalled()
})
it('should not leak active entries after onThrow completes', () => {
const probe = createProbe('throw-leak-probe', 'throwLeakTest')
addProbe(probe)
const probes = getProbes('TestClass;throwLeakTest')!
onEntry(probes, {}, {})
onThrow(probes, new Error('test'), {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
mockBatchAdd.calls.reset()
// A second onThrow without onEntry should not produce a snapshot
onThrow(probes, new Error('test'), {}, {})
expect(mockBatchAdd).not.toHaveBeenCalled()
})
})
describe('snapshot timeout', () => {
function createSnapshotProbe(methodName: string): Probe {
return {
id: `timeout-probe-${methodName}`,
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName },
template: 'Test',
captureSnapshot: true,
capture: { maxReferenceDepth: 3 },
sampling: { snapshotsPerSecond: 5000 },
evaluateAt: 'ENTRY',
}
}
it('should drop snapshot when entry capture exceeds timeout', () => {
const probe = createSnapshotProbe('entryTimeout')
addProbe(probe)
let callCount = 0
const realNow = performance.now.bind(performance)
spyOn(performance, 'now').and.callFake(() => {
callCount++
// Let the first few calls (start time, deadline creation) use real time,
// then jump past the deadline to simulate slow capture.
if (callCount <= 3) {
return realNow()
}
return realNow() + 20
})
const probes = getProbes('TestClass;entryTimeout')!
const deepObj = { level1: { level2: { level3: { level4: 'deep' } } } }
onEntry(probes, {}, { arg: deepObj })
onReturn(probes, null, {}, { arg: deepObj }, {})
// The entry capture timed out, so onEntry pushed null.
// onReturn still gets an active entry from its own onEntry call, but
// the entry snapshot is dropped. The return capture has its own timeout.
// Since performance.now is still returning future values, the return
// capture also times out and no snapshot is sent.
expect(mockBatchAdd).not.toHaveBeenCalled()
})
it('should drop snapshot when return capture exceeds timeout', () => {
const probe = createSnapshotProbe('returnTimeout')
addProbe(probe)
const probes = getProbes('TestClass;returnTimeout')!
// Let onEntry succeed with real time
onEntry(probes, {}, { x: 1 })
// Now make performance.now jump forward so the return capture times out
let callCount = 0
const realNow = performance.now.bind(performance)
spyOn(performance, 'now').and.callFake(() => {
callCount++
if (callCount <= 2) {
return realNow()
}
return realNow() + 20
})
onReturn(probes, null, {}, { x: 1 }, { local: 'value' })
expect(mockBatchAdd).not.toHaveBeenCalled()
})
it('should drop snapshot when throw capture exceeds timeout', () => {
const probe = createSnapshotProbe('throwTimeout')
addProbe(probe)
const probes = getProbes('TestClass;throwTimeout')!
// Let onEntry succeed with real time
onEntry(probes, {}, { x: 1 })
// Now make performance.now jump forward so the throw capture times out
let callCount = 0
const realNow = performance.now.bind(performance)
spyOn(performance, 'now').and.callFake(() => {
callCount++
if (callCount <= 2) {
return realNow()
}
return realNow() + 20
})
onThrow(probes, new Error('test'), {}, { x: 1 })
expect(mockBatchAdd).not.toHaveBeenCalled()
})
it('should not affect non-snapshot probes', () => {
const probe: Probe = {
id: 'non-snapshot-timeout',
version: 0,
type: 'LOG_PROBE',
where: { typeName: 'TestClass', methodName: 'nonSnapshot' },
template: 'Test',
captureSnapshot: false,
capture: {},
sampling: { snapshotsPerSecond: 5000 },
evaluateAt: 'ENTRY',
}
addProbe(probe)
// Spike performance.now to simulate slow execution
let callCount = 0
const realNow = performance.now.bind(performance)
spyOn(performance, 'now').and.callFake(() => {
callCount++
if (callCount <= 2) {
return realNow()
}
return realNow() + 20
})
const probes = getProbes('TestClass;nonSnapshot')!
onEntry(probes, {}, {})
onReturn(probes, null, {}, {}, {})
expect(mockBatchAdd).toHaveBeenCalledTimes(1)
})
it('should not leak active entries when entry capture times out', () => {
const probe = createSnapshotProbe('entryLeakTest')
addProbe(probe)