|
| 1 | +import { getCurrentHub } from '@sentry/node'; |
| 2 | +import { Integration, Span, Transaction } from '@sentry/types'; |
| 3 | +import { fill } from '@sentry/utils'; |
| 4 | +import { EventEmitter } from 'events'; |
| 5 | + |
| 6 | +interface GrpcFunction extends CallableFunction { |
| 7 | + (...args: unknown[]): EventEmitter; |
| 8 | +} |
| 9 | + |
| 10 | +interface GrpcFunctionObject extends GrpcFunction { |
| 11 | + requestStream: boolean; |
| 12 | + responseStream: boolean; |
| 13 | + originalName: string; |
| 14 | +} |
| 15 | + |
| 16 | +interface StubOptions { |
| 17 | + servicePath?: string; |
| 18 | +} |
| 19 | + |
| 20 | +interface CreateStubFunc extends CallableFunction { |
| 21 | + (createStub: unknown, options: StubOptions): PromiseLike<Stub>; |
| 22 | +} |
| 23 | + |
| 24 | +interface Stub { |
| 25 | + [key: string]: GrpcFunctionObject; |
| 26 | +} |
| 27 | + |
| 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'; |
| 34 | + |
| 35 | + /** |
| 36 | + * @inheritDoc |
| 37 | + */ |
| 38 | + public name: string = GoogleCloudGrpc.id; |
| 39 | + |
| 40 | + private readonly _optional: boolean; |
| 41 | + |
| 42 | + public constructor(options: { optional?: boolean } = {}) { |
| 43 | + this._optional = options.optional || false; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @inheritDoc |
| 48 | + */ |
| 49 | + public setupOnce(): void { |
| 50 | + try { |
| 51 | + const gaxModule = require('google-gax'); |
| 52 | + fill( |
| 53 | + gaxModule.GrpcClient.prototype, // eslint-disable-line @typescript-eslint/no-unsafe-member-access |
| 54 | + 'createStub', |
| 55 | + wrapCreateStub, |
| 56 | + ); |
| 57 | + } catch (e) { |
| 58 | + if (!this._optional) { |
| 59 | + throw e; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** Returns a wrapped function that returns a stub with tracing enabled */ |
| 66 | +function wrapCreateStub(origCreate: CreateStubFunc): CreateStubFunc { |
| 67 | + return async function(this: unknown, ...args: Parameters<CreateStubFunc>) { |
| 68 | + const servicePath = args[1]?.servicePath; |
| 69 | + if (servicePath == null || servicePath == undefined) { |
| 70 | + return origCreate.apply(this, args); |
| 71 | + } |
| 72 | + const serviceIdentifier = identifyService(servicePath); |
| 73 | + const stub = await origCreate.apply(this, args); |
| 74 | + for (const methodName of Object.keys(Object.getPrototypeOf(stub))) { |
| 75 | + fillGrpcFunction(stub, serviceIdentifier, methodName); |
| 76 | + } |
| 77 | + return stub; |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +/** Patches the function in grpc stub to enable tracing */ |
| 82 | +function fillGrpcFunction(stub: Stub, serviceIdentifier: string, methodName: string): void { |
| 83 | + const funcObj = stub[methodName]; |
| 84 | + if (typeof funcObj !== 'function') { |
| 85 | + return; |
| 86 | + } |
| 87 | + const callType = |
| 88 | + !funcObj.requestStream && !funcObj.responseStream |
| 89 | + ? 'unary call' |
| 90 | + : funcObj.requestStream && !funcObj.responseStream |
| 91 | + ? 'client stream' |
| 92 | + : !funcObj.requestStream && funcObj.responseStream |
| 93 | + ? 'server stream' |
| 94 | + : 'bidi stream'; |
| 95 | + if (callType != 'unary call') { |
| 96 | + return; |
| 97 | + } |
| 98 | + fill( |
| 99 | + stub, |
| 100 | + methodName, |
| 101 | + (orig: GrpcFunction): GrpcFunction => (...args) => { |
| 102 | + const ret = orig.apply(stub, args); |
| 103 | + if (typeof ret?.on !== 'function') { |
| 104 | + return ret; |
| 105 | + } |
| 106 | + let transaction: Transaction | undefined; |
| 107 | + let span: Span | undefined; |
| 108 | + const scope = getCurrentHub().getScope(); |
| 109 | + if (scope) { |
| 110 | + transaction = scope.getTransaction(); |
| 111 | + } |
| 112 | + if (transaction) { |
| 113 | + span = transaction.startChild({ |
| 114 | + description: `${callType} ${methodName}`, |
| 115 | + op: `gcloud.grpc.${serviceIdentifier}`, |
| 116 | + }); |
| 117 | + } |
| 118 | + ret.on('status', () => { |
| 119 | + if (span) { |
| 120 | + span.finish(); |
| 121 | + } |
| 122 | + }); |
| 123 | + return ret; |
| 124 | + }, |
| 125 | + ); |
| 126 | +} |
| 127 | + |
| 128 | +/** Identifies service by its address */ |
| 129 | +function identifyService(servicePath: string): string { |
| 130 | + const match = servicePath.match(/^(\w+)\.googleapis.com$/); |
| 131 | + return match ? match[1] : servicePath; |
| 132 | +} |
0 commit comments