-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Grafana feature health provider (#5098)
- Loading branch information
Showing
29 changed files
with
1,000 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import json | ||
import typing | ||
|
||
from features.feature_health.models import FeatureHealthEvent | ||
from features.feature_health.types import FeatureHealthEventData | ||
|
||
if typing.TYPE_CHECKING: | ||
from environments.models import Environment | ||
from features.models import Feature | ||
|
||
|
||
def map_feature_health_event_data_to_feature_health_event( | ||
*, | ||
feature_health_event_data: FeatureHealthEventData, | ||
feature: "Feature", | ||
environment: "Environment | None", | ||
) -> FeatureHealthEvent: | ||
if reason := feature_health_event_data.reason: | ||
instance_reason = json.dumps(reason) | ||
else: | ||
instance_reason = None | ||
instance = FeatureHealthEvent( | ||
feature=feature, | ||
environment=environment, | ||
type=feature_health_event_data.type.value, | ||
reason=instance_reason, | ||
external_id=feature_health_event_data.external_id, | ||
provider_name=feature_health_event_data.provider_name, | ||
) | ||
if feature_health_event_data.created_at: | ||
instance.created_at = feature_health_event_data.created_at | ||
return instance |
38 changes: 38 additions & 0 deletions
38
...res/feature_health/migrations/0002_featurehealthevent_add_external_id_alter_created_at.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Generated by Django 4.2.18 on 2025-02-17 15:13 | ||
|
||
from django.db import migrations, models | ||
import django.utils.timezone | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("feature_health", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="featurehealthevent", | ||
name="external_id", | ||
field=models.CharField(blank=True, max_length=255, null=True), | ||
), | ||
migrations.AddField( | ||
model_name="historicalfeaturehealthevent", | ||
name="external_id", | ||
field=models.CharField(blank=True, max_length=255, null=True), | ||
), | ||
migrations.AlterField( | ||
model_name="featurehealthevent", | ||
name="created_at", | ||
field=models.DateTimeField( | ||
blank=True, default=django.utils.timezone.now, editable=False | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="historicalfeaturehealthevent", | ||
name="created_at", | ||
field=models.DateTimeField( | ||
blank=True, default=django.utils.timezone.now, editable=False | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from features.feature_health.providers.grafana.services import ( | ||
get_provider_response, | ||
) | ||
|
||
__all__ = ("get_provider_response",) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
GRAFANA_FEATURE_LABEL_NAME = "flagsmith_feature" | ||
GRAFANA_ENVIRONMENT_LABEL_NAME = "flagsmith_environment" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from features.feature_health.models import ( | ||
FeatureHealthEventType, | ||
FeatureHealthProviderName, | ||
) | ||
from features.feature_health.providers.grafana.constants import ( | ||
GRAFANA_ENVIRONMENT_LABEL_NAME, | ||
GRAFANA_FEATURE_LABEL_NAME, | ||
) | ||
from features.feature_health.providers.grafana.types import ( | ||
GrafanaAlertInstance, | ||
GrafanaWebhookData, | ||
) | ||
from features.feature_health.types import ( | ||
FeatureHealthEventData, | ||
FeatureHealthEventReason, | ||
) | ||
|
||
|
||
def map_payload_to_alert_instances(payload: str) -> list[GrafanaAlertInstance]: | ||
webhook_data = GrafanaWebhookData.model_validate_json(payload) | ||
|
||
return webhook_data.alerts | ||
|
||
|
||
def map_alert_instance_to_feature_health_event_reason( | ||
alert_instance: GrafanaAlertInstance, | ||
) -> FeatureHealthEventReason: | ||
reason_data: FeatureHealthEventReason = {"text_blocks": [], "url_blocks": []} | ||
|
||
annotations = alert_instance.annotations | ||
|
||
# Populate text blocks. | ||
alert_name = alert_instance.labels.get("alertname") or "Alertmanager Alert" | ||
description = annotations.get("description") or "" | ||
reason_data["text_blocks"].append( | ||
{ | ||
"title": alert_name, | ||
"text": description, | ||
} | ||
) | ||
if summary := annotations.get("summary"): | ||
reason_data["text_blocks"].append( | ||
{ | ||
"title": "Summary", | ||
"text": summary, | ||
} | ||
) | ||
|
||
# Populate URL blocks. | ||
reason_data["url_blocks"].append( | ||
{"title": "Alert", "url": alert_instance.generatorURL} | ||
) | ||
if dashboard_url := alert_instance.dashboardURL: | ||
reason_data["url_blocks"].append({"title": "Dashboard", "url": dashboard_url}) | ||
if panel_url := alert_instance.panelURL: | ||
reason_data["url_blocks"].append({"title": "Panel", "url": panel_url}) | ||
if runbook_url := annotations.get("runbook_url"): | ||
reason_data["url_blocks"].append({"title": "Runbook", "url": runbook_url}) | ||
|
||
return reason_data | ||
|
||
|
||
def map_alert_instance_to_feature_health_event_data( | ||
alert_instance: GrafanaAlertInstance, | ||
) -> FeatureHealthEventData | None: | ||
labels = alert_instance.labels | ||
if feature_name := labels.get(GRAFANA_FEATURE_LABEL_NAME): | ||
if alert_instance.status == "firing": | ||
created_at = alert_instance.startsAt | ||
event_type = FeatureHealthEventType.UNHEALTHY | ||
else: | ||
created_at = alert_instance.endsAt | ||
event_type = FeatureHealthEventType.HEALTHY | ||
return FeatureHealthEventData( | ||
created_at=created_at, | ||
feature_name=feature_name, | ||
environment_name=labels.get(GRAFANA_ENVIRONMENT_LABEL_NAME), | ||
type=event_type, | ||
external_id=alert_instance.fingerprint, | ||
reason=map_alert_instance_to_feature_health_event_reason(alert_instance), | ||
provider_name=FeatureHealthProviderName.GRAFANA.value, | ||
) | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from features.feature_health.providers.grafana.mappers import ( | ||
map_alert_instance_to_feature_health_event_data, | ||
map_payload_to_alert_instances, | ||
) | ||
from features.feature_health.types import ( | ||
FeatureHealthEventData, | ||
FeatureHealthProviderResponse, | ||
) | ||
|
||
|
||
def get_provider_response(payload: str) -> FeatureHealthProviderResponse: | ||
events: list[FeatureHealthEventData] = [] | ||
|
||
for alert_instance in map_payload_to_alert_instances(payload): | ||
if event_data := map_alert_instance_to_feature_health_event_data( | ||
alert_instance | ||
): | ||
events.append(event_data) | ||
|
||
return FeatureHealthProviderResponse(events=events) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from datetime import datetime | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class AlertmanagerAlertInstance(BaseModel): | ||
annotations: dict[str, str] | ||
generatorURL: str | ||
endsAt: datetime | ||
fingerprint: str | ||
labels: dict[str, str] | ||
startsAt: datetime | ||
status: str | ||
|
||
|
||
class GrafanaAlertInstance(AlertmanagerAlertInstance): | ||
dashboardURL: str = "" | ||
panelURL: str = "" | ||
silenceURL: str = "" | ||
|
||
|
||
class GrafanaWebhookData(BaseModel): | ||
alerts: list[GrafanaAlertInstance] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from features.feature_health.providers.sample.mappers import ( | ||
map_payload_to_provider_response, | ||
from features.feature_health.providers.sample.services import ( | ||
get_provider_response, | ||
) | ||
|
||
__all__ = ("map_payload_to_provider_response",) | ||
__all__ = ("get_provider_response",) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.