33var fs = require ( 'fs' )
44var path = require ( 'path' )
55
6- var AsyncValuePromise = require ( 'async-value-promise' )
76var hook = require ( 'require-in-the-middle' )
87var semver = require ( 'semver' )
98
10- var Queue = require ( '../queue' )
11- var request = require ( '../request' )
129var Transaction = require ( './transaction' )
10+ var truncate = require ( '../truncate' )
1311var shimmer = require ( './shimmer' )
1412
1513var MODULES = [
@@ -44,7 +42,6 @@ module.exports = Instrumentation
4442
4543function Instrumentation ( agent ) {
4644 this . _agent = agent
47- this . _queue = null
4845 this . _started = false
4946 this . currentTransaction = null
5047}
@@ -57,19 +54,6 @@ Instrumentation.prototype.start = function () {
5754 var self = this
5855 this . _started = true
5956
60- var qopts = {
61- flushInterval : this . _agent . _conf . flushInterval ,
62- maxQueueSize : this . _agent . _conf . maxQueueSize ,
63- logger : this . _agent . logger
64- }
65- this . _queue = new Queue ( qopts , function onFlush ( transactions , done ) {
66- AsyncValuePromise . all ( transactions ) . then ( function ( transactions ) {
67- if ( self . _agent . _conf . active && transactions . length > 0 ) {
68- request . transactions ( self . _agent , transactions , done )
69- }
70- } , done )
71- } )
72-
7357 if ( this . _agent . _conf . asyncHooks && semver . gte ( process . version , '8.2.0' ) ) {
7458 require ( './async-hooks' ) ( this )
7559 } else {
@@ -107,27 +91,44 @@ Instrumentation.prototype._patchModule = function (exports, name, version, enabl
10791}
10892
10993Instrumentation . prototype . addEndedTransaction = function ( transaction ) {
94+ var agent = this . _agent
95+
11096 if ( this . _started ) {
111- var queue = this . _queue
97+ var payload = agent . _filters . process ( transaction . _encode ( ) ) // TODO: Update filter to expect this format
98+ if ( ! payload ) return agent . logger . debug ( 'transaction ignored by filter %o' , { id : transaction . id } )
99+ truncate . transaction ( payload )
100+ agent . logger . debug ( 'sending transaction %o' , { id : transaction . id } )
101+ agent . _apmServer . sendTransaction ( payload )
102+ } else {
103+ agent . logger . debug ( 'ignoring transaction %o' , { id : transaction . id } )
104+ }
105+ }
112106
113- this . _agent . logger . debug ( 'adding transaction to queue %o' , { id : transaction . id } )
107+ Instrumentation . prototype . addEndedSpan = function ( span ) {
108+ var agent = this . _agent
114109
115- var payload = new AsyncValuePromise ( )
110+ if ( this . _started ) {
111+ agent . logger . debug ( 'encoding span %o' , { trans : span . transaction . id , name : span . name , type : span . type } )
112+ span . _encode ( function ( err , payload ) {
113+ if ( err ) {
114+ agent . logger . error ( 'error encoding span %o' , { trans : span . transaction . id , name : span . name , type : span . type , error : err . message } )
115+ return
116+ }
116117
117- payload . catch ( function ( err ) {
118- this . _agent . logger . error ( 'error encoding transaction %s: %s' , transaction . id , err . message )
119- } )
118+ payload = agent . _filters . process ( payload ) // TODO: Update filter to expect this format
120119
121- // Add the transaction payload to the queue instead of the transation
122- // object it self to free up the transaction for garbage collection
123- transaction . _encode ( function ( err , _payload ) {
124- if ( err ) payload . reject ( err )
125- else payload . resolve ( _payload )
126- } )
120+ if ( ! payload ) {
121+ agent . logger . debug ( 'span ignored by filter %o' , { trans : span . transaction . id , name : span . name , type : span . type } )
122+ return
123+ }
127124
128- queue . add ( payload )
125+ truncate . span ( payload )
126+
127+ agent . logger . debug ( 'sending span %o' , { trans : span . transaction . id , name : span . name , type : span . type } )
128+ if ( agent . _apmServer ) agent . _apmServer . sendSpan ( payload )
129+ } )
129130 } else {
130- this . _agent . logger . debug ( 'ignoring transaction %o' , { id : transaction . id } )
131+ agent . logger . debug ( 'ignoring span %o' , { trans : span . transaction . id , name : span . name , type : span . type } )
131132 }
132133}
133134
@@ -217,7 +218,3 @@ Instrumentation.prototype._recoverTransaction = function (trans) {
217218
218219 this . currentTransaction = trans
219220}
220-
221- Instrumentation . prototype . flush = function ( cb ) {
222- this . _queue . flush ( cb )
223- }
0 commit comments