Skip to content

Commit

Permalink
Prometheus exporter meters and instrument name (open-telemetry#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

0 comments on commit 95effbd

Please sign in to comment.