Skip to main content

Multiprocess-safe metrics

Project description

mpmetrics

mpmetrics implements metrics suitable for use with OpenMetrics. It provides multiprocess-safe replacements for prometheus_client's Counter, Gauge, Summary, and Histogram. To use it, just import these classes from mpmetrics instead of from prometheus_client:

from mpmetrics import Summary
from prometheus_client import start_http_server
import multiprocessing
import random
import time

# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')

# Decorate function with metric.
@REQUEST_TIME.time()
def process_request(t):
    """A dummy function that takes some time."""
    time.sleep(t)

# Create function for subprocess
def generate_requests():
    while True:
        process_request(random.random())

if __name__ == '__main__':
    # Start up the server to expose the metrics.
    start_http_server(8000)
    # Generate some requests from two processes
    multiprocessing.Process(target=generate_requests).start()
    generate_requests()

Navigate to http://localhost:8000/metrics to view the results.

Features

  • Completely thread- and process-safe.
  • All operations are atomic. Metrics will never be partially updated.
  • Updating metrics is lock-free.
  • TODO: better performance?

Users of prometheus_flask_exporter can import mpmetrics.flask instead.

Compatibility

The following behaviors differ from prometheus_client:

  • Labeled metrics cannot be removed or cleared.
  • Info metrics are not implemented. Use prometheus_client.Info instead.
  • Enums (StateSets) are not implemented.
  • Exemplars are not implemented.
  • Using a value of None for registry is not supported.
  • multiprocessing_mode is not supported. Gauges have a single series with one value.

Limitations

The following limitations apply to this library

  • Only Unix is supported, and only Linux x86-64 has been tested.
  • Only the fork start method has been tested, though the others should work.
  • The python interpreter stats will only be from the current process.

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

mpmetrics-0.0.2.tar.gz (38.8 kB view details)

Uploaded Source

File details

Details for the file mpmetrics-0.0.2.tar.gz.

File metadata

  • Download URL: mpmetrics-0.0.2.tar.gz
  • Upload date:
  • Size: 38.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for mpmetrics-0.0.2.tar.gz
Algorithm Hash digest
SHA256 13986d4fa674a2bc77cd21f54ef12258b378a806e33ba9f51f9e2e2e4388359e
MD5 99b0ca43a53011bded6c86e00b0a940c
BLAKE2b-256 78b9509b3312e2c898fdb2acbf37e9acb5e89f19b3dc8dcc79b4f45676a56f1e

See more details on using hashes here.

Provenance

Supported by

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