Skip to content

Commit

Permalink
ref(relay): Make cardinality limits more configurable (#68158)
Browse files Browse the repository at this point in the history
Makes cardinality limits for Relay more configurable, to be able to test
and roll them out more easily.

`drop` from the sessions config was removed in
getsentry/relay#3271 - required for the librelay
update to work
  • Loading branch information
Dav1dde committed Apr 3, 2024
1 parent b34eaf0 commit a245c42
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 9 deletions.
2 changes: 1 addition & 1 deletion requirements-base.txt
Expand Up @@ -65,7 +65,7 @@ sentry-arroyo>=2.16.5
sentry-kafka-schemas>=0.1.65
sentry-ophio==0.2.6
sentry-redis-tools>=0.1.7
sentry-relay>=0.8.55
sentry-relay>=0.8.56
sentry-sdk>=1.44.0
snuba-sdk>=2.0.31
simplejson>=3.17.6
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev-frozen.txt
Expand Up @@ -179,7 +179,7 @@ sentry-forked-djangorestframework-stubs==3.14.5.post1
sentry-kafka-schemas==0.1.65
sentry-ophio==0.2.6
sentry-redis-tools==0.1.7
sentry-relay==0.8.55
sentry-relay==0.8.56
sentry-sdk==1.44.0
sentry-usage-accountant==0.0.10
simplejson==3.17.6
Expand Down
2 changes: 1 addition & 1 deletion requirements-frozen.txt
Expand Up @@ -121,7 +121,7 @@ sentry-arroyo==2.16.5
sentry-kafka-schemas==0.1.65
sentry-ophio==0.2.6
sentry-redis-tools==0.1.7
sentry-relay==0.8.55
sentry-relay==0.8.56
sentry-sdk==1.44.0
sentry-usage-accountant==0.0.10
simplejson==3.17.6
Expand Down
9 changes: 9 additions & 0 deletions src/sentry/options/defaults.py
Expand Up @@ -994,6 +994,15 @@
register(
"relay.cardinality-limiter.error-sample-rate", default=0.01, flags=FLAG_AUTOMATOR_MODIFIABLE
)
# List of additional cardinality limits and selectors.
#
# ```
# {
# "rollout_rate": 0.001,
# "limit": { .. Cardinality Limit .. }
# }
# ```
register("relay.cardinality-limiter.limits", default=[], flags=FLAG_AUTOMATOR_MODIFIABLE)

# Controls the encoding used in Relay for encoding distributions and sets
# when writing to Kafka.
Expand Down
20 changes: 17 additions & 3 deletions src/sentry/relay/config/__init__.py
Expand Up @@ -248,6 +248,11 @@ class CardinalityLimit(TypedDict):
namespace: str | None


class CardinalityLimitOption(TypedDict):
rollout_rate: NotRequired[float]
limit: CardinalityLimit


def get_metrics_config(timeout: TimeChecker, project: Project) -> Mapping[str, Any] | None:
metrics_config = {}

Expand Down Expand Up @@ -280,6 +285,18 @@ def get_metrics_config(timeout: TimeChecker, project: Project) -> Mapping[str, A
if id in passive_limits:
limit["passive"] = True
cardinality_limits.append(limit)

clos: list[CardinalityLimitOption] = options.get("relay.cardinality-limiter.limits")
for clo in clos:
rollout_rate = clo.get("rollout_rate", 1.0)
if (project.organization.id % 100000) / 100000 >= rollout_rate:
continue

try:
cardinality_limits.append(clo["limit"])
except KeyError:
pass

metrics_config["cardinalityLimits"] = cardinality_limits

if features.has("organizations:metrics-blocking", project.organization):
Expand Down Expand Up @@ -828,9 +845,6 @@ def _get_project_config(
if _should_extract_abnormal_mechanism(project)
else EXTRACT_METRICS_VERSION
),
"drop": features.has(
"organizations:release-health-drop-sessions", project.organization
),
}

performance_score_profiles = [
Expand Down
3 changes: 1 addition & 2 deletions tests/sentry/api/endpoints/test_relay_projectconfigs_v2.py
Expand Up @@ -357,7 +357,7 @@ def test_session_metrics_extraction(call_endpoint, task_runner, drop_sessions):

for config in result["configs"].values():
config = config["config"]
assert config["sessionMetrics"] == {"version": 1, "drop": drop_sessions}
assert config["sessionMetrics"] == {"version": 1}


@django_db_all
Expand All @@ -378,5 +378,4 @@ def test_session_metrics_abnormal_mechanism_tag_extraction(
config = config["config"]
assert config["sessionMetrics"] == {
"version": 2 if abnormal_mechanism_rollout else 1,
"drop": False,
}
Expand Up @@ -37,3 +37,10 @@ cardinalityLimits:
window:
granularitySeconds: 600
windowSeconds: 3600
- id: test2
limit: 80
report: true
scope: name
window:
granularitySeconds: 800
windowSeconds: 8000
Expand Up @@ -42,3 +42,10 @@ cardinalityLimits:
window:
granularitySeconds: 600
windowSeconds: 3600
- id: test2
limit: 80
report: true
scope: name
window:
granularitySeconds: 800
windowSeconds: 8000
23 changes: 22 additions & 1 deletion tests/sentry/relay/test_config.py
Expand Up @@ -497,7 +497,6 @@ def test_project_config_with_organizations_metrics_extraction(
session_metrics = get_path(cfg, "config", "sessionMetrics")
if has_metrics_extraction:
assert session_metrics == {
"drop": False,
"version": 2 if abnormal_mechanism_rollout else 1,
}
else:
Expand Down Expand Up @@ -957,6 +956,28 @@ def test_project_config_cardinality_limits(default_project, insta_snapshot, pass
]
}

options["relay.cardinality-limiter.limits"] = [
{
"rollout_rate": 0,
"limit": {
"id": "test1",
"window": {"windowSeconds": 7000, "granularitySeconds": 700},
"limit": 70,
"scope": "name",
},
},
{
"rollout_rate": 1,
"limit": {
"id": "test2",
"window": {"windowSeconds": 8000, "granularitySeconds": 800},
"limit": 80,
"scope": "name",
"report": True,
},
},
]

features = Feature({"organizations:relay-cardinality-limiter": True})

with override_options(options), features:
Expand Down

0 comments on commit a245c42

Please sign in to comment.