-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathplugin.js
More file actions
77 lines (63 loc) · 1.74 KB
/
plugin.js
File metadata and controls
77 lines (63 loc) · 1.74 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
'use strict'
// TODO: move anything related to tracing to TracingPlugin instead
const dc = require('../../../diagnostics_channel')
const { storage } = require('../../../datadog-core')
class Subscription {
constructor (event, handler) {
this._channel = dc.channel(event)
this._handler = (message, name) => {
const store = storage.getStore()
if (!store || !store.noop) {
handler(message, name)
}
}
}
enable () {
this._channel.subscribe(this._handler)
}
disable () {
this._channel.unsubscribe(this._handler)
}
}
module.exports = class Plugin {
constructor (tracer) {
this._subscriptions = []
this._enabled = false
this._tracer = tracer
}
get tracer () {
return this._tracer._tracer
}
enter (span, store) {
store = store || storage.getStore()
storage.enterWith({ ...store, span })
}
// TODO: Implement filters on resource name for all plugins.
/** Prevents creation of spans here and for all async descendants. */
skip () {
storage.enterWith({ noop: true })
}
addSub (channelName, handler) {
this._subscriptions.push(new Subscription(channelName, handler))
}
addError (error) {
const store = storage.getStore()
if (!store || !store.span) return
if (!store.span._spanContext._tags['error']) {
store.span.setTag('error', error || 1)
}
}
configure (config) {
if (typeof config === 'boolean') {
config = { enabled: config }
}
this.config = config
if (config.enabled && !this._enabled) {
this._enabled = true
this._subscriptions.forEach(sub => sub.enable())
} else if (!config.enabled && this._enabled) {
this._enabled = false
this._subscriptions.forEach(sub => sub.disable())
}
}
}