-
Notifications
You must be signed in to change notification settings - Fork 438
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
[EXPORTER] Prometheus: Add unit to names, convert to word #2213
[EXPORTER] Prometheus: Add unit to names, convert to word #2213
Conversation
940fed8
to
06b2288
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the inner loops of
PrometheusExporterUtils::TranslateToPrometheus(),
and the change there, please clarify.
It seems the name mapping is done for every data point of a metric, instead of once per metric.
} | ||
|
||
std::vector<std::string> rate_entities; | ||
size_t pos = rate_expressed_unit.find("/"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit
do not execute the find twice, save the result in pos
and test for pos == std::string::npos
at the start of this method.
prometheus_client::MetricFamily metric_family; | ||
metric_family.type = type; | ||
metric_family.name = MapToPrometheusName(metric_data.instrument_descriptor.name_, | ||
metric_data.instrument_descriptor.unit_, type); | ||
metric_family.help = metric_data.instrument_descriptor.description_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code moved inside the loop on data points.
The metric name, type, help, etc should be constant for all data points, so can this be moved outside ?
Even if type
is needed and not available elsewhere, peek at the first data point before entering the loop on data points ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1. As long as it is safe to assume all data points have the same type, peeking at the first seems like the right thing to do. I took that approach in https://github.com/open-telemetry/opentelemetry-cpp/pull/2288/files#diff-e92aebc994eed5c134b09ecde972d9db39a9bfc46edc6d93b0b5a4c98e4ff27fR46.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, moved back to where it was.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for duplicating work, and thanks for working on this! It looks like this fixes #2287 as well, so i'll close my PR.
prometheus_client::MetricFamily metric_family; | ||
metric_family.type = type; | ||
metric_family.name = MapToPrometheusName(metric_data.instrument_descriptor.name_, | ||
metric_data.instrument_descriptor.unit_, type); | ||
metric_family.help = metric_data.instrument_descriptor.description_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1. As long as it is safe to assume all data points have the same type, peeking at the first seems like the right thing to do. I took that approach in https://github.com/open-telemetry/opentelemetry-cpp/pull/2288/files#diff-e92aebc994eed5c134b09ecde972d9db39a9bfc46edc6d93b0b5a4c98e4ff27fR46.
@@ -114,8 +114,8 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT | |||
"invalid SumPointData type"); | |||
} | |||
} | |||
output.emplace_back(metric_family); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the prometheus C++ library merge metric families? We definitely don't want one metric family for each data point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, creating new family only if it doesn't exist already.
{"B", "bytes"}, | ||
{"KB", "kilobytes"}, | ||
{"MB", "megabytes"}, | ||
{"GB", "gigabytes"}, | ||
{"TB", "terabytes"}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've removed B and $ from mappings elsewhere, as B means bel
in UCUM. See open-telemetry/opentelemetry-java#5719
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, removed.
} | ||
|
||
// Special case - counter | ||
if (prometheus_type == prometheus_client::MetricType::Counter && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Counters MUST end in _total, so we need to be less tolerant here. We can only skip the _total suffix if the name ends in _total. Since we need unit suffixes to come before type suffixes, one approach would be to trim _total suffixes from the name before appending the unit suffix, and finally append the final _total suffix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, will not add total unless if it has already.
if (prometheus_type == prometheus_client::MetricType::Counter) | ||
{ | ||
auto t_pos = sanitized_name.rfind("_total"); | ||
bool ends_with_total = t_pos == sanitized_name.size() - 6; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this work if the metric has a unit? E.g. if I have a metric: foo.bar.total
, with unit s
, will I get foo_bar_total_seconds_total
or foo_bar_seconds_total
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dashpole the output will be foo_bar_total_seconds_total
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, you would trim _total from counters before appending the unit, and then always add _total after the unit for counters.
I believe this would also fix #2317. |
Hi @esigo Please merge with a recent main to resolve conflicts, so this PR can be reviewed and merged. Thanks. |
@@ -39,7 +39,7 @@ std::vector<prometheus_client::MetricFamily> PrometheusCollector::Collect() cons | |||
reader_->Collect([&result](sdk::metrics::ResourceMetrics &metric_data) { | |||
auto prometheus_metric_data = PrometheusExporterUtils::TranslateToPrometheus(metric_data); | |||
for (auto &data : prometheus_metric_data) | |||
result.emplace_back(data); | |||
result.emplace_back(data.second); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it better to use std::move(data.second)
here? ::prometheus::MetricFamily
may contains a lot of data, and copy these data may have performance problem.
{ | ||
|
||
// initialize output vector | ||
std::vector<prometheus_client::MetricFamily> output; | ||
std::unordered_map<std::string, prometheus_client::MetricFamily> output; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use output.reserve()
first to reduce rehash?
@esigo Is there any way I can help with this? I would love to get this over the finish line soon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix.
…s/exporter_utils.h Co-authored-by: David Ashpole <[email protected]>
This is mainly to get the fix made in open-telemetry/opentelemetry-cpp#2213 When opentelemetry-cpp makes a stable release with this fix, we'll switch to that. Closes #35151 COPYBARA_INTEGRATE_REVIEW=#35151 from yashykt:UpdateOTel 1041fbc PiperOrigin-RevId: 586815878
This is mainly to get the fix made in open-telemetry/opentelemetry-cpp#2213 When opentelemetry-cpp makes a stable release with this fix, we'll switch to that. Closes grpc#35151 COPYBARA_INTEGRATE_REVIEW=grpc#35151 from yashykt:UpdateOTel 1041fbc PiperOrigin-RevId: 586815878
Fixes #1840 Fixes #2317 (issue)
Changes
Please provide a brief description of the changes here.
For significant contributions please make sure you have completed the following items:
CHANGELOG.md
updated for non-trivial changes