11import type { EventEmitter } from 'events' ;
2+ import { convertIntegrationFnToClass , defineIntegration , getClient } from '@sentry/core' ;
23import { startInactiveSpan } from '@sentry/node' ;
3- import type { Integration } from '@sentry/types' ;
4+ import type { Client , Integration , IntegrationClass , IntegrationFn } from '@sentry/types' ;
45import { fill } from '@sentry/utils' ;
56
67interface GrpcFunction extends CallableFunction {
@@ -25,45 +26,49 @@ interface Stub {
2526 [ key : string ] : GrpcFunctionObject ;
2627}
2728
28- /** Google Cloud Platform service requests tracking for GRPC APIs */
29- export class GoogleCloudGrpc implements Integration {
30- /**
31- * @inheritDoc
32- */
33- public static id : string = 'GoogleCloudGrpc' ;
29+ const SERVICE_PATH_REGEX = / ^ ( \w + ) \. g o o g l e a p i s .c o m $ / ;
3430
35- /**
36- * @inheritDoc
37- */
38- public name : string ;
31+ const INTEGRATION_NAME = 'GoogleCloudGrpc' ;
3932
40- private readonly _optional : boolean ;
33+ const SETUP_CLIENTS = new WeakMap < Client , boolean > ( ) ;
4134
42- public constructor ( options : { optional ?: boolean } = { } ) {
43- this . name = GoogleCloudGrpc . id ;
35+ const _googleCloudGrpcIntegration = ( ( options : { optional ?: boolean } = { } ) => {
36+ const optional = options . optional || false ;
37+ return {
38+ name : INTEGRATION_NAME ,
39+ setupOnce ( ) {
40+ try {
41+ // eslint-disable-next-line @typescript-eslint/no-var-requires
42+ const gaxModule = require ( 'google-gax' ) ;
43+ fill (
44+ gaxModule . GrpcClient . prototype , // eslint-disable-line @typescript-eslint/no-unsafe-member-access
45+ 'createStub' ,
46+ wrapCreateStub ,
47+ ) ;
48+ } catch ( e ) {
49+ if ( ! optional ) {
50+ throw e ;
51+ }
52+ }
53+ } ,
54+ setup ( client ) {
55+ SETUP_CLIENTS . set ( client , true ) ;
56+ } ,
57+ } ;
58+ } ) satisfies IntegrationFn ;
4459
45- this . _optional = options . optional || false ;
46- }
60+ export const googleCloudGrpcIntegration = defineIntegration ( _googleCloudGrpcIntegration ) ;
4761
48- /**
49- * @inheritDoc
50- */
51- public setupOnce ( ) : void {
52- try {
53- // eslint-disable-next-line @typescript-eslint/no-var-requires
54- const gaxModule = require ( 'google-gax' ) ;
55- fill (
56- gaxModule . GrpcClient . prototype , // eslint-disable-line @typescript-eslint/no-unsafe-member-access
57- 'createStub' ,
58- wrapCreateStub ,
59- ) ;
60- } catch ( e ) {
61- if ( ! this . _optional ) {
62- throw e ;
63- }
64- }
65- }
66- }
62+ /**
63+ * Google Cloud Platform service requests tracking for GRPC APIs.
64+ *
65+ * @deprecated Use `googleCloudGrpcIntegration()` instead.
66+ */
67+ // eslint-disable-next-line deprecation/deprecation
68+ export const GoogleCloudGrpc = convertIntegrationFnToClass (
69+ INTEGRATION_NAME ,
70+ googleCloudGrpcIntegration ,
71+ ) as IntegrationClass < Integration > ;
6772
6873/** Returns a wrapped function that returns a stub with tracing enabled */
6974function wrapCreateStub ( origCreate : CreateStubFunc ) : CreateStubFunc {
@@ -107,23 +112,26 @@ function fillGrpcFunction(stub: Stub, serviceIdentifier: string, methodName: str
107112 if ( typeof ret ?. on !== 'function' ) {
108113 return ret ;
109114 }
110- const span = startInactiveSpan ( {
111- name : `${ callType } ${ methodName } ` ,
112- op : `grpc.${ serviceIdentifier } ` ,
113- origin : 'auto.grpc.serverless' ,
114- } ) ;
115- ret . on ( 'status' , ( ) => {
116- if ( span ) {
117- span . end ( ) ;
118- }
119- } ) ;
115+ // only instrument if integration is active on client
116+ if ( SETUP_CLIENTS . has ( getClient ( ) as Client ) ) {
117+ const span = startInactiveSpan ( {
118+ name : `${ callType } ${ methodName } ` ,
119+ op : `grpc.${ serviceIdentifier } ` ,
120+ origin : 'auto.grpc.serverless' ,
121+ } ) ;
122+ ret . on ( 'status' , ( ) => {
123+ if ( span ) {
124+ span . end ( ) ;
125+ }
126+ } ) ;
127+ }
120128 return ret ;
121129 } ,
122130 ) ;
123131}
124132
125133/** Identifies service by its address */
126134function identifyService ( servicePath : string ) : string {
127- const match = servicePath . match ( / ^ ( \w + ) \. g o o g l e a p i s . c o m $ / ) ;
135+ const match = servicePath . match ( SERVICE_PATH_REGEX ) ;
128136 return match ? match [ 1 ] : servicePath ;
129137}
0 commit comments