-
Notifications
You must be signed in to change notification settings - Fork 296
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
Add service label for alerting integrations #5373
Draft
Ferril
wants to merge
10
commits into
dev
Choose a base branch
from
julia/add-service-label-for-alerting-integrations
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
44acaec
Add endpoint to get label by name
Ferril 1d3b0ea
Add tasks to update labels for alerting integrations
Ferril a659004
Add service label on creating alerting integration
Ferril 20d7b16
Move the second part of removing field migration to /migration directory
Ferril 5eeff8c
Merge branch 'refs/heads/dev' into julia/add-service-label-for-alerti…
Konstantinov-Innokentii fc0b643
rename things
Konstantinov-Innokentii 3b62ede
Split logic of creating default service_name label
Konstantinov-Innokentii 1d5b3af
Update creating service name dynamic label for alerting integration
Ferril 9f56968
fix import
Ferril cb65375
fix migration
Ferril 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
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
2 changes: 0 additions & 2 deletions
2
...echannelassociatedlabel_inheritable_db.py → ...echannelassociatedlabel_inheritable_db.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
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 |
---|---|---|
|
@@ -7,13 +7,15 @@ | |
|
||
from apps.labels.client import LabelsAPIClient, LabelsRepoAPIException | ||
from apps.labels.types import LabelOption, LabelPair | ||
from apps.labels.utils import LABEL_OUTDATED_TIMEOUT_MINUTES, get_associating_label_model | ||
from apps.labels.utils import LABEL_OUTDATED_TIMEOUT_MINUTES, get_associating_label_model, get_service_label_custom | ||
from apps.user_management.models import Organization | ||
from common.custom_celery_tasks import shared_dedicated_queue_retry_task | ||
|
||
logger = get_task_logger(__name__) | ||
logger.setLevel(logging.DEBUG) | ||
|
||
MAX_RETRIES = 1 if settings.DEBUG else 10 | ||
|
||
|
||
class KVPair(typing.TypedDict): | ||
value_name: str | ||
|
@@ -129,9 +131,7 @@ def _update_labels_cache(values_id_to_pair: typing.Dict[str, LabelPair]): | |
LabelValueCache.objects.bulk_update(values, fields=["name", "last_synced", "prescribed"]) | ||
|
||
|
||
@shared_dedicated_queue_retry_task( | ||
autoretry_for=(Exception,), retry_backoff=True, max_retries=1 if settings.DEBUG else 10 | ||
) | ||
@shared_dedicated_queue_retry_task(autoretry_for=(Exception,), retry_backoff=True, max_retries=MAX_RETRIES) | ||
def update_instances_labels_cache(organization_id: int, instance_ids: typing.List[int], instance_model_name: str): | ||
from apps.labels.models import LabelValueCache | ||
|
||
|
@@ -162,3 +162,56 @@ def update_instances_labels_cache(organization_id: int, instance_ids: typing.Lis | |
continue | ||
if label_option: | ||
update_label_option_cache.apply_async((label_option,)) | ||
|
||
|
||
@shared_dedicated_queue_retry_task(autoretry_for=(Exception,), retry_backoff=True, max_retries=MAX_RETRIES) | ||
def add_service_label_for_alerting_integrations(): | ||
"""Starts tasks that add `service_name` dynamic label to custom labels for alerting integrations""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, add comment that it's intended for 1-time run. Also, let's stop use term custom labels |
||
|
||
from apps.alerts.models import AlertReceiveChannel | ||
|
||
organization_ids = ( | ||
AlertReceiveChannel.objects.filter( | ||
integration=AlertReceiveChannel.INTEGRATION_GRAFANA_ALERTING, | ||
organization__is_grafana_labels_enabled=True, | ||
organization__deleted_at__isnull=True, | ||
) | ||
.values_list("organization", flat=True) | ||
.distinct() | ||
) | ||
|
||
for idx, organization_id in enumerate(organization_ids): | ||
countdown = idx // 10 | ||
add_service_label_per_org.apply_async((organization_id,), countdown=countdown) | ||
|
||
|
||
@shared_dedicated_queue_retry_task(autoretry_for=(Exception,), retry_backoff=True, max_retries=MAX_RETRIES) | ||
def add_service_label_per_org(organization_id: int): | ||
"""Add `service_name` dynamic label to custom labels for alerting integrations""" | ||
|
||
from apps.alerts.models import AlertReceiveChannel | ||
from apps.user_management.models import Organization | ||
|
||
organization = Organization.objects.get(id=organization_id) | ||
service_label_custom = get_service_label_custom(organization) | ||
if not service_label_custom: | ||
return | ||
integrations = AlertReceiveChannel.objects.filter( | ||
integration=AlertReceiveChannel.INTEGRATION_GRAFANA_ALERTING, | ||
organization=organization, | ||
) | ||
integrations_to_update = [] | ||
# add service label to integration custom labels if it's not already there | ||
for integration in integrations: | ||
custom_service_label_exists = False | ||
custom_labels = integration.alert_group_labels_custom if integration.alert_group_labels_custom else [] | ||
for label in custom_labels: | ||
if label[0] == service_label_custom[0]: | ||
custom_service_label_exists = True | ||
break | ||
if custom_service_label_exists: | ||
continue | ||
integration.alert_group_labels_custom = custom_labels + [service_label_custom] | ||
integrations_to_update.append(integration) | ||
|
||
AlertReceiveChannel.objects.bulk_update(integrations_to_update, fields=["alert_group_labels_custom"]) |
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
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.
Let's remove update_labels_cache task, it seems it's deprecated