@@ -31,6 +31,7 @@ import {
31
31
ServerCallWithMeta ,
32
32
SendUnaryDataCallback ,
33
33
GrpcClientFunc ,
34
+ GrpcInternalClientTypes ,
34
35
} from './types' ;
35
36
import {
36
37
findIndex ,
@@ -39,17 +40,18 @@ import {
39
40
} from './utils' ;
40
41
41
42
import * as events from 'events' ;
42
- import * as grpcModule from 'grpc' ;
43
+ import * as grpcTypes from 'grpc' ;
43
44
import * as shimmer from 'shimmer' ;
44
45
import * as path from 'path' ;
45
46
46
47
/** The metadata key under which span context is stored as a binary value. */
47
48
export const GRPC_TRACE_KEY = 'grpc-trace-bin' ;
48
49
49
- let grpcClientModule : object ;
50
+ let grpcClientModule : GrpcInternalClientTypes ;
50
51
51
52
export class GrpcPlugin extends BasePlugin < grpc > {
52
53
static readonly component = 'grpc' ;
54
+ readonly supportedVersions = [ '^1.23.3' ] ;
53
55
54
56
protected _config ! : GrpcPluginOptions ;
55
57
@@ -64,7 +66,7 @@ export class GrpcPlugin extends BasePlugin<grpc> {
64
66
} ;
65
67
protected readonly _basedir = basedir ;
66
68
67
- protected patch ( ) : typeof grpcModule {
69
+ protected patch ( ) : typeof grpcTypes {
68
70
this . _logger . debug (
69
71
'applying patch to %s@%s' ,
70
72
this . moduleName ,
@@ -80,12 +82,24 @@ export class GrpcPlugin extends BasePlugin<grpc> {
80
82
) ;
81
83
}
82
84
85
+ // Wrap the externally exported client constructor
86
+ if ( this . _moduleExports . makeGenericClientConstructor ) {
87
+ shimmer . wrap (
88
+ this . _moduleExports ,
89
+ 'makeGenericClientConstructor' ,
90
+ this . _patchClient ( )
91
+ ) ;
92
+ }
93
+
83
94
if ( this . _internalFilesExports [ 'client' ] ) {
84
- grpcClientModule = this . _internalFilesExports [ 'client' ] as object ;
95
+ grpcClientModule = this . _internalFilesExports [
96
+ 'client'
97
+ ] as GrpcInternalClientTypes ;
85
98
99
+ // Wrap the internally used client constructor
86
100
shimmer . wrap (
87
101
grpcClientModule ,
88
- 'makeClientConstructor' as never ,
102
+ 'makeClientConstructor' ,
89
103
this . _patchClient ( )
90
104
) ;
91
105
}
@@ -103,12 +117,16 @@ export class GrpcPlugin extends BasePlugin<grpc> {
103
117
shimmer . unwrap ( this . _moduleExports . Server . prototype , 'register' ) ;
104
118
}
105
119
120
+ if ( this . _moduleExports . makeGenericClientConstructor ) {
121
+ shimmer . unwrap ( this . _moduleExports , 'makeGenericClientConstructor' ) ;
122
+ }
123
+
106
124
if ( grpcClientModule ) {
107
- shimmer . unwrap ( grpcClientModule , 'makeClientConstructor' as never ) ;
125
+ shimmer . unwrap ( grpcClientModule , 'makeClientConstructor' ) ;
108
126
}
109
127
}
110
128
111
- private _getSpanContext ( metadata : grpcModule . Metadata ) : SpanContext | null {
129
+ private _getSpanContext ( metadata : grpcTypes . Metadata ) : SpanContext | null {
112
130
const metadataValue = metadata . getMap ( ) [ GRPC_TRACE_KEY ] as Buffer ;
113
131
// Entry doesn't exist
114
132
if ( ! metadataValue ) {
@@ -118,7 +136,7 @@ export class GrpcPlugin extends BasePlugin<grpc> {
118
136
}
119
137
120
138
private _setSpanContext (
121
- metadata : grpcModule . Metadata ,
139
+ metadata : grpcTypes . Metadata ,
122
140
spanContext : SpanContext
123
141
) : void {
124
142
const serializedSpanContext = this . _tracer
@@ -129,17 +147,17 @@ export class GrpcPlugin extends BasePlugin<grpc> {
129
147
}
130
148
131
149
private _patchServer ( ) {
132
- return ( originalRegister : typeof grpcModule . Server . prototype . register ) => {
150
+ return ( originalRegister : typeof grpcTypes . Server . prototype . register ) => {
133
151
const plugin = this ;
134
152
plugin . _logger . debug ( 'patched gRPC server' ) ;
135
153
136
154
return function register < RequestType , ResponseType > (
137
155
// tslint:disable-next-line:no-any
138
- this : grpcModule . Server & { handlers : any } ,
156
+ this : grpcTypes . Server & { handlers : any } ,
139
157
name : string ,
140
- handler : grpcModule . handleCall < RequestType , ResponseType > ,
141
- serialize : grpcModule . serialize < RequestType > ,
142
- deserialize : grpcModule . deserialize < RequestType > ,
158
+ handler : grpcTypes . handleCall < RequestType , ResponseType > ,
159
+ serialize : grpcTypes . serialize < RequestType > ,
160
+ deserialize : grpcTypes . deserialize < RequestType > ,
143
161
type : string
144
162
) {
145
163
// tslint:disable-next-line:no-any
@@ -149,7 +167,7 @@ export class GrpcPlugin extends BasePlugin<grpc> {
149
167
shimmer . wrap (
150
168
handlerSet ,
151
169
'func' ,
152
- ( originalFunc : grpcModule . handleCall < RequestType , ResponseType > ) => {
170
+ ( originalFunc : grpcTypes . handleCall < RequestType , ResponseType > ) => {
153
171
return function func (
154
172
this : typeof handlerSet ,
155
173
call : ServerCallWithMeta ,
@@ -216,16 +234,16 @@ export class GrpcPlugin extends BasePlugin<grpc> {
216
234
call : ServerCallWithMeta ,
217
235
callback : SendUnaryDataCallback ,
218
236
original :
219
- | grpcModule . handleCall < RequestType , ResponseType >
220
- | grpcModule . ClientReadableStream < RequestType > ,
237
+ | grpcTypes . handleCall < RequestType , ResponseType >
238
+ | grpcTypes . ClientReadableStream < RequestType > ,
221
239
self : { }
222
240
) {
223
241
function patchedCallback (
224
- err : grpcModule . ServiceError ,
242
+ err : grpcTypes . ServiceError ,
225
243
// tslint:disable-next-line:no-any
226
244
value : any ,
227
- trailer : grpcModule . Metadata ,
228
- flags : grpcModule . writeFlags
245
+ trailer : grpcTypes . Metadata ,
246
+ flags : grpcTypes . writeFlags
229
247
) {
230
248
if ( err ) {
231
249
if ( err . code ) {
@@ -246,7 +264,7 @@ export class GrpcPlugin extends BasePlugin<grpc> {
246
264
span . setStatus ( { code : CanonicalCode . OK } ) ;
247
265
span . setAttribute (
248
266
AttributeNames . GRPC_STATUS_CODE ,
249
- grpcModule . status . OK . toString ( )
267
+ plugin . _moduleExports . status . OK . toString ( )
250
268
) ;
251
269
}
252
270
span . addEvent ( 'received' ) ;
@@ -264,7 +282,7 @@ export class GrpcPlugin extends BasePlugin<grpc> {
264
282
plugin : GrpcPlugin ,
265
283
span : Span ,
266
284
call : ServerCallWithMeta ,
267
- original : grpcModule . handleCall < RequestType , ResponseType > ,
285
+ original : grpcTypes . handleCall < RequestType , ResponseType > ,
268
286
self : { }
269
287
) {
270
288
let spanEnded = false ;
@@ -290,7 +308,7 @@ export class GrpcPlugin extends BasePlugin<grpc> {
290
308
}
291
309
} ) ;
292
310
293
- call . on ( 'error' , ( err : grpcModule . ServiceError ) => {
311
+ call . on ( 'error' , ( err : grpcTypes . ServiceError ) => {
294
312
span . addEvent ( 'finished with error' ) ;
295
313
span . setAttributes ( {
296
314
[ AttributeNames . GRPC_ERROR_NAME ] : err . name ,
@@ -305,15 +323,13 @@ export class GrpcPlugin extends BasePlugin<grpc> {
305
323
306
324
private _patchClient ( ) {
307
325
const plugin = this ;
308
- return (
309
- original : typeof grpcModule . makeGenericClientConstructor
310
- ) : never => {
326
+ return ( original : typeof grpcTypes . makeGenericClientConstructor ) : never => {
311
327
plugin . _logger . debug ( 'patching client' ) ;
312
328
return function makeClientConstructor < ImplementationType > (
313
- this : typeof grpcModule . Client ,
314
- methods : grpcModule . ServiceDefinition < ImplementationType > ,
329
+ this : typeof grpcTypes . Client ,
330
+ methods : grpcTypes . ServiceDefinition < ImplementationType > ,
315
331
serviceName : string ,
316
- options : grpcModule . GenericClientOptions
332
+ options : grpcTypes . GenericClientOptions
317
333
) {
318
334
// tslint:disable-next-line:no-any
319
335
const client = original . apply ( this , arguments as any ) ;
@@ -332,7 +348,7 @@ export class GrpcPlugin extends BasePlugin<grpc> {
332
348
const plugin = this ;
333
349
return ( original : GrpcClientFunc ) => {
334
350
plugin . _logger . debug ( 'patch all client methods' ) ;
335
- return function clientMethodTrace ( this : grpcModule . Client ) {
351
+ return function clientMethodTrace ( this : grpcTypes . Client ) {
336
352
const name = `grpc.${ original . path . replace ( '/' , '' ) } ` ;
337
353
const args = Array . prototype . slice . call ( arguments ) ;
338
354
const currentSpan = plugin . _tracer . getCurrentSpan ( ) ;
@@ -356,7 +372,7 @@ export class GrpcPlugin extends BasePlugin<grpc> {
356
372
original : GrpcClientFunc ,
357
373
// tslint:disable-next-line:no-any
358
374
args : any [ ] ,
359
- self : grpcModule . Client ,
375
+ self : grpcTypes . Client ,
360
376
plugin : GrpcPlugin
361
377
) {
362
378
/**
@@ -366,10 +382,10 @@ export class GrpcPlugin extends BasePlugin<grpc> {
366
382
function patchedCallback (
367
383
span : Span ,
368
384
callback : SendUnaryDataCallback ,
369
- metadata : grpcModule . Metadata
385
+ metadata : grpcTypes . Metadata
370
386
) {
371
387
// tslint:disable-next-line:no-any
372
- const wrappedFn = ( err : grpcModule . ServiceError , res : any ) => {
388
+ const wrappedFn = ( err : grpcTypes . ServiceError , res : any ) => {
373
389
if ( err ) {
374
390
if ( err . code ) {
375
391
span . setStatus ( _grpcStatusCodeToSpanStatus ( err . code ) ) ;
@@ -386,7 +402,7 @@ export class GrpcPlugin extends BasePlugin<grpc> {
386
402
span . setStatus ( { code : CanonicalCode . OK } ) ;
387
403
span . setAttribute (
388
404
AttributeNames . GRPC_STATUS_CODE ,
389
- grpcModule . status . OK . toString ( )
405
+ plugin . _moduleExports . status . OK . toString ( )
390
406
) ;
391
407
}
392
408
@@ -439,7 +455,7 @@ export class GrpcPlugin extends BasePlugin<grpc> {
439
455
plugin . _tracer . bind ( call ) ;
440
456
( ( call as unknown ) as events . EventEmitter ) . on (
441
457
'error' ,
442
- ( err : grpcModule . ServiceError ) => {
458
+ ( err : grpcTypes . ServiceError ) => {
443
459
span . setStatus ( {
444
460
code : _grpcStatusCodeToCanonicalCode ( err . code ) ,
445
461
message : err . message ,
@@ -472,8 +488,8 @@ export class GrpcPlugin extends BasePlugin<grpc> {
472
488
original : GrpcClientFunc ,
473
489
// tslint:disable-next-line:no-any
474
490
args : any [ ]
475
- ) : grpcModule . Metadata {
476
- let metadata : grpcModule . Metadata ;
491
+ ) : grpcTypes . Metadata {
492
+ let metadata : grpcTypes . Metadata ;
477
493
478
494
// This finds an instance of Metadata among the arguments.
479
495
// A possible issue that could occur is if the 'options' parameter from
@@ -489,7 +505,7 @@ export class GrpcPlugin extends BasePlugin<grpc> {
489
505
) ;
490
506
} ) ;
491
507
if ( metadataIndex === - 1 ) {
492
- metadata = new grpcModule . Metadata ( ) ;
508
+ metadata = new this . _moduleExports . Metadata ( ) ;
493
509
if ( ! original . requestStream ) {
494
510
// unary or server stream
495
511
if ( args . length === 0 ) {
@@ -516,5 +532,4 @@ export class GrpcPlugin extends BasePlugin<grpc> {
516
532
517
533
const basedir = path . dirname ( require . resolve ( 'grpc' ) ) ;
518
534
const version = require ( path . join ( basedir , 'package.json' ) ) . version ;
519
- const plugin = new GrpcPlugin ( GrpcPlugin . component , version ) ;
520
- export { plugin } ;
535
+ export const plugin = new GrpcPlugin ( GrpcPlugin . component , version ) ;
0 commit comments