@@ -54,6 +54,12 @@ Object.defineProperty(Agent.prototype, 'currentTransaction', {
5454 }
5555} )
5656
57+ Object . defineProperty ( Agent . prototype , 'currentSpan' , {
58+ get ( ) {
59+ return this . _instrumentation . currentSpan
60+ }
61+ } )
62+
5763Agent . prototype . destroy = function ( ) {
5864 if ( this . _apmServer ) this . _apmServer . destroy ( )
5965}
@@ -242,8 +248,9 @@ Agent.prototype.captureError = function (err, opts, cb) {
242248
243249 var agent = this
244250 var trans = this . currentTransaction
251+ var span = this . currentSpan
245252 var timestamp = Date . now ( ) * 1000
246- var id = crypto . randomBytes ( 128 / 8 ) . toString ( 'hex' )
253+ var context = ( span || trans || { } ) . context || { }
247254 var req = opts && opts . request instanceof IncomingMessage
248255 ? opts . request
249256 : trans && trans . req
@@ -271,7 +278,9 @@ Agent.prototype.captureError = function (err, opts, cb) {
271278 }
272279
273280 function prepareError ( error ) {
274- error . id = id
281+ error . id = crypto . randomBytes ( 16 ) . toString ( 'hex' )
282+ error . parent_id = context . id
283+ error . trace_id = context . traceId
275284 error . timestamp = timestamp
276285 error . context = {
277286 user : Object . assign (
@@ -291,7 +300,8 @@ Agent.prototype.captureError = function (err, opts, cb) {
291300 opts && opts . custom
292301 )
293302 }
294- if ( trans ) error . transaction = { id : trans . id }
303+
304+ if ( trans ) error . transaction_id = trans . id
295305
296306 if ( error . exception ) {
297307 error . exception . handled = ! opts || opts . handled
@@ -345,21 +355,21 @@ Agent.prototype.captureError = function (err, opts, cb) {
345355 error = agent . _errorFilters . process ( error )
346356
347357 if ( ! error ) {
348- agent . logger . debug ( 'error ignored by filter %o' , { id : id } )
349- if ( cb ) cb ( null , id )
358+ agent . logger . debug ( 'error ignored by filter %o' , { id : error . id } )
359+ if ( cb ) cb ( null , error . id )
350360 return
351361 }
352362
353363 if ( agent . _apmServer ) {
354- agent . logger . info ( `Sending error ${ id } to Elastic APM` )
364+ agent . logger . info ( `Sending error to Elastic APM` , { id : error . id } )
355365 agent . _apmServer . sendError ( error , function ( ) {
356366 agent . _apmServer . flush ( function ( err ) {
357- if ( cb ) cb ( err , id )
367+ if ( cb ) cb ( err , error . id )
358368 } )
359369 } )
360370 } else if ( cb ) {
361371 // TODO: Swallow this error just as it's done in agent.flush()?
362- process . nextTick ( cb . bind ( null , new Error ( 'cannot capture error before agent is started' ) , id ) )
372+ process . nextTick ( cb . bind ( null , new Error ( 'cannot capture error before agent is started' ) , error . id ) )
363373 }
364374 }
365375}
0 commit comments