4
4
*/
5
5
6
6
'use strict'
7
- const TraceSegment = require ( '../../transaction/trace/segment' )
8
7
const genericRecorder = require ( '../../metrics/recorders/generic' )
9
8
const { _nameMessageSegment } = require ( './common' )
10
9
const specs = require ( '../specs' )
@@ -25,7 +24,7 @@ module.exports = createRecorder
25
24
function updateSpecFromArgs ( { shim, fn, fnName, args, spec } ) {
26
25
let msgDesc = null
27
26
if ( shim . isFunction ( spec ) ) {
28
- msgDesc = spec . call ( this , shim , fn , fnName , args )
27
+ msgDesc = spec ( shim , fn , fnName , args )
29
28
} else {
30
29
msgDesc = spec
31
30
const destIdx = shim . normalizeIndex ( args . length , spec . destinationName )
@@ -37,130 +36,21 @@ function updateSpecFromArgs({ shim, fn, fnName, args, spec }) {
37
36
return msgDesc
38
37
}
39
38
40
- /**
41
- * Binds the consumer callback to the active segment.
42
- *
43
- * @private
44
- * @param {object } params to function
45
- * @param {MessageShim } params.shim instance of shim
46
- * @param {Array } params.args arguments passed to original consume function
47
- * @param {specs.MessageSpec } params.msgDesc spec for the wrapped consume function
48
- * @param {TraceSegment } params.segment active segment to bind callback
49
- * @param {boolean } params.getParams flag to copy message parameters to segment
50
- * @param {Function } params.resHandler function to handle response from callback to obtain the message parameters
51
- */
52
- function bindCallback ( { shim, args, msgDesc, segment, getParams, resHandler } ) {
53
- const cbIdx = shim . normalizeIndex ( args . length , msgDesc . callback )
54
- if ( cbIdx !== null ) {
55
- shim . bindCallbackSegment ( args , cbIdx , segment )
56
-
57
- // If we have a callback and a results handler, then wrap the callback so
58
- // we can call the results handler and get the message properties.
59
- if ( resHandler ) {
60
- shim . wrap ( args , cbIdx , function wrapCb ( shim , cb , cbName ) {
61
- if ( shim . isFunction ( cb ) ) {
62
- return function cbWrapper ( ) {
63
- const cbArgs = shim . argsToArray . apply ( shim , arguments )
64
- const msgProps = resHandler . call ( this , shim , cb , cbName , cbArgs )
65
- if ( getParams && msgProps && msgProps . parameters ) {
66
- shim . copySegmentParameters ( segment , msgProps . parameters )
67
- }
68
-
69
- return cb . apply ( this , arguments )
70
- }
71
- }
72
- } )
73
- }
74
- }
75
- }
76
-
77
- /**
78
- * Binds the consumer function to the async context and checks return to possibly
79
- * bind the promise
80
- *
81
- * @private
82
- * @param {object } params to function
83
- * @param {MessageShim } params.shim instance of shim
84
- * @param {Function } params.fn consumer function
85
- * @param {string } params.fnName name of function
86
- * @param {Array } params.args arguments passed to original consume function
87
- * @param {specs.MessageSpec } params.msgDesc spec for the wrapped consume function
88
- * @param {TraceSegment } params.segment active segment to bind callback
89
- * @param {boolean } params.getParams flag to copy message parameters to segment
90
- * @param {Function } params.resHandler function to handle response from callback to obtain the message parameters
91
- * @returns {Promise|* } response from consume function
92
- */
93
- function bindConsumer ( { shim, fn, fnName, args, msgDesc, segment, getParams, resHandler } ) {
94
- // Call the method in the context of our segment.
95
- let ret = shim . applySegment ( fn , segment , true , this , args )
96
-
97
- if ( ret && msgDesc . promise && shim . isPromise ( ret ) ) {
98
- ret = shim . bindPromise ( ret , segment )
99
-
100
- // Intercept the promise to handle the result.
101
- if ( resHandler ) {
102
- ret = ret . then ( function interceptValue ( res ) {
103
- const msgProps = resHandler . call ( this , shim , fn , fnName , res )
104
- if ( getParams && msgProps && msgProps . parameters ) {
105
- shim . copySegmentParameters ( segment , msgProps . parameters )
106
- }
107
- return res
108
- } )
109
- }
110
- }
111
-
112
- return ret
113
- }
114
-
115
39
/**
116
40
*
117
41
* @private
118
42
* @param {object } params to function
119
43
* @param {MessageShim } params.shim instance of shim
120
44
* @param {Function } params.fn function that is being wrapped
121
45
* @param {string } params.fnName name of function
46
+ * @param params.args
122
47
* @param {specs.MessageSpec } params.spec spec for the wrapped consume function
123
- * @returns {Function } recorder for consume function
48
+ * @returns {specs.MessageSpec } updated spec with logic to name segment and apply the genericRecorder
124
49
*/
125
- function createRecorder ( { shim, fn, fnName, spec } ) {
126
- return function consumeRecorder ( ) {
127
- const parent = shim . getSegment ( )
128
- if ( ! parent || ! parent . transaction . isActive ( ) ) {
129
- shim . logger . trace ( 'Not recording consume, no active transaction.' )
130
- return fn . apply ( this , arguments )
131
- }
132
-
133
- // Process the message args.
134
- const args = shim . argsToArray . apply ( shim , arguments )
135
- const msgDesc = updateSpecFromArgs . call ( this , { shim, fn, fnName, args, spec } )
136
-
137
- // Make the segment if we can.
138
- if ( ! msgDesc ) {
139
- shim . logger . trace ( 'Not recording consume, no message descriptor.' )
140
- return fn . apply ( this , args )
141
- }
142
-
143
- const name = _nameMessageSegment ( shim , msgDesc , shim . _metrics . CONSUME )
144
-
145
- // Adds details needed by createSegment when used with a spec
146
- msgDesc . name = name
147
- msgDesc . recorder = genericRecorder
148
- msgDesc . parent = parent
149
-
150
- const segment = shim . createSegment ( msgDesc )
151
- const getParams = shim . agent . config . message_tracer . segment_parameters . enabled
152
- const resHandler = shim . isFunction ( msgDesc . messageHandler ) ? msgDesc . messageHandler : null
153
-
154
- bindCallback ( { shim, args, msgDesc, segment, getParams, resHandler } )
155
- return bindConsumer . call ( this , {
156
- shim,
157
- fn,
158
- fnName,
159
- args,
160
- msgDesc,
161
- segment,
162
- getParams,
163
- resHandler
164
- } )
165
- }
50
+ function createRecorder ( { spec, shim, fn, fnName, args } ) {
51
+ const msgDesc = updateSpecFromArgs ( { shim, fn, fnName, args, spec } )
52
+ // Adds details needed by createSegment when used with a spec
53
+ msgDesc . name = _nameMessageSegment ( shim , msgDesc , shim . _metrics . CONSUME )
54
+ msgDesc . recorder = genericRecorder
55
+ return msgDesc
166
56
}
0 commit comments