-
Notifications
You must be signed in to change notification settings - Fork 549
feat(integration): Add health-tracking integration feature #7956
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
Open
afsuyadi
wants to merge
65
commits into
Flagsmith:main
Choose a base branch
from
afsuyadi:feat/track-integration-health
base: main
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.
+1,209
−148
Open
Changes from all commits
Commits
Show all changes
65 commits
Select commit
Hold shift + click to select a range
89f175c
feat(integrations): add model to track integration health status
afsuyadi 3bdf907
feat(integrations): add services to provide method for third-party wr…
afsuyadi 46dde6d
feat(integrations): add unit test for record_integration_health
afsuyadi 6d6f0ce
feat(integrations): add method to retrieve latest integration health
afsuyadi 433b73c
feat(integrations): add latest_health_field to base environment integ…
afsuyadi 8aed422
feat(integrations): add latest_health field for meta class
afsuyadi f3233c3
feat(integrations): add unit test for integration common services and…
afsuyadi 1f58eb4
feat(integrations): add types for ActiveIntegration interface
afsuyadi 8033b4d
feat(integrations): add indicator for integration health status
afsuyadi a36895a
feat(integrations): add record_integration_health method for HTTP-bas…
afsuyadi b4cb0ae
feat(integrations): add unit tests for wrappers
afsuyadi fce7899
feat(integrations): add payload field to accomodate new serializer fo…
afsuyadi f90e7f7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 22d5bbb
fix(integrations): fix component by lint
afsuyadi 590d7e4
fix(serializer): add type notation
afsuyadi 2a372ac
fix(serializer): parameterize return type and use .pk instead of .id
afsuyadi dbf3a1e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a3082a5
fix(integrations-newrelic): normalize base_url
afsuyadi 581b8de
fix(integrations-newrelic): use try and except
afsuyadi a99eee6
fix(integrations): use try and except to prevent crash
afsuyadi 6c3c86a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 99ff48e
fix(integrations-amplitude): add timeout for requests.post()
afsuyadi dff9cb2
fix(integrations-amlitude): use try and except
afsuyadi afc6a27
fix(integrations-datadog): use try and except
afsuyadi ab65d07
fix(integrations-dynatrace): use try and except
afsuyadi 24e5f7c
fix(integrations-grafana): use try and except
afsuyadi a02158f
fix(integrations-heap): use try and except
afsuyadi a92e0b4
fix(integrations-mixpanel): use try and except
afsuyadi 3de347f
fix(integrations-newrelic): use try and except
afsuyadi 7967069
fix(integrations-webhook): use try and except
afsuyadi 5920997
feat(docs): add guide on how to cleanup integration health record
afsuyadi 94a845c
feat(integrations): add scheduled action to cleanup health records th…
afsuyadi 3ca740e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 81dd7bb
feat(integrations): add Meta class for IntegrationHealthRecord class
afsuyadi 0a0a4cf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cb547ed
feat(integrations): add migration for health record indexing
afsuyadi 30b2a54
fix(integrations-dynatrace): normalize base_url
afsuyadi 4e6318d
fix(integrations-dynatrace): add timeout for requests.post()
afsuyadi a798310
fix(integrations-heap): add timeout for requests.post()
afsuyadi 5bfd90d
fix(tests): add content_type when searching for health records
afsuyadi dba07f4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] dbbbe9a
fix(tests): add coverage for non-2xx status code
afsuyadi b640ed0
fix(tests): add None type for function
afsuyadi b00b090
fix(tests): assert result is not None
afsuyadi 75696aa
fix(tests): add testing for anti-pattern
afsuyadi d76ce48
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7ea716f
fix(integrations): fix import path
afsuyadi 65152bb
fix(integrations): extract ternary expressions into a variable
afsuyadi 8ae9c2b
fix(integrations): guard base_url when it's None
afsuyadi 69a7b1b
fix(integrations): run lint
afsuyadi c6d4a6d
fix(integrations): address typing errors in unit tests from CI
afsuyadi c6cb750
fix(integrations): address typing errors in serializers tests from CI
afsuyadi cada674
fix(integrations): add guard if base_url is null
afsuyadi 3b2a49e
fix(integrations): add type annotations
afsuyadi dd9a4f5
fix(integrations): add missing type annotations
afsuyadi 125ab22
fix(tests): fix runtime bug
afsuyadi 42fb57f
docs(event-catalogue): update changes based on CI
afsuyadi 0311e07
fix(integrations): remove ternary expression on component
afsuyadi e585a76
fix(integrations): add separator for expected url
afsuyadi f36fea5
fix(tests): add separator for expected url and match assertions
afsuyadi 7b1733d
fix(integrations): add savepoint
afsuyadi 2f587fe
fix(tests): add unit test for cleanup health records function
afsuyadi 2475be5
fix(tests): add exception-path tests to address codecov's review
afsuyadi d463141
chore: trigger CI re-run
afsuyadi daa92f6
fix(integrations): establish 3-colour dot for health tracking status …
afsuyadi 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 hidden or 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 hidden or 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 hidden or 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,29 @@ | ||
| # Integrations | ||
|
|
||
| ## Integration Health Records Cleanup | ||
|
|
||
| Integration health records are stored to track the status of external integration calls. These records can grow over time. A management command is provided to clean up old records. | ||
|
|
||
| ### Running the cleanup command | ||
|
|
||
| To delete integration health records older than 30 days (default): | ||
|
|
||
| ```bash | ||
| python manage.py cleanup_integration_health_records | ||
| ``` | ||
|
|
||
| To specify a custom retention period: | ||
|
|
||
| ```bash | ||
| python manage.py cleanup_integration_health_records --days=60 | ||
| ``` | ||
|
|
||
| ### Scheduling automatic cleanup | ||
|
|
||
| To prevent the `IntegrationHealthRecord` table from growing unbounded, schedule the cleanup command to run periodically using cron or your preferred task scheduler. | ||
|
|
||
| Example cron entry (runs daily at 3 AM): | ||
|
|
||
| ```cron | ||
| 0 3 * * * cd /path/to/flagsmith && python manage.py cleanup_integration_health_records | ||
| ``` |
This file contains hidden or 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 hidden or 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 hidden or 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,6 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class IntegrationCommonConfig(AppConfig): | ||
| name = "integrations.common" |
Empty file.
Empty file.
28 changes: 28 additions & 0 deletions
28
api/integrations/common/management/commands/cleanup_integration_health_records.py
This file contains hidden or 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,28 @@ | ||
| from datetime import timedelta | ||
|
|
||
| from django.core.management import BaseCommand, CommandParser | ||
| from django.utils import timezone | ||
|
|
||
| from integrations.common.models import IntegrationHealthRecord | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Delete integration health records older than the specified number of days." | ||
|
|
||
| def add_arguments(self, parser: CommandParser) -> None: | ||
| parser.add_argument( | ||
| "--days", | ||
| type=int, | ||
| default=30, | ||
| help="Delete records older than this many days (default: 30).", | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): # type: ignore[no-untyped-def] | ||
| days = options["days"] | ||
| cutoff = timezone.now() - timedelta(days=days) | ||
| deleted, _ = IntegrationHealthRecord.objects.filter( | ||
| created_at__lt=cutoff | ||
| ).delete() | ||
| self.stdout.write( | ||
| f"Deleted {deleted} integration health record(s) older than {days} days." | ||
| ) |
This file contains hidden or 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,43 @@ | ||
| # Generated by Django 5.2.15 on 2026-07-04 10:38 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ("contenttypes", "0002_remove_content_type_name"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="IntegrationHealthRecord", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.AutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ( | ||
| "created_at", | ||
| models.DateTimeField(auto_now_add=True, verbose_name="Created At"), | ||
| ), | ||
| ("object_id", models.PositiveIntegerField()), | ||
| ("status_code", models.PositiveIntegerField()), | ||
| ( | ||
| "content_type", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| to="contenttypes.contenttype", | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| ] |
21 changes: 21 additions & 0 deletions
21
api/integrations/common/migrations/0002_add_health_record_index.py
This file contains hidden or 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,21 @@ | ||
| # Generated by Django 5.2.15 on 2026-07-09 01:26 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("common", "0001_initial"), | ||
| ("contenttypes", "0002_remove_content_type_name"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddIndex( | ||
| model_name="integrationhealthrecord", | ||
| index=models.Index( | ||
| fields=["content_type", "object_id", "-created_at"], | ||
| name="common_inte_content_bd807d_idx", | ||
| ), | ||
| ), | ||
| ] |
Empty file.
This file contains hidden or 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 hidden or 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 hidden or 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,45 @@ | ||
| import logging | ||
| from typing import Any | ||
|
|
||
| from django.contrib.contenttypes.models import ContentType | ||
| from django.db import models, transaction | ||
|
|
||
| from integrations.common.models import IntegrationHealthRecord | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def record_integration_health( | ||
| integration_config: models.Model, | ||
| status_code: int, | ||
| ) -> None: | ||
| try: | ||
| with transaction.atomic(): | ||
| IntegrationHealthRecord.objects.create( | ||
| content_object=integration_config, | ||
| status_code=status_code, | ||
| ) | ||
| except Exception: | ||
| logger.exception("Failed to record integration health.") | ||
|
|
||
|
|
||
| def get_latest_integration_health( | ||
| integration_config: models.Model, | ||
| ) -> dict[str, Any] | None: | ||
| latest_record = ( | ||
| IntegrationHealthRecord.objects.filter( | ||
| content_type=ContentType.objects.get_for_model(integration_config), | ||
| object_id=integration_config.pk, | ||
| ) | ||
| .order_by("-created_at") | ||
| .first() | ||
| ) | ||
|
|
||
| if not latest_record: | ||
| return None | ||
|
|
||
| return { | ||
| "status_code": latest_record.status_code, | ||
| "is_healthy": 200 <= latest_record.status_code < 300, | ||
| "created_at": latest_record.created_at, | ||
| } |
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.