Skip to content

Latest commit

 

History

History
71 lines (49 loc) · 1.58 KB

README.md

File metadata and controls

71 lines (49 loc) · 1.58 KB

Prometheus Client - Python

Sample app for collecting metrics into Prometheus by specific event class.

Dependencies

Installation

Initiate virtual environment and generate Pipfile and Pipfile.lock by running:

pipenv lock

Install dependencies and get into virtual environment.

pipenv install && pipenv shell

Development

Running in local by

python app.py

Usage

in case of you want to create your own event

  • implement your event class (must end with _event suffix) to metrics/event/
  • add your new event class into constant WHITELISTED_EVENT in metrics/__init__.py
  • add Metrics.record("{event_name}", "{value}") in the last line of code block with Metrics("{event_name}") as metrics:.

eg. sample event in metrics/event/sample_event.py

from metrics import Metrics

with Metrics("sample") as metrics:
    try:
        result = "response"
    except Exception as e:
        result = {"error": e}

    metrics.record(result)

Custom Labels

  • add label(s) you want to save to metric into LABELS constant
LABELS["error", "status"]
  • modify _transform_result_to_labels method which is responsible for wrapping labels to dict before saving to Prometheus in event class.
def _transform_result_to_labels(self, result: Union[dict, Any]) -> dict:
    if isinstance(result, dict) and "error" in result:
        return {"status": "error", "error": result["error"]}
    else:
        return {"status": result["status"], "error": None}