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