Skip to main content
Version: Self Hosted Lite

Python on Instance

Django

Install Otel Data Forwarder

Follow the below steps to install Otel data forwarder.

  1. Run the following command to download the Otel data forwader.

    wget https://raw.githubusercontent.com/snappyflow/apm-agent/master/otel-forwarder-install.sh 

    Refer the link for all available configurations: OpenTelemetry OTLP Exporters — OpenTelemetry Python documentation (opentelemetry-python.readthedocs.io)

  2. Run the following command.

    chmod +x otel-forwarder-install.sh  
    sudo ./otel-forwarder-install.sh  
  3. Add profileKey, projectName and applicationName in the env.conf file located at /opt/sfagent/otel-trace-data-forwarder path.

    profile_key=<profile-key>
    _tag_projectName=<project-name>
    _tag_appName=<application-name>
  4. Run the below command to check the status.

    sudo service otel-data-forwarder status
    to start:
    sudo service otel-data-forwarder start

Add OpenTelemetry Instrumentation

Add Dependencies

Install the following packages.

pip install opentelemetry-sdk
pip install opentelemetry-instrumentation-django
pip install requests
pip install opentelemetry-distro opentelemetry-exporter-otlp
opentelemetry-bootstrap -a install

OpenTelemetry OTLP Exporters

  1. Add the following code in manage.py file to configure OTLP Span Exporter and to send generated traces to otel-data-forwarder.

    from opentelemetry import trace
    from opentelemetry.sdk.resources import Resource
    from opentelemetry.sdk.trace import TracerProvider
    from opentelemetry.sdk.trace.export import (BatchSpanProcessor,ConsoleSpanExporter)
    from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
    # Set the environment variables for the OTLP exporter
    os.environ['OTEL_EXPORTER_OTLP_TRACES_TIMEOUT'] = '5000' # Set the timeout in milliseconds
    os.environ['OTEL_EXPORTER_OTLP_TRACES_PROTOCOL'] = 'https' # Set the protocol (http or https)
    #os.environ['OTEL_EXPORTER_OTLP_TRACES_HEADERS'] = '{"custom-header": "header-value"}' # To Set custom headers
    os.environ['OTEL_EXPORTER_OTLP_TRACES_ENDPOINT'] = 'https://otelforwarderuser:Forwarder-Snappyflow-Agent%407%24@127.0.0.1:9595/otel-service/export/trace' # Set the endpoint URL
    # os.environ['OTEL_EXPORTER_OTLP_TRACES_COMPRESSION'] = 'gzip' # Set the compression method (e.g., gzip)
    os.environ['OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE'] = '/opt/sfagent/otel-trace-data-forwarder/certs/ca.pem' # Set the certificate path

    resource = Resource(attributes={
    #service name appears in dashboard
    "service.name": "python-django"
    })

    otlp_exporter = OTLPSpanExporter()

    trace.set_tracer_provider(TracerProvider(resource=resource))

    trace.get_tracer_provider().add_span_processor(
    BatchSpanProcessor(otlp_exporter)
    )
  2. Run the following code to instrument the application.

    from opentelemetry.instrumentation.django import DjangoInstrumentor
    # call inside main()
    DjangoInstrumentor().instrument()
  3. Add the below code to set the environment variables.

    export DJANGO_SETTINGS_MODULE=proj1.settings
    # proj1 is the application name
  4. Run the application with the following command.

    python3 manage.py runserver --noreload
  5. Run the following command to get logs.

    tail -f /var/log/otel-data-forwarder.log

Verification

Follow the below steps to verify whether SnappyFlow has started to collect the trace data.

  1. Go to the Application tab in SnappyFlow and navigate to your Project > Application > Dashboard.
  2. Navigate to the Tracing section and click the View Transactions button.
  3. You can view the traces in the Aggregate and the Real Time tabs.