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 = [
@@ -43,7 +41,6 @@ module.exports = Instrumentation
4341
4442function Instrumentation ( agent ) {
4543 this . _agent = agent
46- this . _queue = null
4744 this . _started = false
4845 this . currentTransaction = null
4946}
@@ -56,19 +53,6 @@ Instrumentation.prototype.start = function () {
5653 var self = this
5754 this . _started = true
5855
59- var qopts = {
60- flushInterval : this . _agent . _conf . flushInterval ,
61- maxQueueSize : this . _agent . _conf . maxQueueSize ,
62- logger : this . _agent . logger
63- }
64- this . _queue = new Queue ( qopts , function onFlush ( transactions , done ) {
65- AsyncValuePromise . all ( transactions ) . then ( function ( transactions ) {
66- if ( self . _agent . _conf . active && transactions . length > 0 ) {
67- request . transactions ( self . _agent , transactions , done )
68- }
69- } , done )
70- } )
71-
7256 if ( this . _agent . _conf . asyncHooks && semver . gte ( process . version , '8.2.0' ) ) {
7357 require ( './async-hooks' ) ( this )
7458 } else {
@@ -106,27 +90,44 @@ Instrumentation.prototype._patchModule = function (exports, name, version, enabl
10690}
10791
10892Instrumentation . prototype . addEndedTransaction = function ( transaction ) {
93+ var agent = this . _agent
94+
10995 if ( this . _started ) {
110- var queue = this . _queue
96+ var payload = agent . _filters . process ( transaction . _encode ( ) ) // TODO: Update filter to expect this format
97+ if ( ! payload ) return agent . logger . debug ( 'transaction ignored by filter %o' , { id : transaction . id } )
98+ truncate . transaction ( payload )
99+ agent . logger . debug ( 'sending transaction %o' , { id : transaction . id } )
100+ if ( agent . _apmServer ) agent . _apmServer . sendTransaction ( payload )
101+ } else {
102+ agent . logger . debug ( 'ignoring transaction %o' , { id : transaction . id } )
103+ }
104+ }
111105
112- this . _agent . logger . debug ( 'adding transaction to queue %o' , { id : transaction . id } )
106+ Instrumentation . prototype . addEndedSpan = function ( span ) {
107+ var agent = this . _agent
113108
114- var payload = new AsyncValuePromise ( )
109+ if ( this . _started ) {
110+ agent . logger . debug ( 'encoding span %o' , { trans : span . transaction . id , name : span . name , type : span . type } )
111+ span . _encode ( function ( err , payload ) {
112+ if ( err ) {
113+ agent . logger . error ( 'error encoding span %o' , { trans : span . transaction . id , name : span . name , type : span . type , error : err . message } )
114+ return
115+ }
115116
116- payload . catch ( function ( err ) {
117- this . _agent . logger . error ( 'error encoding transaction %s: %s' , transaction . id , err . message )
118- } )
117+ payload = agent . _filters . process ( payload ) // TODO: Update filter to expect this format
119118
120- // Add the transaction payload to the queue instead of the transation
121- // object it self to free up the transaction for garbage collection
122- transaction . _encode ( function ( err , _payload ) {
123- if ( err ) payload . reject ( err )
124- else payload . resolve ( _payload )
125- } )
119+ if ( ! payload ) {
120+ agent . logger . debug ( 'span ignored by filter %o' , { trans : span . transaction . id , name : span . name , type : span . type } )
121+ return
122+ }
126123
127- queue . add ( payload )
124+ truncate . span ( payload )
125+
126+ agent . logger . debug ( 'sending span %o' , { trans : span . transaction . id , name : span . name , type : span . type } )
127+ if ( agent . _apmServer ) agent . _apmServer . sendSpan ( payload )
128+ } )
128129 } else {
129- this . _agent . logger . debug ( 'ignoring transaction %o' , { id : transaction . id } )
130+ agent . logger . debug ( 'ignoring span %o' , { trans : span . transaction . id , name : span . name , type : span . type } )
130131 }
131132}
132133
@@ -216,7 +217,3 @@ Instrumentation.prototype._recoverTransaction = function (trans) {
216217
217218 this . currentTransaction = trans
218219}
219-
220- Instrumentation . prototype . flush = function ( cb ) {
221- this . _queue . flush ( cb )
222- }
0 commit comments