Skip to content

Sanitize metric type prefixes #319

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

Conversation

sysedwinistrator
Copy link
Contributor

@sysedwinistrator sysedwinistrator commented Mar 19, 2024

When more than one prefix matches the same metric descriptor, this will throw the error collected metric xxx was collected before with the same name and label values.

For example, using the metric type prefixes
foo.googleapis.com/bar (a prefix)
and
foo.googleapis.com/bar/baz (a metric)
will result in an error because both match the metric
foo.googleapis.com/bar/baz.

Further, using the metric type prefixes
foo.googleapis.com/bar/baz (a metric)
and
foo.googleapis.com/bar/baz_count (a metric)
will result in an error because both match the metric
foo.googleapis.com/bar/baz_count.

While the first pitfall could be expected by the user, the latter will come as a complete surprise to anyone who is not aware that stackdriver-exporter internally uses an MQL query in the form of
metric.type = starts_with("<prefix>")
to filter the metrics.

Avoid this by sanitizing the provided metric type prefixes in the following way:

  • Drop any duplicate prefixes
  • Sort the prefixes (required by the next step)
  • Drop any prefixes that start with another prefix present in the input

Fixes #103

When more than one prefix matches the same metric descriptor, this will
throw the error "collected metric xxx was collected before with the
same name and label values".

For example, using the metric type prefixes
  foo.googleapis.com/bar (a prefix)
and
  foo.googleapis.com/bar/baz (a metric)
will result in an error because both match the metric
  foo.googleapis.com/bar/baz.

Further, using the metric type prefixes
  foo.googleapis.com/bar/baz (a metric)
and
  foo.googleapis.com/bar/baz_count (a metric)
will result in an error because both match the metric
  foo.googleapis.com/bar/baz_count.

While the first pitfall could be expected by the user, the latter will
come as a complete surprise to anyone who is not aware that
stackdriver-exporter internally uses an MQL query in the form of
  metric.type = starts_with("<prefix>")
to filter the metrics.

Avoid this by sanitizing the provided metric type prefixes in the
following way:
- Drop any duplicate prefixes
- Sort the prefixes (required by the next step)
- Drop any prefixes that start with another prefix present in the input

Signed-off-by: Edwin Mackenzie-Owen <[email protected]>
@sysedwinistrator sysedwinistrator force-pushed the sanitize-metric-type-prefixes branch from 4a82b2e to b3247d4 Compare March 19, 2024 18:07
@sysedwinistrator sysedwinistrator marked this pull request as draft March 20, 2024 13:36
@jotka
Copy link

jotka commented Jun 13, 2024

hi there. is it going to be merged soon?

@SuperQ
Copy link
Contributor

SuperQ commented Jun 13, 2024

It's still in draft, is it ready for review?

@jotka
Copy link

jotka commented Jun 13, 2024

@sysedwinistrator will you release it from draft status?

@sysedwinistrator
Copy link
Contributor Author

sysedwinistrator commented Jun 13, 2024

@sysedwinistrator will you release it from draft status?

Sorry, I had completely forgotten about this PR. In the project I'm working on we already have to merge metric prefixes from multiple sources so we just worked around this issue by deduplicating the prefixes where we merge them.

I'd like to add a simple test for this function. I guess I can just add a a stackdriver_exporter_test.go?

Also, as I learned when working on our internal solution, the second case (previous prefix starts with, i.e. is longer than, the current prefix) will never happen in an alphanumerically sorted list :)

In alphanumerically sorted list of strings,
abcdef
will never come before
abc.

Signed-off-by: Edwin Mackenzie-Owen <[email protected]>
@sysedwinistrator sysedwinistrator force-pushed the sanitize-metric-type-prefixes branch from 676c67a to 8e85d13 Compare June 13, 2024 11:17
Signed-off-by: Edwin Mackenzie-Owen <[email protected]>
@sysedwinistrator sysedwinistrator force-pushed the sanitize-metric-type-prefixes branch from 8e85d13 to 4d76015 Compare June 13, 2024 12:47
@sysedwinistrator sysedwinistrator marked this pull request as ready for review June 18, 2024 13:59
@jotka
Copy link

jotka commented Jun 28, 2024

@SuperQ it's ready to review now :)

Signed-off-by: Edwin Mackenzie-Owen <[email protected]>
@sysedwinistrator sysedwinistrator force-pushed the sanitize-metric-type-prefixes branch from 6d10539 to c46b249 Compare August 22, 2024 13:56
Signed-off-by: Edwin Mackenzie-Owen <[email protected]>
Signed-off-by: Edwin Mackenzie-Owen <[email protected]>
@sysedwinistrator
Copy link
Contributor Author

sysedwinistrator commented Aug 22, 2024

I've also changed the code to initialize the return array in the beginning of the function (to ensure it doesn't return a nil array if the input is an empty array). Because of that the return array doesn't need to be intiialized in the first iteration of the for loop anymore, so I was also able to simplify the if condition.

@srclosson
Copy link

What is ithe status of this PR?

@4wdonny
Copy link

4wdonny commented Aug 28, 2024

I'm blocked from using the exporter right now...waiting for this PR to hopefully fix that 🤞

@sysedwinistrator
Copy link
Contributor Author

What is ithe status of this PR?

It needs further review.

I don't think there any blockers. It just took me a while after the initial review to get back to this PR and address the open issues.

@SuperQ SuperQ requested a review from kgeckhart August 29, 2024 09:05
Copy link
Contributor

@kgeckhart kgeckhart left a comment

Choose a reason for hiding this comment

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

Thanks for this change!

I don't think this fully fixes #103 though but dramactially cut down the instances of it caused by overlapping prefixes. This case specifically is interesting #103 (comment) where they are using two non-overlapping prefixes but something about the data being returned from GCP APIs is causing duplicates.

@SuperQ SuperQ merged commit 098cde0 into prometheus-community:master Aug 30, 2024
4 checks passed
@initharrington
Copy link

@kgeckhart will there be a release with this change soon?

@kgeckhart kgeckhart mentioned this pull request Oct 7, 2024
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.

How to workaround duplicate metrics from Stackdriver
7 participants