Package: prometheus-alert-model-leo
Prometheus Alert Modelfor PythonThis project provides the Pydantic modelsAlertGroupandAlertthat
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 addAlertGroupas a parameter to the handler.FeaturesPydantic models that matches the official Alertmanager payload schema.Fieldsspecific_annotationsandspecific_labelsin 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 ContentsUsageMotivationDevelopmentUsageUsing 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.fromprometheus_alert_modelimportAlertGroupfromfastapiimportFastAPIfromreimportcompileapp=FastAPI()@app.post("/alert")defpost_alert(alert_group:AlertGroup):alert_group.remove_re(annotations=r"^(__.*)$",labels=compile(r"^(__.*)$"))alert_group.add_prefix(labels={"summary":"Prototyping system: "})foralertinalert_group.alerts:print(alert.specific_annotationsprint(alert.specific_labels)AlertModelIn the following all attributes you can find within aAlert. Notice
the custom attributesspecific_annotationsandspecific_labelsthat
include elements that are specific to the respective alert in context of the
completeAlertGroup/ payload.fingerprint:strstatus:strstarts_at:datetimeends_at:datetimegenerator_url:strannotations:Dict[str,str]labels:Dict[str,str]specific_annotations:Dict[str,str]specific_labels:Dict[str,str]AlertGroupModelIn the following all attributes you can find within aAlertGroup. It
represents a single payload from Alertmanager.receiver:strstatus:strexternal_url:strversion:strgroup_key:strtruncated_alerts:int==0group_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.MotivationI 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.DevelopmentPlease refer to"DEVELOPMENT.md".