Skip to main content

playing with metrics

Project description

pytheus-logo

pytheus

playing with metrics


Experimenting with a different way of creating prometheus metrics in python:

  • support for default labels value (wip ⚠️)
  • partial labels value (built in an incremental way) ✅
  • multiple multiprocess support:
    • mmap file based (wip ⚠️)
    • redis backend ✅
  • customizable registry support ✅
  • registry prefix support ✅

Install

pip install pytheus

NOTE: Currently you need the redis library installed but the requirement will be removed soon.

pip install redis
# or
pip install pytheus[redis]

Partial labels support:

from pytheus.metrics import create_counter

# without labels
my_metric = create_counter('metric_name', 'desc')
my_metric.inc()  # example for counter

# with labels
my_metric = create_counter('metric_name', 'desc', required_labels=['req1', 'req2'])

my_metric.labels({'req1': '1', 'req2': '2'}).inc()  # you can pass all the labels at once
partial_my_metric = my_metric.labels({'req1': '1'})  # a cacheable object with one of the required labels already set
observable_my_metric = partial_my_metric.labels({'req2': '2'}).inc()  # finish setting the remaining values before observing

Exposing metrics:

You can use the generate_metrics function from pytheus.exposition to generate the metrics and serve them as an endpoint with your favourite web framework.

Alternatively you can use the make_wsgi_app function that creates a simple wsgi app to serve the metrics.

How to use different backends

Things work out of the box, using the SingleProcessBackend:

from pytheus.metrics import create_counter

counter = create_counter(
    name="my_metric",
    description="My description",
    required_labels=["label_a", "label_b"],
)
print(counter._metric_value_backend.__class__)
# <class 'pytheus.backends.SingleProcessBackend'>
print(counter._metric_value_backend.config)
# {}

You can define environment configuration to have different defaults, using two environment variables:

export PYTHEUS_BACKEND_CLASS="pytheus.backends.MultipleProcessFileBackend"
export PYTHEUS_BACKEND_CONFIG="./config.json"

Now, create the config file, ./config.json:

{
  "pytheus_file_directory": "./"
}

Now we can try the same snippet as above:

from pytheus.metrics import create_counter

counter = create_counter(
    name="my_metric",
    description="My description",
    required_labels=["label_a", "label_b"],
)
print(counter._metric_value_backend.__class__)
# <class 'pytheus.backends.MultipleProcessFileBackend'>
print(counter._metric_value_backend.config)
# {'pytheus_file_directory': "./"}

You can also pass the values directly in Python, which would take precedence over the environment setup we have just described:

from pytheus.metrics import create_counter
from pytheus.backends import MultipleProcessRedisBackend, load_backend

load_backend(
    backend_class=MultipleProcessRedisBackend,
    backend_config={
      "host": "127.0.0.1",
      "port":  6379
    }
)
# Notice that if you simply call load_backend(), it would reload config from the environment.

# load_backend() is called automatically at package import, that's why we didn't need to call it
# directly in the previous example

counter = create_counter(
    name="my_metric",
    description="My description",
    required_labels=["label_a", "label_b"],
)
print(counter._metric_value_backend.__class__)
# <class 'pytheus.backends.MultipleProcessRedisBackend'>
print(counter._metric_value_backend.config)
# {'host': '127.0.0.1', 'port': 6379}

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

pytheus-0.0.2.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

pytheus-0.0.2-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytheus-0.0.2.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for pytheus-0.0.2.tar.gz
Algorithm Hash digest
SHA256 20e2da686f7b7a2fcb9613ad5d1b3f2280a3b09820cacdd90a33380389805628
MD5 500bff5f77707180b9d2848e68e57658
BLAKE2b-256 dbded3e3162103b016e90f4ca713a87e34b5ab0f68e15bdbfe8b2df8476b7a15

See more details on using hashes here.

File details

Details for the file pytheus-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pytheus-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for pytheus-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8b2e53439e11a1159e65ab738123e22df67cd9ee44806ae60a39ba6592924c77
MD5 e4209a1664a2b592cf7f6ec0adf0ad4d
BLAKE2b-256 4dd36c0bd410846221812e49e4e3343c23bd6a440bf21711d111cd981a20b29a

See more details on using hashes here.

Supported by

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