From ca36aa915f1d79d796b4863790bdfa54bdd163f1 Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Wed, 18 Dec 2024 19:52:11 +0100 Subject: [PATCH] agents: share channel between OTLP exporters This will allow less grpc threads/resources to be used. --- agents/grpc/src/grpc_agent.cc | 51 ++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/agents/grpc/src/grpc_agent.cc b/agents/grpc/src/grpc_agent.cc index 27a5f623c6..3677309fbd 100644 --- a/agents/grpc/src/grpc_agent.cc +++ b/agents/grpc/src/grpc_agent.cc @@ -9,9 +9,14 @@ #include "absl/log/initialize.h" #include "opentelemetry/sdk/metrics/data/metric_data.h" #include "opentelemetry/sdk/metrics/export/metric_producer.h" +#include "opentelemetry/exporters/otlp/otlp_grpc_client.h" +#include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h" #include "opentelemetry/exporters/otlp/otlp_grpc_exporter.h" +#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter.h" +#include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_grpc_metric_exporter.h" +#include "opentelemetry/exporters/otlp/otlp_grpc_metric_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_metric_utils.h" #include "opentelemetry/trace/semantic_conventions.h" @@ -29,12 +34,17 @@ using opentelemetry::sdk::metrics::ScopeMetrics; using opentelemetry::sdk::resource::Resource; using opentelemetry::sdk::resource::ResourceAttributes; using opentelemetry::sdk::trace::Recordable; +using opentelemetry::v1::exporter::otlp::OtlpGrpcClient; +using opentelemetry::v1::exporter::otlp::OtlpGrpcClientFactory; using opentelemetry::v1::exporter::otlp::OtlpGrpcClientOptions; using opentelemetry::v1::exporter::otlp::OtlpGrpcExporter; +using opentelemetry::v1::exporter::otlp::OtlpGrpcExporterFactory; using opentelemetry::v1::exporter::otlp::OtlpGrpcExporterOptions; using opentelemetry::v1::exporter::otlp::OtlpGrpcLogRecordExporter; +using opentelemetry::v1::exporter::otlp::OtlpGrpcLogRecordExporterFactory; using opentelemetry::v1::exporter::otlp::OtlpGrpcLogRecordExporterOptions; using opentelemetry::v1::exporter::otlp::OtlpGrpcMetricExporter; +using opentelemetry::v1::exporter::otlp::OtlpGrpcMetricExporterFactory; using opentelemetry::v1::exporter::otlp::OtlpGrpcMetricExporterOptions; using opentelemetry::v1::exporter::otlp::OtlpMetricUtils; using opentelemetry::v1::trace::SemanticConventions::kProcessOwner; @@ -1020,6 +1030,24 @@ int GrpcAgent::config(const json& config) { it->get() : console_id_ + ".grpc.nodesource.io:443"; Debug("GrpcAgent configured. Endpoint: %s. Insecure: %d\n", endpoint.c_str(), static_cast(insecure)); + + + OtlpGrpcClientOptions opts; + opts.endpoint = endpoint; + opts.metadata = {{"nsolid-agent-id", agent_id_}, + {"nsolid-saas", saas_}}; + if (!insecure) { + opts.use_ssl_credentials = true; + if (!custom_certs_.empty()) { + opts.ssl_credentials_cacert_as_string = custom_certs_; + } else { + opts.ssl_credentials_cacert_as_string = cacert_; + } + } + + std::shared_ptr client = OtlpGrpcClientFactory::Create(opts); + nsolid_service_stub_ = GrpcClient::MakeNSolidServiceStub(opts); + { OtlpGrpcExporterOptions options; options.endpoint = endpoint; @@ -1034,7 +1062,7 @@ int GrpcAgent::config(const json& config) { } } - trace_exporter_ = std::make_unique(options); + trace_exporter_ = std::make_unique(options, client); } { OtlpGrpcMetricExporterOptions options; @@ -1050,7 +1078,8 @@ int GrpcAgent::config(const json& config) { } } - metrics_exporter_ = std::make_unique(options); + metrics_exporter_ = + std::make_unique(options, client); } { OtlpGrpcLogRecordExporterOptions options; @@ -1066,24 +1095,10 @@ int GrpcAgent::config(const json& config) { } } - log_exporter_ = std::make_unique(options); + log_exporter_ = + std::make_unique(options, client); } - { - OtlpGrpcClientOptions options; - options.endpoint = endpoint; - options.metadata = {{"nsolid-agent-id", agent_id_}, - {"nsolid-saas", saas_}}; - if (!insecure) { - options.use_ssl_credentials = true; - if (!custom_certs_.empty()) { - options.ssl_credentials_cacert_as_string = custom_certs_; - } else { - options.ssl_credentials_cacert_as_string = cacert_; - } - } - nsolid_service_stub_ = GrpcClient::MakeNSolidServiceStub(options); - } reset_command_stream(); } }