Skip to content

fix(prometheus): tolerate invalid OTEL_EXPORTER_PROMETHEUS_PORT env var - #5705

Open
RichardoMrMu wants to merge 3 commits into
open-telemetry:mainfrom
RichardoMrMu:fix/prometheus-port-env-fallback
Open

RichardoMrMu wants to merge 3 commits into
open-telemetry:mainfrom
RichardoMrMu:fix/prometheus-port-env-fallback

Conversation

@RichardoMrMu

Copy link
Copy Markdown
Contributor

Description

_AutoPrometheusMetricReader (the opentelemetry_metrics_exporter=prometheus entry point used by opentelemetry-instrument) parses OTEL_EXPORTER_PROMETHEUS_PORT with a bare int() in __init__:

start_http_server(
    port=int(environ.get(OTEL_EXPORTER_PROMETHEUS_PORT, "9464")),
    addr=environ.get(OTEL_EXPORTER_PROMETHEUS_HOST, "localhost"),
)

A non-numeric value such as OTEL_EXPORTER_PROMETHEUS_PORT=9464abc (or an empty string) raises ValueError: 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:

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:

  • returns the default (9464) silently when the variable is unset or empty/whitespace;
  • logs a warning and returns the default when the value is not an integer;
  • otherwise returns the parsed port.

Only _AutoPrometheusMetricReader (the env-driven auto server) is changed; the public PrometheusMetricReader takes no port and is unaffected, so there is no explicit-argument precedence to preserve here.

Type of change

  • Bug fix (non-breaking change)

How Has This Been Tested?

Pure-Python; start_http_server is mocked so no socket is opened. Added to tests/test_entrypoints.py:

  • test_starts_http_server_invalid_port_envvar_falls_back: sets OTEL_EXPORTER_PROMETHEUS_PORT=9464abc, asserts the reader builds and start_http_server is called with port=9464, and that a WARNING mentioning "Invalid value" is emitted.
  • test_starts_http_server_empty_port_envvar_falls_back: sets the variable to "", asserts port=9464.

Red (before the fix), pytest -k port_envvar:

>   port=int(environ.get(OTEL_EXPORTER_PROMETHEUS_PORT, "9464")),
E   ValueError: invalid literal for int() with base 10: '9464abc'
2 failed, 1 passed, 3 deselected

Green (after the fix):

tests/test_entrypoints.py ........ 6 passed
full exporter suite ...... 39 passed

The pre-existing controls continue to pass: test_starts_http_server_defaults (unset -> 9464), test_starts_http_server_port_envvar (valid "9999" -> 9999), and test_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).

_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
@RichardoMrMu
RichardoMrMu requested a review from a team as a code owner September 28, 2026 17:46
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-10-07 17:07 UTC

Investigate required status check failures.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Approved PRs

Development

Successfully merging this pull request may close these issues.

3 participants