forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpc.ts
532 lines (484 loc) · 15.4 KB
/
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*!
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { BasePlugin } from '@opentelemetry/core';
import {
SpanKind,
SpanOptions,
Span,
Status,
CanonicalCode,
SpanContext,
} from '@opentelemetry/types';
import { AttributeNames } from './enums/AttributeNames';
import {
grpc,
ModuleExportsMapping,
GrpcPluginOptions,
ServerCallWithMeta,
SendUnaryDataCallback,
GrpcClientFunc,
GrpcInternalClientTypes,
} from './types';
import {
findIndex,
_grpcStatusCodeToCanonicalCode,
_grpcStatusCodeToSpanStatus,
} from './utils';
import * as events from 'events';
import * as grpcTypes from 'grpc';
import * as shimmer from 'shimmer';
import * as path from 'path';
/** The metadata key under which span context is stored as a binary value. */
export const GRPC_TRACE_KEY = 'grpc-trace-bin';
let grpcClientModule: GrpcInternalClientTypes;
export class GrpcPlugin extends BasePlugin<grpc> {
static readonly component = 'grpc';
readonly supportedVersions = ['1.*'];
protected _config!: GrpcPluginOptions;
constructor(readonly moduleName: string, readonly version: string) {
super();
this._config = {};
}
protected readonly _internalFilesList: ModuleExportsMapping = {
'0.13 - 1.6': { client: 'src/node/src/client.js' },
'^1.7': { client: 'src/client.js' },
};
protected readonly _basedir = basedir;
protected patch(): typeof grpcTypes {
this._logger.debug(
'applying patch to %s@%s',
this.moduleName,
this.version
);
if (this._moduleExports.Server) {
shimmer.wrap(
this._moduleExports.Server.prototype,
'register',
// tslint:disable-next-line:no-any
this._patchServer() as any
);
}
// Wrap the externally exported client constructor
shimmer.wrap(
this._moduleExports,
'makeGenericClientConstructor',
this._patchClient()
);
if (this._internalFilesExports['client']) {
grpcClientModule = this._internalFilesExports[
'client'
] as GrpcInternalClientTypes;
// Wrap the internally used client constructor
shimmer.wrap(
grpcClientModule,
'makeClientConstructor',
this._patchClient()
);
}
return this._moduleExports;
}
protected unpatch(): void {
this._logger.debug(
'removing patch to %s@%s',
this.moduleName,
this.version
);
if (this._moduleExports.Server) {
shimmer.unwrap(this._moduleExports.Server.prototype, 'register');
}
shimmer.unwrap(this._moduleExports, 'makeGenericClientConstructor');
if (grpcClientModule) {
shimmer.unwrap(grpcClientModule, 'makeClientConstructor');
}
}
private _getSpanContext(metadata: grpcTypes.Metadata): SpanContext | null {
const metadataValue = metadata.getMap()[GRPC_TRACE_KEY] as Buffer;
// Entry doesn't exist
if (!metadataValue) {
return null;
}
return this._tracer.getBinaryFormat().fromBytes(metadataValue);
}
private _setSpanContext(
metadata: grpcTypes.Metadata,
spanContext: SpanContext
): void {
const serializedSpanContext = this._tracer
.getBinaryFormat()
.toBytes(spanContext);
const buffer = Buffer.from(serializedSpanContext);
metadata.set(GRPC_TRACE_KEY, buffer);
}
private _patchServer() {
return (originalRegister: typeof grpcTypes.Server.prototype.register) => {
const plugin = this;
plugin._logger.debug('patched gRPC server');
return function register<RequestType, ResponseType>(
// tslint:disable-next-line:no-any
this: grpcTypes.Server & { handlers: any },
name: string,
handler: grpcTypes.handleCall<RequestType, ResponseType>,
serialize: grpcTypes.serialize<RequestType>,
deserialize: grpcTypes.deserialize<RequestType>,
type: string
) {
// tslint:disable-next-line:no-any
const originalResult = originalRegister.apply(this, arguments as any);
const handlerSet = this.handlers[name];
shimmer.wrap(
handlerSet,
'func',
(originalFunc: grpcTypes.handleCall<RequestType, ResponseType>) => {
return function func(
this: typeof handlerSet,
call: ServerCallWithMeta,
callback: SendUnaryDataCallback
) {
const self = this;
const spanName = `grpc.${name.replace('/', '')}`;
const spanOptions: SpanOptions = {
kind: SpanKind.SERVER,
parent: plugin._getSpanContext(call.metadata),
};
plugin._logger.debug(
'patch func: %s',
JSON.stringify(spanOptions)
);
const span = plugin._tracer
.startSpan(spanName, spanOptions)
.setAttributes({
[AttributeNames.GRPC_KIND]: spanOptions.kind,
[AttributeNames.COMPONENT]: GrpcPlugin.component,
});
plugin._tracer.withSpan(span, () => {
switch (type) {
case 'unary':
case 'client_stream':
return plugin._clientStreamAndUnaryHandler(
plugin,
span,
call,
callback,
originalFunc,
self
);
case 'server_stream':
case 'bidi':
return plugin._serverStreamAndBidiHandler(
plugin,
span,
call,
originalFunc,
self
);
default:
break;
}
});
};
}
);
return originalResult;
};
};
}
private _clientStreamAndUnaryHandler<RequestType, ResponseType>(
plugin: GrpcPlugin,
span: Span,
call: ServerCallWithMeta,
callback: SendUnaryDataCallback,
original:
| grpcTypes.handleCall<RequestType, ResponseType>
| grpcTypes.ClientReadableStream<RequestType>,
self: {}
) {
function patchedCallback(
err: grpcTypes.ServiceError,
// tslint:disable-next-line:no-any
value: any,
trailer: grpcTypes.Metadata,
flags: grpcTypes.writeFlags
) {
if (err) {
if (err.code) {
span.setStatus({
code: _grpcStatusCodeToCanonicalCode(err.code),
message: err.message,
});
span.setAttribute(
AttributeNames.GRPC_STATUS_CODE,
err.code.toString()
);
}
span.setAttributes({
[AttributeNames.GRPC_ERROR_NAME]: err.name,
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
});
} else {
span.setStatus({ code: CanonicalCode.OK });
span.setAttribute(
AttributeNames.GRPC_STATUS_CODE,
plugin._moduleExports.status.OK.toString()
);
}
span.addEvent('received');
// end the span
span.end();
return callback(err, value, trailer, flags);
}
plugin._tracer.bind(call);
return (original as Function).call(self, call, patchedCallback);
}
private _serverStreamAndBidiHandler<RequestType, ResponseType>(
plugin: GrpcPlugin,
span: Span,
call: ServerCallWithMeta,
original: grpcTypes.handleCall<RequestType, ResponseType>,
self: {}
) {
let spanEnded = false;
const endSpan = () => {
if (!spanEnded) {
spanEnded = true;
span.end();
}
};
plugin._tracer.bind(call);
call.on('finish', () => {
span.setStatus(_grpcStatusCodeToSpanStatus(call.status.code));
span.setAttribute(
AttributeNames.GRPC_STATUS_CODE,
call.status.code.toString()
);
// if there is an error, span will be ended on error event, otherwise end it here
if (call.status.code === 0) {
span.addEvent('finished');
endSpan();
}
});
call.on('error', (err: grpcTypes.ServiceError) => {
span.addEvent('finished with error');
span.setAttributes({
[AttributeNames.GRPC_ERROR_NAME]: err.name,
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
});
endSpan();
});
// tslint:disable-next-line:no-any
return (original as any).call(self, call);
}
private _patchClient() {
const plugin = this;
return (original: typeof grpcTypes.makeGenericClientConstructor): never => {
plugin._logger.debug('patching client');
return function makeClientConstructor<ImplementationType>(
this: typeof grpcTypes.Client,
methods: grpcTypes.ServiceDefinition<ImplementationType>,
serviceName: string,
options: grpcTypes.GenericClientOptions
) {
// tslint:disable-next-line:no-any
const client = original.apply(this, arguments as any);
shimmer.massWrap(
client.prototype as never,
Object.keys(methods) as never[],
// tslint:disable-next-line:no-any
plugin._getPatchedClientMethods() as any
);
return client;
} as never;
};
}
private _getPatchedClientMethods() {
const plugin = this;
return (original: GrpcClientFunc) => {
plugin._logger.debug('patch all client methods');
return function clientMethodTrace(this: grpcTypes.Client) {
const name = `grpc.${original.path.replace('/', '')}`;
const args = Array.prototype.slice.call(arguments);
const span = plugin._tracer
.startSpan(name, {
kind: SpanKind.CLIENT,
parent: plugin._tracer.getCurrentSpan(),
})
.setAttribute(AttributeNames.COMPONENT, GrpcPlugin.component);
return plugin._makeGrpcClientRemoteCall(
original,
args,
this,
plugin
)(span);
};
};
}
/**
* This method handles the client remote call
*/
private _makeGrpcClientRemoteCall(
original: GrpcClientFunc,
// tslint:disable-next-line:no-any
args: any[],
self: grpcTypes.Client,
plugin: GrpcPlugin
) {
/**
* Patches a callback so that the current span for this trace is also ended
* when the callback is invoked.
*/
function patchedCallback(
span: Span,
callback: SendUnaryDataCallback,
metadata: grpcTypes.Metadata
) {
// tslint:disable-next-line:no-any
const wrappedFn = (err: grpcTypes.ServiceError, res: any) => {
if (err) {
if (err.code) {
span.setStatus(_grpcStatusCodeToSpanStatus(err.code));
span.setAttribute(
AttributeNames.GRPC_STATUS_CODE,
err.code.toString()
);
}
span.setAttributes({
[AttributeNames.GRPC_ERROR_NAME]: err.name,
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
});
} else {
span.setStatus({ code: CanonicalCode.OK });
span.setAttribute(
AttributeNames.GRPC_STATUS_CODE,
plugin._moduleExports.status.OK.toString()
);
}
span.end();
callback(err, res);
};
return plugin._tracer.bind(wrappedFn);
}
return (span: Span) => {
if (!span) {
return original.apply(self, args);
}
const metadata = this._getMetadata(original, args);
// if unary or clientStream
if (!original.responseStream) {
const callbackFuncIndex = findIndex(args, arg => {
return typeof arg === 'function';
});
if (callbackFuncIndex !== -1) {
args[callbackFuncIndex] = patchedCallback(
span,
args[callbackFuncIndex],
metadata
);
}
}
span.addEvent('sent');
span.setAttributes({
[AttributeNames.GRPC_METHOD]: original.path,
[AttributeNames.GRPC_KIND]: SpanKind.CLIENT,
});
this._setSpanContext(metadata, span.context());
const call = original.apply(self, args);
// if server stream or bidi
if (original.responseStream) {
// Both error and status events can be emitted
// the first one emitted set spanEnded to true
let spanEnded = false;
const endSpan = () => {
if (!spanEnded) {
span.end();
spanEnded = true;
}
};
plugin._tracer.bind(call);
((call as unknown) as events.EventEmitter).on(
'error',
(err: grpcTypes.ServiceError) => {
span.setStatus({
code: _grpcStatusCodeToCanonicalCode(err.code),
message: err.message,
});
span.setAttributes({
[AttributeNames.GRPC_ERROR_NAME]: err.name,
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
});
endSpan();
}
);
((call as unknown) as events.EventEmitter).on(
'status',
(status: Status) => {
span.setStatus({ code: CanonicalCode.OK });
span.setAttribute(
AttributeNames.GRPC_STATUS_CODE,
status.code.toString()
);
endSpan();
}
);
}
return call;
};
}
private _getMetadata(
original: GrpcClientFunc,
// tslint:disable-next-line:no-any
args: any[]
): grpcTypes.Metadata {
let metadata: grpcTypes.Metadata;
// This finds an instance of Metadata among the arguments.
// A possible issue that could occur is if the 'options' parameter from
// the user contains an '_internal_repr' as well as a 'getMap' function,
// but this is an extremely rare case.
// tslint:disable-next-line:no-any
let metadataIndex = findIndex(args, (arg: any) => {
return (
arg &&
typeof arg === 'object' &&
arg._internal_repr &&
typeof arg.getMap === 'function'
);
});
if (metadataIndex === -1) {
metadata = new this._moduleExports.Metadata();
if (!original.requestStream) {
// unary or server stream
if (args.length === 0) {
// No argument (for the gRPC call) was provided, so we will have to
// provide one, since metadata cannot be the first argument.
// The internal representation of argument defaults to undefined
// in its non-presence.
// Note that we can't pass null instead of undefined because the
// serializer within gRPC doesn't accept it.
args.push(undefined);
}
metadataIndex = 1;
} else {
// client stream or bidi
metadataIndex = 0;
}
args.splice(metadataIndex, 0, metadata);
} else {
metadata = args[metadataIndex];
}
return metadata;
}
}
const basedir = path.dirname(require.resolve('grpc'));
const version = require(path.join(basedir, 'package.json')).version;
export const plugin = new GrpcPlugin(GrpcPlugin.component, version);