Skip to main content

jetblack-metrics

Generic metric classes and context based monitoring.

Installation

Install from the pie store.

pip install jetblack-metrics

Usage

First you need to implement a metric which interacts with an actual instrumentation implementation. The following provides an HTTP request metric using Prometheus to gather the metrics and the TimedMetric to provide a latency metric.

from jetblack_metrics import monitor, TimedMetric
from prometheus_client import Counter, Gauge, Histogram

class HttpRequestMetric(TimedMetric):
    """
    A metric which holds HTTP information.
    """

    def __init__(self, name: str, method: str, path: str) -> None:
        super().__init__()
        self.name = name
        self.scope = method
        self.info = path
        self.status = 500

    REQUEST_COUNT = Counter(
        "http_request_count",
        "Number of requests received",
        ["name", "method", "path", "status"]
    )
    REQUEST_LATENCY = Histogram(
        "http_request_latency",
        "Elapsed time per request",
        ["name", "method", "path"]
    )
    REQUEST_IN_PROGRESS = Gauge(
        "http_requests_in_progress",
        "Requests in progress",
        ["name", "method", "path"]
    )

    def on_enter(self):
        super().on_enter()
        self.REQUEST_IN_PROGRESS.labels(
            self.name,
            self.scope['method'],
            self.scope['path']
        ).inc()

    def on_exit(self) -> None:
        super().on_exit()
        self.REQUEST_COUNT.labels(
            self.name,
            self.scope['method'],
            self.scope['path'],
            self.status
        ).inc()
        self.REQUEST_LATENCY.labels(
            self.name,
            self.scope['method'],
            self.scope['path']
        ).observe(self.elapsed)
        self.REQUEST_IN_PROGRESS.labels(
            self.name,
            self.scope['method'],
            self.scope['path']
        ).dec()

Once we have the metric we can use the monitor function to manage the process of gathering the statistics.

def some_http_middleware(request, next_handler):
    """Some kind of HTTP middleware function"""
    with monitor(HttpRequestMetric('MyApp', request.method, request.path)) as metric:
        # Call the request handler
        response = next_handler(request)
        metric.status = response.status

Metadata

Release files for jetblack-metrics 1.0.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 jetblack-metrics 1.0.2
File Size Uploaded
jetblack-metrics-1.0.2.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jetblack-metrics 1.0.2
File Interpreter ABI Platform
jetblack_metrics-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 15.2 kB

Release files / jetblack-metrics-1.0.2.tar.gz

Download URL jetblack-metrics-1.0.2.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a5a09a7a87406e6b8b731bea0596e6ea3ff2aa03168a9e4cf6df4124634fa4f5
BLAKE2b-256 checksum
How to use checksums
5967a8616049ae4441df44a244ac930681bc9eb40b9e87db3ed766625b3c6b5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.1 CPython/3.8.10 Linux/5.10.0-1033-oem

Release files / jetblack_metrics-1.0.2-py3-none-any.whl

Download URL jetblack_metrics-1.0.2-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
497c70a3e3c8bef092f2c1009c077af83fe0a4be03249f2c8c3f17a220e09547
BLAKE2b-256 checksum
How to use checksums
74dcf75b359c8157d814374fc8595517649dd3f24c6a1547f6c0a80d3f1b9560
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.1 CPython/3.8.10 Linux/5.10.0-1033-oem

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

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