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 meters and instrument name #1378

Merged
merged 5 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
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
94 changes: 38 additions & 56 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,52 @@ 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);
std::puts(sanitized.c_str());
lalitb marked this conversation as resolved.
Show resolved Hide resolved
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 +96,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
22 changes: 18 additions & 4 deletions sdk/src/metrics/meter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,24 @@ 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_ = instrument_descriptor.name_;
lalitb marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
view_instr_desc.name_ = view.GetName();
}
if (view.GetDescription().empty())
{
view_instr_desc.description_ = instrument_descriptor.description_;
lalitb marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
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