mlrun.alerts.alert#

class mlrun.alerts.alert.AlertConfig(project: str | None = None, name: str | None = None, template: AlertTemplate | str | None = None, description: str | None = None, summary: str | None = None, severity: AlertSeverity | None = None, trigger: AlertTrigger | None = None, criteria: AlertCriteria | None = None, reset_policy: ResetPolicy | None = None, notifications: list[mlrun.common.schemas.alert.AlertNotification] | None = None, entities: EventEntities | None = None, id: int | None = None, state: AlertActiveState | None = None, created: str | None = None, count: int | None = None)[source]#

Bases: ModelObj

Alert config object

Example:

# create an alert on endpoint_id, which will be triggered to slack if there is a "data_drift_detected" event
# 3 times in the next hour.

from mlrun.alerts import AlertConfig
import mlrun.common.schemas.alert as alert_objects

entity_kind = alert_objects.EventEntityKind.MODEL_ENDPOINT_RESULT
entity_id = get_default_result_instance_fqn(endpoint_id)
event_name = alert_objects.EventKind.DATA_DRIFT_DETECTED
notification = mlrun.model.Notification(
    kind="slack",
    name="slack_notification",
    message="drift was detected",
    severity="warning",
    when=["now"],
    condition="failed",
    secret_params={
        "webhook": "https://hooks.slack.com/",
    },
).to_dict()

alert_data = AlertConfig(
    project="my-project",
    name="drift-alert",
    summary="a drift was detected",
    severity=alert_objects.AlertSeverity.LOW,
    entities=alert_objects.EventEntities(
        kind=entity_kind, project="my-project", ids=[entity_id]
    ),
    trigger=alert_objects.AlertTrigger(events=[event_name]),
    criteria=alert_objects.AlertCriteria(count=3, period="1h"),
    notifications=[alert_objects.AlertNotification(notification=notification)],
)
project.store_alert_config(alert_data)
Parameters:
  • project -- Name of the project to associate the alert with

  • name -- Name of the alert

  • template -- Optional parameter that allows creating an alert based on a predefined template. You can pass either an AlertTemplate object or a string (the template name). If a template is used, many fields of the alert will be auto-generated based on the template.However, you still need to provide the following fields: name, project, entity, notifications

  • description -- Description of the alert

  • summary -- Summary of the alert, will be sent in the generated notifications

  • severity -- Severity of the alert

  • trigger -- The events that will trigger this alert, may be a simple trigger based on events or complex trigger which is based on a prometheus alert

  • criteria -- When the alert will be triggered based on the specified number of events within the defined time period.

  • reset_policy -- When to clear the alert. May be "manual" for manual reset of the alert, or "auto" if the criteria contains a time period

  • notifications -- List of notifications to invoke once the alert is triggered

  • entities -- Entities that the event relates to. The entity object will contain fields that uniquely identify a given entity in the system

  • id -- Internal id of the alert (user should not supply it)

  • state -- State of the alert, may be active/inactive (user should not supply it)

  • created -- When the alert is created (user should not supply it)

  • count -- Internal counter of the alert (user should not supply it)

classmethod from_dict(struct=None, fields=None, deprecated_fields: dict | None = None)[source]#

create an object from a python dictionary

to_dict(fields: list | None = None, exclude: list | None = None, strip: bool = False)[source]#
validate_required_fields()[source]#
with_entities(entities: EventEntities)[source]#
with_notifications(notifications: list[mlrun.common.schemas.alert.AlertNotification])[source]#