Skip to content

Commit

Permalink
add test for OtlpMetricUtils::PopulateRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarker committed Dec 5, 2024
1 parent 61e5dff commit 3e15cce
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
19 changes: 6 additions & 13 deletions exporters/otlp/test/otlp_file_metric_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,12 @@ class OtlpFileMetricExporterTestPeer : public ::testing::Test
sum_point_data2.value_ = 20.0;
opentelemetry::sdk::metrics::ResourceMetrics data;

const std::string resource_schema_url{"https://opentelemetry.io/schemas/1.2.1"};

auto resource = opentelemetry::sdk::resource::Resource::Create(
opentelemetry::sdk::resource::ResourceAttributes{}, resource_schema_url);
opentelemetry::sdk::resource::ResourceAttributes{}, "resource_url");
data.resource_ = &resource;

const std::string instrumentation_scope_name{"library_name"};
const std::string instrumentation_scope_version{"1.5.0"};
const std::string instrumentation_scope_schema_url{"https://opentelemetry.io/schemas/1.2.0"};

auto scope = opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
instrumentation_scope_name, instrumentation_scope_version, instrumentation_scope_schema_url,
"library_name", "1.5.0", "scope_url",
{{"scope_key", "scope_value"}});

opentelemetry::sdk::metrics::MetricData metric_data{
Expand Down Expand Up @@ -131,11 +125,10 @@ class OtlpFileMetricExporterTestPeer : public ::testing::Test
auto scope_metrics = *resource_metrics["scopeMetrics"].begin();
auto scope = scope_metrics["scope"];

EXPECT_EQ(resource_schema_url, resource_metrics["schemaUrl"].get<std::string>());

EXPECT_EQ(instrumentation_scope_schema_url, scope_metrics["schemaUrl"].get<std::string>());
EXPECT_EQ(instrumentation_scope_name, scope["name"].get<std::string>());
EXPECT_EQ(instrumentation_scope_version, scope["version"].get<std::string>());
EXPECT_EQ("resource_url", resource_metrics["schemaUrl"].get<std::string>());
EXPECT_EQ("library_name", scope["name"].get<std::string>());
EXPECT_EQ("1.5.0", scope["version"].get<std::string>());
EXPECT_EQ("scope_url", scope_metrics["schemaUrl"].get<std::string>());
ASSERT_EQ(1, scope["attributes"].size());
const auto scope_attribute = scope["attributes"].front();
EXPECT_EQ("scope_key", scope_attribute["key"].get<std::string>());
Expand Down
38 changes: 38 additions & 0 deletions exporters/otlp/test/otlp_metrics_serialization_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
#include "opentelemetry/common/timestamp.h"
#include "opentelemetry/exporters/otlp/otlp_metric_utils.h"
#include "opentelemetry/exporters/otlp/otlp_preferred_temporality.h"
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
#include "opentelemetry/sdk/metrics/data/metric_data.h"
#include "opentelemetry/sdk/metrics/data/point_data.h"
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
#include "opentelemetry/sdk/metrics/instruments.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/version.h"

// clang-format off
#include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" // IWYU pragma: keep
#include "opentelemetry/proto/collector/metrics/v1/metrics_service.pb.h"
#include "opentelemetry/proto/common/v1/common.pb.h"
#include "opentelemetry/proto/metrics/v1/metrics.pb.h"
#include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" // IWYU pragma: keep
Expand Down Expand Up @@ -302,6 +305,41 @@ TEST(OtlpMetricSerializationTest, ObservableUpDownCounter)
EXPECT_EQ(1, 1);
}

TEST(OtlpMetricSerializationTest, PopulateExportMetricsServiceRequest)
{
const auto resource = resource::Resource::Create({{"service.name", "test_service_name"}}, "resource_schema_url");
const auto scope = opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create("scope_name", "scope_version", "scope_schema_url",
{{"scope_key", "scope_value"}});

metrics_sdk::ScopeMetrics scope_metrics{scope.get(), CreateSumAggregationData()};
metrics_sdk::ResourceMetrics resource_metrics{&resource, scope_metrics};

proto::collector::metrics::v1::ExportMetricsServiceRequest request_proto;
otlp_exporter::OtlpMetricUtils::PopulateRequest(resource_metrics, &request_proto);

ASSERT_EQ(1, request_proto.resource_metrics_size());
const auto& resource_metrics_proto = request_proto.resource_metrics(0);
const auto& resource_proto = resource_metrics_proto.resource();
EXPECT_EQ("resource_schema_url", resource_metrics_proto.schema_url());

ASSERT_EQ(1, resource_metrics_proto.scope_metrics_size());
const auto& scope_metrics_proto = resource_metrics_proto.scope_metrics(0);
EXPECT_EQ("scope_schema_url", scope_metrics_proto.schema_url());

ASSERT_EQ(1, scope_metrics_proto.metrics_size());
const auto& metric_proto = scope_metrics_proto.metrics(0);
EXPECT_EQ("Counter", metric_proto.name());

const auto& scope_proto = scope_metrics_proto.scope();
EXPECT_EQ("scope_name", scope_proto.name());
EXPECT_EQ("scope_version", scope_proto.version());

ASSERT_EQ(1, scope_proto.attributes_size());
const auto& scope_attributes_proto = scope_proto.attributes(0);
EXPECT_EQ("scope_key", scope_attributes_proto.key());
EXPECT_EQ("scope_value", scope_attributes_proto.value().string_value());
}

} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE

0 comments on commit 3e15cce

Please sign in to comment.