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

Fix prometheus metric format (#2825) #2899

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ZhengweiZhu
Copy link

To meet specification of prometheus metric, there should be only one TYPE field for the same metric name.

We have meet this problem when implementing prometheus exporter for our own product, and this patch is applied with this problem fixed. The code is simple and efficient.

BTW, there are some other issue regarding to prometheus output format, which I will create issues and push PR. Thanks

What problem does this PR solve?

Issue Number:
#2825

Problem Summary:

What is changed and the side effects?

Changed:

Side effects:

  • Performance effects(性能影响): NO

  • Breaking backward compatibility(向后兼容性):


Check List:

  • Please make sure your changes are compilable(请确保你的更改可以通过编译).
  • When providing us with a new feature, it is best to add related tests(如果你向我们增加一个新的功能, 请添加相关测试).
  • Please follow Contributor Covenant Code of Conduct.(请遵循贡献者准则).

To meet specification of prometheus metric, there should be only
one TYPE field for the same metric name
<< name << " " << desc << '\n';
// To meet specification of prometheus metric, there should be only
// one TYPE field for the same metric name.
if (metrics_name.as_string() != _last_metric_name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply ignoring metrics that are duplicates of the previous ones does not completely solve the problem. If the metrics themselves are repetitive, perhaps we should try to identify and address the root cause; if it's just a matter of duplicate names, consider fixing the metrics name.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me add some background knowledge. The metrics are stored in butil::FlatMap and the uniqueness of metric name is guaranteed. The problem arises from the introduction of mvar, where one metric name will correspond to multiple metric with different labels. My patch just handles this problem

@wwbmmm
Copy link
Contributor

wwbmmm commented Feb 24, 2025

This PR is similar to #2870, which one is better?

@ZhengweiZhu
Copy link
Author

ZhengweiZhu commented Feb 24, 2025

This PR is similar to #2870, which one is better?

The #2870 introduced a std::set container to remove duplicates, which is unnecessary and impacts performance. As the metrics are stored in butil::FlatMap and the uniqueness of metric name is guaranteed.

When we just expose bvar, the current upstream code is fine. The problem arises from the introduction of mvar, where one metric name will correspond to multiple metric with different labels. My patch handles this problem and follow the specifition of Prometheus metric.

@wwbmmm
Copy link
Contributor

wwbmmm commented Feb 24, 2025

LGTM

@wasphin
Copy link
Member

wasphin commented Feb 24, 2025

There's a new dump_comment API when introducing mvar by @serverglen, is this the case to set some HELP/TYPE for it? dump_comment is only used by mvar.

$ git grep dump_comment

src/bvar/multi_dimension_inl.h:242:    if (label_names.empty() || !dumper->dump_comment(name(), METRIC_TYPE_GAUGE)) {
src/bvar/multi_dimension_inl.h:279:        if (!dumper->dump_comment(name() + "_latency", METRIC_TYPE_GAUGE)) {
src/bvar/multi_dimension_inl.h:312:        if (!dumper->dump_comment(name() + "_max_latency", METRIC_TYPE_GAUGE)) {
src/bvar/multi_dimension_inl.h:323:        if (!dumper->dump_comment(name() + "_qps", METRIC_TYPE_GAUGE)) {
src/bvar/multi_dimension_inl.h:334:        if (!dumper->dump_comment(name() + "_count", METRIC_TYPE_COUNTER)) {
src/bvar/variable.h:56:    virtual bool dump_comment(const std::string&, const std::string& /*type*/) {

@ZhengweiZhu
Copy link
Author

ZhengweiZhu commented Feb 24, 2025

There's a new dump_comment API when introducing mvar by @serverglen, is this the case to set some HELP/TYPE for it? dump_comment is only used by mvar.

$ git grep dump_comment

src/bvar/multi_dimension_inl.h:242:    if (label_names.empty() || !dumper->dump_comment(name(), METRIC_TYPE_GAUGE)) {
src/bvar/multi_dimension_inl.h:279:        if (!dumper->dump_comment(name() + "_latency", METRIC_TYPE_GAUGE)) {
src/bvar/multi_dimension_inl.h:312:        if (!dumper->dump_comment(name() + "_max_latency", METRIC_TYPE_GAUGE)) {
src/bvar/multi_dimension_inl.h:323:        if (!dumper->dump_comment(name() + "_qps", METRIC_TYPE_GAUGE)) {
src/bvar/multi_dimension_inl.h:334:        if (!dumper->dump_comment(name() + "_count", METRIC_TYPE_COUNTER)) {
src/bvar/variable.h:56:    virtual bool dump_comment(const std::string&, const std::string& /*type*/) {

Reasonable. By overriding dump_comment method of PrometheusMetricsDumper to output HELP/TYPE for a metric, then the dump method should be changed to only output name and value (not including HELP/TYPE). I will try it this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants