2023-11-27 17:53:54 +00:00
|
|
|
import logging
|
2023-10-20 09:30:11 +02:00
|
|
|
import typing
|
|
|
|
|
|
|
|
|
|
from django.apps import apps # noqa: I251
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
|
|
if typing.TYPE_CHECKING:
|
2023-11-27 17:53:54 +00:00
|
|
|
from apps.alerts.models import AlertGroup
|
2023-10-20 09:30:11 +02:00
|
|
|
from apps.labels.models import AssociatedLabel
|
2023-11-08 14:04:58 +00:00
|
|
|
from apps.user_management.models import Organization
|
2023-10-20 09:30:11 +02:00
|
|
|
|
2023-11-27 17:53:54 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2023-10-20 09:30:11 +02:00
|
|
|
|
|
|
|
|
LABEL_OUTDATED_TIMEOUT_MINUTES = 30
|
|
|
|
|
ASSOCIATED_MODEL_NAME = "AssociatedLabel"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_associating_label_model(obj_model_name: str) -> typing.Type["AssociatedLabel"]:
|
|
|
|
|
associating_label_model_name = obj_model_name + ASSOCIATED_MODEL_NAME
|
|
|
|
|
label_model = apps.get_model("labels", associating_label_model_name)
|
|
|
|
|
return label_model
|
|
|
|
|
|
|
|
|
|
|
2023-11-08 14:04:58 +00:00
|
|
|
def is_labels_feature_enabled(organization: "Organization") -> bool:
|
2024-01-30 15:29:16 +08:00
|
|
|
"""
|
|
|
|
|
is_labels_feature_enabled checks if env with labels feature is enabled and plugin is provisioned.
|
|
|
|
|
"""
|
|
|
|
|
env_enabled = settings.FEATURE_LABELS_ENABLED_FOR_ALL or organization.id in settings.FEATURE_LABELS_ENABLED_PER_ORG
|
|
|
|
|
return organization.is_grafana_labels_enabled and env_enabled
|
2023-11-08 14:04:58 +00:00
|
|
|
|
|
|
|
|
|
2023-11-28 20:47:57 +08:00
|
|
|
def get_labels_dict(labelable) -> dict[str, str]:
|
|
|
|
|
"""
|
|
|
|
|
get_labels_dict returns dict of labels' key and values names for the given object
|
|
|
|
|
"""
|
|
|
|
|
return {label.key.name: label.value.name for label in labelable.labels.all().select_related("key", "value")}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_alert_group_labels_dict(alert_group: "AlertGroup") -> dict[str, str]:
|
|
|
|
|
"""
|
|
|
|
|
get_alert_group_labels_dict returns dict of labels' key and values names for the given alert group.
|
|
|
|
|
It's different from get_labels_dict, because AlertGroupAssociated labels store key/value_name, not key/value_id
|
|
|
|
|
"""
|
Webhook labels (#3383)
This PR add labels for webhooks.
1. Make webhook "labelable" with ability to filter by labels.
2. Add labels to the webhook payload. It contain new field webhook with
it's name, id and labels. Field integration and alert_group has a
corresponding label field as well. See example of a new payload below:
```
{
"event": {
"type": "escalation"
},
"user": null,
"alert_group": {
"id": "IRFN6ZD31N31B",
"integration_id": "CTWM7U4A2QG97",
"route_id": "RUE7U7Z46SKGY",
"alerts_count": 1,
"state": "firing",
"created_at": "2023-11-22T08:54:55.178243Z",
"resolved_at": null,
"acknowledged_at": null,
"title": "Incident",
"permalinks": {
"slack": null,
"telegram": null,
"web": "http://grafana:3000/a/grafana-oncall-app/alert-groups/IRFN6ZD31N31B"
},
"labels": {
"severity": "critical"
}
},
"alert_group_id": "IRFN6ZD31N31B",
"alert_payload": {
"message": "This alert was sent by user for demonstration purposes"
},
"integration": {
"id": "CTWM7U4A2QG97",
"type": "webhook",
"name": "hi - Webhook",
"team": null,
"labels": {
"hello": "world",
"severity": "critical"
}
},
"notified_users": [],
"users_to_be_notified": [],
"webhook": {
"id": "WHAXK4BTC7TAEQ",
"name": "test",
"labels": {
"hello": "kesha"
}
}
}
```
I feel that there is an opportunity to make code cleaner - remove all
label logic from serializers, views and utils to models or dedicated
LabelerService and introduce Labelable interface with something like
label_verbal, update_labels methods. However, I don't want to tie
webhook labels with a refactoring.
---------
Co-authored-by: Dominik <dominik.broj@grafana.com>
2023-11-22 19:17:41 +08:00
|
|
|
return {label.key_name: label.value_name for label in alert_group.labels.all()}
|