Python Logs Auto-Instrumentation

Collecting Python Application Logs Using Auto-Instrumentation

If you are using python auto-instrumentation for instrumenting your python application you can send logs to SigNoz easily with auto-instrumentation.

To enable logs auto-instrumentation just add this environment variable

OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true

Example application

Here is a sample python application.

  1. Create a file named main.py and paste the following code:

    from flask import Flask
    import logging
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello_world():
        logging.warning("hello world log message")
        return 'Hello World'
    
    if __name__ == '__main__':
        app.run()
    
  2. Create a virual environment

    python -m venv venv
    source ./venv/bin/activate
    
  3. Install dependencies

    pip install opentelemetry-distro
    pip install flask requests
    pip install opentelemetry-exporter-otlp
    
  4. Run the opentelemetry-bootstrap command:

    opentelemetry-bootstrap -a install
    
  5. Run the application

    OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true opentelemetry-instrument --traces_exporter none --metrics_exporter none --logs_exporter console python main.py
    

You will be able to see the otel logs on the console once you visit http://localhost:5000

Run the below command to start sending your traces to SigNoz.

OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true \
OTEL_EXPORTER_OTLP_ENDPOINT=ingest.<region>.signoz.cloud:443 \
OTEL_EXPORTER_OTLP_HEADERS=signoz-ingestion-key=<INGESTION_KEY> \
opentelemetry-instrument --traces_exporter otlp --metrics_exporter otlp --logs_exporter otlp python main.py

Was this page helpful?