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 = [
@@ -46,7 +44,6 @@ module.exports = Instrumentation
4644
4745function Instrumentation ( agent ) {
4846 this . _agent = agent
49- this . _queue = null
5047 this . _hook = null // this._hook is only exposed for testing purposes
5148 this . _started = false
5249 this . currentTransaction = null
@@ -60,21 +57,6 @@ Instrumentation.prototype.start = function () {
6057 var self = this
6158 this . _started = true
6259
63- var qopts = {
64- flushInterval : this . _agent . _conf . flushInterval ,
65- maxQueueSize : this . _agent . _conf . maxQueueSize ,
66- logger : this . _agent . logger
67- }
68- this . _queue = new Queue ( qopts , function onFlush ( transactions , done ) {
69- AsyncValuePromise . all ( transactions ) . then ( function ( transactions ) {
70- if ( self . _agent . _conf . active && transactions . length > 0 ) {
71- request . transactions ( self . _agent , transactions , done )
72- } else {
73- done ( )
74- }
75- } , done )
76- } )
77-
7860 if ( this . _agent . _conf . asyncHooks && semver . gte ( process . version , '8.2.0' ) ) {
7961 require ( './async-hooks' ) ( this )
8062 } else {
@@ -111,27 +93,44 @@ Instrumentation.prototype._patchModule = function (exports, name, version, enabl
11193}
11294
11395Instrumentation . prototype . addEndedTransaction = function ( transaction ) {
96+ var agent = this . _agent
97+
11498 if ( this . _started ) {
115- var queue = this . _queue
99+ var payload = agent . _filters . process ( transaction . _encode ( ) ) // TODO: Update filter to expect this format
100+ if ( ! payload ) return agent . logger . debug ( 'transaction ignored by filter %o' , { id : transaction . id } )
101+ truncate . transaction ( payload )
102+ agent . logger . debug ( 'sending transaction %o' , { id : transaction . id } )
103+ agent . _apmServer . sendTransaction ( payload )
104+ } else {
105+ agent . logger . debug ( 'ignoring transaction %o' , { id : transaction . id } )
106+ }
107+ }
116108
117- this . _agent . logger . debug ( 'adding transaction to queue %o' , { id : transaction . id } )
109+ Instrumentation . prototype . addEndedSpan = function ( span ) {
110+ var agent = this . _agent
118111
119- var payload = new AsyncValuePromise ( )
112+ if ( this . _started ) {
113+ agent . logger . debug ( 'encoding span %o' , { trans : span . transaction . id , name : span . name , type : span . type } )
114+ span . _encode ( function ( err , payload ) {
115+ if ( err ) {
116+ agent . logger . error ( 'error encoding span %o' , { trans : span . transaction . id , name : span . name , type : span . type , error : err . message } )
117+ return
118+ }
120119
121- payload . catch ( function ( err ) {
122- this . _agent . logger . error ( 'error encoding transaction %s: %s' , transaction . id , err . message )
123- } )
120+ payload = agent . _filters . process ( payload ) // TODO: Update filter to expect this format
124121
125- // Add the transaction payload to the queue instead of the transation
126- // object it self to free up the transaction for garbage collection
127- transaction . _encode ( function ( err , _payload ) {
128- if ( err ) payload . reject ( err )
129- else payload . resolve ( _payload )
130- } )
122+ if ( ! payload ) {
123+ agent . logger . debug ( 'span ignored by filter %o' , { trans : span . transaction . id , name : span . name , type : span . type } )
124+ return
125+ }
131126
132- queue . add ( payload )
127+ truncate . span ( payload )
128+
129+ agent . logger . debug ( 'sending span %o' , { trans : span . transaction . id , name : span . name , type : span . type } )
130+ if ( agent . _apmServer ) agent . _apmServer . sendSpan ( payload )
131+ } )
133132 } else {
134- this . _agent . logger . debug ( 'ignoring transaction %o' , { id : transaction . id } )
133+ agent . logger . debug ( 'ignoring span %o' , { trans : span . transaction . id , name : span . name , type : span . type } )
135134 }
136135}
137136
@@ -221,11 +220,3 @@ Instrumentation.prototype._recoverTransaction = function (trans) {
221220
222221 this . currentTransaction = trans
223222}
224-
225- Instrumentation . prototype . flush = function ( cb ) {
226- if ( this . _queue ) {
227- this . _queue . flush ( cb )
228- } else {
229- process . nextTick ( cb )
230- }
231- }
0 commit comments