Logfire example which connects to SigNoz instead of Logfire

The following script is a test script for using the Logfire library to send traces to a SigNoz instance instead of Logfire’s own backend. It demonstrates how to set up environment variables for the OpenTelemetry Protocol (OTLP) exporter to point to a SigNoz endpoint, and how to create nested spans for tracing different parts of a hypothetical order processing function.

For the SigNoz setup, see Signoz standalone docker-compose with Traefik config.

example.py
#!/usr/bin/env python3
import os
import logfire

# Configure environment variables
os.environ['OTEL_EXPORTER_OTLP_ENDPOINT'] = 'https://http.signoz.mydomain.com/'

# Configure Logfire
logfire.configure(
    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 SigNoz:

Signoz Trace Example


Check out similar posts by category: Python, Monitoring