From a23ffa955c2573ec1492f48f9a00c3839d46669c Mon Sep 17 00:00:00 2001 From: Pavel Pautov Date: Thu, 21 Nov 2024 21:46:01 -0800 Subject: [PATCH] Consolidate transport related parameters into a struct. Also, replace leftover cast with getMainConf(). --- src/batch_exporter.hpp | 4 ++-- src/ngx_otel_module.cpp | 11 +++++++---- src/trace_service_client.hpp | 16 +++++++++++----- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/batch_exporter.hpp b/src/batch_exporter.hpp index cb3e075..a2e65b1 100644 --- a/src/batch_exporter.hpp +++ b/src/batch_exporter.hpp @@ -111,10 +111,10 @@ public: int attrSize{0}; }; - BatchExporter(StrView target, bool ssl, const std::string& trustedCert, + BatchExporter(const Target& target, size_t batchSize, size_t batchCount, const std::map& resourceAttrs) : - batchSize(batchSize), client(std::string(target), ssl, trustedCert) + batchSize(batchSize), client(target) { free.reserve(batchCount); while (batchCount-- > 0) { diff --git a/src/ngx_otel_module.cpp b/src/ngx_otel_module.cpp index 65d0b92..4f78aee 100644 --- a/src/ngx_otel_module.cpp +++ b/src/ngx_otel_module.cpp @@ -576,10 +576,13 @@ ngx_int_t initWorkerProcess(ngx_cycle_t* cycle) } try { + Target target; + target.endpoint = std::string(toStrView(mcf->endpoint)); + target.ssl = mcf->ssl; + target.trustedCert = mcf->trustedCert; + gExporter.reset(new BatchExporter( - toStrView(mcf->endpoint), - mcf->ssl, - mcf->trustedCert, + target, mcf->batchSize, mcf->batchCount, mcf->resourceAttrs)); @@ -772,7 +775,7 @@ void* createMainConf(ngx_conf_t* cf) char* initMainConf(ngx_conf_t* cf, void* conf) { - auto mcf = (MainConf*)conf; + auto mcf = getMainConf(cf); ngx_conf_init_msec_value(mcf->interval, 5000); ngx_conf_init_size_value(mcf->batchSize, 512); diff --git a/src/trace_service_client.hpp b/src/trace_service_client.hpp index d248f00..485143c 100644 --- a/src/trace_service_client.hpp +++ b/src/trace_service_client.hpp @@ -8,6 +8,12 @@ namespace otel_proto_trace = opentelemetry::proto::collector::trace::v1; +struct Target { + std::string endpoint; + bool ssl; + std::string trustedCert; +}; + class TraceServiceClient { public: typedef otel_proto_trace::ExportTraceServiceRequest Request; @@ -17,18 +23,18 @@ public: typedef std::function ResponseCb; - TraceServiceClient(const std::string& target, bool ssl, - const std::string& trustedCert) + TraceServiceClient(const Target& target) { std::shared_ptr creds; - if (ssl) { + if (target.ssl) { grpc::SslCredentialsOptions options; - options.pem_root_certs = trustedCert; + options.pem_root_certs = target.trustedCert; + creds = grpc::SslCredentials(options); } else { creds = grpc::InsecureChannelCredentials(); } - auto channel = grpc::CreateChannel(target, creds); + auto channel = grpc::CreateChannel(target.endpoint, creds); channel->GetState(true); // trigger 'connecting' state stub = TraceService::NewStub(channel);