Skip to main content

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']

Metadata

Release files for timing-asgi 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for timing-asgi 0.3.2
File Size Uploaded Provenance Actions
timing_asgi-0.3.2.tar.gz 4.6 kB Without provenance Details for timing_asgi-0.3.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for timing-asgi 0.3.2
File Interpreter ABI Platform Actions
timing_asgi-0.3.2-py3-none-any.whl Python 3 none any Details for timing_asgi-0.3.2-py3-none-any.whl

Total release size: 11.1 kB

Release files / timing_asgi-0.3.2.tar.gz

Download URL timing_asgi-0.3.2.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
128aac498c3f87184ace1406bc077d97dd1318586a569d1b0307b0f3083097c4
BLAKE2b-256 checksum
How to use checksums
7624fec16231e437152dd84c0e41795a41dd6d71580074ae9097bf80af5daa44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.13.7 Darwin/24.6.0

Release files / timing_asgi-0.3.2-py3-none-any.whl

Download URL timing_asgi-0.3.2-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cadd0d7d8864fce5bef39d97ecd6963ff557eeeaaa53ee0e1cd61cca1becad7a
BLAKE2b-256 checksum
How to use checksums
0c9729a634bad0b16c2aace29982c7c885a0baff90b48332d0fffb0ff02dbb55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.13.7 Darwin/24.6.0

Security for timing-asgi 0.3.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page