Skip to content

Commit

Permalink
Prometheus exporter support Gauge Type (#1553)
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo authored Aug 10, 2022
1 parent 45037ac commit f0109eb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
41 changes: 37 additions & 4 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# include <sstream>
# include <utility>
# include <vector>
# include "prometheus/metric_family.h"

# include <prometheus/metric_type.h>
# include "opentelemetry/exporters/prometheus/exporter_utils.h"
Expand Down Expand Up @@ -74,12 +75,38 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
SetData(std::vector<double>{sum, (double)histogram_point_data.count_}, boundaries,
counts, point_data_attr.attributes, time, &metric_family);
}
else if (type == prometheus_client::MetricType::Gauge)
{
if (nostd::holds_alternative<sdk::metrics::LastValuePointData>(
point_data_attr.point_data))
{
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, time, &metric_family);
}
else
{
OTEL_INTERNAL_LOG_WARN(
"[Prometheus Exporter] TranslateToPrometheus - "
"invalid LastValuePointData type");
}
}
else // Counter, Untyped
{
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, time, &metric_family);
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, time, &metric_family);
}
else
{
OTEL_INTERNAL_LOG_WARN(
"[Prometheus Exporter] TranslateToPrometheus - "
"invalid SumPointData type");
}
}
}
output.emplace_back(metric_family);
Expand Down Expand Up @@ -140,6 +167,8 @@ prometheus_client::MetricType PrometheusExporterUtils::TranslateType(
return prometheus_client::MetricType::Counter;
case metric_sdk::AggregationType::kHistogram:
return prometheus_client::MetricType::Histogram;
case metric_sdk::AggregationType::kLastValue:
return prometheus_client::MetricType::Gauge;
default:
return prometheus_client::MetricType::Untyped;
}
Expand Down Expand Up @@ -276,6 +305,10 @@ void PrometheusExporterUtils::SetValue(std::vector<T> values,
metric->counter.value = value;
break;
}
case prometheus_client::MetricType::Gauge: {
metric->gauge.value = value;
break;
}
case prometheus_client::MetricType::Untyped: {
metric->untyped.value = value;
break;
Expand Down
31 changes: 28 additions & 3 deletions exporters/prometheus/test/exporter_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ void assert_basic(prometheus_client::MetricFamily &metric,
ASSERT_EQ(buckets.size(), vals[2]);
break;
}
case prometheus_client::MetricType::Gauge:
// Gauge type not supported
ASSERT_TRUE(false);
case prometheus_client::MetricType::Gauge: {
ASSERT_DOUBLE_EQ(metric_data.gauge.value, vals[0]);
break;
}
break;
case prometheus_client::MetricType::Summary:
// Summary type not supported
ASSERT_TRUE(false);
Expand Down Expand Up @@ -110,6 +111,30 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerCounter)
vals);
}

TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerLastValue)
{
std::vector<std::unique_ptr<metric_sdk::ResourceMetrics>> collection;

collection.emplace_back(new metric_sdk::ResourceMetrics{CreateLastValuePointData()});

auto translated = PrometheusExporterUtils::TranslateToPrometheus(collection);
ASSERT_EQ(translated.size(), collection.size());

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

collection.emplace_back(new metric_sdk::ResourceMetrics{CreateLastValuePointData()});

translated = PrometheusExporterUtils::TranslateToPrometheus(collection);
ASSERT_EQ(translated.size(), collection.size());

auto metric2 = translated[1];
assert_basic(metric2, "library_name", "description", prometheus_client::MetricType::Gauge, 1,
vals);
}

TEST(PrometheusExporterUtils, TranslateToPrometheusHistogramNormal)
{
std::vector<std::unique_ptr<metric_sdk::ResourceMetrics>> collection;
Expand Down

2 comments on commit f0109eb

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: f0109eb Previous: 45037ac Ratio
BM_AttributseProcessorFilter 382.6826720933761 ns/iter 157.6747169056145 ns/iter 2.43

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp api Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: f0109eb Previous: 45037ac Ratio
BM_SpinLockThrashing/1/process_time/real_time 0.41531981308719795 ms/iter 0.12137675032598812 ms/iter 3.42
BM_SpinLockThrashing/2/process_time/real_time 0.697853744670909 ms/iter 0.2420464741814997 ms/iter 2.88
BM_ProcYieldSpinLockThrashing/1/process_time/real_time 0.3741506757682928 ms/iter 0.17975550896843687 ms/iter 2.08
BM_ProcYieldSpinLockThrashing/2/process_time/real_time 1.125807762145996 ms/iter 0.24280890575430011 ms/iter 4.64
BM_NaiveSpinLockThrashing/1/process_time/real_time 0.42398571968078613 ms/iter 0.12442272281400725 ms/iter 3.41

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.