Skip to content

Treat empty environment variables as unset - #160

Open
RaphaelFakhri wants to merge 1 commit into
pydantic:mainfrom
RaphaelFakhri:fix/empty-env-vars-unset
Open

RaphaelFakhri wants to merge 1 commit into
pydantic:mainfrom
RaphaelFakhri:fix/empty-env-vars-unset

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

get_optional_env returns Some("") for an environment variable set to an empty string, so configure() treats it as a real value. The OpenTelemetry specification requires an empty value to be interpreted as unset, and the Logfire Python SDK already does this.

Empty variables are common in container setups (for example LOGFIRE_TOKEN=${LOGFIRE_TOKEN} in a Compose file when the host variable is undefined). Before this change:

  • LOGFIRE_TOKEN="" counts as a token: SendToLogfire::IfTokenPresent exports with an empty bearer token, and the default mode skips the TokenRequired error.
  • LOGFIRE_SEND_TO_LOGFIRE="" makes configure() fail with an invalid configuration value error.
  • LOGFIRE_BASE_URL="" replaces the region URL derived from the token.
  • LOGFIRE_SERVICE_NAME="" sets service.name to an empty string instead of falling back to OTEL_SERVICE_NAME.

Changes

  • get_optional_env returns None for empty values, both from the process environment and from the explicit env map used in tests. All LOGFIRE_*/OTEL_* lookups in configure() and the OTLP protocol lookup go through this helper.
  • Adds empty-value cases to test_send_to_logfire and to test_service_resource_attributes_from_otel_env.

Testing

cargo test -p logfire --lib test_send_to_logfire and cargo test -p logfire --test test_resource_attributes fail without the change and pass with it.

Fixes #159

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

None yet

Development

Successfully merging this pull request may close these issues.

Empty environment variables are treated as set

1 participant