How to use Logfire with OpenObserve backend

In our previous post Logfire example which connects to SigNoz instead of Logfire we showed how to use Logfire with SigNoz as backend. In this post, we will demonstrate how to configure Logfire to send logs to an OpenObserve backend.

See OpenObserve 5-minute Docker + Traefik setup for our recommended OpenObserve setup.

First, configure OTLP for OpenObserve by visiting https://openobserve.mydomain.com/web/ingestion/recommended/traces?org_identifier=default.

You need both the HTTP Endpoint: and the Authorization: header.

Here’s the complete example code to send logs from Logfire to OpenObserve:

test-openobserve.py
#!/usr/bin/env python3
import os
import logfire

# Configure environment variables
os.environ['OTEL_EXPORTER_OTLP_ENDPOINT'] = 'https://openobserve.mydomain.com/api/default' # Insert the OpenObserve "HTTP Endpoint" here
os.environ['OTEL_EXPORTER_OTLP_HEADERS'] = 'Authorization=Basic W280PMn2Y6rnxZURbS1XEQONEZ0GtKpErE6A+sae' # Insert the "Authorization" header here

# Configure Logfire
logfire.configure(
    service_name='order-processing-service',
    send_to_logfire=False,
)

# Example function with nested spans
def process_order(order_id: str, user_id: str):
    with logfire.span('validate_order'):
        # Simulate validation
        print(f"Validating order {order_id} for user {user_id}")

    with logfire.span('process_payment'):
        # Simulate payment processing
        print(f"Processing payment for order {order_id}")

        with logfire.span('call_payment_gateway'):
            # Nested span example
            print("Calling payment gateway API")

    with logfire.span('fulfill_order'):
        # Simulate order fulfillment
        print(f"Fulfilling order {order_id}")

# Main execution
if __name__ == '__main__':
    with logfire.span('main'):
        process_order('12345', 'user-67890')

This is how it looks in OpenObserve (Traces in the left menu, then Run Query on the top right, then click the trace to see details):

OpenObserve Trace Example


Check out similar posts by category: Monitoring, Python