Skip to content

Commit

Permalink
Merge remote-tracking branch 'opentelemetry/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
owent committed Oct 3, 2023
2 parents db783e2 + 0803e6a commit 3517444
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 32 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Increment the:
[#2224](https://github.com/open-telemetry/opentelemetry-cpp/pull/2224)
* [REMOVAL] Drop C++11 support
[#2342](https://github.com/open-telemetry/opentelemetry-cpp/pull/2342)
* [EXPORTER] Add otel_scope_name and otel_scope_version labels to the prometheus
exporter.
[#2293](https://github.com/open-telemetry/opentelemetry-cpp/pull/2293)

Important changes:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class PrometheusExporterUtils
template <typename T>
static void SetData(std::vector<T> values,
const opentelemetry::sdk::metrics::PointAttributes &labels,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
::prometheus::MetricType type,
::prometheus::MetricFamily *metric_family,
const opentelemetry::sdk::resource::Resource *resource);
Expand All @@ -80,15 +81,18 @@ class PrometheusExporterUtils
const std::vector<double> &boundaries,
const std::vector<uint64_t> &counts,
const opentelemetry::sdk::metrics::PointAttributes &labels,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
::prometheus::MetricFamily *metric_family,
const opentelemetry::sdk::resource::Resource *resource);

/**
* Set time and labels to metric data
*/
static void SetMetricBasic(::prometheus::ClientMetric &metric,
const opentelemetry::sdk::metrics::PointAttributes &labels,
const opentelemetry::sdk::resource::Resource *resource);
static void SetMetricBasic(
::prometheus::ClientMetric &metric,
const opentelemetry::sdk::metrics::PointAttributes &labels,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
const opentelemetry::sdk::resource::Resource *resource);

/**
* Convert attribute value to string
Expand Down
71 changes: 51 additions & 20 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ namespace metrics
namespace
{

static constexpr const char *kScopeNameKey = "otel_scope_name";
static constexpr const char *kScopeVersionKey = "otel_scope_version";

/**
* Sanitize the given metric name by replacing invalid characters with _,
* ensuring that multiple consecutive _ characters are collapsed to a single _.
Expand Down Expand Up @@ -173,7 +176,8 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
sum = static_cast<double>(nostd::get<int64_t>(histogram_point_data.sum_));
}
SetData(std::vector<double>{sum, (double)histogram_point_data.count_}, boundaries, counts,
point_data_attr.attributes, &metric_family, data.resource_);
point_data_attr.attributes, instrumentation_info.scope_, &metric_family,
data.resource_);
}
else if (type == prometheus_client::MetricType::Gauge)
{
Expand All @@ -183,14 +187,16 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
auto last_value_point_data =
nostd::get<sdk::metrics::LastValuePointData>(point_data_attr.point_data);
std::vector<metric_sdk::ValueType> values{last_value_point_data.value_};
SetData(values, point_data_attr.attributes, type, &metric_family, data.resource_);
SetData(values, point_data_attr.attributes, instrumentation_info.scope_, type,
&metric_family, data.resource_);
}
else if (nostd::holds_alternative<sdk::metrics::SumPointData>(point_data_attr.point_data))
{
auto sum_point_data =
nostd::get<sdk::metrics::SumPointData>(point_data_attr.point_data);
std::vector<metric_sdk::ValueType> values{sum_point_data.value_};
SetData(values, point_data_attr.attributes, type, &metric_family, data.resource_);
SetData(values, point_data_attr.attributes, instrumentation_info.scope_, type,
&metric_family, data.resource_);
}
else
{
Expand All @@ -206,7 +212,8 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
auto sum_point_data =
nostd::get<sdk::metrics::SumPointData>(point_data_attr.point_data);
std::vector<metric_sdk::ValueType> values{sum_point_data.value_};
SetData(values, point_data_attr.attributes, type, &metric_family, data.resource_);
SetData(values, point_data_attr.attributes, instrumentation_info.scope_, type,
&metric_family, data.resource_);
}
else
{
Expand Down Expand Up @@ -320,15 +327,17 @@ void PrometheusExporterUtils::SetTarget(const sdk::metrics::ResourceMetrics &dat
* sum => Prometheus Counter
*/
template <typename T>
void PrometheusExporterUtils::SetData(std::vector<T> values,
const metric_sdk::PointAttributes &labels,
prometheus_client::MetricType type,
prometheus_client::MetricFamily *metric_family,
const opentelemetry::sdk::resource::Resource *resource)
void PrometheusExporterUtils::SetData(
std::vector<T> values,
const metric_sdk::PointAttributes &labels,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
prometheus_client::MetricType type,
prometheus_client::MetricFamily *metric_family,
const opentelemetry::sdk::resource::Resource *resource)
{
metric_family->metric.emplace_back();
prometheus_client::ClientMetric &metric = metric_family->metric.back();
SetMetricBasic(metric, labels, resource);
SetMetricBasic(metric, labels, scope, resource);
SetValue(values, type, &metric);
}

Expand All @@ -337,25 +346,29 @@ void PrometheusExporterUtils::SetData(std::vector<T> values,
* Histogram => Prometheus Histogram
*/
template <typename T>
void PrometheusExporterUtils::SetData(std::vector<T> values,
const std::vector<double> &boundaries,
const std::vector<uint64_t> &counts,
const metric_sdk::PointAttributes &labels,
prometheus_client::MetricFamily *metric_family,
const opentelemetry::sdk::resource::Resource *resource)
void PrometheusExporterUtils::SetData(
std::vector<T> values,
const std::vector<double> &boundaries,
const std::vector<uint64_t> &counts,
const metric_sdk::PointAttributes &labels,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
prometheus_client::MetricFamily *metric_family,
const opentelemetry::sdk::resource::Resource *resource)
{
metric_family->metric.emplace_back();
prometheus_client::ClientMetric &metric = metric_family->metric.back();
SetMetricBasic(metric, labels, resource);
SetMetricBasic(metric, labels, scope, resource);
SetValue(values, boundaries, counts, &metric);
}

/**
* Set labels to metric data
*/
void PrometheusExporterUtils::SetMetricBasic(prometheus_client::ClientMetric &metric,
const metric_sdk::PointAttributes &labels,
const opentelemetry::sdk::resource::Resource *resource)
void PrometheusExporterUtils::SetMetricBasic(
prometheus_client::ClientMetric &metric,
const metric_sdk::PointAttributes &labels,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
const opentelemetry::sdk::resource::Resource *resource)
{
if (labels.empty() && nullptr == resource)
{
Expand Down Expand Up @@ -390,6 +403,24 @@ void PrometheusExporterUtils::SetMetricBasic(prometheus_client::ClientMetric &me
<< "'. Ignoring this label.");
}
}
if (!scope)
{
return;
}
auto scope_name = scope->GetName();
if (!scope_name.empty())
{
metric.label.emplace_back();
metric.label.back().name = kScopeNameKey;
metric.label.back().value = std::move(scope_name);
}
auto scope_version = scope->GetVersion();
if (!scope_version.empty())
{
metric.label.emplace_back();
metric.label.back().name = kScopeVersionKey;
metric.label.back().value = std::move(scope_version);
}
}

std::string PrometheusExporterUtils::AttributeValueToString(
Expand Down
23 changes: 14 additions & 9 deletions exporters/prometheus/test/exporter_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerCounter)

auto metric1 = translated[1];
std::vector<int> vals = {10};

assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Counter, 1,
assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Counter, 3,
vals);

int checked_label_num = 0;
Expand Down Expand Up @@ -158,7 +157,7 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerLastValue)

