From 021e54ef469d7d95bae64c687b65489cbfc56cfa Mon Sep 17 00:00:00 2001 From: surbhigarg92 Date: Wed, 17 May 2023 06:51:31 +0000 Subject: [PATCH] fix: set grpc useragent (#1847) * fix: set grpc useragent * fix: review comments --- src/common-grpc/service.ts | 6 ++++++ test/common/service.ts | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/common-grpc/service.ts b/src/common-grpc/service.ts index 24e84b884..08f5642d7 100644 --- a/src/common-grpc/service.ts +++ b/src/common-grpc/service.ts @@ -417,6 +417,12 @@ export class GrpcService extends Service { this.maxRetries = options.maxRetries; this.userAgent = util.getUserAgentFromPackageJson(config.packageJson); + + if (this.providedUserAgent) { + this.userAgent = `${this.providedUserAgent} ${this.userAgent}`; + } + options['grpc.primary_user_agent'] = this.userAgent; + this.activeServiceMap_ = new Map(); this.protos = {}; const protoServices = config.protoServices; diff --git a/test/common/service.ts b/test/common/service.ts index bb5d417df..293f98d0f 100644 --- a/test/common/service.ts +++ b/test/common/service.ts @@ -402,6 +402,18 @@ describe('GrpcService', () => { assert.strictEqual(grpcService.userAgent, userAgent); }); + it('should set the primary_user_agent from user-agent', () => { + const userAgent = 'user-agent/0.0.0'; + + getUserAgentFromPackageJsonOverride = packageJson => { + assert.strictEqual(packageJson, CONFIG.packageJson); + return userAgent; + }; + + new GrpcService(CONFIG, OPTIONS); + assert.strictEqual(OPTIONS['grpc.primary_user_agent'], userAgent); + }); + it('should localize the service', () => { assert.deepStrictEqual( Object.keys(grpcService.protos),