Skip to content

Commit

Permalink
Prometheus exporter meters and instrument name (#1378)
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo authored May 10, 2022
1 parent 54abc27 commit 95effbd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ class PrometheusExporterUtils
const std::vector<std::unique_ptr<sdk::metrics::ResourceMetrics>> &data);

private:
/**
* Set value to metric family according to record
*/
static void SetMetricFamily(sdk::metrics::ResourceMetrics &data,
::prometheus::MetricFamily *metric_family);

/**
* Sanitize the given metric name or label according to Prometheus rule.
*
Expand All @@ -49,13 +43,6 @@ class PrometheusExporterUtils
*/
static std::string SanitizeNames(std::string name);

/**
* Set value to metric family for different aggregator
*/
static void SetMetricFamilyByAggregator(const sdk::metrics::ResourceMetrics &data,
std::string labels_str,
::prometheus::MetricFamily *metric_family);

static opentelemetry::sdk::metrics::AggregationType getAggregationType(
const opentelemetry::sdk::metrics::PointType &point_type);

Expand Down
93 changes: 37 additions & 56 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,51 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
}

// initialize output vector
std::vector<prometheus_client::MetricFamily> output(data.size());
std::vector<prometheus_client::MetricFamily> output;

// iterate through the vector and set result data into it
int i = 0;
for (const auto &r : data)
{
SetMetricFamily(*r, &output[i]);
i++;
for (const auto &instrumentation_info : r->instrumentation_info_metric_data_)
{
for (const auto &metric_data : instrumentation_info.metric_data_)
{
auto origin_name = metric_data.instrument_descriptor.name_;
auto sanitized = SanitizeNames(origin_name);
prometheus_client::MetricFamily metric_family;
metric_family.name = sanitized;
metric_family.help = metric_data.instrument_descriptor.description_;
auto time = metric_data.start_ts.time_since_epoch();
for (const auto &point_data_attr : metric_data.point_data_attr_)
{
auto kind = getAggregationType(point_data_attr.point_data);
const prometheus_client::MetricType type = TranslateType(kind);
metric_family.type = type;
if (type == prometheus_client::MetricType::Histogram) // Histogram
{
auto histogram_point_data =
nostd::get<sdk::metrics::HistogramPointData>(point_data_attr.point_data);
auto boundaries = histogram_point_data.boundaries_;
auto counts = histogram_point_data.counts_;
SetData(std::vector<double>{nostd::get<double>(histogram_point_data.sum_),
(double)histogram_point_data.count_},
boundaries, counts, "", time, &metric_family);
}
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, "", type, time, &metric_family);
}
}
output.emplace_back(metric_family);
}
}
}

return output;
}

/**
* Set value to metric family according to record
*/
void PrometheusExporterUtils::SetMetricFamily(sdk::metrics::ResourceMetrics &data,
prometheus_client::MetricFamily *metric_family)
{
SetMetricFamilyByAggregator(data, "", metric_family);
}

/**
* Sanitize the given metric name or label according to Prometheus rule.
*
Expand All @@ -72,48 +95,6 @@ std::string PrometheusExporterUtils::SanitizeNames(std::string name)
return name;
}

/**
* Set value to metric family for different aggregator
*/
void PrometheusExporterUtils::SetMetricFamilyByAggregator(
const sdk::metrics::ResourceMetrics &data,
std::string labels_str,
prometheus_client::MetricFamily *metric_family)
{
for (const auto &instrumentation_info : data.instrumentation_info_metric_data_)
{
auto origin_name = instrumentation_info.instrumentation_library_->GetName();
auto sanitized = SanitizeNames(origin_name);
metric_family->name = sanitized;
for (const auto &metric_data : instrumentation_info.metric_data_)
{
auto time = metric_data.start_ts.time_since_epoch();
for (const auto &point_data_attr : metric_data.point_data_attr_)
{
auto kind = getAggregationType(point_data_attr.point_data);
const prometheus_client::MetricType type = TranslateType(kind);
metric_family->type = type;
if (type == prometheus_client::MetricType::Histogram) // Histogram
{
auto histogram_point_data =
nostd::get<sdk::metrics::HistogramPointData>(point_data_attr.point_data);
auto boundaries = histogram_point_data.boundaries_;
auto counts = histogram_point_data.counts_;
SetData(std::vector<double>{nostd::get<double>(histogram_point_data.sum_),
(double)histogram_point_data.count_},
boundaries, counts, labels_str, time, metric_family);
}
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, labels_str, type, time, metric_family);
}
}
}
}
}

metric_sdk::AggregationType PrometheusExporterUtils::getAggregationType(
const metric_sdk::PointType &point_type)
{
Expand Down
14 changes: 10 additions & 4 deletions sdk/src/metrics/meter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,16 @@ std::unique_ptr<WritableMetricStorage> Meter::RegisterMetricStorage(
auto success = view_registry->FindViews(
instrument_descriptor, *instrumentation_library_,
[this, &instrument_descriptor, &storages](const View &view) {
auto view_instr_desc = instrument_descriptor;
view_instr_desc.name_ = view.GetName();
view_instr_desc.description_ = view.GetDescription();
auto storage = std::shared_ptr<SyncMetricStorage>(new SyncMetricStorage(
auto view_instr_desc = instrument_descriptor;
if (!view.GetName().empty())
{
view_instr_desc.name_ = view.GetName();
}
if (!view.GetDescription().empty())
{
view_instr_desc.description_ = view.GetDescription();
}
auto storage = std::shared_ptr<SyncMetricStorage>(new SyncMetricStorage(
view_instr_desc, view.GetAggregationType(), &view.GetAttributesProcessor(),
NoExemplarReservoir::GetNoExemplarReservoir()));
storage_registry_[instrument_descriptor.name_] = storage;
Expand Down

2 comments on commit 95effbd

@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: 95effbd Previous: 54abc27 Ratio
BM_BaselineBuffer/1 7373359.203338623 ns/iter 469961.3173500367 ns/iter 15.69
BM_LockFreeBuffer/1 3347652.6737213135 ns/iter 503524.48813610896 ns/iter 6.65

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: 95effbd Previous: 54abc27 Ratio
BM_NaiveSpinLockThrashing/2/process_time/real_time 0.4430154095525327 ms/iter 0.21433610520604568 ms/iter 2.07

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

Please sign in to comment.