Monitor Pyramid performance with OpenTelemetry

OpenTelemetry Pyramid is an implementation of the OpenTelemetry specification for Pyramid framework. It provides a middleware that can be used to automatically generate traces for incoming requests and outgoing responses.

Pyramid is a free, open-source web framework for building web applications in Python. It was previously known as Pylons and was later rebranded as Pyramid.

What is OpenTelemetry?

OpenTelemetryopen in new window is an open-source observability framework that aims to standardize and simplify the collection, processing, and export of telemetry data from applications and systems.

OpenTelemetry supports multiple programming languages and platforms, making it suitable for a wide range of applications and environments.

OpenTelemetry enables developers to instrument their code and collect telemetry data, which can then be exported to various OpenTelemetry backendsopen in new window or observability platforms for analysis and visualization.

Pyramid instrumentation

To use OpenTelemetry with Pyramid, you need to install the opentelemetry.instrumentation.pyramid package.

pip install opentelemetry-instrumentation-pyramid

Usage

Here's an example of how to configure Pyramid with OpenTelemetry using the Uptrace backend:

from opentelemetry.instrumentation.pyramid import PyramidInstrumentor
import uptrace

if __name__ == "__main__":
    uptrace.configure_opentelemetry(
        # Set dsn or UPTRACE_DSN env var.
        # dsn="",
        service_name="myservice",
        service_version="1.0.0",
    )
    PyramidInstrumentor().instrument()

    with Configurator() as config:
        config.add_route("home", "/")
        config.scan()

        app = config.make_wsgi_app()

    print("listening on http://localhost:6543")
    server = make_server("0.0.0.0", 6543, app)
    server.serve_forever()

See the exampleopen in new window on GitHub for more details.

What is Uptrace?

Uptrace is a Grafana alternativeopen in new window that supports distributed tracing, metrics, and logs. You can use it to monitor applications and troubleshoot issues.

Uptrace Overview

Uptrace comes with an intuitive query builder, rich dashboards, alerting rules with notifications, and integrations for most languages and frameworks.

Uptrace can process billions of spans and metrics on a single server and allows you to monitor your applications at 10x lower cost.

In just a few minutes, you can try Uptrace by visiting the cloud demoopen in new window (no login required) or running it locally with Dockeropen in new window. The source code is available on GitHubopen in new window.

Excluding URLs

You can disable tracing on certain URLs by providing an environment variable with the list of URLs, for example, the following config will exclude URLs like https://site/client/123/info and https://site/xyz/healthcheck:

export OTEL_PYTHON_PYRAMID_EXCLUDED_URLS="client/.*/info,healthcheck"

See documentationopen in new window for details.

Headers

To capture HTTP request headers, set the environment variable to a comma delimited list of HTTP header names:

# list of headers
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST="content-type,custom_request_header"

# regexp
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST="Accept.*,X-.*"

# all headers
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST=".*"

To capture HTTP response headers:

# list of headers
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE="content-type,custom_response_header"

# regexp
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE="Content.*,X-.*"

# all headers
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE=".*"

You can also sanitize captured headers to prevent storing sensitive data:

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS=".*session.*,set-cookie"

See documentationopen in new window for details.

What's next?

Next, instrument more operations to get a more detailed picture. Try to prioritize network calls, disk operations, database queries, error and logs.

You can also create your own instrumentations using OpenTelemetry Python Tracing APIopen in new window.

Last Updated: 7/25/2024, 12:36:08 PM