-
Notifications
You must be signed in to change notification settings - Fork 429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Grafana feature health provider #5098
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
499c0b4
add grafana codegen
khvn26 697507f
feat: Grafana feature health provider
khvn26 021eec6
remove dead code
khvn26 6435cad
clarify expected errors again
khvn26 039b8f7
docs: Add Feature Health docs
khvn26 1b41736
add backling to feature health
khvn26 b60db56
add `django-squash-migrations` Makefile target
khvn26 5addee8
introduce universal reason schema
khvn26 abf3442
consistent enum usage
khvn26 91f068b
public webhook schema
khvn26 7ffe3b6
revert to `TextField` for `FeatureHealthEvent.reason`, fix typing
khvn26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
matthewelwell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this added here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm used to interacting with Django environment through make. The Makefile loads the appropriate environment for me. During this PR development, I added some new migrations, but I only want to merge one, so I'm squashing them. See
flagsmith/api/features/feature_health/migrations/0002_alter_featurehealthevent_external_id_and_more_squashed_0003_alter_featurehealthevent_reason_and_more.py
Line 9 in 6c121bc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If they were both added here though, why not delete them both, and just recreate a single migration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because I care about other engineers that play with the PR's Docker images against their local databases. With squashed migrations, they won't have to recreate their schemas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... I don't think we should, but ok.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This thread is now irrelevant as per 7ffe3b6.