diff --git a/CHANGELOG.md b/CHANGELOG.md index 52f280eb22..5b5a9ac763 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,9 @@ Increment the: * [EXPORTER] Fix scope attributes missing from otlp traces metrics [#3185](https://github.com/open-telemetry/opentelemetry-cpp/pull/3185) +* [EXPORTER] Fix throw in OtlpGrpcMetricExporter with shared grpc client + [#3243](https://github.com/open-telemetry/opentelemetry-cpp/pull/3243) + ## [1.18 2024-11-25] * [EXPORTER] Fix crash in ElasticsearchLogRecordExporter diff --git a/exporters/otlp/src/otlp_grpc_metric_exporter.cc b/exporters/otlp/src/otlp_grpc_metric_exporter.cc index 1723dd557a..2ed7b4ed6c 100644 --- a/exporters/otlp/src/otlp_grpc_metric_exporter.cc +++ b/exporters/otlp/src/otlp_grpc_metric_exporter.cc @@ -50,6 +50,8 @@ OtlpGrpcMetricExporter::OtlpGrpcMetricExporter( OtlpGrpcMetricExporter::OtlpGrpcMetricExporter(const OtlpGrpcMetricExporterOptions &options, const std::shared_ptr &client) : options_(options), + aggregation_temporality_selector_{ + OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, client_(client), client_reference_guard_(OtlpGrpcClientFactory::CreateReferenceGuard()) { @@ -62,6 +64,8 @@ OtlpGrpcMetricExporter::OtlpGrpcMetricExporter( std::unique_ptr stub, const std::shared_ptr &client) : options_(OtlpGrpcMetricExporterOptions()), + aggregation_temporality_selector_{ + OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, client_(client), client_reference_guard_(OtlpGrpcClientFactory::CreateReferenceGuard()), metrics_service_stub_(std::move(stub)) diff --git a/exporters/otlp/test/otlp_grpc_metric_exporter_test.cc b/exporters/otlp/test/otlp_grpc_metric_exporter_test.cc index 6689233a9d..d36def10f5 100644 --- a/exporters/otlp/test/otlp_grpc_metric_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_metric_exporter_test.cc @@ -25,6 +25,9 @@ # include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" +# include "opentelemetry/exporters/otlp/otlp_grpc_client.h" +# include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h" + # include "opentelemetry/sdk/trace/simple_processor.h" # include "opentelemetry/sdk/trace/tracer_provider.h" # include "opentelemetry/trace/provider.h" @@ -50,12 +53,34 @@ class OtlpGrpcMetricExporterTestPeer : public ::testing::Test { public: std::unique_ptr GetExporter( - std::unique_ptr &stub_interface) + const OtlpGrpcMetricExporterOptions &options) + { + return std::unique_ptr(new OtlpGrpcMetricExporter(options)); + } + + std::unique_ptr GetExporter( + std::unique_ptr stub_interface) { return std::unique_ptr( new OtlpGrpcMetricExporter(std::move(stub_interface))); } + std::unique_ptr GetExporter( + std::unique_ptr stub_interface, + std::shared_ptr client) + { + return std::unique_ptr( + new OtlpGrpcMetricExporter(std::move(stub_interface), std::move(client))); + } + + std::unique_ptr GetExporter( + const OtlpGrpcMetricExporterOptions &options, + std::shared_ptr client) + { + return std::unique_ptr( + new OtlpGrpcMetricExporter(options, std::move(client))); + } + // Get the options associated with the given exporter. const OtlpGrpcMetricExporterOptions &GetOptions(std::unique_ptr &exporter) { @@ -204,6 +229,35 @@ TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigUnknownInsecureFromEnv) } # endif +TEST_F(OtlpGrpcMetricExporterTestPeer, CheckGetAggregationTemporality) +{ + auto options = OtlpGrpcMetricExporterOptions(); + options.aggregation_temporality = PreferredAggregationTemporality::kCumulative; + + auto client = OtlpGrpcClientFactory::Create(options); + + auto exporter0 = GetExporter(options); + auto exporter1 = GetExporter(client->MakeMetricsServiceStub()); + auto exporter2 = GetExporter(options, client); + auto exporter3 = GetExporter(client->MakeMetricsServiceStub(), client); + + EXPECT_EQ( + opentelemetry::sdk::metrics::AggregationTemporality::kCumulative, + exporter0->GetAggregationTemporality(opentelemetry::sdk::metrics::InstrumentType::kCounter)); + + EXPECT_EQ( + opentelemetry::sdk::metrics::AggregationTemporality::kCumulative, + exporter1->GetAggregationTemporality(opentelemetry::sdk::metrics::InstrumentType::kCounter)); + + EXPECT_EQ( + opentelemetry::sdk::metrics::AggregationTemporality::kCumulative, + exporter2->GetAggregationTemporality(opentelemetry::sdk::metrics::InstrumentType::kCounter)); + + EXPECT_EQ( + opentelemetry::sdk::metrics::AggregationTemporality::kCumulative, + exporter3->GetAggregationTemporality(opentelemetry::sdk::metrics::InstrumentType::kCounter)); +} + } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE