dd-trace 2.12.0 → 2.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dd-trace",
3
- "version": "2.12.0",
3
+ "version": "2.12.1",
4
4
  "description": "Datadog APM tracing client for JavaScript",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -78,12 +78,12 @@ function wrapLayerHandle (layer) {
78
78
 
79
79
  try {
80
80
  return original.apply(this, arguments)
81
- } catch (e) {
82
- errorChannel.publish(e)
81
+ } catch (error) {
82
+ errorChannel.publish({ req, error })
83
83
  nextChannel.publish({ req })
84
84
  exitChannel.publish({ req })
85
85
 
86
- throw e
86
+ throw error
87
87
  }
88
88
  })
89
89
  })
@@ -92,7 +92,7 @@ function wrapLayerHandle (layer) {
92
92
  function wrapNext (req, next) {
93
93
  return function (error) {
94
94
  if (error) {
95
- errorChannel.publish(error)
95
+ errorChannel.publish({ req, error })
96
96
  }
97
97
 
98
98
  nextChannel.publish({ req })
@@ -6,7 +6,6 @@ const { addHook, channel, AsyncResource } = require('./helpers/instrument')
6
6
  const errorChannel = channel('apm:fastify:middleware:error')
7
7
  const handleChannel = channel('apm:fastify:request:handle')
8
8
 
9
- const requestResources = new WeakMap()
10
9
  const parsingResources = new WeakMap()
11
10
 
12
11
  function wrapFastify (fastify, hasParsingEvents) {
@@ -42,16 +41,13 @@ function wrapAddHook (addHook) {
42
41
 
43
42
  arguments[arguments.length - 1] = shimmer.wrap(fn, function (request, reply, done) {
44
43
  const req = getReq(request)
45
- const requestResource = requestResources.get(req)
46
-
47
- if (!requestResource) return fn.apply(this, arguments)
48
44
 
49
45
  try {
50
46
  if (typeof done === 'function') {
51
47
  done = arguments[arguments.length - 1]
52
48
 
53
49
  arguments[arguments.length - 1] = function (err) {
54
- publishError(err, requestResource)
50
+ publishError(err, req)
55
51
 
56
52
  if (name === 'onRequest' || name === 'preParsing') {
57
53
  const parsingResource = new AsyncResource('bound-anonymous-fn')
@@ -71,13 +67,13 @@ function wrapAddHook (addHook) {
71
67
  const promise = fn.apply(this, arguments)
72
68
 
73
69
  if (promise && typeof promise.catch === 'function') {
74
- return promise.catch(err => publishError(err, requestResource))
70
+ return promise.catch(err => publishError(err, req))
75
71
  }
76
72
 
77
73
  return promise
78
74
  }
79
75
  } catch (e) {
80
- throw publishError(e, requestResource)
76
+ throw publishError(e, req)
81
77
  }
82
78
  })
83
79
 
@@ -90,14 +86,10 @@ function onRequest (request, reply, done) {
90
86
 
91
87
  const req = getReq(request)
92
88
  const res = getRes(reply)
93
- const requestResource = new AsyncResource('bound-anonymous-fn')
94
89
 
95
- requestResources.set(req, requestResource)
90
+ handleChannel.publish({ req, res })
96
91
 
97
- return requestResource.runInAsyncScope(() => {
98
- handleChannel.publish({ req, res })
99
- return done()
100
- })
92
+ return done()
101
93
  }
102
94
 
103
95
  function preHandler (request, reply, done) {
@@ -105,9 +97,8 @@ function preHandler (request, reply, done) {
105
97
  if (!reply || typeof reply.send !== 'function') return done()
106
98
 
107
99
  const req = getReq(request)
108
- const requestResource = requestResources.get(req)
109
100
 
110
- reply.send = wrapSend(reply.send, requestResource)
101
+ reply.send = wrapSend(reply.send, req)
111
102
 
112
103
  done()
113
104
  }
@@ -133,12 +124,10 @@ function preParsing (request, reply, payload, done) {
133
124
  parsingResource.runInAsyncScope(() => done())
134
125
  }
135
126
 
136
- function wrapSend (send, resource) {
137
- return function sendWithTrace (payload) {
138
- if (payload instanceof Error) {
139
- resource.runInAsyncScope(() => {
140
- errorChannel.publish(payload)
141
- })
127
+ function wrapSend (send, req) {
128
+ return function sendWithTrace (error) {
129
+ if (error instanceof Error) {
130
+ errorChannel.publish({ req, error })
142
131
  }
143
132
 
144
133
  return send.apply(this, arguments)
@@ -153,11 +142,9 @@ function getRes (reply) {
153
142
  return reply && (reply.raw || reply.res || reply)
154
143
  }
155
144
 
156
- function publishError (error, resource) {
145
+ function publishError (error, req) {
157
146
  if (error) {
158
- resource.runInAsyncScope(() => {
159
- errorChannel.publish(error)
160
- })
147
+ errorChannel.publish({ error, req })
161
148
  }
162
149
 
163
150
  return error
@@ -126,13 +126,13 @@ function wrapMiddleware (fn, layer) {
126
126
  }
127
127
 
128
128
  function fulfill (ctx, error) {
129
- if (error) {
130
- errorChannel.publish(error)
131
- }
132
-
133
129
  const req = ctx.req
134
130
  const route = ctx.routePath
135
131
 
132
+ if (error) {
133
+ errorChannel.publish({ req, error })
134
+ }
135
+
136
136
  // TODO: make sure that the parent class cannot override this in `enter`
137
137
  if (route) {
138
138
  routeChannel.publish({ req, route })
@@ -49,12 +49,12 @@ function createWrapRouterMethod (name) {
49
49
 
50
50
  try {
51
51
  return original.apply(this, arguments)
52
- } catch (e) {
53
- errorChannel.publish(e)
52
+ } catch (error) {
53
+ errorChannel.publish({ req, error })
54
54
  nextChannel.publish({ req })
55
55
  exitChannel.publish({ req })
56
56
 
57
- throw e
57
+ throw error
58
58
  }
59
59
  })
60
60
  })
@@ -91,7 +91,7 @@ function createWrapRouterMethod (name) {
91
91
  function wrapNext (req, next) {
92
92
  return function (error) {
93
93
  if (error) {
94
- errorChannel.publish(error)
94
+ errorChannel.publish({ req, error })
95
95
  }
96
96
 
97
97
  nextChannel.publish({ req })
@@ -16,7 +16,10 @@ class RouterPlugin extends WebPlugin {
16
16
  this._contexts = new WeakMap()
17
17
 
18
18
  this.addSub(`apm:${this.constructor.name}:middleware:enter`, ({ req, name, route }) => {
19
- const childOf = this._getActive(req)
19
+ const childOf = this._getActive(req) || this._getStoreSpan()
20
+
21
+ if (!childOf) return
22
+
20
23
  const span = this._getMiddlewareSpan(name, childOf)
21
24
  const context = this._createContext(req, route, childOf)
22
25
 
@@ -46,14 +49,16 @@ class RouterPlugin extends WebPlugin {
46
49
  context.middleware.pop().finish()
47
50
  })
48
51
 
49
- this.addSub(`apm:${this.constructor.name}:middleware:error`, err => {
50
- const store = storage.getStore()
52
+ this.addSub(`apm:${this.constructor.name}:middleware:error`, ({ req, error }) => {
53
+ web.addError(req, error)
51
54
 
52
- web.addError(store.req, err)
55
+ if (!this.config.middleware) return
53
56
 
54
- if (this.config.middleware) {
55
- this.addError(err)
56
- }
57
+ const span = this._getActive(req)
58
+
59
+ if (!span) return
60
+
61
+ span.setTag('error', error)
57
62
  })
58
63
 
59
64
  this.addSub(`apm:http:server:request:finish`, ({ req }) => {
@@ -72,7 +77,7 @@ class RouterPlugin extends WebPlugin {
72
77
  _getActive (req) {
73
78
  const context = this._contexts.get(req)
74
79
 
75
- if (!context) return this._getStoreSpan()
80
+ if (!context) return
76
81
  if (context.middleware.length === 0) return context.span
77
82
 
78
83
  return context.middleware[context.middleware.length - 1]
@@ -270,7 +270,10 @@ const web = {
270
270
  addError (req, error) {
271
271
  if (error instanceof Error) {
272
272
  const context = contexts.get(req)
273
- context.error = error
273
+
274
+ if (context) {
275
+ context.error = error
276
+ }
274
277
  }
275
278
  },
276
279