[pkg/translator/prometheus] fix panic for duplicated feature gate registration in otel-collector and prometheus packages #32334
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
This mitigates the bug that causes the otelcollector to crash when a project depends on both opentelemetry-collector-contrib and prometheus packages. This is a fix until the code is maintained in a single package and MustRegister() could be used again.
The variable here is initialized by a function that attempts registration. If that errors, search the global registry for a gate that already exist with that ID. If none is still found, then panic.
The same changes will need to be copied over to the duplicate files in the prometheus repository so that both are not calling MustRegister().
Link to tracking Issue:
prometheus/prometheus#13842
Testing:
Added tests that explicitly re-register the flag to ensure there's no panic and the existing gate returned is expected.