-
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
56
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.
Open
Changes from all commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
c5e6330
feat(integrations): add model to track integration health status
afsuyadi 219ac2e
feat(integrations): add services to provide method for third-party wr…
afsuyadi 1b39140
feat(integrations): add unit test for record_integration_health
afsuyadi a1099d5
feat(integrations): add method to retrieve latest integration health
afsuyadi beb221c
feat(integrations): add latest_health_field to base environment integ…
afsuyadi 3bbfe1a
feat(integrations): add latest_health field for meta class
afsuyadi 21c54d9
feat(integrations): add unit test for integration common services and…
afsuyadi 9e28b0f
feat(integrations): add types for ActiveIntegration interface
afsuyadi 9fb45e0
feat(integrations): add indicator for integration health status
afsuyadi 642cbeb
feat(integrations): add record_integration_health method for HTTP-bas…
afsuyadi 55838f6
feat(integrations): add unit tests for wrappers
afsuyadi 6add1a7
feat(integrations): add payload field to accomodate new serializer fo…
afsuyadi 3cd4ce3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7a3e786
fix(integrations): fix component by lint
afsuyadi 62670ad
fix(serializer): add type notation
afsuyadi 3cd6231
fix(serializer): parameterize return type and use .pk instead of .id
afsuyadi 5152409
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a21a5d0
fix(integrations-newrelic): normalize base_url
afsuyadi 5371467
fix(integrations-newrelic): use try and except
afsuyadi 9d6f476
fix(integrations): use try and except to prevent crash
afsuyadi 43f80d8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d5a982f
fix(integrations-amplitude): add timeout for requests.post()
afsuyadi 13022b1
fix(integrations-amlitude): use try and except
afsuyadi 1ab3906
fix(integrations-datadog): use try and except
afsuyadi 28bf293
fix(integrations-dynatrace): use try and except
afsuyadi cf3bb8c
fix(integrations-grafana): use try and except
afsuyadi 8e658e9
fix(integrations-heap): use try and except
afsuyadi 15020a9
fix(integrations-mixpanel): use try and except
afsuyadi 2fef4b0
fix(integrations-newrelic): use try and except
afsuyadi 179151a
fix(integrations-webhook): use try and except
afsuyadi 2719fad
feat(docs): add guide on how to cleanup integration health record
afsuyadi 1656c15
feat(integrations): add scheduled action to cleanup health records th…
afsuyadi f41d82f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4daa9a8
feat(integrations): add Meta class for IntegrationHealthRecord class
afsuyadi c18a57c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0105e6b
feat(integrations): add migration for health record indexing
afsuyadi 2cf644a
fix(integrations-dynatrace): normalize base_url
afsuyadi bd91942
fix(integrations-dynatrace): add timeout for requests.post()
afsuyadi 7ed5659
fix(integrations-heap): add timeout for requests.post()
afsuyadi 275956f
fix(tests): add content_type when searching for health records
afsuyadi 25b4fb1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 92e3c81
fix(tests): add coverage for non-2xx status code
afsuyadi b15cadd
fix(tests): add None type for function
afsuyadi 47c2978
fix(tests): assert result is not None
afsuyadi de63aed
fix(tests): add testing for anti-pattern
afsuyadi fd74354
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 61c715e
fix(integrations): fix import path
afsuyadi d82d0e6
fix(integrations): extract ternary expressions into a variable
afsuyadi 0fd89f0
fix(integrations): guard base_url when it's None
afsuyadi a98cb12
fix(integrations): run lint
afsuyadi 1ef0795
fix(integrations): address typing errors in unit tests from CI
afsuyadi 1dd55b4
fix(integrations): address typing errors in serializers tests from CI
afsuyadi 627de8c
fix(integrations): add guard if base_url is null
afsuyadi 3c97ab1
fix(integrations): add type annotations
afsuyadi 048633d
fix(integrations): add missing type annotations
afsuyadi 4c60801
fix(tests): fix runtime bug
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,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.