From 38613da8ac00c8c78bf369ec3b87136c71c93ad9 Mon Sep 17 00:00:00 2001 From: Yongseok Date: Wed, 3 Jul 2024 21:23:41 +0900 Subject: [PATCH] [#182] Support ServiceConfig spec( https://github.com/grpc/grpc/blob/master/doc/service_config.md ) --- lib/client/grpc-data-sender.js | 15 +++++++++++---- .../grpc/{options.js => options-builder.js} | 8 +++++--- lib/config.js | 4 ++-- 3 files changed, 18 insertions(+), 9 deletions(-) rename lib/client/grpc/{options.js => options-builder.js} (80%) diff --git a/lib/client/grpc-data-sender.js b/lib/client/grpc-data-sender.js index ad641b6e..cba6547d 100644 --- a/lib/client/grpc-data-sender.js +++ b/lib/client/grpc-data-sender.js @@ -17,6 +17,7 @@ const Scheduler = require('../utils/scheduler') const makeAgentInformationMetadataInterceptor = require('./grpc/make-agent-information-metadata-interceptor') const socketIdInterceptor = require('./grpc/socketid-interceptor') const CallArgumentsBuilder = require('./call-arguments-builder') +const OptionsBuilder = require('./grpc/options-builder') // AgentInfoSender.java // refresh daily @@ -40,7 +41,7 @@ class GrpcDataSender { config ) { this.agentInfo = agentInfo - this.initializeClients(collectorIp, collectorTcpPort) + this.initializeClients(collectorIp, collectorTcpPort, config) this.initializeSpanStream(collectorIp, collectorSpanPort, config) this.initializeStatStream(collectorIp, collectorStatPort, config) this.initializePingStream() @@ -72,9 +73,15 @@ class GrpcDataSender { } } - initializeClients(collectorIp, collectorTcpPort) { - this.agentClient = new services.AgentClient(collectorIp + ":" + collectorTcpPort, grpc.credentials.createInsecure(), - { interceptors: [makeAgentInformationMetadataInterceptor(this.agentInfo), socketIdInterceptor] }) + initializeClients(collectorIp, collectorTcpPort, config) { + const builder = new OptionsBuilder() + .addInterceptor(makeAgentInformationMetadataInterceptor(this.agentInfo)) + .addInterceptor(socketIdInterceptor) + + if (config && config.grpcServiceConfig && typeof config.grpcServiceConfig.getAgentServiceConfig === 'function') { + builder.setGrpcServiceConfig(config.grpcServiceConfig.getAgentServiceConfig()) + } + this.agentClient = new services.AgentClient(collectorIp + ":" + collectorTcpPort, grpc.credentials.createInsecure(), builder.build()) this.metadataClient = new services.MetadataClient(collectorIp + ":" + collectorTcpPort, grpc.credentials.createInsecure(), { interceptors: [makeAgentInformationMetadataInterceptor(this.agentInfo)] }) diff --git a/lib/client/grpc/options.js b/lib/client/grpc/options-builder.js similarity index 80% rename from lib/client/grpc/options.js rename to lib/client/grpc/options-builder.js index b897cfab..c33877b7 100644 --- a/lib/client/grpc/options.js +++ b/lib/client/grpc/options-builder.js @@ -6,7 +6,7 @@ 'use strict' -class Options { +class OptionsBuilder { constructor() { this.options = { interceptors: [] @@ -15,15 +15,17 @@ class Options { addInterceptor(interceptor) { this.options.interceptors.push(interceptor) + return this } setGrpcServiceConfig(serviceConfig) { this.options['grpc.service_config'] = serviceConfig + return this } - options() { + build() { return this.options } } -module.exports = Options \ No newline at end of file +module.exports = OptionsBuilder \ No newline at end of file diff --git a/lib/config.js b/lib/config.js index 688af700..aa0dc968 100644 --- a/lib/config.js +++ b/lib/config.js @@ -170,9 +170,9 @@ const init = (initOptions = {}) => { if (initOptions['grpc.service_config'] && initOptions['grpc.service_config'].name === 'ServiceConfig') { const config = initOptions['grpc.service_config'] - agentConfig.serviceConfig = config + agentConfig.grpcServiceConfig = config } else { - agentConfig.serviceConfig = defaultServiceConfigBuilder.build() + agentConfig.grpcServiceConfig = defaultServiceConfigBuilder.build() } }