Skip to main content

ASGI middleware to emit timing metrics with something like statsd

Project description

timing-asgi

CI PyPI Downloads PyPI Version License

This is a timing middleware for ASGI, useful for automatic instrumentation of ASGI endpoints.

This was developed at GRID for use with our backend services which are built using python and the ASGI framework Starlette, and intended to emit metrics to Datadog, a statsd-based cloud monitoring service.

ASGI version

Since 0.2.0 this middleware only supports ASGI3, if you need ASGI2 support please use version 0.1.2.

installation

pip install timing-asgi

usage

Here's an example using the Starlette ASGI framework which prints out the timing metrics..

A more realistic example which emits the timing metrics to Datadog can be found at https://github.com/steinnes/timing-starlette-asgi-example.

import logging
import uvicorn

from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from timing_asgi import TimingMiddleware, TimingClient
from timing_asgi.integrations import StarletteScopeToName


class PrintTimings(TimingClient):
    def timing(self, metric_name, timing, tags):
        print(metric_name, timing, tags)


app = Starlette()


@app.route("/")
def homepage(request):
    return PlainTextResponse("hello world")


app.add_middleware(
    TimingMiddleware,
    client=PrintTimings(),
    metric_namer=StarletteScopeToName(prefix="myapp", starlette_app=app)
)

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    uvicorn.run(app)

Running this example and sending some requests:

$ python app.py
INFO: Started server process [35895]
INFO: Waiting for application startup.
2019-03-07 11:38:01 INFO  [timing_asgi.middleware:44] ASGI scope of type lifespan is not supported yet
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: ('127.0.0.1', 58668) - "GET / HTTP/1.1" 200
myapp.__main__.homepage 0.0006690025329589844 ['http_status:200', 'http_method:GET', 'time:wall']
myapp.__main__.homepage 0.0006950000000000012 ['http_status:200', 'http_method:GET', 'time:cpu']
INFO: ('127.0.0.1', 58684) - "GET /asdf HTTP/1.1" 404
myapp.asdf 0.0005478858947753906 ['http_status:404', 'http_method:GET', 'time:wall']
myapp.asdf 0.0005909999999999804 ['http_status:404', 'http_method:GET', 'time:cpu']

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

timing_asgi-0.3.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

timing_asgi-0.3.2-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file timing_asgi-0.3.2.tar.gz.

File metadata

  • Download URL: timing_asgi-0.3.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.7 Darwin/24.6.0

File hashes

Hashes for timing_asgi-0.3.2.tar.gz
Algorithm Hash digest
SHA256 128aac498c3f87184ace1406bc077d97dd1318586a569d1b0307b0f3083097c4
MD5 069f7ec2a522b056c3e593906d82c0aa
BLAKE2b-256 7624fec16231e437152dd84c0e41795a41dd6d71580074ae9097bf80af5daa44

See more details on using hashes here.

File details

Details for the file timing_asgi-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: timing_asgi-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.7 Darwin/24.6.0

File hashes

Hashes for timing_asgi-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cadd0d7d8864fce5bef39d97ecd6963ff557eeeaaa53ee0e1cd61cca1becad7a
MD5 15652c271208ac2d666bc242ee4e13fe
BLAKE2b-256 0c9729a634bad0b16c2aace29982c7c885a0baff90b48332d0fffb0ff02dbb55

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page