-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathprobes.ts
More file actions
313 lines (285 loc) · 10.2 KB
/
Copy pathprobes.ts
File metadata and controls
313 lines (285 loc) · 10.2 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
import { display } from '@datadog/browser-core'
import { clearActiveEntries } from './activeEntries'
import { compile } from './expression'
import { compileCondition } from './condition'
import type { CompiledCondition } from './condition'
import { templateRequiresEvaluation, compileSegments } from './template'
import type { TemplateSegment, CompiledTemplate } from './template'
import type { CaptureOptions } from './capture'
// Sampling rate limits
const DEFAULT_MAX_SNAPSHOTS_PER_SECOND_GLOBALLY = 25
const DEFAULT_MAX_SNAPSHOTS_PER_SECOND_PER_PROBE = 1
const DEFAULT_MAX_NON_SNAPSHOTS_PER_SECOND_PER_PROBE = 5000
// Global snapshot rate limiting
let globalSnapshotSamplingRateWindowStart = 0
let snapshotsSampledWithinTheLastSecond = 0
export interface ProbeWhere {
typeName?: string
methodName?: string
sourceFile?: string
lines?: string[]
}
export interface ProbeWhen {
dsl: string
json: any
}
export interface ProbeSampling {
snapshotsPerSecond?: number
}
export interface ProbeBudgetConfiguration {
maxSnapshotsPerSecondGlobally?: number
maxSnapshotsPerSecondPerProbe?: number
maxNonSnapshotsPerSecondPerProbe?: number
}
export interface Probe {
id: string
version: number
type: string
where: ProbeWhere
when?: ProbeWhen
template: string | CompiledTemplate
segments?: TemplateSegment[]
captureSnapshot: boolean
capture: CaptureOptions
sampling: ProbeSampling
evaluateAt: 'ENTRY' | 'EXIT'
location?: {
file?: string
lines?: string[]
method?: string
}
}
export interface InitializedProbe extends Probe {
templateRequiresEvaluation: boolean
functionId: string
condition?: CompiledCondition
msBetweenSampling: number
lastCaptureMs: number
}
// Pre-populate with a placeholder key to help V8 optimize property lookups.
// Removing this shows a much larger performance overhead.
// Benchmarks show that using an object is much faster than a Map.
const activeProbes: Record<string, InitializedProbe[]> = {
// @ts-expect-error - Pre-populate with a placeholder key to help V8 optimize property lookups.
__placeholder__: undefined,
}
const probeIdToFunctionId: Record<string, string> = {
// @ts-expect-error - Pre-populate with a placeholder key to help V8 optimize property lookups.
__placeholder__: undefined,
}
let currentProbeBudgetConfiguration: Required<ProbeBudgetConfiguration> = {
maxSnapshotsPerSecondGlobally: DEFAULT_MAX_SNAPSHOTS_PER_SECOND_GLOBALLY,
maxSnapshotsPerSecondPerProbe: DEFAULT_MAX_SNAPSHOTS_PER_SECOND_PER_PROBE,
maxNonSnapshotsPerSecondPerProbe: DEFAULT_MAX_NON_SNAPSHOTS_PER_SECOND_PER_PROBE,
}
export function setProbeBudgetConfiguration(configuration: ProbeBudgetConfiguration = {}): void {
currentProbeBudgetConfiguration = {
maxSnapshotsPerSecondGlobally: normalizeProbeBudgetRate(
configuration.maxSnapshotsPerSecondGlobally,
DEFAULT_MAX_SNAPSHOTS_PER_SECOND_GLOBALLY
),
maxSnapshotsPerSecondPerProbe: normalizeProbeBudgetRate(
configuration.maxSnapshotsPerSecondPerProbe,
DEFAULT_MAX_SNAPSHOTS_PER_SECOND_PER_PROBE
),
maxNonSnapshotsPerSecondPerProbe: normalizeProbeBudgetRate(
configuration.maxNonSnapshotsPerSecondPerProbe,
DEFAULT_MAX_NON_SNAPSHOTS_PER_SECOND_PER_PROBE
),
}
}
export function resetProbeBudgetConfiguration(): void {
setProbeBudgetConfiguration()
}
/**
* Add a probe to the registry
*
* @param probe - The probe configuration
*/
export function addProbe(probe: Probe): void {
initializeProbe(probe)
let probes = activeProbes[probe.functionId]
if (!probes) {
probes = []
activeProbes[probe.functionId] = probes
}
probes.push(probe)
probeIdToFunctionId[probe.id] = probe.functionId
}
/**
* Get initialized probes by function ID
*
* @param functionId - The probe function ID
* @returns The initialized probes
*/
export function getProbes(functionId: string): InitializedProbe[] | undefined {
return activeProbes[functionId]
}
/**
* Get all active probes across all functions
*
* @returns Array of all active probes
*/
export function getAllProbes(): InitializedProbe[] {
const allProbes: InitializedProbe[] = []
for (const probes of Object.values(activeProbes)) {
if (probes) {
allProbes.push(...probes)
}
}
return allProbes
}
/**
* Remove a probe from the registry
*
* @param id - The probe ID
*/
export function removeProbe(id: string): void {
const functionId = probeIdToFunctionId[id]
if (!functionId) {
throw new Error(`Probe with id ${id} not found`)
}
const probes = activeProbes[functionId]
if (!probes) {
throw new Error(`Probes with function id ${functionId} not found`)
}
for (let i = 0; i < probes.length; i++) {
const probe = probes[i]
if (probe.id === id) {
if (typeof probe.template === 'object' && probe.template !== null && probe.template.clearCache) {
probe.template.clearCache()
}
if (typeof probe.condition === 'object' && probe.condition !== null && probe.condition.clearCache) {
probe.condition.clearCache()
}
probes.splice(i, 1)
// TODO: Gracefully drain in-flight entries instead of clearing them immediately.
// Deleting a probe can currently race with return/throw handling, whether removal
// comes from delivery updates or budget-based auto-unregistering.
clearActiveEntries(id)
break
}
}
delete probeIdToFunctionId[id]
if (probes.length === 0) {
delete activeProbes[functionId]
}
}
/**
* Clear all probes (useful for testing)
*/
export function clearProbes(): void {
for (const probes of Object.values(activeProbes)) {
if (probes) {
for (const probe of probes) {
if (typeof probe.template === 'object' && probe.template !== null && probe.template.clearCache) {
probe.template.clearCache()
}
if (typeof probe.condition === 'object' && probe.condition !== null && probe.condition.clearCache) {
probe.condition.clearCache()
}
}
}
}
for (const functionId of Object.keys(activeProbes)) {
if (functionId !== '__placeholder__') {
delete activeProbes[functionId]
}
}
for (const probeId of Object.keys(probeIdToFunctionId)) {
if (probeId !== '__placeholder__') {
delete probeIdToFunctionId[probeId]
}
}
clearActiveEntries()
globalSnapshotSamplingRateWindowStart = 0
snapshotsSampledWithinTheLastSecond = 0
}
/**
* Check global snapshot sampling budget
*
* @param now - Current timestamp in milliseconds
* @param captureSnapshot - Whether this probe captures snapshots
* @returns True if within budget, false if rate limited
*/
export function checkGlobalSnapshotBudget(now: number, captureSnapshot: boolean): boolean {
// Only enforce global budget for probes that capture snapshots
if (!captureSnapshot) {
return true
}
// Reset counter if a second has passed
// This algorithm is not a perfect sliding window, but it's quick and easy
if (now - globalSnapshotSamplingRateWindowStart > 1000) {
snapshotsSampledWithinTheLastSecond = 1
globalSnapshotSamplingRateWindowStart = now
return true
}
// Check if we've exceeded the global limit
if (snapshotsSampledWithinTheLastSecond >= currentProbeBudgetConfiguration.maxSnapshotsPerSecondGlobally) {
return false
}
// Increment counter and allow
snapshotsSampledWithinTheLastSecond++
return true
}
/**
* Initialize a probe by preprocessing template segments, conditions, and sampling
*
* @param probe - The probe configuration
*/
export function initializeProbe(probe: Probe): asserts probe is InitializedProbe {
// TODO: Add support for anonymous functions (Currently only uniquely named functions are supported)
;(probe as InitializedProbe).functionId = `${probe.where.typeName};${probe.where.methodName}`
// Compile condition if present
try {
if (probe.when?.json) {
;(probe as InitializedProbe).condition = compileCondition(String(compile(probe.when.json)))
}
} catch (err) {
// TODO: Handle error properly
display.error(
`Cannot compile condition expression: ${probe.when!.dsl} (probe: ${probe.id}, version: ${probe.version})`,
err as Error
)
}
// Optimize for fast calculations when probe is hit
;(probe as InitializedProbe).templateRequiresEvaluation = templateRequiresEvaluation(probe.segments)
if ((probe as InitializedProbe).templateRequiresEvaluation) {
const segmentsCode = compileSegments(probe.segments!)
// Pre-build the function body so we avoid rebuilding this string on every probe hit.
// The actual Function is created at runtime because the parameter names (context keys)
// aren't known until call time. For ENTRY probes there is exactly one set of keys; for
// EXIT probes there can be two (normal-return vs exception path).
const fnBodyTemplate = `return ${segmentsCode};`
// Cache compiled functions by context keys to avoid recreating them
const functionCache = new Map<string, (...args: any[]) => any[]>()
// Store the template with a factory that caches functions
probe.template = {
createFunction: (contextKeys: string[]) => {
const cacheKey = contextKeys.join(',')
let fn = functionCache.get(cacheKey)
if (!fn) {
// eslint-disable-next-line no-new-func, @typescript-eslint/no-implied-eval
fn = new Function('$dd_inspect', ...contextKeys, fnBodyTemplate) as (...args: any[]) => any[]
functionCache.set(cacheKey, fn)
}
return fn
},
clearCache: () => {
functionCache.clear()
},
}
}
delete probe.segments
// Optimize for fast calculations when probe is hit - calculate sampling budget
const snapshotsPerSecond =
probe.sampling?.snapshotsPerSecond ??
(probe.captureSnapshot
? currentProbeBudgetConfiguration.maxSnapshotsPerSecondPerProbe
: currentProbeBudgetConfiguration.maxNonSnapshotsPerSecondPerProbe)
;(probe as InitializedProbe).msBetweenSampling = (1 / snapshotsPerSecond) * 1000 // Convert to milliseconds
;(probe as InitializedProbe).lastCaptureMs = -Infinity // Initialize to -Infinity to allow first call
}
function normalizeProbeBudgetRate(rate: number | undefined, defaultRate: number): number {
return typeof rate === 'number' && Number.isFinite(rate) && rate > 0 ? rate : defaultRate
}