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

[exporter/prometheus] fix: validate metric types and help/descriptions #36356

Merged

Conversation

kristinapathak
Copy link
Contributor

@kristinapathak kristinapathak commented Nov 13, 2024

Description

Fixes bug where exporting fails due to different help messages for the same metric. With this solution, the exporter will always export metrics of the same name with the first description it receives. This also rejects metrics whose types have changed. These changes follow the spec:

Exporters MUST drop entire metrics to prevent conflicting TYPE comments, but SHOULD NOT drop metric points as a result of conflicting UNIT or HELP comments. Instead, all but one of the conflicting UNIT and HELP comments (but not metric points) SHOULD be dropped. If dropping a comment or metric points, the exporter SHOULD warn the user through error logging.

Based on open-telemetry/opentelemetry-go#3469

Link to tracking issue

Fixes #28617

Testing

Unit test cases added.

@kristinapathak kristinapathak force-pushed the prometheus-metric-types-help branch from 94be2f5 to c7af22a Compare November 13, 2024 19:11
@kristinapathak kristinapathak changed the title prometheus exporter: validate metric types and help/descriptions [exporter/prometheus] fix: validate metric types and help/descriptions Nov 13, 2024
@kristinapathak kristinapathak force-pushed the prometheus-metric-types-help branch from a3e03fa to 544dc23 Compare November 13, 2024 21:36
@kristinapathak kristinapathak force-pushed the prometheus-metric-types-help branch from 544dc23 to a540704 Compare November 13, 2024 22:12
@dashpole dashpole added the ready to merge Code review completed; ready to merge by maintainers label Nov 14, 2024
c.metricFamilies.Range(func(key, value any) bool {
v := value.(metricFamily)
if expirationTime.After(v.lastSeen) {
c.logger.Debug(fmt.Sprintf("metric expired: %s", key))
Copy link
Contributor

Choose a reason for hiding this comment

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

any way to avoid calling fmt.Sprintf here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed!

Copy link
Contributor

Choose a reason for hiding this comment

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

Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
@@ -403,4 +428,50 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
ch <- m
c.logger.Debug(fmt.Sprintf("metric served: %s", m.Desc().String()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't need to be in this PR but as a followup would be good to clean up this sprintf as well

@codeboten codeboten merged commit ade3e4b into open-telemetry:main Nov 18, 2024
157 of 158 checks passed
@github-actions github-actions bot added this to the next release milestone Nov 18, 2024
@kristinapathak kristinapathak deleted the prometheus-metric-types-help branch November 18, 2024 18:13
RutvikS-crest pushed a commit to RutvikS-crest/opentelemetry-collector-contrib that referenced this pull request Dec 9, 2024
open-telemetry#36356)


#### Description
Fixes bug where exporting fails due to different help messages for the
same metric. With this solution, the exporter will always export metrics
of the same name with the first description it receives. This also
rejects metrics whose types have changed. These changes follow the
[spec](https://opentelemetry.io/docs/specs/otel/compatibility/prometheus_and_openmetrics/#metric-metadata-1):
>Exporters MUST drop entire metrics to prevent conflicting TYPE
comments, but SHOULD NOT drop metric points as a result of conflicting
UNIT or HELP comments. Instead, all but one of the conflicting UNIT and
HELP comments (but not metric points) SHOULD be dropped. If dropping a
comment or metric points, the exporter SHOULD warn the user through
error logging.

Based on open-telemetry/opentelemetry-go#3469

#### Link to tracking issue
Fixes
open-telemetry#28617

<!--Describe what testing was performed and which tests were added.-->
#### Testing
Unit test cases added.

---------

Co-authored-by: David Ashpole <dashpole@google.com>
Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
sbylica-splunk pushed a commit to sbylica-splunk/opentelemetry-collector-contrib that referenced this pull request Dec 17, 2024
open-telemetry#36356)


#### Description
Fixes bug where exporting fails due to different help messages for the
same metric. With this solution, the exporter will always export metrics
of the same name with the first description it receives. This also
rejects metrics whose types have changed. These changes follow the
[spec](https://opentelemetry.io/docs/specs/otel/compatibility/prometheus_and_openmetrics/#metric-metadata-1):
>Exporters MUST drop entire metrics to prevent conflicting TYPE
comments, but SHOULD NOT drop metric points as a result of conflicting
UNIT or HELP comments. Instead, all but one of the conflicting UNIT and
HELP comments (but not metric points) SHOULD be dropped. If dropping a
comment or metric points, the exporter SHOULD warn the user through
error logging.

Based on open-telemetry/opentelemetry-go#3469

#### Link to tracking issue
Fixes
open-telemetry#28617

<!--Describe what testing was performed and which tests were added.-->
#### Testing
Unit test cases added.

---------

Co-authored-by: David Ashpole <dashpole@google.com>
Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exporter/prometheus ready to merge Code review completed; ready to merge by maintainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prometheus exporter should allow conflicting help and unit metadata
6 participants