auto metric1 = translated[1];
std::vector<int> vals = {10};
assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Gauge, 1,
assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Gauge, 3,
vals);

int checked_label_num = 0;
Expand Down Expand Up @@ -197,7 +196,7 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusHistogramNormal)

auto metric = translated[1];
std::vector<double> vals = {3, 900.5, 4};
assert_basic(metric, "library_name", "description", prometheus_client::MetricType::Histogram, 1,
assert_basic(metric, "library_name", "description", prometheus_client::MetricType::Histogram, 3,
vals);
assert_histogram(metric, std::list<double>{10.1, 20.2, 30.2}, {200, 300, 400, 500});

Expand Down Expand Up @@ -320,20 +319,26 @@ class AttributeCollisionTest : public ::testing::Test

TEST_F(AttributeCollisionTest, SeparatesDistinctKeys)
{
CheckTranslation({{"foo.a", "value1"}, {"foo.b", "value2"}},
{{"foo_a", "value1"}, {"foo_b", "value2"}});
CheckTranslation({{"foo.a", "value1"}, {"foo.b", "value2"}}, {{"foo_a", "value1"},
{"foo_b", "value2"},
{"otel_scope_name", "library_name"},
{"otel_scope_version", "1.2.0"}});
}

TEST_F(AttributeCollisionTest, JoinsCollidingKeys)
{
CheckTranslation({{"foo.a", "value1"}, {"foo_a", "value2"}}, //
{{"foo_a", "value1;value2"}});
CheckTranslation({{"foo.a", "value1"}, {"foo_a", "value2"}}, {{"foo_a", "value1;value2"},
{"otel_scope_name", "library_name"},
{"otel_scope_version", "1.2.0"}});
}

TEST_F(AttributeCollisionTest, DropsInvertedKeys)
{
CheckTranslation({{"foo.a", "value1"}, {"foo.b", "value2"}, {"foo__a", "value3"}},
{{"foo_a", "value1"}, {"foo_b", "value2"}});
{{"foo_a", "value1"},
{"foo_b", "value2"},
{"otel_scope_name", "library_name"},
{"otel_scope_version", "1.2.0"}});
}

OPENTELEMETRY_END_NAMESPACE

0 comments on commit 3517444

Please sign in to comment.