Repository navigation
fix(prometheus): tolerate invalid OTEL_EXPORTER_PROMETHEUS_PORT env var - #5705
Open
RichardoMrMu wants to merge 3 commits into
Open
RichardoMrMu wants to merge 3 commits into
RichardoMrMu wants to merge 3 commits into
Conversation
_AutoPrometheusMetricReader parsed OTEL_EXPORTER_PROMETHEUS_PORT with a bare int() in __init__, so a non-integer value (e.g. "9464abc") or an empty string raised ValueError while building the auto-configured reader and took opentelemetry-instrument/SDK startup down. This is the same class of startup crash already fixed for the OTLP exporters (HTTP _resolve_timeout in open-telemetry#5389, gRPC _timeout_from_env in open-telemetry#5448) and the Zipkin exporters (open-telemetry#5704). Add a private _port_from_env() that skips unset/empty values silently, logs a warning for non-integer values, and falls back to the 9464 default. Explicit configuration is unaffected (the reader's auto server only reads the env var). Assisted-by: Doubao
Pull request dashboard statusWaiting on the author · refreshed 2026-10-07 17:07 UTC Investigate required status check failures. Status above doesn't look right?
|
herin049
approved these changes
Sep 30, 2026
lzchen
approved these changes
Oct 7, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
_AutoPrometheusMetricReader(theopentelemetry_metrics_exporter=prometheusentry point used byopentelemetry-instrument) parsesOTEL_EXPORTER_PROMETHEUS_PORTwith a bareint()in__init__:A non-numeric value such as
OTEL_EXPORTER_PROMETHEUS_PORT=9464abc(or an empty string) raisesValueError: invalid literal for int()while the auto-configured reader is being constructed, which takes auto-instrumentation / SDK startup down before any metrics are exported.This is the same class of startup crash already addressed for the other exporters, which parse this kind of environment variable with warn-and-fall-back helpers:
_resolve_timeout()(opentelemetry-exporter-otlp-proto-http: refactor to use shared opentelemetry-exporter-otlp-common and opentelemetry-exporter-http-transport packages #5389)_timeout_from_env()(opentelemetry-exporter-otlp-proto-grpc: tolerate invalid timeout env vars #5448)_timeout_from_env()(fix(zipkin): tolerate invalid OTEL_EXPORTER_ZIPKIN_TIMEOUT env var #5704)The Prometheus auto reader was the remaining unguarded numeric port parse on an exporter entry point.
Change
Add a private
_port_from_env()next to the reader that:9464) silently when the variable is unset or empty/whitespace;Only
_AutoPrometheusMetricReader(the env-driven auto server) is changed; the publicPrometheusMetricReadertakes no port and is unaffected, so there is no explicit-argument precedence to preserve here.Type of change
How Has This Been Tested?
Pure-Python;
start_http_serveris mocked so no socket is opened. Added totests/test_entrypoints.py:test_starts_http_server_invalid_port_envvar_falls_back: setsOTEL_EXPORTER_PROMETHEUS_PORT=9464abc, asserts the reader builds andstart_http_serveris called withport=9464, and that a WARNING mentioning "Invalid value" is emitted.test_starts_http_server_empty_port_envvar_falls_back: sets the variable to"", assertsport=9464.Red (before the fix),
pytest -k port_envvar:Green (after the fix):
The pre-existing controls continue to pass:
test_starts_http_server_defaults(unset -> 9464),test_starts_http_server_port_envvar(valid"9999"-> 9999), andtest_starts_http_server_host_envvar.Verification boundary: configuration/constructor logic only with the HTTP-server start mocked; no real Prometheus endpoint is bound (consistent with the existing tests in this module).