-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(mep): Move transaction thresholds to new tagging system
In getsentry/relay#1225 we introduced a general-purpose tagging system for transaction metrics. This may later be extended: * we will add outlier tagging for histograms soon * session metrics could be tagged the same way (unlikely to happen for now, needs changes in Relay) Here we move away from the purpose-built user satisfaction impl to the more generic one
- Loading branch information
Showing
10 changed files
with
462 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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 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 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,130 @@ | ||
from dataclasses import dataclass | ||
from typing import Any, List, Literal, Sequence, TypedDict, Union | ||
|
||
from sentry.api.endpoints.project_transaction_threshold import DEFAULT_THRESHOLD | ||
from sentry.models import ( | ||
Project, | ||
ProjectTransactionThreshold, | ||
ProjectTransactionThresholdOverride, | ||
TransactionMetric, | ||
) | ||
|
||
|
||
class RuleConditionInner(TypedDict): | ||
op: Literal["eq", "gt"] | ||
name: str | ||
value: Any | ||
|
||
|
||
# mypy does not support recursive types. type definition is a very small subset | ||
# of the values relay actually accepts | ||
class RuleCondition(TypedDict): | ||
op: Literal["and"] | ||
inner: Sequence[RuleConditionInner] | ||
|
||
|
||
class MetricConditionalTaggingRule(TypedDict): | ||
condition: RuleCondition | ||
targetMetrics: Sequence[str] | ||
targetTag: str | ||
tagValue: str | ||
|
||
|
||
_TRANSACTION_METRICS_TO_RULE_FIELD = { | ||
TransactionMetric.LCP.value: "transaction.measurements.lcp", | ||
TransactionMetric.DURATION.value: "transaction.duration", | ||
} | ||
|
||
_SATISFACTION_TARGET_METRICS = frozenset( | ||
[ | ||
"s:transactions/user@none", | ||
"d:transactions/duration@millisecond", | ||
] | ||
) | ||
|
||
_SATISFACTION_TARGET_TAG = "satisfaction" | ||
|
||
|
||
@dataclass | ||
class _DefaultThreshold: | ||
metric: TransactionMetric | ||
threshold: int | ||
|
||
|
||
_DEFAULT_THRESHOLD = _DefaultThreshold( | ||
metric=TransactionMetric[DEFAULT_THRESHOLD["metric"].upper()].value, | ||
threshold=int(DEFAULT_THRESHOLD["threshold"]), | ||
) | ||
|
||
|
||
def get_metric_conditional_tagging_rules( | ||
project: Project, | ||
) -> Sequence[MetricConditionalTaggingRule]: | ||
rules: List[MetricConditionalTaggingRule] = [] | ||
|
||
for threshold in project.projecttransactionthresholdoverride_set.all(): | ||
rules.extend( | ||
_threshold_to_rules( | ||
threshold, | ||
[{"op": "eq", "name": "event.transaction", "value": threshold.transaction}], | ||
) | ||
) | ||
|
||
try: | ||
threshold = ProjectTransactionThreshold.objects.get(project=project) | ||
rules.extend(_threshold_to_rules(threshold, [])) | ||
except ProjectTransactionThreshold.DoesNotExist: | ||
rules.extend(_threshold_to_rules(_DEFAULT_THRESHOLD, [])) | ||
pass | ||
|
||
return rules | ||
|
||
|
||
def _threshold_to_rules( | ||
threshold: Union[ | ||
ProjectTransactionThreshold, ProjectTransactionThresholdOverride, _DefaultThreshold | ||
], | ||
extra_conditions: Sequence[RuleConditionInner], | ||
) -> Sequence[MetricConditionalTaggingRule]: | ||
frustrated: MetricConditionalTaggingRule = { | ||
"condition": { | ||
"op": "and", | ||
"inner": [ | ||
{ | ||
"op": "gt", | ||
"name": _TRANSACTION_METRICS_TO_RULE_FIELD[threshold.metric], | ||
# The frustration threshold is always four times the threshold | ||
# (see https://docs.sentry.io/product/performance/metrics/#apdex) | ||
"value": threshold.threshold * 4, | ||
}, | ||
*extra_conditions, | ||
], | ||
}, | ||
"targetMetrics": list(_SATISFACTION_TARGET_METRICS), | ||
"targetTag": _SATISFACTION_TARGET_TAG, | ||
"tagValue": "frustrated", | ||
} | ||
tolerated: MetricConditionalTaggingRule = { | ||
"condition": { | ||
"op": "and", | ||
"inner": [ | ||
{ | ||
"op": "gt", | ||
"name": _TRANSACTION_METRICS_TO_RULE_FIELD[threshold.metric], | ||
"value": threshold.threshold, | ||
}, | ||
*extra_conditions, | ||
], | ||
}, | ||
"targetMetrics": list(_SATISFACTION_TARGET_METRICS), | ||
"targetTag": _SATISFACTION_TARGET_TAG, | ||
"tagValue": "tolerated", | ||
} | ||
satisfied: MetricConditionalTaggingRule = { | ||
"condition": {"op": "and", "inner": list(extra_conditions)}, | ||
"targetMetrics": list(_SATISFACTION_TARGET_METRICS), | ||
"targetTag": _SATISFACTION_TARGET_TAG, | ||
"tagValue": "satisfied", | ||
} | ||
|
||
return [frustrated, tolerated, satisfied] |
36 changes: 31 additions & 5 deletions
36
...elay/snapshots/test_config/test_project_config_satisfaction_thresholds/False/False.pysnap
This file contains 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 |
---|---|---|
@@ -1,9 +1,35 @@ | ||
--- | ||
created: '2022-02-28T13:20:49.317250Z' | ||
created: '2022-04-19T12:36:49.105349Z' | ||
creator: sentry | ||
source: tests/sentry/relay/test_config.py | ||
--- | ||
projectThreshold: | ||
metric: duration | ||
threshold: 300.0 | ||
transactionThresholds: {} | ||
- condition: | ||
inner: | ||
- name: transaction.duration | ||
op: gt | ||
value: 1200 | ||
op: and | ||
tagValue: frustrated | ||
targetMetrics: | ||
- s:transactions/user@none | ||
- d:transactions/duration@millisecond | ||
targetTag: satisfaction | ||
- condition: | ||
inner: | ||
- name: transaction.duration | ||
op: gt | ||
value: 300 | ||
op: and | ||
tagValue: tolerated | ||
targetMetrics: | ||
- s:transactions/user@none | ||
- d:transactions/duration@millisecond | ||
targetTag: satisfaction | ||
- condition: | ||
inner: [] | ||
op: and | ||
tagValue: satisfied | ||
targetMetrics: | ||
- s:transactions/user@none | ||
- d:transactions/duration@millisecond | ||
targetTag: satisfaction |
36 changes: 31 additions & 5 deletions
36
...relay/snapshots/test_config/test_project_config_satisfaction_thresholds/False/True.pysnap
This file contains 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 |
---|---|---|
@@ -1,9 +1,35 @@ | ||
--- | ||
created: '2022-02-28T13:20:49.410307Z' | ||
created: '2022-04-19T12:33:09.203815Z' | ||
creator: sentry | ||
source: tests/sentry/relay/test_config.py | ||
--- | ||
projectThreshold: | ||
metric: lcp | ||
threshold: 500 | ||
transactionThresholds: {} | ||
- condition: | ||
inner: | ||
- name: transaction.measurements.lcp | ||
op: gt | ||
value: 2000 | ||
op: and | ||
tagValue: frustrated | ||
targetMetrics: | ||
- s:transactions/user@none | ||
- d:transactions/duration@millisecond | ||
targetTag: satisfaction | ||
- condition: | ||
inner: | ||
- name: transaction.measurements.lcp | ||
op: gt | ||
value: 500 | ||
op: and | ||
tagValue: tolerated | ||
targetMetrics: | ||
- s:transactions/user@none | ||
- d:transactions/duration@millisecond | ||
targetTag: satisfaction | ||
- condition: | ||
inner: [] | ||
op: and | ||
tagValue: satisfied | ||
targetMetrics: | ||
- s:transactions/user@none | ||
- d:transactions/duration@millisecond | ||
targetTag: satisfaction |
Oops, something went wrong.