11var shimmer = require ( '../utils/shimmer' )
22
3- function connectionWrapper ( Connection , agent ) {
4- shimmer . wrap ( Connection . Connection . prototype , 'sendMessage' , function ( original ) {
5- return function ( channel , Method , fields , Properties , props , content ) {
6- return original . apply ( this , arguments )
3+ var defaults = require ( 'lodash.defaults' )
4+ var get = require ( 'lodash.get' )
5+ var microtime = require ( '../optionalDependencies/microtime' )
6+ var debug = require ( 'debug' ) ( 'risingstack/trace:instrumentations:amqplib' )
7+ var format = require ( 'util' ) . format
8+
9+ function consumerWrapper ( agent , original ) {
10+ return function ( queue , callback , options , cb0 ) {
11+ var orig = callback
12+ var wrapped = function ( msg ) {
13+ var originTimestamp = parseInt ( get ( msg , 'properties.headers[\'x-client-send\']' ) , 10 )
14+ var parentServiceKey = parseInt ( get ( msg , 'properties.headers[\'x-parent\']' ) , 10 )
15+ var timestamp = microtime . now ( )
16+ var transportDelay = isNaN ( originTimestamp ) ? undefined : timestamp - originTimestamp
17+ debug ( '#consumerWrapper' , format ( 'incomingEdge [%s %s %s]' , 'amqp' , parentServiceKey , transportDelay ) )
18+ agent . incomingEdgeMetrics . report ( {
19+ serviceKey : parentServiceKey ,
20+ protocol : 'amqp' ,
21+ transportDelay : transportDelay
22+ } )
23+ if ( typeof orig === 'function' ) {
24+ return orig . apply ( this , arguments )
25+ }
26+ }
27+ var args = Array . prototype . slice . apply ( arguments )
28+ args [ 1 ] = wrapped
29+ return original . apply ( this , args )
30+ }
31+ }
32+
33+ function sendMessageWrapper ( agent , original ) {
34+ return function ( channel , Method , fields , Properties , props , content ) {
35+ var traceHeaders = {
36+ 'x-request-id' : agent . getRequestId ( ) || agent . generateRequestId ( ) ,
37+ 'x-span-id' : agent . generateCommId ( ) ,
38+ 'x-client-send' : String ( microtime . now ( ) ) ,
39+ 'x-parent' : String ( agent . getServiceKey ( ) )
740 }
8- } )
41+ debug ( '#sendMessageWrapper' , 'sending instrumented message...' )
42+
43+ defaults ( fields . headers , traceHeaders )
44+
45+ return original . apply ( this , arguments )
46+ }
47+ }
48+
49+ function connectionWrapper ( Connection , agent ) {
50+ shimmer . wrap ( Connection . Connection . prototype , 'sendMessage' , sendMessageWrapper . bind ( null , agent ) )
951 return Connection
1052}
1153
1254function callbackModelWrapper ( CallbackModel , agent ) {
13- shimmer . wrap ( CallbackModel . Channel . prototype , 'consume' , function ( original ) {
14- return function ( queue , callback , options , cb0 ) {
15- return original . apply ( this , arguments )
16- }
17- } )
55+ shimmer . wrap ( CallbackModel . Channel . prototype , 'consume' , consumerWrapper . bind ( null , agent ) )
1856 return CallbackModel
1957}
2058
2159function channelModelWrapper ( ChannelModel , agent ) {
22- shimmer . wrap ( ChannelModel . Channel . prototype , 'consume' , function ( original ) {
23- return function ( queue , callback , options ) {
24- return original . apply ( this , arguments )
25- }
26- } )
60+ shimmer . wrap ( ChannelModel . Channel . prototype , 'consume' , consumerWrapper . bind ( null , agent ) )
2761 return ChannelModel
2862}
2963
@@ -40,19 +74,19 @@ module.exports = {
4074 instrumentations : [ {
4175 path : 'amqplib' ,
4276 pre : function ( ) {
43- require ( 'amqplib/lib/channel_model ' )
77+ require ( 'amqplib/channel_api ' )
4478 return Array . prototype . slice . call ( arguments , 2 )
4579 }
4680 } , {
4781 path : 'amqplib/channel_api' ,
4882 pre : function ( ) {
49- require ( 'amqplib/lib/connection ' )
83+ require ( 'amqplib/lib/channel_model ' )
5084 return Array . prototype . slice . call ( arguments , 2 )
5185 }
5286 } , {
5387 path : 'amqplib/callback_api' ,
5488 pre : function ( ) {
55- require ( 'amqplib/lib/connection ' )
89+ require ( 'amqplib/lib/callback_model ' )
5690 return Array . prototype . slice . call ( arguments , 2 )
5791 }
5892 } , {
0 commit comments