Repository navigation
refactor: Move telemetry reports to the telemetry app #9015
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
+552
−570
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b366326
refactor: Move telemetry reports to the telemetry app
germangarces c7c4fed
chore: Update documentation artefacts
flagsmith-engineering[bot] 68317c1
chore: Update documentation artefacts
flagsmith-engineering[bot] 7929a36
refactor(Telemetry): Drop analytics grouping from this PR
germangarces 688059a
chore: Update documentation artefacts
flagsmith-engineering[bot] 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
Some comments aren't visible on the classic Files Changed page.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 base64 | ||
| import dataclasses | ||
| import json | ||
|
|
||
| import requests | ||
| import structlog | ||
| from django.core.serializers.json import DjangoJSONEncoder | ||
|
|
||
| from telemetry.dataclasses import UsageSnapshot | ||
|
|
||
| logger = structlog.get_logger(__name__) | ||
|
|
||
| USAGE_ENDPOINT_PATH = "/v1/public/usage" | ||
| REQUEST_TIMEOUT_SECONDS = 30 | ||
|
|
||
|
|
||
| def post_usage_snapshot( | ||
| *, | ||
| base_url: str, | ||
| snapshot: UsageSnapshot, | ||
| signature_b64: str, | ||
| ) -> None: | ||
| url = f"{base_url.rstrip('/')}{USAGE_ENDPOINT_PATH}" | ||
| headers = { | ||
| "Authorization": (f"Bearer {_map_signature_to_bearer_token(signature_b64)}"), | ||
| "Content-Type": "application/json", | ||
| } | ||
| body = json.dumps(dataclasses.asdict(snapshot), cls=DjangoJSONEncoder) | ||
|
|
||
| response = requests.post( | ||
| url, | ||
| data=body, | ||
| headers=headers, | ||
| timeout=REQUEST_TIMEOUT_SECONDS, | ||
| ) | ||
|
|
||
| if response.ok: | ||
| logger.info("snapshot.pushed", status_code=response.status_code) | ||
| else: | ||
| logger.warning("snapshot.push_failed", status_code=response.status_code) | ||
|
|
||
|
|
||
| def _map_signature_to_bearer_token(signature_b64: str) -> str: | ||
| raw_signature = base64.b64decode(signature_b64) | ||
| return base64.urlsafe_b64encode(raw_signature).decode("ascii").rstrip("=") | ||
File renamed without changes.
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,106 @@ | ||
| from datetime import timedelta | ||
|
|
||
| import structlog | ||
| from common.core.utils import get_version | ||
| from django.conf import settings | ||
| from django.utils import timezone | ||
|
|
||
| from app_analytics.analytics_db_service import get_usage_data_for_window | ||
| from app_analytics.dataclasses import UsageData | ||
| from organisations.models import Organisation | ||
| from telemetry.control_plane.client import post_usage_snapshot | ||
| from telemetry.dataclasses import ApiCallBreakdown, ProjectUsage, UsageSnapshot | ||
|
|
||
| logger = structlog.get_logger(__name__) | ||
|
|
||
| MAX_PROJECT_USAGE_ROWS = 5_000 | ||
|
|
||
|
|
||
| def push_usage_snapshots() -> None: | ||
| if not (settings.ENABLE_TELEMETRY): | ||
| logger.debug("run.skipped", reason="telemetry_disabled") | ||
| return | ||
| if not (base_url := settings.CONTROL_PLANE_URL): | ||
| logger.debug("run.skipped", reason="control_plane_url_unset") | ||
| return | ||
| if not (organisations := _get_licensed_organisations()): | ||
| logger.debug("run.skipped", reason="no_licensed_organisations") | ||
| return | ||
|
|
||
| for organisation in organisations: | ||
| with structlog.contextvars.bound_contextvars(organisation__id=organisation.id): | ||
| try: | ||
| post_usage_snapshot( | ||
| base_url=base_url, | ||
| snapshot=_get_usage_snapshot(organisation), | ||
| signature_b64=organisation.licence.signature, | ||
| ) | ||
| except Exception: | ||
| logger.exception("snapshot.errored") | ||
|
|
||
|
|
||
| def _get_licensed_organisations() -> list[Organisation]: | ||
| if settings.LICENSING_INSTALLED: # pragma: no cover | ||
|
germangarces marked this conversation as resolved.
Resolved
|
||
| return list( | ||
| Organisation.objects.filter(licence__isnull=False).select_related("licence") | ||
| ) | ||
| return [] | ||
|
|
||
|
|
||
| def _sum_api_calls(usage_data: list[UsageData]) -> int: | ||
| return sum( | ||
| data.flags + data.identities + data.traits + data.environment_document | ||
| for data in usage_data | ||
| ) | ||
|
|
||
|
|
||
| def _map_to_top_project_usage( | ||
| usage_data_by_project_id: dict[int, list[UsageData]], | ||
| ) -> list[ProjectUsage]: | ||
| rows = [ | ||
| ProjectUsage( | ||
| project_id=project_id, | ||
| api_call_count=_sum_api_calls(usage_data), | ||
| ) | ||
| for project_id, usage_data in usage_data_by_project_id.items() | ||
| ] | ||
| # Highest usage first | ||
| rows.sort(key=lambda row: row.api_call_count, reverse=True) | ||
| return rows[:MAX_PROJECT_USAGE_ROWS] | ||
|
|
||
|
|
||
| def _map_to_api_call_breakdown( | ||
| usage_data: list[UsageData], | ||
| ) -> ApiCallBreakdown: | ||
| return ApiCallBreakdown( | ||
| flags=sum(data.flags for data in usage_data), | ||
| identities=sum(data.identities for data in usage_data), | ||
| traits=sum(data.traits for data in usage_data), | ||
| environment_documents=sum(data.environment_document for data in usage_data), | ||
| ) | ||
|
|
||
|
|
||
| def _get_usage_snapshot(organisation: Organisation) -> UsageSnapshot: | ||
| hour_end = timezone.now().replace(minute=0, second=0, microsecond=0) | ||
| hour_start = hour_end - timedelta(hours=24) | ||
| seat_count = organisation.num_seats | ||
| instance_version = get_version() | ||
| usage_data = get_usage_data_for_window(organisation, hour_start, hour_end) | ||
| usage_data_by_project_id = { | ||
| project.id: get_usage_data_for_window( | ||
| organisation, hour_start, hour_end, project_id=project.id | ||
| ) | ||
| for project in organisation.projects.all() | ||
| } | ||
|
|
||
| # No data access past this point! | ||
|
|
||
| return UsageSnapshot( | ||
| timestamp=hour_start, | ||
| seat_count=seat_count, | ||
| api_call_total=_sum_api_calls(usage_data), | ||
| api_call_breakdown=_map_to_api_call_breakdown(usage_data), | ||
| project_count=len(usage_data_by_project_id), | ||
| instance_version=instance_version, | ||
| project_usage=_map_to_top_project_usage(usage_data_by_project_id), | ||
| ) | ||
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,10 @@ | ||
| from datetime import timedelta | ||
|
|
||
| from task_processor.decorators import register_recurring_task | ||
|
|
||
| from telemetry.services import push_usage_snapshots | ||
|
|
||
|
|
||
| @register_recurring_task(run_every=timedelta(hours=24)) | ||
|
germangarces marked this conversation as resolved.
Resolved
|
||
| def push_usage() -> None: | ||
| push_usage_snapshots() | ||
Empty file.
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.