OpenTelemetry guide for Django and PostgreSQL/MySQL

    Distributed tracingDjango - 图2open in new window allows you to see how a request progresses through different services and systems, timings of each operation, any logs and errors as they occur.

    In a distributed environment, tracing also helps you understand relationships and interactions between microservices. Distributed tracing gives an insight into how a particular microservice is performing and how that service affects other microservices.

    Using tracing, you can break down requests into . Span is an operation (unit of work) your app performs handling a request, for example, a database query or a network call.

    Trace is a tree of spans that shows the path that a request makes through an app. Root span is the first span in a trace.

    To learn more about tracing, see Distributed tracing using OpenTelemetryDjango - 图6open in new window.

    What is OpenTelemetry?

    Otel specifies how to collect and export telemetry data in a vendor agnostic way. With OpenTelemetry, you can instrumentopen in new window your application once and then add or change vendors without changing the instrumentation, for example, many already support OpenTelemetry.

    OpenTelemetry is available for most programming languages and provides interoperability across different languages and environments.

    Creating spans

    You can create a span using like this:

    UptraceDjango - 图13open in new window is an open source that helps developers pinpoint failures and find performance bottlenecks. Uptrace can process billions of spans on a single server and allows to monitor your software at 10x lower cost.

    You can install UptraceDjango - 图15open in new window by downloading a DEB/RPM package or a pre-compiled binary.

    Example application

    In this tutorial, you will be instrumenting a toy appopen in new window that uses Django and PostgreSQL database client. You can retrieve the source code with the following command:

    1. cd example/django

    The app comes with some dependencies that you can install with:

      Then you can run migrations to initialize the database:

      Configuring OpenTelemetry

      1. pip install uptrace

      Then you need to initialize OpenTelemetry whenever the app is started, for example, in manage.py:

      1. # manage.py
      2. import uptrace
      3. def main():
      4. # Copy DSN here or use UPTRACE_DSN env var.
      5. # dsn="",
      6. service_name="myservice",
      7. service_version="v1.0.0",
      8. )
      9. # other code

      See documentationDjango - 图18open in new window for details.

      To instrument Django, you need a correspoding :

      Django instrumentation uses DJANGO_SETTINGS_MODULE env variable to find settings file. Django defines that variable in manage.py file so you should instrument Django app from that file:

      1. from opentelemetry.instrumentation.django import DjangoInstrumentor
      2. def main():
      3. # DjangoInstrumentor uses DJANGO_SETTINGS_MODULE to instrument the project.
      4. # Make sure the var is available before you call the DjangoInstrumentor.
      5. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
      6. DjangoInstrumentor().instrument()

      Running the example

      You can start Uptrace with a single command using :

      And then start the appopen in new window passing Uptrace DSN as an env variable:

      The app should be serving requests on http://localhost:8000 and should render a link to Uptrace UI. After opening the link, you should see this:

      Django OpenTelemetry

      Instrumenting PostgreSQL/MySQL

      Next, you can learn about OpenTelemetry Python APIopen in new window to create your own instrumentations or browse existing provided by the community.