@@ -16,6 +16,7 @@ const grpcutils = require('../../core/grpc-utils');
16
16
const routeManager = require ( '../../core/route-manager' ) ;
17
17
let serverAddress ;
18
18
const ExceptionReporting = require ( '../../../nr-security-agent/lib/core/ExceptionReporting' ) ;
19
+ const responseErrorSet = new Set ( [ 2 , 12 , 13 , 14 , 15 ] )
19
20
20
21
21
22
@@ -58,6 +59,33 @@ module.exports.wrapStartCall = function wrappedClient(shim, callStream) {
58
59
shim . wrap ( callStream . Http2CallStream . prototype , 'start' , wrapStart )
59
60
}
60
61
62
+ /**
63
+ * Wrapper for error collection
64
+ * @param {* } shim
65
+ * @param {* } call
66
+ */
67
+ module . exports . wrapCall = function wrappedCall ( shim , call ) {
68
+ shim . wrap ( call , 'callErrorFromStatus' , function wrapCallErrorFromStatus ( shim , fn ) {
69
+ if ( ! shim . isFunction ( fn ) ) {
70
+ return fn
71
+ }
72
+ return function wrappedCallErrorFromStatus ( ) {
73
+ const errRes = fn . apply ( this , arguments ) ;
74
+ try {
75
+ let error = new Error ( errRes . message ) ;
76
+ error . stack = errRes . stack ;
77
+ const request = requestManager . getRequest ( shim ) ;
78
+ if ( request ) {
79
+ ExceptionReporting . generate5xxReportingEvent ( error , request ) ;
80
+ }
81
+ } catch ( error ) {
82
+ }
83
+
84
+ return errRes ;
85
+ }
86
+ } )
87
+ }
88
+
61
89
module . exports . wrapServer = function wrappedServer ( shim , server ) {
62
90
const grpcVersion = shim . require ( './package.json' ) . version
63
91
logger . info ( "Instrumenting @grpc/grpc-js" , grpcVersion )
@@ -109,6 +137,7 @@ function wrapStart(shim, original) {
109
137
"path" : method ,
110
138
"protocol" : protocol ,
111
139
}
140
+
112
141
if ( request ) {
113
142
const traceObject = secUtils . getTraceObject ( shim ) ;
114
143
const secMetadata = securityMetaData . getSecurityMetaData ( request , urlObj , traceObject , secUtils . getExecutionId ( ) , EVENT_TYPE . HTTP_REQUEST , EVENT_CATEGORY . HTTP )
@@ -256,11 +285,11 @@ function wrapCallback(shim, mod, method) {
256
285
if ( arguments [ 0 ] . details && request ) {
257
286
ExceptionReporting . generateExceptionReportingEvent ( arguments [ 0 ] . details , request ) ;
258
287
}
259
- //TODO GRPC response errors reporting
260
- // if (arguments[0].code && arguments[0].code > 0 && request) {
261
- // let responseCode = arguments[0].code
262
- // ExceptionReporting.generate5xxReportingEvent(null, request, responseCode);
263
- // }
288
+
289
+ if ( arguments [ 0 ] . code && responseErrorSet . has ( arguments [ 0 ] . code ) && request ) {
290
+ let responseCode = arguments [ 0 ] . code
291
+ ExceptionReporting . generate5xxReportingEvent ( null , request , responseCode ) ;
292
+ }
264
293
}
265
294
} catch ( error ) {
266
295
@@ -282,7 +311,7 @@ function wrapCallback(shim, mod, method) {
282
311
function addRequestData ( shim , type , path , args ) {
283
312
const data = Object . assign ( { } ) ;
284
313
try {
285
- const transaction = shim . tracer . getTransaction ( ) ;
314
+ const transaction = shim . tracer . getTransaction ( ) ;
286
315
if ( transaction ) {
287
316
data . method = path ;
288
317
data . url = path ;
0 commit comments