Skip to content

Commit 9009a63

Browse files
committed
refactor: use symbol instead of weak map for associating data with contexts (#6276)
* Use symbol instead of weak map for associating data with contexts * Use private fields in EventPlugin
1 parent 98f92f1 commit 9009a63

File tree

1 file changed

+22
-22
lines changed
  • packages/dd-trace/src/profiling/profilers/event_plugins

1 file changed

+22
-22
lines changed

packages/dd-trace/src/profiling/profilers/event_plugins/event.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,55 @@ const { performance } = require('perf_hooks')
77
// start/error/finish methods to the appropriate diagnostic channels.
88
// TODO: Decouple this from TracingPlugin.
99
class EventPlugin extends TracingPlugin {
10+
#eventHandler
11+
#eventFilter
12+
#dataSymbol
13+
#entryType
14+
1015
constructor (eventHandler, eventFilter) {
1116
super()
12-
this.eventHandler = eventHandler
13-
this.eventFilter = eventFilter
14-
this.contextData = new WeakMap()
15-
this.entryType = this.constructor.entryType
17+
this.#eventHandler = eventHandler
18+
this.#eventFilter = eventFilter
19+
this.#entryType = this.constructor.entryType
20+
this.#dataSymbol = Symbol(`dd-trace.profiling.event.${this.#entryType}.${this.constructor.operation}`)
1621
}
1722

1823
start (ctx) {
19-
this.contextData.set(ctx, {
20-
startEvent: ctx,
21-
startTime: performance.now()
22-
})
24+
ctx[this.#dataSymbol] = performance.now()
2325
}
2426

2527
error (ctx) {
26-
const data = this.contextData.get(ctx)
27-
if (data) {
28-
data.error = true
29-
}
28+
// We don't emit perf events for failed operations
29+
ctx[this.#dataSymbol] = undefined
3030
}
3131

3232
finish (ctx) {
33-
const data = this.contextData.get(ctx)
34-
35-
if (!data) return
36-
this.contextData.delete(ctx)
33+
const startTime = ctx[this.#dataSymbol]
34+
if (startTime === undefined) {
35+
return
36+
}
37+
ctx[this.#dataSymbol] = undefined
3738

38-
const { startEvent, startTime, error } = data
39-
if (error || this.ignoreEvent(startEvent)) {
40-
return // don't emit perf events for failed operations or ignored events
39+
if (this.ignoreEvent(ctx)) {
40+
return // don't emit perf events for ignored events
4141
}
4242

4343
const duration = performance.now() - startTime
4444
const event = {
45-
entryType: this.entryType,
45+
entryType: this.#entryType,
4646
startTime,
4747
duration
4848
}
4949

50-
if (!this.eventFilter(event)) {
50+
if (!this.#eventFilter(event)) {
5151
return
5252
}
5353

5454
const context = (ctx.currentStore?.span || this.activeSpan)?.context()
5555
event._ddSpanId = context?.toSpanId()
5656
event._ddRootSpanId = context?._trace.started[0]?.context().toSpanId() || event._ddSpanId
5757

58-
this.eventHandler(this.extendEvent(event, startEvent))
58+
this.#eventHandler(this.extendEvent(event, ctx))
5959
}
6060

6161
ignoreEvent () {

0 commit comments

Comments
 (0)