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

protobuf: Fix label encoding #123

Merged
merged 10 commits into from
Mar 6, 2023

Conversation

ackintosh
Copy link
Contributor

@ackintosh ackintosh commented Jan 6, 2023

Fixes #124

Signed-off-by: ackintosh <sora.akatsuki@gmail.com>
@ackintosh
Copy link
Contributor Author

I have added a test case to reproduce #124. I was looking into this but couldn't find how to fix. 😓

@ackintosh ackintosh changed the title fix(protobuf): a bug on labels protobuf: a test case to reproduce a bug on labels Jan 8, 2023
@divagant-martian
Copy link
Contributor

Code is a bit hard to get due to lack of documentation / generic names. Based on the text::MetricEncoder field family_labels, I assume the protobuf::MetricEncoder labels are also meant to be shared labels.

So if I add this diff

diff --git a/src/encoding/protobuf.rs b/src/encoding/protobuf.rs
index f40338b..d1f58b2 100644
--- a/src/encoding/protobuf.rs
+++ b/src/encoding/protobuf.rs
@@ -193,6 +193,7 @@ impl<'a> MetricEncoder<'a> {
         &'b mut self,
         label_set: &S,
     ) -> Result<MetricEncoder<'b>, std::fmt::Error> {
+        self.labels.clear();
         label_set.encode(
             LabelSetEncoder {
                 labels: self.labels,
@@ -563,7 +564,7 @@ mod tests {
         family
             .get_or_create(&vec![
                 ("method".to_string(), "GET".to_string()),
-                ("status".to_string(), "200".to_string()),
+                ("status".to_string(), "404".to_string()),
             ])
             .inc();
 
@@ -591,7 +592,7 @@ mod tests {
         assert_eq!("method", metric.labels[0].name);
         assert_eq!("GET", metric.labels[0].value);
         assert_eq!("status", metric.labels[1].name);
-        assert_eq!("200", metric.labels[1].value);
+        assert_eq!("404", metric.labels[1].value);
 
         match extract_metric_point_value(&metric_set) {
             openmetrics_data_model::metric_point::Value::CounterValue(value) => {

the test you added gets the correct metric labels, but the test encoding::protobuf::tests::encode_counter_family_with_prefix_with_label misses the prefix label.

So I think the problem is that the "family" labels are being populated with normal labels somewhere, can't point to where yet

@ackintosh
Copy link
Contributor Author

I'm not fully sure if I understand current implementation, we should separate labels to e.g. const_labels and family_labels like text encoding? @mxinden How do you think? 🙏

@mxinden
Copy link
Member

mxinden commented Jan 17, 2023

@divagant-martian proposal above, namely to clear() the self.labels on a call to encode_family sounds good to me. This is inline with what the text encoder does, namely to override any existing family labels:

family_labels: Some(label_set),

@ackintosh would you mind applying the suggestion from @divagant-martian?

Signed-off-by: ackintosh <sora.akatsuki@gmail.com>
@ackintosh
Copy link
Contributor Author

I have applied the suggestion but the encode_counter_family_with_prefix_with_label test fails. 🤔

https://github.com/prometheus/client_rust/actions/runs/3958633415/jobs/6780437752#step:9:575

---- encoding::protobuf::tests::encode_counter_family_with_prefix_with_label stdout ----
thread 'encoding::protobuf::tests::encode_counter_family_with_prefix_with_label' panicked at 'assertion failed: (left == right)
left: 3,
right: 2', src/encoding/protobuf.rs:645:9

@divagant-martian
Copy link
Contributor

I said this test would fail in my comment

the test you added gets the correct metric labels, but the test encoding::protobuf::tests::encode_counter_family_with_prefix_with_label misses the prefix label.

So I think the problem is that the "family" labels are being populated with normal labels somewhere, can't point to where yet

So what I think should happen is that this field should be empty in the new test since none of the labels are shared, but populated with the prefix label in the second test. Still don't know where this change should be done tho

@ackintosh

This comment was marked as resolved.

@ackintosh ackintosh changed the title protobuf: a test case to reproduce a bug on labels protobuf: a bug on labels Feb 17, 2023
Signed-off-by: ackintosh <sora.akatsuki@gmail.com>
@ackintosh ackintosh marked this pull request as ready for review February 17, 2023 07:42
@ackintosh
Copy link
Contributor Author

@mxinden @divagant-martian The bug should have been fixed, please have a look. 🙏

@divagant-martian
Copy link
Contributor

Thanks @ackintosh I'm not sure if I'm the right person to review, but the test look legit and they run alright, so that's great!

Whether done here or a different PR I think it would be valuable for future us to add documentation to the protofub related structs and fields. In the context of this PR, in particular the labels fields

@ackintosh ackintosh changed the title protobuf: a bug on labels protobuf: Fix label encoding Feb 19, 2023
Signed-off-by: ackintosh <sora.akatsuki@gmail.com>
Signed-off-by: ackintosh <sora.akatsuki@gmail.com>
@ackintosh
Copy link
Contributor Author

I have added a doc for encoding::protobuf::MetricEncoder for now. 🙂

@mxinden
Copy link
Member

mxinden commented Mar 6, 2023

Thanks for the help here @ackintosh and @divagant-martian!

@mxinden mxinden merged commit e35e99d into prometheus:master Mar 6, 2023
@ackintosh ackintosh deleted the fix/protobuf-labels branch March 7, 2023 00:14
@ackintosh ackintosh restored the fix/protobuf-labels branch March 7, 2023 00:14
@ackintosh
Copy link
Contributor Author

@mxinden Could you please cut a new release? I'm thinking of including this fix in rust-libp2p.

@divagant-martian
Copy link
Contributor

friendly ping @mxinden for whenever you have the time or if you are waiting on important changes for a release 🖤

@mxinden
Copy link
Member

mxinden commented Apr 4, 2023

Sorry for the delay. Tagged and published with #133.

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.

protobuf: Labels duplicate between metrics
3 participants