prometheus-community / stackdriver_exporter

Google Stackdriver Prometheus exporter
Apache License 2.0
263 stars 99 forks source link

Sanitize metric type prefixes #319

Closed sysedwinistrator closed 2 months ago

sysedwinistrator commented 8 months ago

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:

Fixes https://github.com/prometheus-community/stackdriver_exporter/issues/103

jotka commented 5 months ago

hi there. is it going to be merged soon?

SuperQ commented 5 months ago

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

jotka commented 5 months ago

@sysedwinistrator will you release it from draft status?

sysedwinistrator commented 5 months ago

@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 :)

jotka commented 4 months ago

@SuperQ it's ready to review now :)

sysedwinistrator commented 3 months ago

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 commented 2 months ago

What is ithe status of this PR?

4wdonny commented 2 months ago

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

sysedwinistrator commented 2 months ago

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.

initharrington commented 1 month ago

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