A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/wsgi/wsgi.html below:

Website Navigation


OpenTelemetry WSGI Instrumentation — OpenTelemetry Python Contrib documentation

OpenTelemetry WSGI Instrumentation

This library provides a WSGI middleware that can be used on any WSGI framework (such as Django / Flask / Web.py) to track requests timing through OpenTelemetry.

Usage (Flask)
from flask import Flask
from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware

app = Flask(__name__)
app.wsgi_app = OpenTelemetryMiddleware(app.wsgi_app)

@app.route("/")
def hello():
    return "Hello!"

if __name__ == "__main__":
    app.run(debug=True)
Usage (Django)

Modify the application’s wsgi.py file as shown below.

import os
from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'application.settings')

application = get_wsgi_application()
application = OpenTelemetryMiddleware(application)
Usage (Web.py)
import web
from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware
from cheroot import wsgi

urls = ('/', 'index')


class index:

    def GET(self):
        return "Hello, world!"


if __name__ == "__main__":
    app = web.application(urls, globals())
    func = app.wsgifunc()

    func = OpenTelemetryMiddleware(func)

    server = wsgi.WSGIServer(
        ("localhost", 5100), func, server_name="localhost"
    )
    server.start()
Configuration Request/Response hooks

This instrumentation supports request and response hooks. These are functions that get called right after a span is created for a request and right before the span is finished for the response.

For example,

from opentelemetry.trace import Span
from wsgiref.types import WSGIEnvironment, StartResponse
from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware

def app(environ: WSGIEnvironment, start_response: StartResponse):
    start_response("200 OK", [("Content-Type", "text/plain"), ("Content-Length", "13")])
    return [b"Hello, World!"]

def request_hook(span: Span, environ: WSGIEnvironment):
    if span and span.is_recording():
        span.set_attribute("custom_user_attribute_from_request_hook", "some-value")

def response_hook(span: Span, environ: WSGIEnvironment, status: str, response_headers: list[tuple[str, str]]):
    if span and span.is_recording():
        span.set_attribute("custom_user_attribute_from_response_hook", "some-value")

OpenTelemetryMiddleware(app, request_hook=request_hook, response_hook=response_hook)
Capture HTTP request and response headers

You can configure the agent to capture specified HTTP headers as span attributes, according to the semantic convention.

Sanitizing methods

In order to prevent unbound cardinality for HTTP methods by default nonstandard ones are labeled as NONSTANDARD. To record all of the names set the environment variable OTEL_PYTHON_INSTRUMENTATION_HTTP_CAPTURE_ALL_METHODS to a value that evaluates to true, e.g. 1.

API
class opentelemetry.instrumentation.wsgi.WSGIGetter[source]

Bases: Getter[Dict[str, Any]]

get(carrier, key)[source]
Getter implementation to retrieve a HTTP header value from the

PEP3333-conforming WSGI environ

Parameters:
  • carrier (dict[str, Any]) – WSGI environ object

  • key (str) –

    header name in environ object

    Returns:

    A list with a single string with the header value if it exists, else None.

Return type:

Optional[list[str]]

keys(carrier)[source]

Function that can retrieve all the keys in a carrier object.

Parameters:

carrier (dict[str, Any]) – An object which contains values that are used to construct a Context.

Returns:

list of keys from the carrier.

opentelemetry.instrumentation.wsgi.collect_request_attributes(environ, sem_conv_opt_in_mode=_StabilityMode.DEFAULT)[source]

Collects HTTP request attributes from the PEP3333-conforming WSGI environ and returns a dictionary to be used as span creation attributes.

Returns custom HTTP request headers which are configured by the user from the PEP3333-conforming WSGI environ to be used as span creation attributes as described in the specification https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers

Returns custom HTTP response headers which are configured by the user from the PEP3333-conforming WSGI environ as described in the specification https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers

opentelemetry.instrumentation.wsgi.add_response_attributes(span, start_response_status, response_headers, duration_attrs=None, sem_conv_opt_in_mode=_StabilityMode.DEFAULT)[source]

Adds HTTP response attributes to span using the arguments passed to a PEP3333-conforming start_response callable.

opentelemetry.instrumentation.wsgi.get_default_span_name(environ)[source]

Default span name is the HTTP method and URL path, or just the method. https://github.com/open-telemetry/opentelemetry-specification/pull/3165 https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/http/#name

Parameters:

environ (dict[str, Any]) – The WSGI environ object.

Return type:

str

Returns:

The span name.

class opentelemetry.instrumentation.wsgi.OpenTelemetryMiddleware(wsgi, request_hook=None, response_hook=None, tracer_provider=None, meter_provider=None)[source]

Bases: object

The WSGI application middleware.

This class is a PEP 3333 conforming WSGI middleware that starts and annotates spans for any requests it is invoked with.

Parameters:
class opentelemetry.instrumentation.wsgi.ResponsePropagationSetter[source]

Bases: object

set(carrier, key, value)[source]

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4