Skip to main content

A Python metrics library

Project description

Pylemetry

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:
    ...


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

    counter = Registry().get_counter("some_method")
    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

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()  # -7.5

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:
    ...


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

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.3.tar.gz (33.7 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.3-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pylemetry-0.1.3.tar.gz
Algorithm Hash digest
SHA256 16b1e537a3957333cd4474f73ea7296e44ce6266270543867c09ef12f483dfb1
MD5 813f340e602a207d1346894a12c40ab7
BLAKE2b-256 a1bc9d93f2d9045403afae72fafd992bb4301ffc5f14eb970f309f7a77010f79

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pylemetry-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5a71b9717d3101145d2c0bfa14404049fb266fed370e144d0be29c55e77770f9
MD5 51902e514db1f35c32e69caf59a67dec
BLAKE2b-256 e42a5132100ac4ba98c6b452e04ba9a9e2d4f2d67cbe2ab098eb24cbeeebdfc1

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