Skip to main content

logfire

Helpers for configuring logfire to export telemetry to the Datalayer OTEL service.

Usage (call configure() before any import logfire in the caller's module)::

from datalayer_core.otel.logfire import configure, flush

logfire = configure( service_name="my-service", instrument_pydantic_ai=True, )

with logfire.span("my-span"): ...

flush() # call before process exit to ensure all spans are exported

The OTLP endpoint is resolved with the same priority as generator.py:

  1. DATALAYER_OTLP_URL — explicit full base URL
  2. DATALAYER_OTEL_RUN_URL — run URL, appends /api/otel/v1/otlp
  3. DATALAYER_RUN_URL — fallback run URL, appends /api/otel/v1/otlp
  4. https://prod1.datalayer.run — production default

Authentication reads DATALAYER_API_KEY as a Bearer token. The JWT payload is decoded to extract the caller's user_uid which is injected as an OTEL resource attribute (datalayer.user_uid) so the backend can associate spans with the authenticated account.

.. note:: This module intentionally does not import logfire at the top level. configure() sets the required OTEL_EXPORTER_OTLP_* environment variables and then imports + initialises logfire so that its internal SDK picks them up. Any import logfire in the caller's module must therefore come after calling configure(), or the caller should use the return value of configure().

def otlp_endpoint() -> 'str'

Return the resolved OTLP base URL (no trailing /v1/traces suffix).

def decode_user_uid(token: 'str') -> 'str | None'

Extract the Datalayer user_uid from a JWT token string.

Returns None if the token is malformed or the claim is missing.

def setup_env(token: 'str | None' = None) -> 'str'

Set OTEL_EXPORTER_OTLP_* and OTEL_RESOURCE_ATTRIBUTES env vars.

This must be called before import logfire so logfire's internal SDK reads the correct values.

Parameters

  • token : str | None

    Datalayer API key (Bearer token). Defaults to DATALAYER_API_KEY.

Returns

  • str

    The resolved OTLP base endpoint that was written to the environment.

def configure(service_name: 'str' = 'datalayer-service', *, instrument_pydantic_ai: 'bool' = False, token: 'str | None' = None) -> 'object'

Configure logfire to ship telemetry to the Datalayer OTEL service.

Sets up the required OTEL_EXPORTER_OTLP_* env vars, then imports and initialises logfire with send_to_logfire=False so that the standard OTLP exporter is used exclusively.

Parameters

  • service_name : str

    OTEL service.name resource attribute.

  • instrument_pydantic_ai : bool

    When True, calls logfire.instrument_pydantic_ai() automatically.

  • token : str | None

    Datalayer API key. Falls back to DATALAYER_API_KEY env var.

Returns

  • module

    The configured logfire module, ready to use.

def flush() -> 'None'

Flush and shut down the active tracer provider.

Call this at the end of short-lived scripts to ensure all buffered spans are exported before the process exits. Safe to call even if configure() was never invoked (it will be a no-op in that case).