Skip to content

Isolate telemetry to garth's session using hooks instead of global instrumentation #181

@matin

Description

@matin

Problem

The current telemetry implementation has two issues:

  1. Global logfire configuration: logfire.configure() sets up the global OpenTelemetry provider, potentially overwriting the user's existing configuration
  2. Global request instrumentation: logfire.instrument_requests() monkey-patches all requests.Session usage application-wide, not just garth's

This means garth's telemetry conflicts with applications that have their own logfire/OpenTelemetry setup.

Proposed Solution

Use requests.Session hooks instead of global instrumentation:

  1. Attach a response hook directly to garth's session instance
  2. Send telemetry data to logfire cloud via HTTP API (not via OpenTelemetry SDK)
  3. Keep the same environment variables as defaults:
    • GARTH_TELEMETRY=true/false
    • LOGFIRE_TOKEN
    • GARTH_TELEMETRY_SERVICE_NAME

Benefits

  • Isolation: Only garth's requests are logged, no global side effects
  • No conflicts: Works alongside existing logfire/OpenTelemetry setups
  • Same defaults: Logfire cloud remains the default destination with same env vars
  • Customizable: Users can provide their own callback for custom destinations

API Design

# Default: sends to logfire cloud (same as today)
garth.configure(telemetry_enabled=True)

# Custom callback for alternative destinations
garth.configure(
    telemetry_enabled=True,
    telemetry_callback=lambda data: my_logger.info("garth", **data)
)

Implementation Notes

  • Remove logfire.configure() and logfire.instrument_requests() calls
  • Add session.hooks['response'].append(...) in Client.__init__ or configure()
  • Default callback sends sanitized data to logfire cloud via their HTTP ingest API
  • Keep all existing sanitization logic

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions