Skip to main content

Prometheus Alert Model for Python

Current Package Version Maintenance Supported Python Versions Downloads docs

release commit codecov Code style: black

This project provides the Pydantic models AlertGroup and Alert that represent a payload from Prometheus Alertmanager. In addition, it also includes a number of useful utility methods that perform actions on alert data. It can be used as a drop-in wherever you want to work with Prometheus Alertmanager alert payload data.

A prominent example for using it is a FastAPI route that receives alert payloads. Simply add AlertGroup as a parameter to the handler.

Features

  • Pydantic models that matches the official Alertmanager payload schema.
  • Fields specific_annotations and specific_labels in every alert that contain elements that are specific to the respective alert.
  • Methods to update common annotations and labels.
  • Methods to remove, add, update, override and prefix annotations and labels.
  • Every single method is well covered by tests.

Table of Contents

Usage

Using the model is pretty straight-forward. Take a look at the automatically generated docs or the source code itself.

Here is a very short example how you could use the model. It removes all annotations and labels starting with two underscores and adds a prefix that contains namespace info to the summary label. Finally it prints specific elements for all alerts in the group.

from prometheus_alert_model import AlertGroup
from fastapi import FastAPI
from re import compile

app = FastAPI()

@app.post("/alert")
def post_alert(alert_group: AlertGroup):
    alert_group.remove_re(
        annotations=r"^(__.*)$",
        labels=compile(r"^(__.*)$")
    )

    alert_group.add_prefix(labels={"summary": "Prototyping system: "})
    
    for alert in alert_group.alerts:
        print(alert.specific_annotations
        print(alert.specific_labels)

Alert Model

In the following all attributes you can find within a Alert. Notice the custom attributes specific_annotations and specific_labels that include elements that are specific to the respective alert in context of the complete AlertGroup / payload.

fingerprint: str
status: str
starts_at: datetime
ends_at: datetime
generator_url: str
annotations: Dict[str, str]
labels: Dict[str, str]
specific_annotations: Dict[str, str]
specific_labels: Dict[str, str]

AlertGroup Model

In the following all attributes you can find within a AlertGroup. It represents a single payload from Alertmanager.

receiver: str
status: str
external_url: str = Field(alias="externalURL")
version: str
group_key: str = Field(alias="groupKey")
truncated_alerts: int = Field(alias="truncatedAlerts", default=0)
group_labels: Dict[str, str] = Field(alias="groupLabels")
common_annotations: Dict[str, str] = Field(alias="commonAnnotations")
common_labels: Dict[str, str] = Field(alias="commonLabels")
alerts: List[Alert]

Here is a short summary over the included utility methods (for full documentation please refer to type hints or the automatically generated docs):

  • update_specific_elements: Updates specific labels and annotations.
  • update_specific_annotations: Updates specific annotations.
  • update_specific_labels: Updates specific labels.
  • update_common_elements: Updates common annotations and labels.
  • update_common_annotations: Updates common annotations.
  • update_common_labels: Updates common labels.
  • remove: Removes annotations and labels by name.
  • remove_re: Removes annotations and labels by matching names with regex.
  • add: Adds annotations and labels but skips existing elements.
  • override: Adds annotations and labels and overrides existing elements.
  • add_prefix: Adds prefix to annotations and labels.

Motivation

I have a bunch of Python scripts that work in some shape or form with Prometheus Alertmanager data. Instead of duplicating the model across all of them I prefer to have a single small package that is well tested and reuse it again and again. This way I don't have to reimplement utility functions / methods.

Development

Please refer to "DEVELOPMENT.md".

Release files for prometheus-alert-model 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prometheus-alert-model 1.0.0
File Size Uploaded
prometheus-alert-model-1.0.0.tar.gz 10.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for prometheus-alert-model 1.0.0
File Interpreter ABI Platform
prometheus_alert_model-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.5 kB

Release files / prometheus-alert-model-1.0.0.tar.gz

Download URL prometheus-alert-model-1.0.0.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
45d3e8fff6e9f0f0f7424c746b4b7118db4775fe3cc103f0de5f333867ea6887
BLAKE2b-256 checksum
How to use checksums
60aadb4d20ef98920df18d32da4a6741076ad6b622598f008ce98b4454848f42
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.9.0 Linux/5.4.0-1031-azure

Release files / prometheus_alert_model-1.0.0-py3-none-any.whl

Download URL prometheus_alert_model-1.0.0-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c96f36ed8b5d9ee0c8a31d700668e1fedcb05d7179e3913c4f529fe5c93c35c2
BLAKE2b-256 checksum
How to use checksums
9f1c7a7ddc354c4e1716207ff1400ec242404561b328aaea9fb979cf98d3c219
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.9.0 Linux/5.4.0-1031-azure

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page