prometheus-alert-model-leo


Nameprometheus-alert-model-leo JSON
Version 1.1.0 PyPI version JSON
download
home_pagehttps://github.com/trallnag/prometheus-alert-model-for-python
SummaryPydantic model of the Prometheus Alertmanager alert payload
upload_time2023-10-07 10:29:02
maintainer
docs_urlNone
authorTim Schwenke
requires_python>=3.8,<4.0
licenseApache-2.0
keywords prometheus alertmanager alerting pydantic
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <!-- omit in toc -->
# Prometheus Alert Model *for Python*

[![Current Package Version](https://badge.fury.io/py/prometheus-alert-model.svg)](https://pypi.python.org/pypi/prometheus-alert-model)
[![Maintenance](https://img.shields.io/badge/maintained%3F-yes-green.svg)](https://GitHub.com/Naereen/StrapDown.js/graphs/commit-activity)
[![Supported Python Versions](https://img.shields.io/pypi/pyversions/prometheus-alert-model.svg)](https://pypi.python.org/pypi/prometheus-alert-model)
[![Downloads](https://pepy.tech/badge/prometheus-alert-model/month)](https://pepy.tech/project/prometheus-alert-model/month)
[![docs](https://img.shields.io/badge/docs-here-blue)](https://trallnag.github.io/prometheus-alert-model-for-python/)

![release](https://github.com/trallnag/prometheus-alert-model-for-python/workflows/release/badge.svg)
![commit](https://github.com/trallnag/prometheus-alert-model-for-python/workflows/commit/badge.svg)
[![codecov](https://codecov.io/gh/trallnag/prometheus-alert-model-for-python/branch/master/graph/badge.svg)](https://codecov.io/gh/trallnag/prometheus-alert-model-for-python)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/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.

<!-- omit in toc -->
## 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.

<!-- omit in toc -->
## Table of Contents

- [Usage](#usage)
- [Motivation](#motivation)
- [Development](#development)

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

```python

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)
```

<!-- omit in toc -->
### `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.

```python
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]
```

<!-- omit in toc -->
### `AlertGroup` Model

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

```python
receiver: str
status: str
external_url: str
version: str
group_key: str
truncated_alerts: int = =0
group_labels: Dict[str, str]
common_annotations: Dict[str, str]
common_labels: Dict[str, str]
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"](DEVELOPMENT.md).


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/trallnag/prometheus-alert-model-for-python",
    "name": "prometheus-alert-model-leo",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "prometheus,alertmanager,alerting,pydantic",
    "author": "Tim Schwenke",
    "author_email": "tim.and.trallnag+code@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/6c/89/19e695f5a6246c8d99528b0947c34f7e2de9abfc5edf5bfaf6fc48b75609/prometheus_alert_model_leo-1.1.0.tar.gz",
    "platform": null,
    "description": "<!-- omit in toc -->\n# Prometheus Alert Model *for Python*\n\n[![Current Package Version](https://badge.fury.io/py/prometheus-alert-model.svg)](https://pypi.python.org/pypi/prometheus-alert-model)\n[![Maintenance](https://img.shields.io/badge/maintained%3F-yes-green.svg)](https://GitHub.com/Naereen/StrapDown.js/graphs/commit-activity)\n[![Supported Python Versions](https://img.shields.io/pypi/pyversions/prometheus-alert-model.svg)](https://pypi.python.org/pypi/prometheus-alert-model)\n[![Downloads](https://pepy.tech/badge/prometheus-alert-model/month)](https://pepy.tech/project/prometheus-alert-model/month)\n[![docs](https://img.shields.io/badge/docs-here-blue)](https://trallnag.github.io/prometheus-alert-model-for-python/)\n\n![release](https://github.com/trallnag/prometheus-alert-model-for-python/workflows/release/badge.svg)\n![commit](https://github.com/trallnag/prometheus-alert-model-for-python/workflows/commit/badge.svg)\n[![codecov](https://codecov.io/gh/trallnag/prometheus-alert-model-for-python/branch/master/graph/badge.svg)](https://codecov.io/gh/trallnag/prometheus-alert-model-for-python)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\nThis project provides the Pydantic models `AlertGroup` and `Alert` that\nrepresent a payload from Prometheus Alertmanager. In addition, it also includes\na number of useful utility methods that perform actions on alert data.  It can\nbe used as a drop-in wherever you want to work with Prometheus Alertmanager\nalert payload data.\n\nA prominent example for using it is a FastAPI route that receives alert payloads.\nSimply add `AlertGroup` as a parameter to the handler.\n\n<!-- omit in toc -->\n## Features\n\n- Pydantic models that matches the official Alertmanager payload schema.\n- Fields `specific_annotations` and `specific_labels` in every alert that\n    contain elements that are specific to the respective alert.\n- Methods to update common annotations and labels.\n- Methods to remove, add, update, override and prefix annotations and labels.\n- Every single method is well covered by tests.\n\n<!-- omit in toc -->\n## Table of Contents\n\n- [Usage](#usage)\n- [Motivation](#motivation)\n- [Development](#development)\n\n## Usage\n\nUsing the model is pretty straight-forward. Take a look at the automatically\ngenerated docs or the source code itself. \n\nHere is a very short example how you could use the model. It removes all\nannotations and labels starting with two underscores and adds a prefix that\ncontains namespace info to the summary label. Finally it prints specific\nelements for all alerts in the group.\n\n```python\n\nfrom prometheus_alert_model import AlertGroup\nfrom fastapi import FastAPI\nfrom re import compile\n\napp = FastAPI()\n\n@app.post(\"/alert\")\ndef post_alert(alert_group: AlertGroup):\n    alert_group.remove_re(\n        annotations=r\"^(__.*)$\",\n        labels=compile(r\"^(__.*)$\")\n    )\n\n    alert_group.add_prefix(labels={\"summary\": \"Prototyping system: \"})\n    \n    for alert in alert_group.alerts:\n        print(alert.specific_annotations\n        print(alert.specific_labels)\n```\n\n<!-- omit in toc -->\n### `Alert` Model\n\nIn the following all attributes you can find within a `Alert`. Notice\nthe custom attributes `specific_annotations` and `specific_labels` that\ninclude elements that are specific to the respective alert in context of the\ncomplete `AlertGroup` / payload.\n\n```python\nfingerprint: str\nstatus: str\nstarts_at: datetime\nends_at: datetime\ngenerator_url: str\nannotations: Dict[str, str]\nlabels: Dict[str, str]\nspecific_annotations: Dict[str, str]\nspecific_labels: Dict[str, str]\n```\n\n<!-- omit in toc -->\n### `AlertGroup` Model\n\nIn the following all attributes you can find within a `AlertGroup`. It\nrepresents a single payload from Alertmanager.\n\n```python\nreceiver: str\nstatus: str\nexternal_url: str\nversion: str\ngroup_key: str\ntruncated_alerts: int = =0\ngroup_labels: Dict[str, str]\ncommon_annotations: Dict[str, str]\ncommon_labels: Dict[str, str]\nalerts: List[Alert]\n```\n\nHere is a short summary over the included utility methods (for full\ndocumentation please refer to type hints or the automatically generated docs):\n\n- `update_specific_elements`: Updates specific labels and annotations.\n- `update_specific_annotations`: Updates specific annotations.\n- `update_specific_labels`: Updates specific labels.\n- `update_common_elements`: Updates common annotations and labels.\n- `update_common_annotations`: Updates common annotations.\n- `update_common_labels`: Updates common labels.\n- `remove`: Removes annotations and labels by name.\n- `remove_re`: Removes annotations and labels by matching names with regex.\n- `add`: Adds annotations and labels but skips existing elements.\n- `override`: Adds annotations and labels and overrides existing elements.\n- `add_prefix`: Adds prefix to annotations and labels.\n\n## Motivation\n\nI have a bunch of Python scripts that work in some shape or form with Prometheus\nAlertmanager data. Instead of duplicating the model across all of them I prefer\nto have a single small package that is well tested and reuse it again and again.\nThis way I don't have to reimplement utility functions / methods.\n\n## Development\n\nPlease refer to [\"DEVELOPMENT.md\"](DEVELOPMENT.md).\n\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Pydantic model of the Prometheus Alertmanager alert payload",
    "version": "1.1.0",
    "project_urls": {
        "Homepage": "https://github.com/trallnag/prometheus-alert-model-for-python",
        "Repository": "https://github.com/trallnag/prometheus-alert-model-for-python"
    },
    "split_keywords": [
        "prometheus",
        "alertmanager",
        "alerting",
        "pydantic"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "39d0854032325e9dcc60b82e4da66aea6359675441a62c5953f16c7d7035d8fd",
                "md5": "dce9610f7d112367f4c2eb9d5e747510",
                "sha256": "68df12dcfa3bdae68fe93f80ea2b4fc573a4f5a1e7c390c6a2a723b923b6d1e0"
            },
            "downloads": -1,
            "filename": "prometheus_alert_model_leo-1.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "dce9610f7d112367f4c2eb9d5e747510",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 10568,
            "upload_time": "2023-10-07T10:29:01",
            "upload_time_iso_8601": "2023-10-07T10:29:01.478766Z",
            "url": "https://files.pythonhosted.org/packages/39/d0/854032325e9dcc60b82e4da66aea6359675441a62c5953f16c7d7035d8fd/prometheus_alert_model_leo-1.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6c8919e695f5a6246c8d99528b0947c34f7e2de9abfc5edf5bfaf6fc48b75609",
                "md5": "5e347c607efffdd30339272c0f90bb62",
                "sha256": "27308fd89d7fb3bc9efa209fb65c8dc5c03aeb3fda47903ae756c7e415a70e37"
            },
            "downloads": -1,
            "filename": "prometheus_alert_model_leo-1.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "5e347c607efffdd30339272c0f90bb62",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 9066,
            "upload_time": "2023-10-07T10:29:02",
            "upload_time_iso_8601": "2023-10-07T10:29:02.367824Z",
            "url": "https://files.pythonhosted.org/packages/6c/89/19e695f5a6246c8d99528b0947c34f7e2de9abfc5edf5bfaf6fc48b75609/prometheus_alert_model_leo-1.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-07 10:29:02",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "trallnag",
    "github_project": "prometheus-alert-model-for-python",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "prometheus-alert-model-leo"
}
        
Elapsed time: 0.27889s