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. For more examples, look in the examples/ directory.

Features

  • Completely thread- and process-safe.
  • All operations are atomic. Metrics will never be partially updated.
  • Updating metrics is lock-free on architectures with 64-bit atomics. On architectures with 32-bit atomics, we transparently fall back to locking implementations.
  • Exemplars are supported, but they are locking.
  • Possibly better performance than prometheus_metrics, but probably not a big contributor to overall performance.
  • All multiprocessing start methods are supported.

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.
  • Using a value of None for registry is not supported.
  • multiprocessing_mode is not supported. Gauges have a single series with one value.

These are unlikely to ever be addressed (except Info support) due to the fundamental architectural changes necessary to support multiprocessing.

Limitations

The following limitations apply to this library

  • Only Unix is supported (due to use of pthreads). Linux and macOS are tested.
  • The python interpreter stats will only be from the current process.
  • The shared memory temporary files are not cleaned up properly. This is to keep non-fork start methods working (as they pickle the Heap to transfer it between processes

Notes

  • Metric labels should not be user-generated in order to prevent a denial-of-service attack due to memory exhaustion. For example, instead of using a "path" label (provided by the user), use an "endpoint" label (provided by the application).

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.5.tar.gz (51.7 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: mpmetrics-0.0.5.tar.gz
  • Upload date:
  • Size: 51.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.5

File hashes

Hashes for mpmetrics-0.0.5.tar.gz
Algorithm Hash digest
SHA256 7aaa7a27a5f412ee556a17a935f6bbf121b3edeeb57ba8786f61ef9b6f4c932d
MD5 efa5e20ceeb0352a60323238812c5848
BLAKE2b-256 f4d1524bbd195603a1440b829dce0a85445c52a7383fb9fc87d274c12a8f3617

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