@@ -19,14 +19,14 @@ import {logger} from '@opencensus/core';
19
19
import * as assert from 'assert' ;
20
20
import * as grpcModule from 'grpc' ;
21
21
import * as path from 'path' ;
22
- import { GrpcModule , GrpcPlugin , plugin , SendUnaryDataCallback } from '../src/' ;
22
+
23
+ import { GRPC_TRACE_KEY , GrpcModule , GrpcPlugin , plugin , SendUnaryDataCallback } from '../src/' ;
23
24
24
25
const PROTO_PATH = __dirname + '/fixtures/grpc-instrumentation-test.proto' ;
25
26
const grpcPort = 50051 ;
26
27
const MAX_ERROR_STATUS = grpcModule . status . UNAUTHENTICATED ;
27
28
const log = logger . logger ( ) ;
28
29
29
-
30
30
const replicate = ( request : TestRequestResponse ) => {
31
31
const result : TestRequestResponse [ ] = [ ] ;
32
32
for ( let i = 0 ; i < request . num ; i ++ ) {
@@ -492,4 +492,94 @@ describe('GrpcPlugin() ', function() {
492
492
} ) ;
493
493
} ) ;
494
494
} ) ;
495
+ describe ( 'setSpanContext' , ( ) => {
496
+ const metadata = new grpcModule . Metadata ( ) ;
497
+ const spanContext = {
498
+ traceId : '3ad17e665f514aabb896341f670179ed' ,
499
+ spanId : '3aaeb440a89d9e82' ,
500
+ options : 0x1
501
+ } ;
502
+
503
+ it ( 'should set span context' , ( ) => {
504
+ GrpcPlugin . setSpanContext ( metadata , spanContext ) ;
505
+ const actualSpanContext = GrpcPlugin . getSpanContext ( metadata ) ;
506
+ assert . deepEqual ( actualSpanContext , spanContext ) ;
507
+ } ) ;
508
+ } ) ;
509
+
510
+ describe ( 'getSpanContext' , ( ) => {
511
+ const metadata = new grpcModule . Metadata ( ) ;
512
+ it ( 'should return null when span context is not set' , ( ) => {
513
+ const actualSpanContext = GrpcPlugin . getSpanContext ( metadata ) ;
514
+ assert . equal ( actualSpanContext , null ) ;
515
+ } ) ;
516
+
517
+ it ( 'should return valid span context' , ( ) => {
518
+ const buffer = new Buffer ( [
519
+ 0x00 , 0x00 , 0xdf , 0x6a , 0x20 , 0x38 , 0xfa , 0x78 , 0xc4 , 0xcd ,
520
+ 0x42 , 0x20 , 0x91 , 0x26 , 0x24 , 0x9c , 0x31 , 0xc7 , 0x01 , 0xc2 ,
521
+ 0xb7 , 0xce , 0x7a , 0x57 , 0x2a , 0x37 , 0xc6 , 0x02 , 0x01
522
+ ] ) ;
523
+ const expectedSpanContext = {
524
+ traceId : 'df6a2038fa78c4cd42209126249c31c7' ,
525
+ spanId : 'c2b7ce7a572a37c6' ,
526
+ options : 1
527
+ } ;
528
+ metadata . set ( GRPC_TRACE_KEY , buffer ) ;
529
+ const actualSpanContext = GrpcPlugin . getSpanContext ( metadata ) ;
530
+ assert . deepEqual ( actualSpanContext , expectedSpanContext ) ;
531
+ } ) ;
532
+
533
+ it ( 'should return null for unsupported version' , ( ) => {
534
+ const buffer = new Buffer ( [
535
+ 0x66 , 0x64 , 0xdf , 0x6a , 0x20 , 0x38 , 0xfa , 0x78 , 0xc4 , 0xcd ,
536
+ 0x42 , 0x20 , 0x91 , 0x26 , 0x24 , 0x9c , 0x31 , 0xc7 , 0x01 , 0xc2 ,
537
+ 0xb7 , 0xce , 0x7a , 0x57 , 0x2a , 0x37 , 0xc6 , 0x02 , 0x01
538
+ ] ) ;
539
+ metadata . set ( GRPC_TRACE_KEY , buffer ) ;
540
+ const actualSpanContext = GrpcPlugin . getSpanContext ( metadata ) ;
541
+ assert . deepEqual ( actualSpanContext , null ) ;
542
+ } ) ;
543
+
544
+ it ( 'should return null when unexpected trace ID offset' , ( ) => {
545
+ const buffer = new Buffer ( [
546
+ 0x00 , 0x04 , 0xdf , 0x6a , 0x20 , 0x38 , 0xfa , 0x78 , 0xc4 , 0xcd ,
547
+ 0x42 , 0x20 , 0x91 , 0x26 , 0x24 , 0x9c , 0x31 , 0xc7 , 0x01 , 0xc2 ,
548
+ 0xb7 , 0xce , 0x7a , 0x57 , 0x2a , 0x37 , 0xc6 , 0x02 , 0x01
549
+ ] ) ;
550
+ metadata . set ( GRPC_TRACE_KEY , buffer ) ;
551
+ const actualSpanContext = GrpcPlugin . getSpanContext ( metadata ) ;
552
+ assert . deepEqual ( actualSpanContext , null ) ;
553
+ } ) ;
554
+
555
+ it ( 'should return null when unexpected span ID offset' , ( ) => {
556
+ const buffer = new Buffer ( [
557
+ 0x00 , 0x00 , 0xdf , 0x6a , 0x20 , 0x38 , 0xfa , 0x78 , 0xc4 , 0xcd ,
558
+ 0x42 , 0x20 , 0x91 , 0x26 , 0x24 , 0x9c , 0x31 , 0xc7 , 0x03 , 0xc2 ,
559
+ 0xb7 , 0xce , 0x7a , 0x57 , 0x2a , 0x37 , 0xc6 , 0x02 , 0x01
560
+ ] ) ;
561
+ metadata . set ( GRPC_TRACE_KEY , buffer ) ;
562
+ const actualSpanContext = GrpcPlugin . getSpanContext ( metadata ) ;
563
+ assert . deepEqual ( actualSpanContext , null ) ;
564
+ } ) ;
565
+
566
+ it ( 'should return null when unexpected options offset' , ( ) => {
567
+ const buffer = new Buffer ( [
568
+ 0x00 , 0x00 , 0xdf , 0x6a , 0x20 , 0x38 , 0xfa , 0x78 , 0xc4 , 0xcd ,
569
+ 0x42 , 0x20 , 0x91 , 0x26 , 0x24 , 0x9c , 0x31 , 0xc7 , 0x03 , 0xc2 ,
570
+ 0xb7 , 0xce , 0x7a , 0x57 , 0x2a , 0x37 , 0xc6 , 0x00 , 0x01
571
+ ] ) ;
572
+ metadata . set ( GRPC_TRACE_KEY , buffer ) ;
573
+ const actualSpanContext = GrpcPlugin . getSpanContext ( metadata ) ;
574
+ assert . deepEqual ( actualSpanContext , null ) ;
575
+ } ) ;
576
+
577
+ it ( 'should return null when invalid input i.e. truncated' , ( ) => {
578
+ const buffer =
579
+ new Buffer ( [ 0x00 , 0x00 , 0xdf , 0x6a , 0x20 , 0x38 , 0xfa , 0x78 , 0xc4 ] ) ;
580
+ metadata . set ( GRPC_TRACE_KEY , buffer ) ;
581
+ const actualSpanContext = GrpcPlugin . getSpanContext ( metadata ) ;
582
+ assert . deepEqual ( actualSpanContext , null ) ;
583
+ } ) ;
584
+ } ) ;
495
585
} ) ;
0 commit comments