Skip to content

Commit

Permalink
fix throw in otlp grpc metrics exporter on get aggregation temporalit…
Browse files Browse the repository at this point in the history
…y with shared grpc client
  • Loading branch information
dbarker committed Jan 13, 2025
1 parent 57114c5 commit ff40212
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
4 changes: 4 additions & 0 deletions exporters/otlp/src/otlp_grpc_metric_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ OtlpGrpcMetricExporter::OtlpGrpcMetricExporter(
OtlpGrpcMetricExporter::OtlpGrpcMetricExporter(const OtlpGrpcMetricExporterOptions &options,
const std::shared_ptr<OtlpGrpcClient> &client)
: options_(options),
aggregation_temporality_selector_{
OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)},
client_(client),
client_reference_guard_(OtlpGrpcClientFactory::CreateReferenceGuard())
{
Expand All @@ -62,6 +64,8 @@ OtlpGrpcMetricExporter::OtlpGrpcMetricExporter(
std::unique_ptr<proto::collector::metrics::v1::MetricsService::StubInterface> stub,
const std::shared_ptr<OtlpGrpcClient> &client)
: options_(OtlpGrpcMetricExporterOptions()),
aggregation_temporality_selector_{
OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)},
client_(client),
client_reference_guard_(OtlpGrpcClientFactory::CreateReferenceGuard()),
metrics_service_stub_(std::move(stub))
Expand Down
56 changes: 55 additions & 1 deletion exporters/otlp/test/otlp_grpc_metric_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -50,12 +53,34 @@ class OtlpGrpcMetricExporterTestPeer : public ::testing::Test
{
public:
std::unique_ptr<sdk::metrics::PushMetricExporter> GetExporter(
std::unique_ptr<proto::collector::metrics::v1::MetricsService::StubInterface> &stub_interface)
const OtlpGrpcMetricExporterOptions &options)
{
return std::unique_ptr<sdk::metrics::PushMetricExporter>(new OtlpGrpcMetricExporter(options));
}

std::unique_ptr<sdk::metrics::PushMetricExporter> GetExporter(
std::unique_ptr<proto::collector::metrics::v1::MetricsService::StubInterface> stub_interface)
{
return std::unique_ptr<sdk::metrics::PushMetricExporter>(
new OtlpGrpcMetricExporter(std::move(stub_interface)));
}

std::unique_ptr<sdk::metrics::PushMetricExporter> GetExporter(
std::unique_ptr<proto::collector::metrics::v1::MetricsService::StubInterface> stub_interface,
std::shared_ptr<OtlpGrpcClient> client)
{
return std::unique_ptr<sdk::metrics::PushMetricExporter>(
new OtlpGrpcMetricExporter(std::move(stub_interface), std::move(client)));
}

std::unique_ptr<sdk::metrics::PushMetricExporter> GetExporter(
const OtlpGrpcMetricExporterOptions &options,
std::shared_ptr<OtlpGrpcClient> client)
{
return std::unique_ptr<sdk::metrics::PushMetricExporter>(
new OtlpGrpcMetricExporter(options, std::move(client)));
}

// Get the options associated with the given exporter.
const OtlpGrpcMetricExporterOptions &GetOptions(std::unique_ptr<OtlpGrpcMetricExporter> &exporter)
{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ff40212

Please sign in to comment.