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

Fix: Handle incoming labels with invalid UTF-8 #1131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vancwo
Copy link

@vancwo vancwo commented Feb 25, 2025

It's possible that incoming labels contain invalid UTF-8 characters. This results in a panic. This fix sanitizes the label's string to ensure only valid UTF-8 characters are included, by replacing invalid characters with � (REPLACEMENT CHARACTER)

It's possible that incoming labels will contain invalid UTF-8 characters. This results in a panic. This fix sanitizes the label's string to ensure only valid UTF-8 characters are included, by replacing invalid characters with � (REPLACEMENT CHARACTER)

Signed-off-by: Cooper Worobetz <[email protected]>
@vancwo
Copy link
Author

vancwo commented Feb 25, 2025

I encounted this bug while debugging a connection to Azure Postgres Flexible Server. It seems Azure's monitoring system sets the backend type to some strange characters:

image

This resulted in a panic during runtime

panic: label value "Єc!\xbap" is not valid UTF-8

goroutine 30 [running]:
github.com/prometheus/client_golang/prometheus.MustNewConstMetric(...)
        /go/pkg/mod/github.com/prometheus/[email protected]/prometheus/value.go:129
main.queryNamespaceMapping(0xc0000b8790, {0xafab8a, 0x10}, {{0xc000162900, 0x7, 0x8}, 0xc0001778c0, 0x1, 0x0})
        /app/cmd/postgres_exporter/namespace.go:161 +0x1b5b
main.queryNamespaceMappings(0xc000154700, 0xc0000b8790)
        /app/cmd/postgres_exporter/namespace.go:227 +0x5f2
main.(*Server).Scrape(0xc0000b8790, 0xc000154700, 0x90?)
        /app/cmd/postgres_exporter/server.go:125 +0x133
main.(*Exporter).scrapeDSN(0xc0001380c0, 0xc000154700, {0xc000166000, 0x7a})
        /app/cmd/postgres_exporter/datasource.go:114 +0x132
main.(*Exporter).scrape(0xc0001380c0, 0xc000154700)
        /app/cmd/postgres_exporter/postgres_exporter.go:681 +0x16b
main.(*Exporter).Collect(0xc0001380c0, 0xc000154700)
        /app/cmd/postgres_exporter/postgres_exporter.go:570 +0x25
github.com/prometheus/client_golang/prometheus.(*Registry).Gather.func1()
        /go/pkg/mod/github.com/prometheus/[email protected]/prometheus/registry.go:458 +0xe5
created by github.com/prometheus/client_golang/prometheus.(*Registry).Gather in goroutine 28
        /go/pkg/mod/github.com/prometheus/[email protected]/prometheus/registry.go:467 +0x568

I was able to make this fix and verify it works myself. If this pull request is not appropriate in its current state, I would appreciate it if a maintainer could take the time to fix this bug. Thanks!

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.

1 participant