-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathgoogle-cloud-grpc.ts
128 lines (115 loc) · 3.77 KB
/
google-cloud-grpc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import type { EventEmitter } from 'events';
import type { Client, IntegrationFn } from '@sentry/core';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, defineIntegration, fill, getClient } from '@sentry/core';
import { startInactiveSpan } from '@sentry/node';
export interface GrpcFunction extends CallableFunction {
(...args: unknown[]): EventEmitter;
}
export interface GrpcFunctionObject extends GrpcFunction {
requestStream: boolean;
responseStream: boolean;
originalName: string;
}
interface StubOptions {
servicePath?: string;
}
interface CreateStubFunc extends CallableFunction {
(createStub: unknown, options: StubOptions): PromiseLike<Stub>;
}
export interface Stub {
[key: string]: GrpcFunctionObject;
}
const SERVICE_PATH_REGEX = /^(\w+)\.googleapis.com$/;
const INTEGRATION_NAME = 'GoogleCloudGrpc';
const SETUP_CLIENTS = new WeakMap<Client, boolean>();
const _googleCloudGrpcIntegration = ((options: { optional?: boolean } = {}) => {
const optional = options.optional || false;
return {
name: INTEGRATION_NAME,
setupOnce() {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const gaxModule = require('google-gax');
fill(
gaxModule.GrpcClient.prototype, // eslint-disable-line @typescript-eslint/no-unsafe-member-access
'createStub',
wrapCreateStub,
);
} catch (e) {
if (!optional) {
throw e;
}
}
},
setup(client) {
SETUP_CLIENTS.set(client, true);
},
};
}) satisfies IntegrationFn;
/**
* Google Cloud Platform service requests tracking for GRPC APIs.
*/
export const googleCloudGrpcIntegration = defineIntegration(_googleCloudGrpcIntegration);
/** Returns a wrapped function that returns a stub with tracing enabled */
function wrapCreateStub(origCreate: CreateStubFunc): CreateStubFunc {
return async function (this: unknown, ...args: Parameters<CreateStubFunc>) {
const servicePath = args[1]?.servicePath;
if (servicePath == null || servicePath == undefined) {
return origCreate.apply(this, args);
}
const serviceIdentifier = identifyService(servicePath);
const stub = await origCreate.apply(this, args);
for (const methodName of Object.keys(Object.getPrototypeOf(stub))) {
fillGrpcFunction(stub, serviceIdentifier, methodName);
}
return stub;
};
}
/** Patches the function in grpc stub to enable tracing */
export function fillGrpcFunction(stub: Stub, serviceIdentifier: string, methodName: string): void {
const funcObj = stub[methodName];
if (typeof funcObj !== 'function') {
return;
}
const callType =
!funcObj.requestStream && !funcObj.responseStream
? 'unary call'
: funcObj.requestStream && !funcObj.responseStream
? 'client stream'
: !funcObj.requestStream && funcObj.responseStream
? 'server stream'
: 'bidi stream';
if (callType != 'unary call') {
return;
}
fill(
stub,
methodName,
(orig: GrpcFunction): GrpcFunction =>
(...args) => {
const ret = orig.apply(stub, args);
if (typeof ret?.on !== 'function' || !SETUP_CLIENTS.has(getClient() as Client)) {
return ret;
}
const span = startInactiveSpan({
name: `${callType} ${methodName}`,
onlyIfParent: true,
op: `grpc.${serviceIdentifier}`,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.grpc.serverless',
},
});
ret.on('status', () => {
if (span) {
span.end();
}
});
return ret;
},
);
}
/** Identifies service by its address */
function identifyService(servicePath: string): string {
const match = servicePath.match(SERVICE_PATH_REGEX);
return match?.[1] || servicePath;
}