Repository navigation
feat(investigations): Validate action request events #128421
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dc0b350
feat(investigations): Validate action request events
wedamija 353348d
fix(investigations): Tighten update_detector contract checks
wedamija d885dc6
feat(investigations): Register the action kinds with their contracts
wedamija 0cda545
ref(investigations): Clarify action payload trim docs
wedamija 2046c64
ref(investigations): Reword comments
wedamija 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
13 changes: 13 additions & 0 deletions
13
src/sentry/investigations/services/action_kinds/__init__.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,13 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from sentry.investigations.services.action_kinds.base import ActionKind | ||
| from sentry.investigations.services.action_kinds.connect_telemetry import ConnectTelemetryKind | ||
| from sentry.investigations.services.action_kinds.update_detector import UpdateDetectorKind | ||
|
|
||
| ACTION_KINDS: dict[str, ActionKind] = { | ||
| kind.kind: kind for kind in (UpdateDetectorKind(), ConnectTelemetryKind()) | ||
| } | ||
|
|
||
|
|
||
| def get_action_kind(kind: str | None) -> ActionKind | None: | ||
| return ACTION_KINDS.get(kind) if kind is not None else None | ||
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,12 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import abc | ||
|
|
||
| from sentry.investigations.contracts import ActionPayloadSerializer | ||
|
|
||
|
|
||
| class ActionKind(abc.ABC): | ||
| """How Sentry checks, applies, and records one kind of action.""" | ||
|
|
||
| kind: str | ||
| payload_serializer: type[ActionPayloadSerializer] |
18 changes: 18 additions & 0 deletions
18
src/sentry/investigations/services/action_kinds/connect_telemetry.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,18 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from rest_framework import serializers | ||
|
|
||
| from sentry.investigations.contracts import ActionPayloadSerializer | ||
| from sentry.investigations.services.action_kinds.base import ActionKind | ||
|
|
||
| CONNECT_TELEMETRY_ACTION = "connect_telemetry" | ||
| TELEMETRY_PROVIDERS = ("datadog", "gcp") | ||
|
|
||
|
|
||
| class ConnectTelemetryActionSerializer(ActionPayloadSerializer): | ||
| provider = serializers.ChoiceField(choices=TELEMETRY_PROVIDERS) | ||
|
|
||
|
|
||
| class ConnectTelemetryKind(ActionKind): | ||
| kind = CONNECT_TELEMETRY_ACTION | ||
| payload_serializer = ConnectTelemetryActionSerializer |
100 changes: 100 additions & 0 deletions
100
src/sentry/investigations/services/action_kinds/update_detector.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,100 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from rest_framework import serializers | ||
|
|
||
| from sentry.db.models.fields.bounded import I32_MAX, I64_MAX | ||
| from sentry.investigations.contracts import ( | ||
| ActionPayloadSerializer, | ||
| StrictCharField, | ||
| StrictContractSerializer, | ||
| StrictFloatField, | ||
| StrictIntegerField, | ||
| ) | ||
| from sentry.investigations.services.action_kinds.base import ActionKind | ||
|
|
||
| UPDATE_DETECTOR_ACTION = "update_detector" | ||
|
|
||
|
|
||
| _DETECTOR_TEXT_LIMITS = {"query": 2_000, "aggregate": 500, "environment": 64} | ||
|
|
||
|
|
||
| class DetectorChangesSerializer(StrictContractSerializer): | ||
| """The detector settings an `update_detector` action reads or writes.""" | ||
|
|
||
| timeWindowSeconds = StrictIntegerField(required=False, min_value=1, max_value=I32_MAX) | ||
| highThreshold = StrictFloatField(required=False) | ||
| mediumThreshold = StrictFloatField(required=False) | ||
| resolutionThreshold = StrictFloatField(required=False) | ||
| query = StrictCharField( | ||
| required=False, allow_blank=True, max_length=_DETECTOR_TEXT_LIMITS["query"] | ||
| ) | ||
| aggregate = StrictCharField(required=False, max_length=_DETECTOR_TEXT_LIMITS["aggregate"]) | ||
| environment = StrictCharField( | ||
| required=False, allow_null=True, max_length=_DETECTOR_TEXT_LIMITS["environment"] | ||
| ) | ||
|
|
||
|
|
||
| class UpdateDetectorActionSerializer(ActionPayloadSerializer): | ||
| detectorId = StrictCharField(max_length=32) | ||
|
wedamija marked this conversation as resolved.
|
||
| before = DetectorChangesSerializer() | ||
| changes = DetectorChangesSerializer() | ||
|
|
||
| def validate_detectorId(self, value: str) -> str: | ||
| if not (value.isascii() and value.isdigit()) or int(value) > I64_MAX: | ||
| raise serializers.ValidationError("Must be a numeric ID.") | ||
| return value | ||
|
|
||
| def validate(self, attrs: dict[str, Any]) -> dict[str, Any]: | ||
| before = attrs["before"] | ||
| changes = attrs["changes"] | ||
| if not changes: | ||
| raise serializers.ValidationError({"changes": "At least one change is required."}) | ||
| if set(before) != set(changes): | ||
| raise serializers.ValidationError("before and changes must have the same keys.") | ||
| if any(changes[key] == before[key] for key in changes): | ||
| raise serializers.ValidationError({"changes": "Every change must differ from before."}) | ||
| return attrs | ||
|
|
||
| @classmethod | ||
| def trim(cls, action: dict[str, Any]) -> dict[str, Any] | None: | ||
| """Drop changes we won't apply: text over our limits, or a null current aggregate. | ||
|
|
||
| No change left makes the request advice. | ||
| """ | ||
|
|
||
| before = action.get("before") | ||
| changes = action.get("changes") | ||
| if ( | ||
| not isinstance(before, dict) | ||
| or not isinstance(changes, dict) | ||
| or set(before) != set(changes) | ||
| ): | ||
| return action | ||
| dropped = { | ||
|
wedamija marked this conversation as resolved.
|
||
| key | ||
| for key, limit in _DETECTOR_TEXT_LIMITS.items() | ||
| for values in (before, changes) | ||
| if key in values | ||
| and ( | ||
| values[key] is None | ||
| if key == "aggregate" | ||
| else isinstance(values[key], str) and len(values[key]) > limit | ||
| ) | ||
| } | ||
| if not dropped: | ||
| return action | ||
| kept = [key for key in changes if key not in dropped] | ||
| if not kept: | ||
| return None | ||
| return { | ||
| **action, | ||
| "before": {key: before[key] for key in kept}, | ||
| "changes": {key: changes[key] for key in kept}, | ||
| } | ||
|
|
||
|
|
||
| class UpdateDetectorKind(ActionKind): | ||
| kind = UPDATE_DETECTOR_ACTION | ||
| payload_serializer = UpdateDetectorActionSerializer | ||
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
25 changes: 25 additions & 0 deletions
25
tests/sentry/investigations/services/test_connect_telemetry_contract.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,25 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from sentry.investigations.services.action_kinds.connect_telemetry import ( | ||
| ConnectTelemetryActionSerializer, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("provider", ["datadog", "gcp"]) | ||
| def test_accepts_supported_providers(provider: str) -> None: | ||
| action = {"kind": "connect_telemetry", "provider": provider} | ||
| serializer = ConnectTelemetryActionSerializer(data=action) | ||
|
|
||
| assert serializer.is_valid() | ||
| assert dict(serializer.validated_data) == action | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("provider", ["honeycomb", None, ""]) | ||
| def test_rejects_unknown_providers(provider: str | None) -> None: | ||
| serializer = ConnectTelemetryActionSerializer( | ||
| data={"kind": "connect_telemetry", "provider": provider} | ||
| ) | ||
|
|
||
| assert not serializer.is_valid() |
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.