Fix Prometheus $digit escape for OTel expandconverter#2187
Conversation
|
|
||
| // escapeDollarDigit escapes bare $ followed by a digit so the expandconverter | ||
| // does not interpret regex backreferences (e.g., $1 in relabel_configs) as env vars. | ||
| func escapeDollarDigit(s string) string { |
There was a problem hiding this comment.
seems like this is a duplicate of the container insights function - https://github.com/aws/amazon-cloudwatch-agent/blob/main/translator/translate/otel/pipeline/opentelemetry/containerinsights/common.go#L68
this should be extracted in a common package and imported in both container insights and prometheus translations
There was a problem hiding this comment.
During testing I noticed prometheus needed it own funtion. Container insights uses Go templates with rawMapConfig that bypass the confmap resolver, so its single $$ escape is sufficient. Prometheus reads an external file that goes through the full resolver and the expandconverter pipeline, requiring $$$$. The same function won't work for both.
Also, I tested manually, see description.
Description of the issue
On EKS environments, Prometheus relabel_configs that use regex capture group references (e.g., $1, $2 in replacement fields) are silently corrupted. The OTel
expandconvertertreats bare $digit sequences as environment variable references and replaces them with empty strings, breaking label relabeling rules.Description of changes
Added escapeDollarDigit to the OTel Prometheus receiver translator. Before the Prometheus config file is unmarshaled, bare
$digitsequences are doubled to$$digit. Theexpandconverterinterprets $$ as a literal $, so the values are correctly preserved as $1, $2, etc. when the collector starts.The same pattern already existed in the Container Insights translator, this change brings Prometheus to parity.
Tests
TestEscapeDollarDigit: 10 unit test cases covering bare $1–$9, $0, multi-digit $10, $ followed by non-digit, mixed text, empty string
translator correctly escapes them to $$$$1 and $$$$1:$$$$2 to survive the confmap resolver + expandconverter pipeline
prometheus.yaml:
License
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Requirements
Before commiting your code, please do the following steps.
make fmtandmake fmt-shmake lintIntegration Tests
To run integration tests against this PR, add the
ready for testinglabel.