Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prometheus exporter support Gauge Type #1553

Merged
merged 2 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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