-
Notifications
You must be signed in to change notification settings - Fork 545
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
50
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.
+864
−130
Open
Changes from all commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
d45664c
feat(integrations): add model to track integration health status
afsuyadi 1fededc
feat(integrations): add services to provide method for third-party wr…
afsuyadi fe66020
feat(integrations): add unit test for record_integration_health
afsuyadi c6ae3f3
feat(integrations): add method to retrieve latest integration health
afsuyadi 2dbce0d
feat(integrations): add latest_health_field to base environment integ…
afsuyadi d863f46
feat(integrations): add latest_health field for meta class
afsuyadi 87e3120
feat(integrations): add unit test for integration common services and…
afsuyadi 5ff350f
feat(integrations): add types for ActiveIntegration interface
afsuyadi 7d667b6
feat(integrations): add indicator for integration health status
afsuyadi 3e44400
feat(integrations): add record_integration_health method for HTTP-bas…
afsuyadi 09a1211
feat(integrations): add unit tests for wrappers
afsuyadi cb2721c
feat(integrations): add payload field to accomodate new serializer fo…
afsuyadi b6d4f87
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9b5a2a9
fix(integrations): fix component by lint
afsuyadi baa0f2c
fix(serializer): add type notation
afsuyadi c60fca7
fix(serializer): parameterize return type and use .pk instead of .id
afsuyadi ec11bad
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cef0895
fix(integrations-newrelic): normalize base_url
afsuyadi 0e7baa2
fix(integrations-newrelic): use try and except
afsuyadi 29cc0ac
fix(integrations): use try and except to prevent crash
afsuyadi cf38f33
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ece3fbb
fix(integrations-amplitude): add timeout for requests.post()
afsuyadi efca717
fix(integrations-amlitude): use try and except
afsuyadi 5452e7f
fix(integrations-datadog): use try and except
afsuyadi 63ef4eb
fix(integrations-dynatrace): use try and except
afsuyadi f399143
fix(integrations-grafana): use try and except
afsuyadi 26cdad8
fix(integrations-heap): use try and except
afsuyadi 2de342e
fix(integrations-mixpanel): use try and except
afsuyadi 86682b5
fix(integrations-newrelic): use try and except
afsuyadi 8e239da
fix(integrations-webhook): use try and except
afsuyadi 35d478c
feat(docs): add guide on how to cleanup integration health record
afsuyadi db9c4a6
feat(integrations): add scheduled action to cleanup health records th…
afsuyadi 3f1c741
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8b1f150
feat(integrations): add Meta class for IntegrationHealthRecord class
afsuyadi 2820f34
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1fec0e9
feat(integrations): add migration for health record indexing
afsuyadi 4a1cf50
fix(integrations-dynatrace): normalize base_url
afsuyadi 02066a8
fix(integrations-dynatrace): add timeout for requests.post()
afsuyadi 3f050fa
fix(integrations-heap): add timeout for requests.post()
afsuyadi f1f33cc
fix(tests): add content_type when searching for health records
afsuyadi 8739e76
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 265d407
fix(tests): add coverage for non-2xx status code
afsuyadi 85060c2
fix(tests): add None type for function
afsuyadi d44cd4e
fix(tests): assert result is not None
afsuyadi 82ed25d
fix(tests): add testing for anti-pattern
afsuyadi 30762d9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9e66261
fix(integrations): fix import path
afsuyadi c53857f
fix(integrations): extract ternary expressions into a variable
afsuyadi c06eb35
fix(integrations): guard base_url when it's None
afsuyadi 85480bd
fix(integrations): run lint
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.
26 changes: 26 additions & 0 deletions
26
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,26 @@ | ||
| from django.core.management import BaseCommand | ||
| 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): | ||
| parser.add_argument( | ||
| "--days", | ||
| type=int, | ||
| default=30, | ||
| help="Delete records older than this many days (default: 30).", | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| days = options["days"] | ||
| cutoff = timezone.now() - timezone.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,44 @@ | ||
| import logging | ||
| from typing import Any | ||
|
|
||
| from django.contrib.contenttypes.models import ContentType | ||
| from django.db import models | ||
|
|
||
| from integrations.common.models import IntegrationHealthRecord | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def record_integration_health( | ||
| integration_config: models.Model, | ||
| status_code: int, | ||
| ) -> None: | ||
| try: | ||
| 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.