1- // '@google-cloud/common' import is expected to be type-only so it's erased in the final .js file.
2- // When TypeScript compiler is upgraded, use `import type` syntax to explicitly assert that we don't want to load a module here.
31import type * as common from '@google-cloud/common' ;
2+ import { convertIntegrationFnToClass , defineIntegration , getClient } from '@sentry/core' ;
43import { startInactiveSpan } from '@sentry/node' ;
5- import type { Integration } from '@sentry/types' ;
4+ import type { Client , Integration , IntegrationClass , IntegrationFn } from '@sentry/types' ;
65import { fill } from '@sentry/utils' ;
76
87type RequestOptions = common . DecorateRequestOptions ;
@@ -12,51 +11,56 @@ interface RequestFunction extends CallableFunction {
1211 ( reqOpts : RequestOptions , callback : ResponseCallback ) : void ;
1312}
1413
15- /** Google Cloud Platform service requests tracking for RESTful APIs */
16- export class GoogleCloudHttp implements Integration {
17- /**
18- * @inheritDoc
19- */
20- public static id : string = 'GoogleCloudHttp' ;
14+ const INTEGRATION_NAME = 'GoogleCloudHttp' ;
2115
22- /**
23- * @inheritDoc
24- */
25- public name : string ;
16+ const SETUP_CLIENTS = new WeakMap < Client , boolean > ( ) ;
2617
27- private readonly _optional : boolean ;
28-
29- public constructor ( options : { optional ?: boolean } = { } ) {
30- this . name = GoogleCloudHttp . id ;
18+ const _googleCloudHttpIntegration = ( ( options : { optional ?: boolean } = { } ) => {
19+ const optional = options . optional || false ;
20+ return {
21+ name : INTEGRATION_NAME ,
22+ setupOnce ( ) {
23+ try {
24+ // eslint-disable-next-line @typescript-eslint/no-var-requires
25+ const commonModule = require ( '@google-cloud/common' ) as typeof common ;
26+ fill ( commonModule . Service . prototype , 'request' , wrapRequestFunction ) ;
27+ } catch ( e ) {
28+ if ( ! optional ) {
29+ throw e ;
30+ }
31+ }
32+ } ,
33+ setup ( client ) {
34+ SETUP_CLIENTS . set ( client , true ) ;
35+ } ,
36+ } ;
37+ } ) satisfies IntegrationFn ;
3138
32- this . _optional = options . optional || false ;
33- }
39+ export const googleCloudHttpIntegration = defineIntegration ( _googleCloudHttpIntegration ) ;
3440
35- /**
36- * @inheritDoc
37- */
38- public setupOnce ( ) : void {
39- try {
40- // eslint-disable-next-line @typescript-eslint/no-var-requires
41- const commonModule = require ( '@google-cloud/common' ) as typeof common ;
42- fill ( commonModule . Service . prototype , 'request' , wrapRequestFunction ) ;
43- } catch ( e ) {
44- if ( ! this . _optional ) {
45- throw e ;
46- }
47- }
48- }
49- }
41+ /**
42+ * Google Cloud Platform service requests tracking for RESTful APIs.
43+ *
44+ * @deprecated Use `googleCloudHttpIntegration()` instead.
45+ */
46+ // eslint-disable-next-line deprecation/deprecation
47+ export const GoogleCloudHttp = convertIntegrationFnToClass (
48+ INTEGRATION_NAME ,
49+ googleCloudHttpIntegration ,
50+ ) as IntegrationClass < Integration > ;
5051
5152/** Returns a wrapped function that makes a request with tracing enabled */
5253function wrapRequestFunction ( orig : RequestFunction ) : RequestFunction {
5354 return function ( this : common . Service , reqOpts : RequestOptions , callback : ResponseCallback ) : void {
5455 const httpMethod = reqOpts . method || 'GET' ;
55- const span = startInactiveSpan ( {
56- name : `${ httpMethod } ${ reqOpts . uri } ` ,
57- op : `http.client.${ identifyService ( this . apiEndpoint ) } ` ,
58- origin : 'auto.http.serverless' ,
59- } ) ;
56+ // Only create span if integration is active on client
57+ const span = SETUP_CLIENTS . has ( getClient ( ) as Client )
58+ ? startInactiveSpan ( {
59+ name : `${ httpMethod } ${ reqOpts . uri } ` ,
60+ op : `http.client.${ identifyService ( this . apiEndpoint ) } ` ,
61+ origin : 'auto.http.serverless' ,
62+ } )
63+ : undefined ;
6064 orig . call ( this , reqOpts , ( ...args : Parameters < ResponseCallback > ) => {
6165 if ( span ) {
6266 span . end ( ) ;
0 commit comments