Skip to main content

A Python metrics library

Project description

Pylemetry

uv Ruff image image image Release

Add metrics to your Python applications with Pylemetry

Currently, three meters are supported, Counter, Gauge, and Timer

Counter

The counter meter allows you to keep track of the number of times a block of code is executed. A Counter can be created either directly

from pylemetry.meters import Counter


def some_method() -> None:
    counter = Counter()

    for _ in range(100):
        counter.add()  # counter += 1 is also supported

    counter.get_count()  # 100

or via a decorator

from pylemetry import Registry
from pylemetry.decorators import count


@count()
def some_method() -> None:
    ...


@count("named_counter")
def another_method() -> None:
    ...


def main() -> None:
    for _ in range(100):
        some_method()
        another_method()

    counter = Registry().get_counter("some_method")
    counter.get_count()  # 100

    counter = Registry.get_counter("named_counter")
    counter.get_count()  # 100

When using this meter via a decorator, the meter gets added to the global Registry, with the method name it's decorating as the meter name. Alternatively, you can provide a name for the meter as a parameter to the decorator

Gauge

A Gauge meter allows you to keep track of varying metrics, e.g. memory usage or items on a queue. This meter currently isn't supported as a decorator

from pylemetry import Registry
from pylemetry.meters import Gauge


def some_method() -> None:
    gauge = Gauge()
    
    Registry().add_gauge("sample_gauge", gauge)

The Gauge supports incrementing, decrementing, and setting a value directly

from pylemetry import Registry


gauge = Registry().get_gauge("sample_gauge")

gauge.add(10)
gauge += 1.5
gauge.get_value()  # 11.5

gauge.subtract(10)
gauge -= 8.5
gauge.get_value()  # -8

gauge.set_value(7.5)
gauge.get_value()  # 7.5

Timer

A Timer meter allows for tracking the time taken for a block of code. This can be done either directly

from pylemetry.meters import Timer


def some_method() -> None:
    timer = Timer()

    for _ in range(100):
        with timer.time():
            ...

    timer.get_count()  # 100
    timer.get_mean_tick_time()  # Mean execution time of the code block

or via a decorator

from pylemetry import Registry
from pylemetry.decorators import time


@time()
def some_method() -> None:
    ...


@time("named_timer")
def another_method() -> None:
    ...


def main() -> None:
    for _ in range(100):
        some_method()
        another_method()
        
    timer = Registry().get_timer("some_method")
    timer.get_count()  # 100
    timer.get_mean_tick_time()  # Mean execution time of the some_method function
    timer.get_max_tick_time()  # Maximum execution time of the some_method function
    timer.get_min_tick_time()  # Minimum execution time of the some_method function

    timer = Registry().get_timer("named_timer")
    timer.get_count()  # 100
    ...

When using this meter via a decorator, the meter gets added to the global Registry, with the method name it's decorating as the meter name. Alternatively, you can provide a name for the meter as a parameter to the decorator

The Registry

Pylemetry maintains a global registry of meters, allowing you to share a meter across multiple files, or reference metrics from a central location. This registry is also used to keep track of all metrics created by decorators, with those meters registered using the method name they are decorating

from pylemetry import Registry
from pylemetry.meters import Counter, Gauge, Timer


counter = Counter()
gauge = Gauge()
timer = Timer()

Registry().add_counter("example", counter)
Registry().add_gauge("example", gauge)
Registry().add_timer("example", timer)

Each meter type has an add_meter, get_meter and remove_meter method to manage meters in the Registry, each requiring a unique meter name.

The Registry can be cleared through the clear() method

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

pylemetry-0.1.4.tar.gz (34.3 kB view details)

Uploaded Source

Built Distribution

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

pylemetry-0.1.4-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file pylemetry-0.1.4.tar.gz.

File metadata

  • Download URL: pylemetry-0.1.4.tar.gz
  • Upload date:
  • Size: 34.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.17

File hashes

Hashes for pylemetry-0.1.4.tar.gz
Algorithm Hash digest
SHA256 044320affdf5dd86473a6beae51ca532e073fbeac7b3fa2950ade94601e5f597
MD5 0478cf31afba80b8cd5828c3b560f34a
BLAKE2b-256 ae6238081da23f86bc083e0c21d20fb7290ea5a33255c9faa648b42c4789e8cc

See more details on using hashes here.

File details

Details for the file pylemetry-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: pylemetry-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.17

File hashes

Hashes for pylemetry-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 66fcb45626db486a654009eb05c67eecdcdd3a7e2cfa9a9fe4e82787e07fd549
MD5 5578bf3ae14b04100365afa02a9046b5
BLAKE2b-256 925f222fbf751d7f1d67d898b98c21d469bba60ae54a8206eec16c11d7c3a988

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