1
+ /* eslint-disable promise/param-names */
1
2
import { Plugin } from 'graphql-yoga' ;
2
3
import newRelic from 'newrelic' ;
3
4
import NAMES from 'newrelic/lib/metrics/names.js' ;
@@ -13,8 +14,8 @@ const EnvelopAttributeName = 'Envelop_NewRelic_Plugin';
13
14
14
15
export default function useMeshNewrelic (
15
16
options : MeshPluginOptions < YamlConfig . NewrelicConfig > ,
17
+ { instrumentationApi = newRelic ?. shim , agentApi = newRelic } : any = { } ,
16
18
) : MeshPlugin < any > & Plugin < any > {
17
- const instrumentationApi = newRelic ?. shim ;
18
19
if ( ! instrumentationApi ?. agent ) {
19
20
options . logger . debug (
20
21
'Agent unavailable. Please check your New Relic Agent configuration and ensure New Relic is enabled.' ,
@@ -29,13 +30,13 @@ export default function useMeshNewrelic(
29
30
const logger = instrumentationApi . logger . child ( { component : EnvelopAttributeName } ) ;
30
31
31
32
const segmentByRequestContext = new WeakMap < any , any > ( ) ;
32
- const transactionCallback = new WeakMap < Request , ( ) => void > ( ) ;
33
33
34
34
return {
35
35
onPluginInit ( { addPlugin } ) {
36
36
addPlugin (
37
37
useNewRelic ( {
38
38
...options ,
39
+ shim : instrumentationApi ,
39
40
extractOperationName : options . extractOperationName
40
41
? context =>
41
42
stringInterpolator . parse ( options . extractOperationName , {
@@ -46,12 +47,11 @@ export default function useMeshNewrelic(
46
47
} ) ,
47
48
) ;
48
49
} ,
49
- onRequest ( { request , url } ) {
50
- const currentTransaction = instrumentationApi . getTransaction ( ) ;
50
+ onRequest ( { url , requestHandler , setRequestHandler } ) {
51
+ const currentTransaction = instrumentationApi . agent . tracer . getTransaction ( ) ;
51
52
if ( ! currentTransaction ) {
52
- instrumentationApi . startWebTransaction (
53
- url . pathname ,
54
- ( ) => new Promise < void > ( resolve => transactionCallback . set ( request , resolve ) ) ,
53
+ setRequestHandler ( ( ...args ) =>
54
+ agentApi . startWebTransaction ( url . pathname , ( ) => requestHandler ( ...args ) ) ,
55
55
) ;
56
56
}
57
57
} ,
@@ -157,11 +157,5 @@ export default function useMeshNewrelic(
157
157
httpDetailSegment . end ( ) ;
158
158
} ;
159
159
} ,
160
- onResponse ( { request } ) {
161
- const callback = transactionCallback . get ( request ) ;
162
- if ( callback ) {
163
- callback ( ) ;
164
- }
165
- } ,
166
160
} ;
167
161
}
0 commit comments