-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathtracer.js
141 lines (111 loc) · 2.9 KB
/
tracer.js
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
'use strict'
const Tracer = require('./opentracing/tracer')
const tags = require('../../../ext/tags')
const scopes = require('../../../ext/scopes')
const platform = require('./platform')
const SPAN_TYPE = tags.SPAN_TYPE
const RESOURCE_NAME = tags.RESOURCE_NAME
const SERVICE_NAME = tags.SERVICE_NAME
const ANALYTICS = tags.ANALYTICS
const NOOP = scopes.NOOP
class DatadogTracer extends Tracer {
constructor (config) {
super(config)
this._scopeManager = getScopeManager(config)
this._scope = getScope(config)
}
trace (name, options, fn) {
options = Object.assign({}, {
childOf: this.scope().active()
}, options)
const span = this.startSpan(name, options)
addTags(span, options)
try {
if (fn.length > 1) {
return this.scope().activate(span, () => fn(span, err => {
addError(span, err)
span.finish()
}))
}
const result = this.scope().activate(span, () => fn(span))
if (result && typeof result.then === 'function') {
result.then(
() => span.finish(),
err => {
addError(span, err)
span.finish()
}
)
} else {
span.finish()
}
return result
} catch (e) {
addError(span, e)
span.finish()
throw e
}
}
wrap (name, options, fn) {
const tracer = this
return function () {
const cb = arguments[arguments.length - 1]
if (typeof cb === 'function') {
return tracer.trace(name, options, (span, done) => {
arguments[arguments.length - 1] = function (err) {
done(err)
return cb.apply(this, arguments)
}
return fn.apply(this, arguments)
})
} else {
return tracer.trace(name, options, () => fn.apply(this, arguments))
}
}
}
scopeManager () {
return this._scopeManager
}
scope () {
return this._scope
}
currentSpan () {
return this.scope().active()
}
}
function addError (span, error) {
if (error && error instanceof Error) {
span.addTags({
'error.type': error.name,
'error.msg': error.message,
'error.stack': error.stack
})
}
}
function addTags (span, options) {
const tags = {}
if (options.type) tags[SPAN_TYPE] = options.type
if (options.service) tags[SERVICE_NAME] = options.service
if (options.resource) tags[RESOURCE_NAME] = options.resource
tags[ANALYTICS] = options.analytics
span.addTags(tags)
}
function getScopeManager (config) {
let ScopeManager
if (config.scope === NOOP) {
ScopeManager = require('./scope/noop/scope_manager')
} else {
ScopeManager = require('./scope/scope_manager')
}
return new ScopeManager()
}
function getScope (config) {
let Scope
if (config.scope === NOOP) {
Scope = require('./scope/base')
} else {
Scope = platform.Scope
}
return new Scope(config)
}
module.exports = DatadogTracer