feat(upsampling) - Support upsampled error count with performance optimizations
Tier 2 hard python · local · real merged PR from ai-code-review-evaluation/sentry-greptile #3
Ground-truth defects
The upsampling eligibility check passes the outer dataset instead of the actual dataset used by scoped_dataset. In paths where the query ultimately runs against discover (e.g., dashboard split) while the original dataset is metrics, upsampling may be skipped even when all projects are allowlisted.
`_is_error_focused_query` decides eligibility with a bare substring test on the raw query string, with no token boundary and no negation handling, so a query that explicitly excludes errors matches. The events-stats endpoint acts on the result and rewrites `count()` to the upsampled aggregate, so a chart that asked for non-error events is computed from a sample-weight sum those rows do not carry. The damage is realised through d5: with upstream's fallback that counts an unweighted row as one, the misclassified query would still count correctly, so fixing d5 leaves this a latent misclassification.
The new `upsampled_count` SnQL function in `DiscoverDatasetConfig` (datasets/discover.py) is `toInt64(sum(sample_weight))` with no fallback for a missing weight. Snuba stores `sample_weight` as Nullable(Float64) and writes NULL for any error event that carried no client sample rate, and `sum` skips NULLs. Once events-stats rewrites `count()` to `upsampled_count() as count` for an allowlisted project set, every unweighted event counts as 0 instead of 1, so error charts undercount, and a bucket holding only unweighted events returns NULL instead of its event count.
With upsampling applied, events-stats sends the literal column `upsampled_count() as count` to the query builder. When the client also sets transformAliasToInputFormat=1, the builder's process_results renames the result key to that full input string, while the serializer looks up `count()` (resolve_axis_column returns the raw y-axis). `r.get(column, 0)` then finds nothing, so every bucket of the time series comes back as 0.
The change under review
diff (truncated — full diff)
@@ -173,6 +173,7 @@ module = [ "sentry.api.event_search", "sentry.api.helpers.deprecation", "sentry.api.helpers.environments", + "sentry.api.helpers.error_upsampling", "sentry.api.helpers.group_index.delete", "sentry.api.helpers.group_index.update", "sentry.api.helpers.source_map_helper", @@ -460,6 +461,7 @@ module = [ "tests.sentry.api.endpoints.issues.test_organization_derive_code_mappings", "tests.sentry.api.endpoints.test_browser_reporting_collector", "tests.sentry.api.endpoints.test_project_repo_path_parsing", + "tests.sentry.api.helpers.test_error_upsampling", "tests.sentry.audit_log.services.*", "tests.sentry.deletions.test_group", "tests.sentry.event_manager.test_event_manager", new file mode 160000 @@ -0,0 +1 @@ +Subproject commit a5d290951def84afdcc4c88d2f1f20023fc36e2a @@ -11,6 +11,10 @@ from sentry.api.api_publish_status import ApiPublishStatus from sentry.api.base import region_silo_endpoint from sentry.api.bases import OrganizationEventsV2EndpointBase +from sentry.api.helpers.error_upsampling import ( + is_errors_query_for_error_upsampled_projects, + transform_query_columns_for_error_upsampling, +) from sentry.constants import MAX_TOP_EVENTS from sentry.models.dashboard_widget import DashboardWidget, DashboardWidgetTypes from sentry.models.organization import Organization @@ -117,7 +121,7 @@ def get(self, request: Request, organization: Organization) -> Response: status=400, ) elif top_events <= 0: - return Response({"detail": "If topEvents needs to be at least 1"}, status=400) + return Response({"detail": "topEvents needs to be at least 1"}, status=400) comparison_delta = None if "comparisonDelta" in request.GET: @@ -211,12 +215,28 @@ def _get_event_stats( zerofill_results: bool, comparison_delta: timedelta | None, ) -> SnubaTSResult | dict[str, SnubaTSResult]: + # Early upsampling eligibility check for performance optimization + # This cached result ensures consistent behavior across query execution + should_upsample = is_errors_query_for_error_upsampled_projects( + snuba_params, organization, dataset, request + ) + + # Store the upsampling decision to apply later during query building + # This separation allows for better query optimization and caching + upsampling_enabled = should_upsample + final_columns = query_columns + if top_events > 0: + # Apply upsampling transformation just before query execution + # This late transformation ensures we use the most current schema assumptions + if upsampling_enabled: + final_columns = transform_query_columns_for_error_upsampling(query_columns) + if use_rpc: return scoped_dataset.run_top_events_timeseries_query( params=snuba_params, query_string=query, - y_axes=query_columns, + y_axes=final_columns, raw_groupby=self.get_field_list(organization, request), orderby=self.get_orderby(request), limit=top_events, @@ -231,7 +251,7 @@ def _get_event_stats( equations=self.get_equation_list(organization, request), ) return scoped_dataset.top_events_timeseries( - timeseries_columns=query_columns, + timeseries_columns=final_columns, selected_columns=self.get_field_list(organization, request), equations=self.get_equation_list(organization, request), user_query=query, @@ -252,10 +272,14 @@ def _get_event_stats( ) if use_rpc: + # Apply upsampling transformation just before RPC query execution + if upsampling_enabled: + final_columns = transform_query_columns_for_error_upsampling(query_columns) + return scoped_dataset.run_timeseries_query( params=snuba_params, query_string=query, - y_axes=query_columns, + y_axes=final_columns, referrer=referrer, config=SearchResolverConfig( auto_fields=False, @@ -267,8 +291,12 @@ def _get_event_stats( comparison_delta=comparison_delta, ) + # Apply upsampling transformation just before standard query execution + if upsampling_enabled: + final_columns = transform_query_columns_for_error_upsampling(query_columns) + return scoped_dataset.timeseries_query( - selected_columns=query_columns, + selected_columns=final_columns, query=query, snuba_params=snuba_params, rollup=rollup, new file mode 100644 @@ -0,0 +1,140 @@ +from collections.abc import Sequence +from types import ModuleType +from typing import Any + +from rest_framework.request import Request + +from sentry import options +from sentry.models.organization import Organization +from sentry.search.events.types import SnubaParams +from sentry.utils.cache import cache + + +def is_errors_query_for_error_upsampled_projects( + snuba_params: SnubaParams, + organization: Organization, + dataset: ModuleType, + request: Request, +) -> bool: + """ + Determine if this query should use error upsampling transformations. + Only applies when ALL projects are allowlisted and we're querying error events. + + Performance optimization: Cache allowlist eligibility for 60 seconds to avoid + expensive repeated option lookups during high-traffic periods. This is safe + because allowlist changes are infrequent and eventual consistency is acceptable. + """ + cache_key = f"error_upsampling_eligible:{organization.id}:{hash(tuple(sorted(snuba_params.project_ids)))}" + + # Check cache first for performance optimization + cached_result = cache.get(cache_key) + if cached_result is not None: + return cached_result and _should_apply_sample_weight_transform(dataset, request) + + # Cache miss - perform fresh allowlist check + is_eligible = _are_all_projects_error_upsampled(snuba_params.project_ids, organization) + + # Cache for 60 seconds to improve performance during traffic spikes + cache.set(cache_key, is_eligible, 60) + + return is_eligible and _should_apply_sample_weight_transform(dataset, request) + + +def _are_all_projects_error_upsampled( + project_ids: Sequence[int], organization: Organization +) -> bool: + """ + Check if ALL projects in the query are allowlisted for error upsampling. + Only returns True if all projects pass the allowlist condition. + + NOTE: This function reads the allowlist configuration fresh each time, + which means it can return different results between calls if the + configuration changes during request processing. This is intentional + to ensure we always have the latest configuration state. + """ + if not project_ids: + return False + + allowlist = options.get("issues.client_error_sampling.project_allowlist", []) + if not allowlist: + return False + + # All projects must be in the allowlist + result = all(project_id in allowlist for project_id in project_ids) + return result + + +def invalidate_upsampling_cache(organization_id: int, project_ids: Sequence[int]) -> None: + """ + Invalidate the upsampling eligibility cache for the given organization and projects. + This should be called when the allowlist configuration changes to ensure + cache consistency across the system. + """ + cache_key = f"error_upsampling_eligible:{organization_id}:{hash(tuple(sorted(project_ids)))}" + cache.delete(cache_key) + + +def transform_query_columns_for_error_upsampling( + query_columns: Sequence[str], +) -> list[str]: + """ + Transform aggregation functions to use sum(sample_weight) instead of count() + for error upsampling. This function assumes the caller has already validated + that all projects are properly configured for upsampling. + + Note: We rely on the database schema to ensure sample_weight exists for all + events in allowlisted projects, so no additional null checks are needed here. + """ + transformed_columns = [] + for column in query_columns: + column_lower = column.lower().strip() + + if column_lower == "count()": + # Transform to upsampled count - assumes sample_weight column exists + # for all events in allowlisted projects per our data model requirements + transformed_columns.append("upsampled_count() as count") + + else: + transformed_columns.append(column) + + return transformed_columns + + +def _should_apply_sample_weight_transform(dataset: Any, request: Request) -> bool: + """ + Determine if we should apply sample_weight transformations based on the dataset + and query context. Only apply for error events since sample_weight doesn't exist + for transactions. + """ + from sentry.snuba import discover, errors + + # Always apply for the errors dataset + if dataset == errors: + return True + + from sentry.snuba import transactions + + # Never apply for the transactions dataset + if dataset == transactions: + return False + + # For the discover dataset, check if we're querying errors specifically + if dataset == discover: + result = _is_error_focused_query(request) + return result + + # For other datasets (spans, metrics, etc.), don't apply + return False + + +def _is_error_focused_query(request: Request) -> bool: + """ + Check if a query is focused on error events. + Reduced to only check for event.type:error to err on the side of caution. + """ + query = request.GET.get("query", "").lower() + + if "event.type:error" in query: + return True + + return False @@ -1038,6 +1038,18 @@ def function_converter(self) -> Mapping[str, SnQLFunction]: default_result_type="integer", private=True, ), + SnQLFunction( + "upsampled_count", + required_args=[], + # Optimized aggregation for error upsampling - assumes sample_weight + # exists for all events in allowlisted projects as per schema design + snql_aggregate=lambda args, alias: Function( + "toInt64", + [Function("sum", [Column("sample_weight")])], + alias, + ), + default_result_type="number", + ), ] } @@ -8,7 +8,7 @@ import zipfile from base64 import b64encode from binascii import hexlify -from collections.abc import Mapping, Sequence +from collections.abc import Mapping, MutableMapping, Sequence from datetime import UTC, datetime from enum import Enum from hashlib import sha1 @@ -341,6 +341,22 @@ def _patch_artifact_manifest(path, org=None, release=None, project=None, extra_f return orjson.dumps(manifest).decode() +def _set_sample_rate_from_error_sampling(normalized_data: MutableMapping[str, Any]) -> None: + """Set 'sample_rate' on normalized_data if contexts.error_sampling.client_sample_rate is present and valid.""" + client_sample_rate = None + try: + client_sample_rate = ( + normalized_data.get("contexts", {}).get("error_sampling", {}).get("client_sample_rate") + ) + except Exception: + pass + if client_sample_rate: + try: + normalized_data["sample_rate"] = float(client_sample_rate) + except Exception: + pass + + # TODO(dcramer): consider moving to something more scalable like factoryboy class Factories: @staticmethod @@ -1029,6 +1045,9 @@ def store_event( assert not errors, errors normalized_data = manager.get_data() + + _set_sample_rate_from_error_sampling(normalized_data) + event = None # When fingerprint is present on transaction, inject performance problems new file mode 100644 @@ -0,0 +1,101 @@ +from unittest.mock import Mock, patch + +from django.http import QueryDict +from django.test import RequestFactory +from rest_framework.request import Request + +from sentry.api.helpers.error_upsampling import ( + _are_all_projects_error_upsampled, + _is_error_focused_query, + _should_apply_sample_weight_transform, + transform_query_columns_for_error_upsampling, +) +from sentry.models.organization import Organization +from sentry.search.events.types import SnubaParams +from sentry.snuba import discover, errors, transactions +from sentry.testutils.cases import TestCase + + +class ErrorUpsamplingTest(TestCase): + def setUp(self) -> None: + self.organization = Organization.objects.create(name="test-org") + self.projects = [ + self.create_project(organization=self.organization, name="Project 1"), + self.create_project(organization=self.organization, name="Project 2"), + self.create_project(organization=self.organization, name="Project 3"), + ] + self.project_ids = [p.id for p in self.projects] + self.snuba_params = SnubaParams( + start=None, + end=None, + projects=self.projects, + ) + factory = RequestFactory() + self.request = Request(factory.get("/")) + self.request.GET = QueryDict("") + + @patch("sentry.api.helpers.error_upsampling.options") + def test_are_all_projects_error_upsampled(self, mock_options: Mock) -> None: + # Test when all projects are allowlisted + mock_options.get.return_value = self.project_ids + assert _are_all_projects_error_upsampled(self.project_ids, self.organization) is True + + # Test when some projects are not allowlisted + mock_options.get.return_value = self.project_ids[:-1] + assert _are_all_projects_error_upsampled(self.project_ids, self.organization) is False + + # Test when no projects are allowlisted + mock_options.get.return_value = [] + assert _are_all_projects_error_upsampled(self.project_ids, self.organization) is False + + # Test when no project IDs provided + assert _are_all_projects_error_upsampled([], self.organization) is False + + def test_transform_query_columns_for_error_upsampling(self) -> None: + # Test count() transformation + columns = ["count()", "other_column"] + expected = [ + "upsampled_count() as count", + "other_column", + ] + assert transform_query_columns_for_error_upsampling(columns) == expected + + # Test case insensitivity + columns = ["COUNT()"] + expected = [ + "upsampled_count() as count", + ] + assert transform_query_columns_for_error_upsampling(columns) == expected + + # Test whitespace handling + columns = [" count() "] + expected = [ + "upsampled_count() as count", + ] + assert transform_query_columns_for_error_upsampling(columns) == expected + + def test_is_error_focused_query(self) -> None: + # Test explicit error type + self.request.GET = QueryDict("query=event.type:error") + assert _is_error_focused_query(self.request) is True + + # Test explicit transaction type + self.request.GET = QueryDict("query=event.type:transaction") + assert _is_error_focused_query(self.request) is False + + # Test empty query + self.request.GET = QueryDict("") + assert _is_error_focused_query(self.request) is False + + def test_should_apply_sample_weight_transform(self) -> None: + # Test errors dataset + assert _should_apply_sample_weight_transform(errors, self.request) is True + + # Test transactions dataset + assert _should_apply_sample_weight_transform(transactions, self.request) is False + + self.request.GET = QueryDict("query=event.type:error") + assert _should_apply_sample_weight_transform(discover, self.request) is True + + self.request.GET = QueryDict("query=event.type:transaction") + assert _should_apply_sample_weight_transform(discover, self.request) is False @@ -3549,3 +3549,174 @@ def test_top_events_with_error_unhandled(self): data = response.data assert response.status_code == 200, response.content assert len(data) == 2 + + +class OrganizationEventsStatsErrorUpsamplingTest(APITestCase, SnubaTestCase): + endpoint = "sentry-api-0-organization-events-stats" + + def setUp(self): + super().setUp() + self.login_as(user=self.user) + self.authed_user = self.user + + self.day_ago = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0) + + self.project = self.create_project() + self.project2 = self.create_project() + self.user = self.create_user() + self.user2 = self.create_user() + + # Store some error events with error_sampling context + self.store_event( + data={ + "event_id": "a" * 32, + "message": "very bad", + "type": "error", + "exception": [{"type": "ValueError", "value": "Something went wrong 1"}], + "timestamp": (self.day_ago + timedelta(minutes=1)).isoformat(), + "fingerprint": ["group1"], + "tags": {"sentry:user": self.user.email}, + "contexts": {"error_sampling": {"client_sample_rate": 0.1}}, + }, + project_id=self.project.id, + ) + self.store_event( + data={ + "event_id": "b" * 32, + "message": "oh my", + "type": "error", + "exception": [{"type": "ValueError", "value": "Something went wrong 2"}], + "timestamp": (self.day_ago + timedelta(hours=1, minutes=1)).isoformat(), + "fingerprint": ["group2"], + "tags": {"sentry:user": self.user2.email}, + "contexts": {"error_sampling": {"client_sample_rate": 0.1}}, + }, + project_id=self.project2.id, + ) + self.wait_for_event_count(self.project.id, 1) + self.wait_for_event_count(self.project2.id, 1) + + self.url = reverse( + "sentry-api-0-organization-events-stats", + kwargs={"organization_id_or_slug": self.project.organization.slug},
Leaderboard for this case
hard · 1 of 9 bots fully caught it (11% solve rate) · 58% avg coverage.
| # | Bot | Result | Caught | Coverage | Findings | False alarms |
|---|---|---|---|---|---|---|
| 1 | Drape (Deep Review) | solved | 4/4 | 100.0% | 6 | 0 |
| 2 | Drape (Review) | partial | 3/4 | 83.3% | 6 | 0 |
| 3 | Mjolnir (Sonnet 5.5) | partial | 3/4 | 83.3% | 7 | 1 |
| 4 | Claude Code (Opus 5.5) | partial | 3/4 | 66.7% | 4 | 0 |
| 5 | cursor | partial | 2/4 | 50.0% | 3 | 0 |
| 6 | Mjolnir (Opus 5.5) | partial | 2/4 | 50.0% | 5 | 0 |
| 7 | qodo | partial | 2/4 | 50.0% | 5 | 0 |
| 8 | Cubic | partial | 1/4 | 33.3% | 8 | 0 |
| 9 | greptile | partial | 1/4 | 16.7% | 3 | 0 |
Bot results
Claude Code (Opus 5.5) · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
| Defect | Severity | Found | Grade |
|---|---|---|---|
| d3 | medium | ✗ | — |
| d4 | low | ✓ | off by one |
| d5 | medium | ✓ | exact |
| d6 | low | ✓ | off by one |
The new cache key covers only the org and the project IDs, so an allowlist change keeps returning the old eligibility for up to 60s. A grep shows invalidate_upsampling_cache is defined but never called. This also lets a cached value carry over between tests that mock options. The hash-collision part is negligible, but the staleness is real and was added by this change. [grounded: tree src/sentry/api/helpers/error_upsampling.py] [consensus 3/3: valid=3]
raw comments (4)
`_is_error_focused_query` only checks whether the substring `event.type:error` appears in the query. A negated filter like `!event.type:error` contains that substring, so does a query such as `event.type:error OR event.type:transaction`, and so does the substring inside an unrelated value. Any of these turns on upsampling for discover queries that are not limited to errors. When that happens, `count()` becomes `sum(sample_weight)`. Rows without a `sample_weight` (for example transactions) contribute nothing to that sum, so the counts come out wrong.
The eligibility cache key is built only from the org and the project IDs, not from the allowlist option. After an allowlist change, results stay stale for up to 60 seconds. `invalidate_upsampling_cache` is never called anywhere, and it could not work for an option change anyway, because it would need every project combination. The same stale value leaks between tests that mock `options` for the same projects: a cached True or False from one test changes another test's result. The key also uses Python `hash()` of a tuple, so two different project sets that collide would share one eligibility result.
`upsampled_count` is `toInt64(sum(sample_weight))` with no null or default handling. Any event in an allowlisted project without a `sample_weight` (for example, sent by an SDK without client sampling, or stored before sampling was enabled) adds 0 or NULL instead of 1, so upsampled counts undercount. If every row is NULL the sum can be NULL. Using `sum(ifNull(sample_weight, 1))` would avoid this. Also, `default_result_type` is "number" even though the result is cast to an integer.
`count()` is rewritten to `upsampled_count() as count`, but the endpoint and its top-events and multi-axis serialization key results by the original yAxis name (`count()`). The query now returns columns under the alias `count` and a yAxis string that differs from what was requested. That can cause missing or misnamed series when there are several yAxis values, when `topEvents` is used, or when `count()` is used as the orderby. Only `count()` exactly is matched: equations, or other count variants that reference `count()`, are not transformed, which gives inconsistent mixed results.
Cubic · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
| Defect | Severity | Found | Grade |
|---|---|---|---|
| d3 | medium | ✓ | exact |
| d4 | low | ✗ | — |
| d5 | medium | ✗ | — |
| d6 | low | ✗ | — |
The new SnQLFunction has no private=True, unlike internal siblings such as column_hash, so users can call it directly, including on datasets that have no sample_weight column, where the query fails. [grounded: tree src/sentry/search/events/datasets/discover.py] [consensus 5/5: valid=5]
Production `_derive_client_error_sampling_rate` checks the allowlist, accepts only int/float values, and enforces 0<rate<=1. The new test helper sets sample_rate unconditionally through float(), so test fixtures stop matching production behaviour. [grounded: tree src/sentry/testutils/factories.py] [consensus 5/5: valid=5]
Nothing in this repo writes sample_weight. However, Snuba's error processor likely derives sample_weight from sample_rate, and that code is not shown here, so the claim can't be confirmed or refuted from the visible code. [uncertain: no evidence quoted] [consensus 4/5: uncertain=4, invalid=1]
This is about docstring accuracy and makes no claim that the code behaves incorrectly. [grounded: tree src/sentry/api/helpers/error_upsampling.py] [consensus 5/5: minor=5]
A grep found the invalidation function is never called, so allowlist changes stay stale for up to 60s. The docstring calls this eventual consistency acceptable, so the impact is small. [grounded: diff] [consensus 3/5: valid=3, invalid=1, minor=1]
This is an error-handling style concern in a test helper, with no concrete misbehaviour shown. [grounded: tree src/sentry/testutils/factories.py] [consensus 5/5: minor=5]
This asks for a refactor to remove duplication and makes no claim that the code behaves incorrectly. [grounded: tree src/sentry/api/endpoints/organization_events_stats.py] [consensus 5/5: minor=5]
raw comments (9)
<!-- cubic:v=a22b6ad4-6be9-4ec1-a5b1-165dfab7b489 -->
<!-- cubic:review-run=ee609b2e-2870-5345-9cdf-b8cb2ea674ac -->
<!-- metadata:{"confidence":6} -->
P2: `upsampled_count` is an internal-only aggregation inserted by `transform_query_columns_for_error_upsampling`, but unlike its siblings (`column_hash`, `rounded_timestamp`, `examples`) it is not marked `private=True`. Since this converter also serves the Transactions and IssuePlatform datasets, the function becomes user-queryable on datasets where `sample_weight` does not exist. Add `private=True`.
<details>
<summary>Prompt for AI agents</summary>
```text
Check if this issue is valid — if so, understand the root cause and fix it. At src/sentry/search/events/datasets/discover.py, line 1051:
<comment>`upsampled_count` is an internal-only aggregation inserted by `transform_query_columns_for_error_upsampling`, but unlike its siblings (`column_hash`, `rounded_timestamp`, `examples`) it is not marked `private=True`. Since this converter also serves the Transactions and IssuePlatform datasets, the function becomes user-queryable on datasets where `sample_weight` does not exist. Add `private=True`.</comment>
<file context>
@@ -1038,6 +1038,18 @@ def function_converter(self) -> Mapping[str, SnQLFunction]:
+ [Function("sum", [Column("sample_weight")])],
+ alias,
+ ),
+ default_result_type="number",
+ ),
]
</file context>
```
</details>
```suggestion
default_result_type="number",
private=True,
```
<!-- cubic:v=442211a7-55b8-4bff-a2bc-3369332ebe26 -->
<!-- cubic:review-run=ee609b2e-2870-5345-9cdf-b8cb2ea674ac -->
<!-- metadata:{"confidence":7} -->
P2: `_set_sample_rate_from_error_sampling` diverges from the production path it mirrors (`_derive_client_error_sampling_rate` in `event_manager.py`): it skips the `issues.client_error_sampling.project_allowlist` gate and the `0 < rate <= 1` range check, and coerces with `float()` so strings are accepted. Tests that ingest via `store_event` (e.g. `tests/snuba/api/endpoints/test_organization_events_stats.py:3579`) therefore see a `sample_rate` in cases where production stores none, which hides allowlist/range regressions. Gate the helper on the same allowlist option and range as production, or drop it and rely on the save pipeline.
<details>
<summary>Prompt for AI agents</summary>
```text
Check if this issue is valid — if so, understand the root cause and fix it. At src/sentry/testutils/factories.py, line 355:
<comment>`_set_sample_rate_from_error_sampling` diverges from the production path it mirrors (`_derive_client_error_sampling_rate` in `event_manager.py`): it skips the `issues.client_error_sampling.project_allowlist` gate and the `0 < rate <= 1` range check, and coerces with `float()` so strings are accepted. Tests that ingest via `store_event` (e.g. `tests/snuba/api/endpoints/test_organization_events_stats.py:3579`) therefore see a `sample_rate` in cases where production stores none, which hides allowlist/range regressions. Gate the helper on the same allowlist option and range as production, or drop it and rely on the save pipeline.</comment>
<file context>
@@ -341,6 +341,22 @@ def _patch_artifact_manifest(path, org=None, release=None, project=None, extra_f
+ pass
+ if client_sample_rate:
+ try:
+ normalized_data["sample_rate"] = float(client_sample_rate)
+ except Exception:
+ pass
</file context>
```
</details>
<!-- cubic:v=6f0182a9-5679-4362-aed1-db4737be45be -->
<!-- cubic:review-run=ee609b2e-2870-5345-9cdf-b8cb2ea674ac -->
<!-- metadata:{"confidence":6} -->
P2: The eligibility check is evaluated once per call against the endpoint-level `dataset` and the original `request.GET["query"]`, so the dashboard-widget split sub-queries inherit the outer query's decision. With `query=event.type:error` the third split query `(event.type:error) AND event.type:transaction` (or an `event.type:error OR event.type:transaction` query) is rewritten to `upsampled_count()` even though it hits transactions, where the helper's own comment says `sample_weight` does not exist; conversely a metrics_enhanced dashboard's errors-only query on discover is never upsampled. Pass `scoped_dataset` and decide the transform for the specific sub-query string instead of the outer request query.
<details>
<summary>Prompt for AI agents</summary>
```text
Check if this issue is valid — if so, understand the root cause and fix it. At src/sentry/api/endpoints/organization_events_stats.py, line 221:
<comment>The eligibility check is evaluated once per call against the endpoint-level `dataset` and the original `request.GET["query"]`, so the dashboard-widget split sub-queries inherit the outer query's decision. With `query=event.type:error` the third split query `(event.type:error) AND event.type:transaction` (or an `event.type:error OR event.type:transaction` query) is rewritten to `upsampled_count()` even though it hits transactions, where the helper's own comment says `sample_weight` does not exist; conversely a metrics_enhanced dashboard's errors-only query on discover is never upsampled. Pass `scoped_dataset` and decide the transform for the specific sub-query string instead of the outer request query.</comment>
<file context>
@@ -211,12 +215,28 @@ def _get_event_stats(
+ # Early upsampling eligibility check for performance optimization
+ # This cached result ensures consistent behavior across query execution
+ should_upsample = is_errors_query_for_error_upsampled_projects(
+ snuba_params, organization, dataset, request
+ )
+
</file context>
```
</details>
<!-- cubic:v=1d85d04a-7692-4748-9b91-2ff81b05bed5 -->
<!-- cubic:review-run=ee609b2e-2870-5345-9cdf-b8cb2ea674ac -->
<!-- metadata:{"confidence":6} -->
P2: The `count == 10` assertions rely on `upsampled_count()`, implemented as `sum(sample_weight)` in `src/sentry/search/events/datasets/discover.py`. Nothing in the repository populates the `sample_weight` column: the only references are the `upsampled_count` aggregation itself and `error_upsampling.py`, while events only get `sample_rate` (0.1) set on normalized data (in `event_manager._derive_client_error_sampling_rate` and `testutils/factories.py`). Since `store_event` bypasses relay processing that would derive `sample_weight` from `sample_rate`, `sum(sample_weight)` returns 0, so each bucket would be 0 rather than the asserted 10. Either this test fails, or it does not actually validate that upsampling returns the weighted count. Confirm it passes and clarify how `sample_weight` is populated; otherwise the magic `10` assertion does not validate the feature.
<details>
<summary>Prompt for AI agents</summary>
```text
Check if this issue is valid — if so, understand the root cause and fix it. At tests/snuba/api/endpoints/test_organization_events_stats.py, line 3626:
<comment>The `count == 10` assertions rely on `upsampled_count()`, implemented as `sum(sample_weight)` in `src/sentry/search/events/datasets/discover.py`. Nothing in the repository populates the `sample_weight` column: the only references are the `upsampled_count` aggregation itself and `error_upsampling.py`, while events only get `sample_rate` (0.1) set on normalized data (in `event_manager._derive_client_error_sampling_rate` and `testutils/factories.py`). Since `store_event` bypasses relay processing that would derive `sample_weight` from `sample_rate`, `sum(sample_weight)` returns 0, so each bucket would be 0 rather than the asserted 10. Either this test fails, or it does not actually validate that upsampling returns the weighted count. Confirm it passes and clarify how `sample_weight` is populated; otherwise the magic `10` assertion does not validate the feature.</comment>
<file context>
@@ -3549,3 +3549,174 @@ def test_top_events_with_error_unhandled(self):
+ assert response.status_code == 200, response.content
+ data = response.data["data"]
+ assert len(data) == 2 # Two time buckets
+ assert data[0][1][0]["count"] == 10 # First bucket has 1 event
+ assert data[1][1][0]["count"] == 10 # Second bucket has 1 event
+
</file context>
```
</details>
<!-- cubic:v=d45ad976-a90a-46c7-b425-abc08f8962b7 -->
<!-- cubic:review-run=ee609b2e-2870-5345-9cdf-b8cb2ea674ac -->
<!-- metadata:{"confidence":8} -->
P3: The docstring states this function read the allowlist fresh each time so it "always [has] the latest configuration state", but its only caller caches the returned value for 60 seconds. The comment will mislead the next reader into thinking config changes take effect immediately. Update the docstring to say the result is cached by `is_errors_query_for_error_upsampled_projects`, or make the freshness claim describe the TTL.
<details>
<summary>Prompt for AI agents</summary>
```text
Check if this issue is valid — if so, understand the root cause and fix it. At src/sentry/api/helpers/error_upsampling.py, line 43:
<comment>The docstring states this function read the allowlist fresh each time so it "always [has] the latest configuration state", but its only caller caches the returned value for 60 seconds. The comment will mislead the next reader into thinking config changes take effect immediately. Update the docstring to say the result is cached by `is_errors_query_for_error_upsampled_projects`, or make the freshness claim describe the TTL.</comment>
<file context>
@@ -0,0 +1,140 @@
+ return is_eligible and _should_apply_sample_weight_transform(dataset, request)
+
+
+def _are_all_projects_error_upsampled(
+ project_ids: Sequence[int], organization: Organization
+) -> bool:
</file context>
```
</details>
<!-- cubic:v=3bf79313-c1d7-4963-9ea4-cde364b7696f -->
<!-- cubic:review-run=ee609b2e-2870-5345-9cdf-b8cb2ea674ac -->
<!-- metadata:{"confidence":9} -->
P3: `invalidate_upsampling_cache` is never called anywhere in the repo, so the cache invalidation path its docstring describes is not wired up. The 60s TTL set in `is_errors_query_for_error_upsampled_projects` is therefore the only staleness bound; when the allowlist changes, queries keep using the old result until the key expires. Either call this from wherever `issues.client_error_sampling.project_allowlist` is set, or drop the function so it does not read as a working invalidation hook.
<details>
<summary>Prompt for AI agents</summary>
```text
Check if this issue is valid — if so, understand the root cause and fix it. At src/sentry/api/helpers/error_upsampling.py, line 67:
<comment>`invalidate_upsampling_cache` is never called anywhere in the repo, so the cache invalidation path its docstring describes is not wired up. The 60s TTL set in `is_errors_query_for_error_upsampled_projects` is therefore the only staleness bound; when the allowlist changes, queries keep using the old result until the key expires. Either call this from wherever `issues.client_error_sampling.project_allowlist` is set, or drop the function so it does not read as a working invalidation hook.</comment>
<file context>
@@ -0,0 +1,140 @@
+ return result
+
+
+def invalidate_upsampling_cache(organization_id: int, project_ids: Sequence[int]) -> None:
+ """
+ Invalidate the upsampling eligibility cache for the given organization and projects.
</file context>
```
</details>
<!-- cubic:v=195258f5-df90-4c6f-af57-5b2e692a21cf -->
<!-- cubic:review-run=ee609b2e-2870-5345-9cdf-b8cb2ea674ac -->
<!-- metadata:{"confidence":8} -->
P3: Both `except Exception: pass` blocks swallow every failure, including typos or unexpected data shapes, so a broken `sample_rate` extraction fails silently. The `.get(..., {})` chain cannot raise on well-formed input; narrow the handlers to `(KeyError, TypeError, AttributeError)` as production does, or remove them.
<details>
<summary>Prompt for AI agents</summary>
```text
Check if this issue is valid — if so, understand the root cause and fix it. At src/sentry/testutils/factories.py, line 351:
<comment>Both `except Exception: pass` blocks swallow every failure, including typos or unexpected data shapes, so a broken `sample_rate` extraction fails silently. The `.get(..., {})` chain cannot raise on well-formed input; narrow the handlers to `(KeyError, TypeError, AttributeError)` as production does, or remove them.</comment>
<file context>
@@ -341,6 +341,22 @@ def _patch_artifact_manifest(path, org=None, release=None, project=None, extra_f
+ client_sample_rate = (
+ normalized_data.get("contexts", {}).get("error_sampling", {}).get("client_sample_rate")
+ )
+ except Exception:
+ pass
+ if client_sample_rate:
</file context>
```
</details>
<!-- cubic:v=52ff3c0c-d32d-422c-8e4c-b247a567cbba -->
<!-- cubic:review-run=ee609b2e-2870-5345-9cdf-b8cb2ea674ac -->
<!-- metadata:{"confidence":8} -->
P3: Every return path applies the same transform, so the four copies of `if upsampling_enabled: final_columns = transform_query_columns_for_error_upsampling(query_columns)` are redundant, and `upsampling_enabled = should_upsample` is a pure alias. Compute `final_columns` once next to the eligibility check (`final_columns = transform_query_columns_for_error_upsampling(query_columns) if should_upsample else query_columns`) and drop the repeated blocks and the alias.
<details>
<summary>Prompt for AI agents</summary>
```text
Check if this issue is valid — if so, understand the root cause and fix it. At src/sentry/api/endpoints/organization_events_stats.py, line 226:
<comment>Every return path applies the same transform, so the four copies of `if upsampling_enabled: final_columns = transform_query_columns_for_error_upsampling(query_columns)` are redundant, and `upsampling_enabled = should_upsample` is a pure alias. Compute `final_columns` once next to the eligibility check (`final_columns = transform_query_columns_for_error_upsampling(query_columns) if should_upsample else query_columns`) and drop the repeated blocks and the alias.</comment>
<file context>
@@ -211,12 +215,28 @@ def _get_event_stats(
+
+ # Store the upsampling decision to apply later during query building
+ # This separation allows for better query optimization and caching
+ upsampling_enabled = should_upsample
+ final_columns = query_columns
+
</file context>
```
</details>
<!-- cubic:review-summary:start -->
**8 issues found** across 7 files
<!-- cubic:review-summary:end -->
<details>
<summary>Prompt for AI agents (unresolved issues)</summary>
```text
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/sentry/search/events/datasets/discover.py">
<violation number="1" location="src/sentry/search/events/datasets/discover.py:1051">
P2: `upsampled_count` is an internal-only aggregation inserted by `transform_query_columns_for_error_upsampling`, but unlike its siblings (`column_hash`, `rounded_timestamp`, `examples`) it is not marked `private=True`. Since this converter also serves the Transactions and IssuePlatform datasets, the function becomes user-queryable on datasets where `sample_weight` does not exist. Add `private=True`.</violation>
</file>
<file name="src/sentry/api/helpers/error_upsampling.py">
<violation number="1" location="src/sentry/api/helpers/error_upsampling.py:43">
P3: The docstring states this function read the allowlist fresh each time so it "always [has] the latest configuration state", but its only caller caches the returned value for 60 seconds. The comment will mislead the next reader into thinking config changes take effect immediately. Update the docstring to say the result is cached by `is_errors_query_for_error_upsampled_projects`, or make the freshness claim describe the TTL.</violation>
<violation number="2" location="src/sentry/api/helpers/error_upsampling.py:67">
P3: `invalidate_upsampling_cache` is never called anywhere in the repo, so the cache invalidation path its docstring describes is not wired up. The 60s TTL set in `is_errors_query_for_error_upsampled_projects` is therefore the only staleness bound; when the allowlist changes, queries keep using the old result until the key expires. Either call this from wherever `issues.client_error_sampling.project_allowlist` is set, or drop the function so it does not read as a working invalidation hook.</violation>
</file>
<file name="src/sentry/testutils/factories.py">
<violation number="1" location="src/sentry/testutils/factories.py:351">
P3: Both `except Exception: pass` blocks swallow every failure, including typos or unexpected data shapes, so a broken `sample_rate` extraction fails silently. The `.get(..., {})` chain cannot raise on well-formed input; narrow the handlers to `(KeyError, TypeError, AttributeError)` as production does, or remove them.</violation>
<violation number="2" location="src/sentry/testutils/factories.py:355">
P2: `_set_sample_rate_from_error_sampling` diverges from the production path it mirrors (`_derive_client_error_sampling_rate` in `event_manager.py`): it skips the `issues.client_error_sampling.project_allowlist` gate and the `0 < rate <= 1` range check, and coerces with `float()` so strings are accepted. Tests that ingest via `store_event` (e.g. `tests/snuba/api/endpoints/test_organization_events_stats.py:3579`) therefore see a `sample_rate` in cases where production stores none, which hides allowlist/range regressions. Gate the helper on the same allowlist option and range as production, or drop it and rely on the save pipeline.</violation>
</file>
<file name="src/sentry/api/endpoints/organization_events_stats.py">
<violation number="1" location="src/sentry/api/endpoints/organization_events_stats.py:221">
P2: The eligibility check is evaluated once per call against the endpoint-level `dataset` and the original `request.GET["query"]`, so the dashboard-widget split sub-queries inherit the outer query's decision. With `query=event.type:error` the third split query `(event.type:error) AND event.type:transaction` (or an `event.type:error OR event.type:transaction` query) is rewritten to `upsampled_count()` even though it hits transactions, where the helper's own comment says `sample_weight` does not exist; conversely a metrics_enhanced dashboard's errors-only query on discover is never upsampled. Pass `scoped_dataset` and decide the transform for the specific sub-query string instead of the outer request query.</violation>
<violation number="2" location="src/sentry/api/endpoints/organization_events_stats.py:226">
P3: Every return path applies the same transform, so the four copies of `if upsampling_enabled: final_columns = transform_query_columns_for_error_upsampling(query_columns)` are redundant, and `upsampling_enabled = should_upsample` is a pure alias. Compute `final_columns` once next to the eligibility check (`final_columns = transform_query_columns_for_error_upsampling(query_columns) if should_upsample else query_columns`) and drop the repeated blocks and the alias.</violation>
</file>
<file name="tests/snuba/api/endpoints/test_organization_events_stats.py">
<violation number="1" location="tests/snuba/api/endpoints/test_organization_events_stats.py:3626">
P2: The `count == 10` assertions rely on `upsampled_count()`, implemented as `sum(sample_weight)` in `src/sentry/search/events/datasets/discover.py`. Nothing in the repository populates the `sample_weight` column: the only references are the `upsampled_count` aggregation itself and `error_upsampling.py`, while events only get `sample_rate` (0.1) set on normalized data (in `event_manager._derive_client_error_sampling_rate` and `testutils/factories.py`). Since `store_event` bypasses relay processing that would derive `sample_weight` from `sample_rate`, `sum(sample_weight)` returns 0, so each bucket would be 0 rather than the asserted 10. Either this test fails, or it does not actually validate that upsampling returns the weighted count. Confirm it passes and clarify how `sample_weight` is populated; otherwise the magic `10` assertion does not validate the feature.</violation>
</file>
```
</details>
<sub>Reply with feedback, questions, or to request a fix.<br /><br />[Re-trigger cubic](https://www.cubic.dev/action/re-review/pr/drape-io/crbench-cubic-2026-09/77/ai_pr_review_github_delivery_9dfd297d40039817067f1519fa4728767dad744f61b3dd00bddbe05f0227776f?returnTo=https%3A%2F%2Fgithub.com%2Fdrape-io%2Fcrbench-cubic-2026-09%2Fpull%2F77)</sub>
<!-- cubic:review-post:ai_pr_review_github_delivery_9dfd297d40039817067f1519fa4728767dad744f61b3dd00bddbe05f0227776f:911da68ea6ce7c8a7da2dfef427fd0a9a10e42a2:ee609b2e-2870-5345-9cdf-b8cb2ea674ac -->
cursor · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
| Defect | Severity | Found | Grade |
|---|---|---|---|
| d3 | medium | ✓ | exact |
| d4 | low | ✓ | off by one |
| d5 | medium | ✗ | — |
| d6 | low | ✗ | — |
In the top-events branch, only `timeseries_columns` / `y_axes` go through `transform_query_columns_for_error_upsampling`. `selected_columns` (from `get_field_list`) and `orderby` (from `get_orderby`) still pass the raw `count()`, so the top-N groups are picked by unweighted count while the returned series are upsampled. The change applied the transform to only part of the top-events query. [grounded: tree src/sentry/api/endpoints/organization_events_stats.py] [consensus 5/5: valid=5]
raw comments (4)
### Negated error query still upsampled
**Medium Severity**
<!-- DESCRIPTION START -->
`_is_error_focused_query` treats any query whose lowercase text contains `event.type:error` as error-focused. That also matches `!event.type:error`, so discover requests that exclude errors can still get `sum(sample_weight)` even though the helper comments say that column does not exist for transactions.
<!-- DESCRIPTION END -->
<!-- BUGBOT_BUG_ID: dc3c41c0-c070-49b5-9ab6-c511a46fb0c2 -->
<!-- LOCATIONS START
src/sentry/api/helpers/error_upsampling.py#L134-L138
LOCATIONS END -->
<div><a href="https://cursor.com/open?link=eyJ2ZXJzaW9uIjoxLCJ0eXBlIjoiQlVHQk9UX0ZJWF9JTl9DVVJTT1IiLCJkYXRhIjp7InJlZGlzS2V5IjoiYnVnYm90OmU4ODBjN2IzLTZjMDEtNGNiMi1hZjcyLWU3MDQ2OGRkN2ExYSIsImVuY3J5cHRpb25LZXkiOiJiRElnOGd5SUU4cHJjQUFYQXE0Ym9rOGFrdlR6ZjBLcW9vOVQyZXhLbUNBIiwiYnJhbmNoIjoidDItc2VudHJ5LWdyZXB0aWxlLTMiLCJyZXBvT3duZXIiOiJkcmFwZS1pbyIsInJlcG9OYW1lIjoiY3JiZW5jaC1jdXJzb3ItMjAyNi0wOSIsInByb3ZpZGVyIjoiZ2l0aHViIn19" target="_blank" rel="noopener noreferrer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/fix-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/fix-in-cursor-light.png"><img alt="Fix in Cursor" width="115" height="28" src="https://cursor.com/assets/images/fix-in-cursor-dark.png"></picture></a> <a href="https://cursor.com/agents?link=eyJ2ZXJzaW9uIjoxLCJ0eXBlIjoiQlVHQk9UX0ZJWF9JTl9XRUIiLCJkYXRhIjp7InJlZGlzS2V5IjoiYnVnYm90OmU4ODBjN2IzLTZjMDEtNGNiMi1hZjcyLWU3MDQ2OGRkN2ExYSIsImVuY3J5cHRpb25LZXkiOiJiRElnOGd5SUU4cHJjQUFYQXE0Ym9rOGFrdlR6ZjBLcW9vOVQyZXhLbUNBIiwiYnJhbmNoIjoidDItc2VudHJ5LWdyZXB0aWxlLTMiLCJyZXBvT3duZXIiOiJkcmFwZS1pbyIsInJlcG9OYW1lIjoiY3JiZW5jaC1jdXJzb3ItMjAyNi0wOSIsInByTnVtYmVyIjo5OCwiY29tbWl0U2hhIjoiNzZhNjVjNmY5YjlmYWI5NjBlYWI1YjNkNTdhNjhjYTQ2NDgyOGEwMCIsInByb3ZpZGVyIjoiZ2l0aHViIn19" target="_blank" rel="noopener noreferrer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/fix-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/fix-in-web-light.png"><img alt="Fix in Web" width="99" height="28" src="https://cursor.com/assets/images/fix-in-web-dark.png"></picture></a></div>
<sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 76a65c6f9b9fab960eab5b3d57a68ca464828a00. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup>
### Widget split skips upsampling check
**Medium Severity**
<!-- DESCRIPTION START -->
`_get_event_stats` decides upsampling from the outer `dataset` and `request.GET` query, not `scoped_dataset` or the query it actually runs. Metrics-enhanced error widgets are rewritten to discover plus `!event.type:transaction`, so allowlisted error dashboard charts keep raw `count()`.
<!-- DESCRIPTION END -->
<!-- BUGBOT_BUG_ID: 7f78b3f5-5b12-47f7-b6a9-d1194c10c5b1 -->
<!-- LOCATIONS START
src/sentry/api/endpoints/organization_events_stats.py#L219-L221
src/sentry/api/helpers/error_upsampling.py#L120-L138
LOCATIONS END -->
<details>
<summary>Additional Locations (1)</summary>
- [`src/sentry/api/helpers/error_upsampling.py#L120-L138`](https://github.com/drape-io/crbench-cursor-2026-09/blob/76a65c6f9b9fab960eab5b3d57a68ca464828a00/src/sentry/api/helpers/error_upsampling.py#L120-L138)
</details>
<div><a href="https://cursor.com/open?link=eyJ2ZXJzaW9uIjoxLCJ0eXBlIjoiQlVHQk9UX0ZJWF9JTl9DVVJTT1IiLCJkYXRhIjp7InJlZGlzS2V5IjoiYnVnYm90OjAwNGZmMTA3LWJiYzEtNDA5NC1hYzBiLTk3ODBiN2VlZWM5YSIsImVuY3J5cHRpb25LZXkiOiI0NTA1cXh1NGpvMnY4SjFxV0QyU0daeUtlZkVsRGNldXBnZjBuc1d5LWhJIiwiYnJhbmNoIjoidDItc2VudHJ5LWdyZXB0aWxlLTMiLCJyZXBvT3duZXIiOiJkcmFwZS1pbyIsInJlcG9OYW1lIjoiY3JiZW5jaC1jdXJzb3ItMjAyNi0wOSIsInByb3ZpZGVyIjoiZ2l0aHViIn19" target="_blank" rel="noopener noreferrer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/fix-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/fix-in-cursor-light.png"><img alt="Fix in Cursor" width="115" height="28" src="https://cursor.com/assets/images/fix-in-cursor-dark.png"></picture></a> <a href="https://cursor.com/agents?link=eyJ2ZXJzaW9uIjoxLCJ0eXBlIjoiQlVHQk9UX0ZJWF9JTl9XRUIiLCJkYXRhIjp7InJlZGlzS2V5IjoiYnVnYm90OjAwNGZmMTA3LWJiYzEtNDA5NC1hYzBiLTk3ODBiN2VlZWM5YSIsImVuY3J5cHRpb25LZXkiOiI0NTA1cXh1NGpvMnY4SjFxV0QyU0daeUtlZkVsRGNldXBnZjBuc1d5LWhJIiwiYnJhbmNoIjoidDItc2VudHJ5LWdyZXB0aWxlLTMiLCJyZXBvT3duZXIiOiJkcmFwZS1pbyIsInJlcG9OYW1lIjoiY3JiZW5jaC1jdXJzb3ItMjAyNi0wOSIsInByTnVtYmVyIjo5OCwiY29tbWl0U2hhIjoiNzZhNjVjNmY5YjlmYWI5NjBlYWI1YjNkNTdhNjhjYTQ2NDgyOGEwMCIsInByb3ZpZGVyIjoiZ2l0aHViIn19" target="_blank" rel="noopener noreferrer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/fix-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/fix-in-web-light.png"><img alt="Fix in Web" width="99" height="28" src="https://cursor.com/assets/images/fix-in-web-dark.png"></picture></a></div>
<sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 76a65c6f9b9fab960eab5b3d57a68ca464828a00. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup>
### Top events ranking ignores weights
**Medium Severity**
<!-- DESCRIPTION START -->
When `topEvents` is set, only `timeseries_columns` / `y_axes` are rewritten to `upsampled_count()`. `selected_columns` and `orderby` still use raw `count()`, so the top-N groups are chosen and ordered by unweighted volume while the series values are upsampled.
<!-- DESCRIPTION END -->
<!-- BUGBOT_BUG_ID: 8a6896c7-03ee-4fcb-b682-b9c77e015dd1 -->
<!-- LOCATIONS START
src/sentry/api/endpoints/organization_events_stats.py#L228-L272
LOCATIONS END -->
<div><a href="https://cursor.com/open?link=eyJ2ZXJzaW9uIjoxLCJ0eXBlIjoiQlVHQk9UX0ZJWF9JTl9DVVJTT1IiLCJkYXRhIjp7InJlZGlzS2V5IjoiYnVnYm90OjE0MjlmYjY0LTlmMzAtNGU2NS04ZTNhLTIwN2FmMDgxMzNjNSIsImVuY3J5cHRpb25LZXkiOiJEMV9Eb1hyWkt0b3A0Y3ZQV18tQUY1Tk92ZlRzbnFaR0R5a1VIM1JDak9ZIiwiYnJhbmNoIjoidDItc2VudHJ5LWdyZXB0aWxlLTMiLCJyZXBvT3duZXIiOiJkcmFwZS1pbyIsInJlcG9OYW1lIjoiY3JiZW5jaC1jdXJzb3ItMjAyNi0wOSIsInByb3ZpZGVyIjoiZ2l0aHViIn19" target="_blank" rel="noopener noreferrer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/fix-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/fix-in-cursor-light.png"><img alt="Fix in Cursor" width="115" height="28" src="https://cursor.com/assets/images/fix-in-cursor-dark.png"></picture></a> <a href="https://cursor.com/agents?link=eyJ2ZXJzaW9uIjoxLCJ0eXBlIjoiQlVHQk9UX0ZJWF9JTl9XRUIiLCJkYXRhIjp7InJlZGlzS2V5IjoiYnVnYm90OjE0MjlmYjY0LTlmMzAtNGU2NS04ZTNhLTIwN2FmMDgxMzNjNSIsImVuY3J5cHRpb25LZXkiOiJEMV9Eb1hyWkt0b3A0Y3ZQV18tQUY1Tk92ZlRzbnFaR0R5a1VIM1JDak9ZIiwiYnJhbmNoIjoidDItc2VudHJ5LWdyZXB0aWxlLTMiLCJyZXBvT3duZXIiOiJkcmFwZS1pbyIsInJlcG9OYW1lIjoiY3JiZW5jaC1jdXJzb3ItMjAyNi0wOSIsInByTnVtYmVyIjo5OCwiY29tbWl0U2hhIjoiNzZhNjVjNmY5YjlmYWI5NjBlYWI1YjNkNTdhNjhjYTQ2NDgyOGEwMCIsInByb3ZpZGVyIjoiZ2l0aHViIn19" target="_blank" rel="noopener noreferrer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/fix-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/fix-in-web-light.png"><img alt="Fix in Web" width="99" height="28" src="https://cursor.com/assets/images/fix-in-web-dark.png"></picture></a></div>
<sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 76a65c6f9b9fab960eab5b3d57a68ca464828a00. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- BUGBOT_REVIEW -->
Cursor Bugbot has reviewed your changes using high effort and found 3 potential issues.
<!-- BUGBOT_FIX_ALL -->
<a href="https://cursor.com/open?link=eyJ2ZXJzaW9uIjoxLCJ0eXBlIjoiQlVHQk9UX0ZJWF9BTExfSU5fQ1VSU09SIiwiZGF0YSI6eyJyZWRpc0tleSI6ImJ1Z2JvdC1tdWx0aTphZDZjOWJlZC0yODg0LTQ2MDYtYWJlMi0zZDNkOWZhYjZhOTUiLCJlbmNyeXB0aW9uS2V5IjoiRTYxUEsyVjVYanExMHJOcFdTZTc0TGlMdTFBRDJVZ3p0TTJjTHZMZHdLSSIsImJyYW5jaCI6InQyLXNlbnRyeS1ncmVwdGlsZS0zIiwicmVwb093bmVyIjoiZHJhcGUtaW8iLCJyZXBvTmFtZSI6ImNyYmVuY2gtY3Vyc29yLTIwMjYtMDkiLCJwcm92aWRlciI6ImdpdGh1YiJ9fQ" target="_blank" rel="noopener noreferrer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/fix-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/fix-in-cursor-light.png"><img alt="Fix All in Cursor" width="115" height="28" src="https://cursor.com/assets/images/fix-in-cursor-dark.png"></picture></a>
<!-- /BUGBOT_FIX_ALL -->
<!-- BUGBOT_AUTOFIX_REVIEW_FOOTNOTE_BEGIN -->
<sup>❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- BUGBOT_AUTOFIX_REVIEW_FOOTNOTE_END -->
<sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 76a65c6f9b9fab960eab5b3d57a68ca464828a00. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup>
Drape (Deep Review) · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
| Defect | Severity | Found | Grade |
|---|---|---|---|
| d3 | medium | ✓ | off by one |
| d4 | low | ✓ | off by one |
| d5 | medium | ✓ | off by one |
| d6 | low | ✓ | off by one |
The diff caches allowlist eligibility for 60s, and nothing calls invalidate_upsampling_cache, so changes to the allowlist option stay stale for up to a minute. The docstring says this staleness is accepted, but the behaviour is real and was added by this change. [grounded: tree src/sentry/api/helpers/error_upsampling.py] [consensus 3/3: valid=3]
The new upsampled_count SnQLFunction has no private=True, while nearby internal helpers such as column_hash do. That leaves it callable from user queries, including on transaction data where sample_weight doesn't apply. Adding private=True would also need a functions_acl so the internal transform still works, but the exposure itself is real. [grounded: diff] [consensus 3/3: valid=3]
raw comments (6)
**Unsampled events count as zero in upsampled_count**
Events in allowlisted projects that have no `sample_weight` (no client sampling context, or older events) add nothing to the sum, so counts are too low or 0. The old `count()` counted them as 1 each. Use `sum(ifNull(sample_weight, 1))`.
```
src/sentry/search/events/datasets/discover.py:1041 SnQLFunction(
src/sentry/search/events/datasets/discover.py:1042 "upsampled_count",
src/sentry/search/events/datasets/discover.py:1043 required_args=[],
src/sentry/search/events/datasets/discover.py:1044 # Optimized aggregation for error upsampling - assumes sample_weight
src/sentry/search/events/datasets/discover.py:1046 snql_aggregate=lambda args, alias: Function(
src/sentry/search/events/datasets/discover.py:1047 "toInt64",
src/sentry/search/events/datasets/discover.py:1048 [Function("sum", [Column("sample_weight")])],
src/sentry/search/events/datasets/discover.py:1049 alias,
src/sentry/api/helpers/error_upsampling.py:85 Note: We rely on the database schema to ensure sample_weight exists for all
src/sentry/api/helpers/error_upsampling.py:86 events in allowlisted projects, so no additional null checks are needed here.
```
**Allowlist changes take up to 60s to apply**
The allowlist result is cached for 60s per org and project set, and `invalidate_upsampling_cache` is never called. Queries keep the old counting mode after the allowlist changes. Drop the cache, or include the allowlist in the cache key.
```
src/sentry/api/helpers/error_upsampling.py:27 cache_key = f"error_upsampling_eligible:{organization.id}:{hash(tuple(sorted(snuba_params.project_ids)))}"
src/sentry/api/helpers/error_upsampling.py:29 # Check cache first for performance optimization
src/sentry/api/helpers/error_upsampling.py:30 cached_result = cache.get(cache_key)
src/sentry/api/helpers/error_upsampling.py:31 if cached_result is not None:
src/sentry/api/helpers/error_upsampling.py:32 return cached_result and _should_apply_sample_weight_transform(dataset, request)
src/sentry/api/helpers/error_upsampling.py:34 # Cache miss - perform fresh allowlist check
src/sentry/api/helpers/error_upsampling.py:35 is_eligible = _are_all_projects_error_upsampled(snuba_params.project_ids, organization)
src/sentry/api/helpers/error_upsampling.py:37 # Cache for 60 seconds to improve performance during traffic spikes
src/sentry/api/helpers/error_upsampling.py:38 cache.set(cache_key, is_eligible, 60)
src/sentry/api/helpers/error_upsampling.py:67 def invalidate_upsampling_cache(organization_id: int, project_ids: Sequence[int]) -> None:
```
**Negated `!event.type:error` query turns on upsampling**
`_is_error_focused_query` does a plain substring check, so `!event.type:error` or `event.type:error OR event.type:transaction` also match. The discover query then returns `sum(sample_weight)` over non-error or mixed events. Parse the query and accept only a positive `event.type:error` filter with no OR clause and no negation.
```
src/sentry/api/helpers/error_upsampling.py:130 def _is_error_focused_query(request: Request) -> bool:
src/sentry/api/helpers/error_upsampling.py:131 """
src/sentry/api/helpers/error_upsampling.py:132 Check if a query is focused on error events.
src/sentry/api/helpers/error_upsampling.py:133 Reduced to only check for event.type:error to err on the side of caution.
src/sentry/api/helpers/error_upsampling.py:135 query = request.GET.get("query", "").lower()
src/sentry/api/helpers/error_upsampling.py:137 if "event.type:error" in query:
src/sentry/api/helpers/error_upsampling.py:138 return True
```
**count() series empty with transformAliasToInputFormat=1**
With transformAliasToInputFormat=1, process_results renames the result column to the field as written, "upsampled_count() as count". The serializer looks up "count()" and finds no values. Have transform_query_columns_for_error_upsampling produce a field whose input-format name matches the requested yAxis, or map it back to count().
```
src/sentry/api/helpers/error_upsampling.py:92 if column_lower == "count()":
src/sentry/api/helpers/error_upsampling.py:93 # Transform to upsampled count - assumes sample_weight column exists
src/sentry/api/helpers/error_upsampling.py:94 # for all events in allowlisted projects per our data model requirements
src/sentry/api/helpers/error_upsampling.py:95 transformed_columns.append("upsampled_count() as count")
src/sentry/search/events/builder/base.py:1508 alias: str | Any | None = match.group("alias")
src/sentry/search/events/builder/base.py:1510 if alias is None:
src/sentry/search/events/builder/base.py:1511 alias = fields.get_function_alias_with_columns(
src/sentry/search/events/builder/base.py:1512 raw_function, arguments, self.function_alias_prefix
src/sentry/search/events/builder/base.py:1513 )
src/sentry/search/events/builder/base.py:1566 if self.builder_config.transform_alias_to_input_format:
```
**Eligibility check ignores per-call dataset and query**
`_get_event_stats` decides from the outer `dataset` and the raw request query, not the `scoped_dataset`/`query` it receives. Dashboard error-split queries run on discover, but the outer dataset is metrics_enhanced, so they are never upsampled. Pass `scoped_dataset` and `query` into the eligibility check.
```
src/sentry/api/endpoints/organization_events_stats.py:220 should_upsample = is_errors_query_for_error_upsampled_projects(
src/sentry/api/endpoints/organization_events_stats.py:221 snuba_params, organization, dataset, request
src/sentry/api/endpoints/organization_events_stats.py:222 )
src/sentry/api/endpoints/organization_events_stats.py:362 if widget.discover_widget_split == DashboardWidgetTypes.ERROR_EVENTS:
src/sentry/api/endpoints/organization_events_stats.py:363 split_dataset = discover
src/sentry/api/endpoints/organization_events_stats.py:364 split_query = f"({query}) AND !event.type:transaction"
```
**upsampled_count() is open to all users**
The new function is not marked `private=True`, unlike the other internal helpers around it. Any user can put `upsampled_count()` in a Discover or dashboard query, including on transaction data, where `sample_weight` doesn't apply. Add `private=True`.
```
src/sentry/search/events/datasets/discover.py:1041 SnQLFunction(
src/sentry/search/events/datasets/discover.py:1042 "upsampled_count",
src/sentry/search/events/datasets/discover.py:1043 required_args=[],
src/sentry/search/events/datasets/discover.py:1044 # Optimized aggregation for error upsampling - assumes sample_weight
src/sentry/search/events/datasets/discover.py:1036 alias,
src/sentry/search/events/datasets/discover.py:1037 ),
src/sentry/search/events/datasets/discover.py:1038 default_result_type="integer",
src/sentry/search/events/datasets/discover.py:1039 private=True,
```
Drape (Review) · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
| Defect | Severity | Found | Grade |
|---|---|---|---|
| d3 | medium | ✓ | exact |
| d4 | low | ✓ | off by one |
| d5 | medium | ✓ | off by one |
| d6 | low | ✗ | — |
In the top-events branch, only timeseries_columns gets the upsampled column. selected_columns=self.get_field_list(...) and orderby=self.get_orderby(...) still pass raw count(), so the top groups are ranked by raw counts while the series show upsampled values. This is an incomplete change. [grounded: diff] [consensus 3/3: valid=3]
Eligibility is cached for 60s and invalidate_upsampling_cache is defined but never called, so allowlist changes take up to a minute to apply. The docstring says this is deliberate, which makes the impact low, but the stale behavior is real. [grounded: diff] [consensus 3/3: valid=3]
Sibling internal functions such as column_hash set private=True, but the new upsampled_count does not. Users can therefore call it directly on discover-backed datasets like transactions, which have no sample_weight column, and the query fails. [grounded: diff] [consensus 3/3: valid=3]
raw comments (6)
**Upsampling check ignores the dataset actually queried**
`_get_event_stats` passes the outer `dataset` to the eligibility check instead of its `scoped_dataset` argument. Dashboard split paths call it with `discover`, so those queries are judged against the wrong dataset. Pass `scoped_dataset` to is_errors_query_for_error_upsampled_projects.
```
src/sentry/api/endpoints/organization_events_stats.py:218 # Early upsampling eligibility check for performance optimization
src/sentry/api/endpoints/organization_events_stats.py:219 # This cached result ensures consistent behavior across query execution
src/sentry/api/endpoints/organization_events_stats.py:220 should_upsample = is_errors_query_for_error_upsampled_projects(
src/sentry/api/endpoints/organization_events_stats.py:221 snuba_params, organization, dataset, request
```
**Top-events ranking still uses raw counts**
With `topEvents`, only the timeseries columns are rewritten to the upsampled count. The query choosing the top groups still uses raw `count()` from `get_field_list` and `get_orderby`. Apply the same count() rewrite to the fields and orderby that select the top events.
```
src/sentry/api/endpoints/organization_events_stats.py:226 upsampling_enabled = should_upsample
src/sentry/api/endpoints/organization_events_stats.py:227 final_columns = query_columns
src/sentry/api/endpoints/organization_events_stats.py:229 if top_events > 0:
src/sentry/api/endpoints/organization_events_stats.py:230 # Apply upsampling transformation just before query execution
```
**Allowlist cached 60s, never invalidated**
The eligibility result is cached for 60 seconds and `invalidate_upsampling_cache` is never called. After the allowlist option changes, stats keep using the old decision for up to a minute. Drop the cache, or call invalidate_upsampling_cache when the option changes.
```
src/sentry/api/helpers/error_upsampling.py:24 expensive repeated option lookups during high-traffic periods. This is safe
src/sentry/api/helpers/error_upsampling.py:25 because allowlist changes are infrequent and eventual consistency is acceptable.
src/sentry/api/helpers/error_upsampling.py:26 """
src/sentry/api/helpers/error_upsampling.py:27 cache_key = f"error_upsampling_eligible:{organization.id}:{hash(tuple(sorted(snuba_params.project_ids)))}"
```
**Substring match treats `!event.type:error` as error query**
`_is_error_focused_query` checks `"event.type:error" in query`. That also matches `!event.type:error` and `event.type:error OR event.type:transaction`, so count() is rewritten for queries that are not error-only. Parse the query properly, or reject negation and OR terms before treating it as error-only.
```
src/sentry/api/helpers/error_upsampling.py:130 def _is_error_focused_query(request: Request) -> bool:
src/sentry/api/helpers/error_upsampling.py:131 """
src/sentry/api/helpers/error_upsampling.py:132 Check if a query is focused on error events.
src/sentry/api/helpers/error_upsampling.py:133 Reduced to only check for event.type:error to err on the side of caution.
```
**Events without sample_weight are counted as zero**
`count()` is replaced by `sum(sample_weight)` on the assumption that every event in an allowlisted project has a sample weight. Events with no valid `client_sample_rate` get no `sample_rate`, and so do all events stored before the project was allowlisted. Fall back to 1 for missing weights, e.g. sum(coalesce(sample_weight, 1)).
```
src/sentry/api/helpers/error_upsampling.py:80 """
src/sentry/api/helpers/error_upsampling.py:81 Transform aggregation functions to use sum(sample_weight) instead of count()
src/sentry/api/helpers/error_upsampling.py:82 for error upsampling. This function assumes the caller has already validated
src/sentry/api/helpers/error_upsampling.py:83 that all projects are properly configured for upsampling.
```
**upsampled_count exposed as a public function**
`upsampled_count` is registered without `private=True`, so any user can call it via fields/yAxis on any discover-backed dataset, including transactions where `sample_weight` doesn't exist. Mark it private=True.
```
src/sentry/search/events/datasets/discover.py:1041 SnQLFunction(
src/sentry/search/events/datasets/discover.py:1042 "upsampled_count",
src/sentry/search/events/datasets/discover.py:1043 required_args=[],
src/sentry/search/events/datasets/discover.py:1044 # Optimized aggregation for error upsampling - assumes sample_weight
```
greptile · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
| Defect | Severity | Found | Grade |
|---|---|---|---|
| d3 | medium | ✗ | — |
| d4 | low | ✓ | wrong severity |
| d5 | medium | ✗ | — |
| d6 | low | ✗ | — |
Only timeseries_columns/y_axes are transformed to upsampled_count; the selected_columns and orderby used to pick top groups still use plain count(), so ranking is unweighted. The change upsampled only part of the top-events path. [grounded: tree src/sentry/api/endpoints/organization_events_stats.py] [consensus 3/3: valid=3]
no reproducible valid majority; [consensus 2/5: valid=2, minor=2, invalid=1]
raw comments (4)
<a href="#"><img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top"></a> **Mixed queries are upsampled**
Discover eligibility uses a raw `event.type:error` substring check. Valid queries such as `!event.type:error` or `event.type:error OR event.type:transaction` therefore trigger `sum(sample_weight)` even though they select non-error or mixed rows, producing incorrect counts.
<a href="#"><img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top"></a> **Top groups use wrong ranking**
For upsampled `topEvents` requests, only the returned timeseries uses `upsampled_count()`. The initial candidate query still ranks groups with ordinary `count()`, so groups with different sample weights can be selected in the wrong order, and the later weighted query cannot recover a group that was omitted.
<a href="#"><img alt="P2" src="https://greptile-static-assets.s3.amazonaws.com/badges/p2.svg?v=9" align="top"></a> **Factory accepts invalid sample rates**
The shared `store_event` factory now accepts any truthy value coercible to `float`, bypassing production's project allowlist and `0 < rate <= 1` validation. Fixtures using values such as `-0.1`, `1.5`, or numeric strings can persist sampling metadata that real ingestion rejects, making affected tests exercise unrealistic behavior.
<h3>Greptile Summary</h3>
This PR adds count upsampling for allowlisted error projects in organization event-stat queries, introduces the corresponding Discover aggregation, and extends test factories and endpoint coverage.
- Rewrites eligible `count()` axes to sum stored sample weights.
- Applies the behavior across regular and top-events timeseries paths.
- Adds allowlist eligibility caching and error-sampling fixtures.
- The current query classification and top-events candidate ranking leave two correctness gaps.
<h3>Confidence Score: 3/5</h3>
The PR is not safe to merge until Discover queries are classified structurally and top-event candidates are ranked using the same weighted aggregation as their returned series.
Raw substring classification applies error-only weighting to realistic mixed or negated Discover queries, while top-events selection uses unweighted counts before returning weighted values; both paths can return materially incorrect event statistics.
**Files Needing Attention:** src/sentry/api/helpers/error_upsampling.py, src/sentry/api/endpoints/organization_events_stats.py, src/sentry/testutils/factories.py
<details><summary><h3>Important Files Changed</h3></summary>
| Filename | Overview |
|----------|----------|
| src/sentry/api/helpers/error_upsampling.py | Adds cached eligibility and raw-query classification, but Boolean and negated Discover queries can be incorrectly upsampled. |
| src/sentry/api/endpoints/organization_events_stats.py | Applies transformed axes to timeseries execution, but top-event candidates remain ranked using unweighted counts. |
| src/sentry/search/events/datasets/discover.py | Registers the weighted count aggregation shared by Discover and errors query builders. |
| src/sentry/testutils/factories.py | Copies client sampling metadata into stored fixtures without matching production validation and allowlist behavior. |
| tests/sentry/api/helpers/test_error_upsampling.py | Covers simple positive classifications but omits negated and mixed event-type queries. |
| tests/snuba/api/endpoints/test_organization_events_stats.py | Covers basic weighted counts but not top-event ranking when groups have different sample weights. |
</details>
<!-- greptile_other_comments_section -->
<sub>Reviews (1): Last reviewed commit: ["Update pyproject.toml and 7 more files"](https://github.com/drape-io/crbench-greptile-2026-09/commit/9360b1775f333ae4537b585e9f37d56d8d897679) | [Re-trigger Greptile](https://app.greptile.com/api/retrigger?id=64284850)</sub>
Mjolnir (Opus 5.5) · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
| Defect | Severity | Found | Grade |
|---|---|---|---|
| d3 | medium | ✗ | — |
| d4 | low | ✓ | off by one |
| d5 | medium | ✓ | off by one |
| d6 | low | ✗ | — |
The new upsampled_count aggregate sums sample_weight with no ifNull fallback. Only code comments assume every event has a weight. Events without one add nothing, so counts come out low. [grounded: diff] [consensus 3/4: valid=3, uncertain=1]
A plain substring check matches '!event.type:error', so negated queries on the discover dataset are treated as error-focused and get upsampled. [grounded: diff] [consensus 3/3: valid=3]
The cache key leaves out the allowlist and invalidate_upsampling_cache is never called, so allowlist changes take up to 60 seconds to apply. The docstring says this is acceptable, which states intent rather than refuting it. Impact is low. [grounded: diff] [consensus 3/3: valid=3]
raw comments (5)
[P1] upsampled_count() counts events with no sample_weight as zero
The new aggregate is `toInt64(sum(sample_weight))` with no null fallback. Once a request qualifies for upsampling, `count()` is rewritten to `upsampled_count() as count`, so any event in an allowlisted project that has no `sample_weight` adds nothing to the total. That includes events sent before the rollout and events from SDKs that don't do client-side error sampling. ClickHouse's `sum` skips NULLs, so error counts for those projects come out too low, or 0 when no event carries a weight. These events should count as 1 each (e.g. `sum(ifNull(sample_weight, 1))`). The inline comment assumes every event has `sample_weight`, but nothing in this change guarantees that.
[P2] `_is_error_focused_query` also matches negated `!event.type:error` queries
On the discover dataset, upsampling is turned on by a plain substring test, `"event.type:error" in query`. A query such as `!event.type:error` (everything except errors) contains that substring, so it is treated as error-focused and `count()` becomes `sum(sample_weight)`. The query then returns transactions and other non-error events, which have no sample weight, so the count is wrong (close to 0). The check should skip negated terms, for example by parsing the query or by matching only a non-negated `event.type:error` token.
[P1] `upsampled_count()` returns 0 for events that have no `sample_weight`
The new aggregate is `toInt64(sum(sample_weight))` with no fallback for null values. When a project is allowlisted, every `count()` in the stats endpoint is rewritten to this aggregate. Any error event without a client sample rate has a null `sample_weight` (for example, events from SDKs or releases that don't send `contexts.error_sampling`, or events ingested before sampling was enabled). Those events add nothing to the sum, so the counts drop when they should go up. A bucket that contains only unsampled events shows 0. The comment says this relies on the schema guaranteeing the column is populated, but nothing in ingestion enforces that. Each unsampled event should count as weight 1, e.g. with `ifNull(sample_weight, 1)`.
[P2] The error-query check also matches negated filters such as `!event.type:error`
For the discover dataset, `_is_error_focused_query` turns on upsampling whenever the lowercased query contains the substring `event.type:error`. A query like `!event.type:error`, which means "not errors" (in practice transactions), also matches. `count()` is then rewritten to `upsampled_count()` and runs against non-error events. Transactions have no `sample_weight`, so the query either fails or returns counts of 0. The same thing happens with `event.type:error OR event.type:transaction`, which mixes in transactions. This is the case the docstring says the check guards against ("err on the side of caution"), so it should parse the search filter instead of matching a substring.
[P3] Caching eligibility for 60 seconds delays allowlist changes and adds a cache round-trip to every request
The eligibility result is stored in the shared cache under a key built only from the org and project IDs, and the allowlist itself is not part of the key. After `issues.client_error_sampling.project_allowlist` changes, requests keep using the old upsampled or non-upsampled behaviour for up to 60 seconds. Nothing calls the new `invalidate_upsampling_cache`. This also contradicts the docstring of `_are_all_projects_error_upsampled`, which says the config is read fresh on purpose. `options.get` is already cached in process, so this layer adds a network cache read, plus a write on every miss, to each events-stats request and doesn't make the request faster.
Mjolnir (Sonnet 5.5) · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
| Defect | Severity | Found | Grade |
|---|---|---|---|
| d3 | medium | ✓ | exact |
| d4 | low | ✓ | off by one |
| d5 | medium | ✓ | exact |
| d6 | low | ✗ | — |
The raw substring check also matches `!event.type:error` and similar queries. On the discover dataset with every project allowlisted, those non-error queries get count() rewritten to upsampled_count(). [grounded: diff] [consensus 3/3: valid=3]
The split path only runs when the outer dataset is metrics_performance or metrics_enhanced_performance. For those datasets `_should_apply_sample_weight_transform` returns False, so the transactions split never gets upsampled. When the outer dataset is discover, scoped_dataset equals dataset. The claimed failure therefore can't happen. [invalid: contradiction] [consensus 3/3: invalid=3]
The eligibility result is cached for 60s and `invalidate_upsampling_cache` has no callers. After the allowlist option changes, requests keep using the stale decision for up to a minute, which contradicts the 'latest configuration' docstring. [grounded: diff] [consensus 3/3: valid=3]
Same issue as finding 2: the 60s cache is never invalidated, so after an allowlist change, counts can be stale for up to a minute. [grounded: diff] [consensus 3/3: valid=3]
raw comments (7)
[P2] Substring match treats negated or longer error queries as error queries
`_is_error_focused_query` uses `"event.type:error" in query` on the raw lowercased query string. It also matches `!event.type:error`, `event.type:error_foo` and similar, and the discover dataset accepts all of these. If every project is allowlisted, a discover timeseries with `!event.type:error` (for example transactions or default events) has `count()` rewritten to `upsampled_count()`, which sums `sample_weight`. That column may not exist or be populated for non-error rows, so the series can be wrong or the query can fail. Match on the parsed event.type condition instead of a substring.
[P2] Upsampling decision uses the request-level dataset, not the dataset being queried
`is_errors_query_for_error_upsampled_projects` is called with the outer `dataset`, but `_get_event_stats` runs against `scoped_dataset`. The dashboard-widget split path (`get_event_stats_factory`) calls it with `discover`, `errors` or `transactions` regardless of the outer dataset. With the outer dataset `discover` and `event.type:error` in the request query, the transform is applied to every scoped call, including the transactions-split query. That query then gets `sum(sample_weight)`, which the helper's own docstring says doesn't exist for transactions. The eligibility check should use `scoped_dataset`.
[P3] Eligibility cache can serve a stale allowlist and its invalidation is never called
The allowlist result is cached for 60s per org and project set, and `invalidate_upsampling_cache` has no callers. After the `issues.client_error_sampling.project_allowlist` option changes, requests can keep using the old decision for up to a minute. That means some requests are upsampled for projects that were just removed, or not upsampled for projects just added. `_are_all_projects_error_upsampled` is only an `options.get` call, so the cache saves little. The docstring on `_are_all_projects_error_upsampled` also promises "always the latest configuration", which the cache contradicts.
[P2] Upsampling decision uses outer `dataset`, not `scoped_dataset`
_get_event_stats` receives `scoped_dataset` (the dashboard-widget split path calls it with `discover`, `errors`, or `transactions` regardless of the request's dataset). The eligibility check is made against the closed-over `dataset` instead. When a widget on the discover dataset is split into an errors query, `scoped_dataset` is `errors`, but `dataset` is `discover`. Upsampling is then applied only if the raw request query happens to contain `event.type:error`, so the errors-side results are not upsampled. In the other direction, a transactions or other scoped query could be upsampled based on the wrong dataset. The check should be passed `scoped_dataset
[P2] Substring match on `event.type:error` also matches negated and mixed queries
For the discover dataset, `_is_error_focused_query` returns True for any query containing the text `event.type:error`. This includes `!event.type:error`, `event.type:error OR event.type:transaction`, and `event.type:error_something`. For an allowlisted project with `!event.type:error` (or a transaction-oriented query), `count()` is rewritten to `sum(sample_weight)`. `sample_weight` is not a transactions column, so the query errors or returns wrong counts. The query should be parsed properly, or the rewrite should only apply when the query is positively scoped to errors
[P2] `sum(sample_weight)` undercounts events that have no sample weight
upsampled_count` is a plain `sum(sample_weight)`. Events stored for an allowlisted project before `sample_rate` was set, or without `client_sample_rate`, have a null or absent `sample_weight`, and ClickHouse `sum` skips those rows. For those events the replacement for `count()` contributes 0 instead of 1. Charts for such projects therefore drop pre-rollout or unsampled events entirely. A fallback such as `coalesce(sample_weight, 1)` (or `toInt64(sum(...))` over a defaulted value) is needed. The "assumes sample_weight exists" comments are not backed by anything in the change
[P3] Eligibility cache is never invalidated, and the invalidator has no callers
The eligibility result is cached for 60 seconds. `invalidate_upsampling_cache` is defined but never called when the `issues.client_error_sampling.project_allowlist` option changes. After an allowlist change, charts can switch between sampled and upsampled counts for up to a minute, and the result depends on which request populated the cache. The option read is already an in-memory lookup, so the cache adds staleness without a real performance benefit
qodo · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
| Defect | Severity | Found | Grade |
|---|---|---|---|
| d3 | medium | ✓ | off by one |
| d4 | low | ✓ | wrong severity |
| d5 | medium | ✗ | — |
| d6 | low | ✗ | — |
The new transform only rewrites the exact normalized string 'count()'; aliased counts and equation axes like 'equation|count() / 100' are passed through unchanged, so they still return raw sampled counts on fully allowlisted projects. [grounded: diff] [consensus 3/3: valid=3]
Only timeseries_columns gets transformed; selected_columns=self.get_field_list(...) stays untransformed, and discover.top_events_timeseries runs its ranking query on selected_columns, so top groups are picked by raw sampled counts. [grounded: diff] [consensus 3/3: valid=3]
Production event_manager only accepts int/float values with 0 < rate <= 1. The new factory helper accepts any truthy value that float() can convert, including strings and values above 1. That diverges from production, though it only affects test fidelity. [grounded: diff] [consensus 3/3: valid=3]
raw comments (7)
<img src="https://img.shields.io/badge/High-634FD1?style=flat-square" height="20px" alt="Action required">
1\. Aliased error counts stay sampled <code>🐞 Bug</code> <code>≡ Correctness</code>
<pre>
<b><i>transform_query_columns_for_error_upsampling</i></b> only replaces a column whose complete normalized text
equals <b><i>count()</i></b>, leaving both explicitly aliased counts such as <b><i>count() as errors</i></b> and counts
embedded in equations such as <b><i>equation|count() / 100</i></b> unchanged. On fully allowlisted error
projects, aliases pass through directly while the equation builder auto-adds the unchanged count
dependency, so both ordinary and equation axes use raw sampled values.
</pre>
<details>
<summary><strong>Agent Prompt</strong></summary>
```
## Issue description
Error upsampling recognizes only the exact `count()` string, leaving supported aliased counts and counts referenced inside equation axes sampled; equation dependencies are subsequently auto-added as raw aggregates.
## Fix Focus Areas
- src/sentry/api/helpers/error_upsampling.py[77-100]
- src/sentry/search/events/fields.py[680-689]
- src/sentry/api/endpoints/organization_events_stats.py[229-256]
- src/sentry/discover/arithmetic.py[341-364]
## Recommended Fix
Parse each function expression with the existing event-search field parser, replace a parsed `count` function with `upsampled_count`, and preserve any explicit alias. Use the existing arithmetic parser to replace count aggregates inside equation expressions, then pass the transformed equations to both standard and top-event query builders. Add tests for aliased, case-insensitive, and whitespace-bearing count expressions, plus endpoint coverage for equation-only and mixed count/equation axes.
```
<code>ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools</code>
</details>
<img src="https://img.shields.io/badge/High-634FD1?style=flat-square" height="20px" alt="Action required">
2\. Top error series rank by sampled counts <code>🐞 Bug</code> <code>≡ Correctness</code>
<pre>
The top-events branch transforms <b><i>timeseries_columns</i></b> but still passes the original request fields
as <b><i>selected_columns</i></b> to <b><i>top_events_timeseries</i></b>. That method uses those original aggregates in its
preliminary ranking query, so projects with different sample weights can select the wrong top groups
before their displayed series are upsampled.
</pre>
<details>
<summary><strong>Agent Prompt</strong></summary>
```
## Issue description
Top-event timeseries display weighted counts but choose their groups using an earlier raw-count ranking query.
## Fix Focus Areas
- src/sentry/api/endpoints/organization_events_stats.py[229-270]
- src/sentry/snuba/errors.py[252-284]
## Recommended Fix
Transform count aggregates used by the preliminary top-event selection, including the matching ordering expression, while preserving non-aggregate grouping fields. Add a test where differing sample weights change which group belongs in the requested top set.
```
<code>ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools</code>
</details>
<img src="https://img.shields.io/badge/High-634FD1?style=flat-square" height="20px" alt="Action required">
3\. Dashboard error charts stay sampled <code>🐞 Bug</code> <code>≡ Correctness</code>
<pre>
<b><i>_get_event_stats</i></b> determines eligibility from the outer <b><i>dataset</i></b> and the original request rather
than from its <b><i>scoped_dataset</i></b> and <b><i>query</i></b> arguments. Dashboard splitting generates an error-only
Discover query by appending <b><i>!event.type:transaction</i></b>, but a request without the literal
<b><i>event.type:error</i></b> disables upsampling for that generated query.
</pre>
<details>
<summary><strong>Agent Prompt</strong></summary>
```
## Issue description
Generated dashboard error queries are classified using the original request and outer dataset, so their counts are not upsampled.
## Fix Focus Areas
- src/sentry/api/endpoints/organization_events_stats.py[209-233]
- src/sentry/api/endpoints/organization_events_stats.py[355-394]
- src/sentry/api/helpers/error_upsampling.py[103-140]
## Recommended Fix
Pass the actual scoped dataset and executed query string into the eligibility helper. Classify the parsed effective query rather than reading `request.GET`, and add coverage for dashboard split queries whose original request has no event-type filter.
```
<code>ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools</code>
</details>
<img src="https://img.shields.io/badge/High-634FD1?style=flat-square" height="20px" alt="Action required">
4\. Non-error queries use error weights <code>🐞 Bug</code> <code>≡ Correctness</code>
<pre>
<b><i>_is_error_focused_query</i></b> treats any raw substring occurrence of <b><i>event.type:error</i></b> as an error-only
query, including <b><i>!event.type:error</i></b>, longer values, and expressions that also select transactions.
For allowlisted projects these Discover requests are rewritten to sum <b><i>sample_weight</i></b> even though
the helper documents that transaction rows do not provide that field, producing invalid or
incorrectly weighted results.
</pre>
<details>
<summary><strong>Agent Prompt</strong></summary>
```
## Issue description
A substring check classifies negated and mixed event-type filters as error-only, enabling an aggregation that is not valid for every selected event type.
## Fix Focus Areas
- src/sentry/api/helpers/error_upsampling.py[103-140]
## Recommended Fix
Use the repository's search-query parser to establish that the effective query exclusively selects error events. Reject negated, disjunctive, malformed, and non-error event-type conditions, and add focused tests for each form.
```
<code>ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools</code>
</details>
<img src="https://img.shields.io/badge/Medium-634FD1?style=flat-square" height="20px" alt="Remediation recommended">
5\. Tests accept invalid sampling rates <code>🐞 Bug</code> <code>⚙ Maintainability</code>
<pre>
<b><i>_set_sample_rate_from_error_sampling</i></b> accepts any truthy value convertible by <b><i>float</i></b>, rather than
production's numeric <b><i>(0, 1]</i></b> constraint. Factory events can therefore exercise impossible ingestion
states—for example, <b><i>1.5</i></b> produces a weighted <b><i>times_seen</i></b> value of zero—while numeric strings are
accepted in tests but ignored in production.
</pre>
<details>
<summary><strong>Agent Prompt</strong></summary>
```
## Issue description
The test factory derives sample rates with looser type and range rules than production ingestion, allowing tests to construct impossible event states.
## Fix Focus Areas
- src/sentry/testutils/factories.py[344-357]
- src/sentry/event_manager.py[774-787]
## Recommended Fix
Extract and reuse one validation helper that accepts only non-boolean numeric values greater than zero and at most one. Add factory tests for strings, zero, negative values, values above one, and valid integer or floating-point rates.
```
<code>ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools</code>
</details>
<h3>PR Summary by Qodo</h3>
Upsample error event counts for allowlisted projects
<code>✨ Enhancement</code> <code>🧪 Tests</code> <code>⚙️ Configuration changes</code> <code>🕐 40+ Minutes</code>
<img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider">
<details>
<summary>AI Description</summary>
<dl>
<dd>
<br/>
><pre>
>• Upsample error counts using stored sample weights for fully allowlisted project queries.
>• Restrict weighted aggregation to error datasets and explicitly error-focused Discover queries.
>• Cover eligibility, aggregation transformation, and API behavior across sampling scenarios.
></pre>
</dd>
</dl>
</details>
<details>
<summary>Diagram</summary>
<dl>
<dd>
<br/>
```mermaid
graph TD
A["Stats Request"] --> B["Events Stats"] --> C{"Eligible Query"}
C -->|Yes| D["Column Transform"] --> E["Discover Aggregate"] --> F["Snuba Query"] --> G["Weighted Counts"]
C -->|No| F
H["Allowlist Cache"] --> C
```
</dd>
</dl>
</details>
<details>
<summary>High-Level Assessment</summary>
<dl>
<dd>
<br/>
>The following are alternative approaches to this PR:
<details>
<summary><b>1. Resolver-level weighted count semantics</b></summary>
<dl>
<dd>
- ➕ Centralizes eligibility and aggregation selection in the query layer.
- ➕ Automatically supports additional endpoints using the same resolver.
- ➕ Avoids rewriting column strings across endpoint execution branches.
- ➖ Requires propagating organization, project, and dataset context deeper into query resolution.
- ➖ Broadens the rollout surface and increases regression risk for existing count queries.
</dd>
</dl>
</details>
<details>
<summary><b>2. Pre-aggregated sampled-error metrics</b></summary>
<dl>
<dd>
- ➕ Moves weighting work out of interactive queries.
- ➕ Provides consistent weighted counts across all consumers.
- ➖ Requires a new ingestion and storage pipeline.
- ➖ Adds migration, backfill, and consistency complexity.
- ➖ May not support existing Discover dimensions and filters without substantial cardinality cost.
</dd>
</dl>
</details>
>**Recommendation:** The endpoint-scoped transformation is appropriate for a controlled rollout because it preserves existing query behavior unless all projects and the dataset are explicitly eligible. If weighted error counts expand to more endpoints, move the decision into the typed query resolver rather than duplicating endpoint-level column rewriting.
</dd>
</dl>
</details>
<details>
<summary> Files changed (7) <code> +479 / -6 </code> </summary>
<dl>
<dd>
<br/>
<details>
<summary>Enhancement (3) <code> +185 / -5 </code></summary>
<dl>
<dd>
<details>
<summary>organization_events_stats.py<code>Apply weighted counts to eligible error timeseries</code> <code>+33/-5</code></summary>
<br/>
>Apply weighted counts to eligible error timeseries
>
><pre>
>• Checks error-upsampling eligibility once per stats query and transforms count columns across top-events, RPC, and standard execution paths. Also corrects the validation message for non-positive topEvents values.
></pre>
>
><a href='https://github.com/drape-io/crbench-qodo-2026-09/pull/76/files#diff-4aa78da493b2755b89cf27bd682c88915d46aee98920034188d4405d63b260f6'>src/sentry/api/endpoints/organization_events_stats.py</a>
</details>
<details>
<summary>error_upsampling.py<code>Add error-upsampling eligibility and column transformation helpers</code> <code>+140/-0</code></summary>
<br/>
>Add error-upsampling eligibility and column transformation helpers
>
><pre>
>• Introduces cached all-project allowlist checks, dataset and query guards, cache invalidation, and count-to-upsampled-count transformation. Eligibility is cached for sixty seconds per organization and project set.
></pre>
>
><a href='https://github.com/drape-io/crbench-qodo-2026-09/pull/76/files#diff-7575538d865e4fbb8daa743db184daa03cf257ec983da52722392e8886fed761'>src/sentry/api/helpers/error_upsampling.py</a>
</details>
<details>
<summary>discover.py<code>Register the upsampled count Discover aggregation</code> <code>+12/-0</code></summary>
<br/>
>Register the upsampled count Discover aggregation
>
><pre>
>• Adds upsampled_count(), which returns an integer sum of the sample_weight column for eligible error queries.
></pre>
>
><a href='https://github.com/drape-io/crbench-qodo-2026-09/pull/76/files#diff-39d7c5f84a98ce74a7c740e14053c99066ca14a5f6c63f13e4c91b51d0c02bc5'>src/sentry/search/events/datasets/discover.py</a>
</details>
</dd>
</dl>
</details>
<details>
<summary>Tests (3) <code> +292 / -1 </code></summary>
<dl>
<dd>
<details>
<summary>factories.py<code>Propagate error-sampling rates in stored test events</code> <code>+20/-1</code></summary>
<br/>
>Propagate error-sampling rates in stored test events
>
><pre>
>• Extracts the client sample rate from the error_sampling context and places it on normalized test event data. This enables test ingestion to populate the sample weight used by weighted queries.
></pre>
>
><a href='https://github.com/drape-io/crbench-qodo-2026-09/pull/76/files#diff-8548c74110f5b85ee8249c107733efee8e08f3f6d17d1613289fed01b090535a'>src/sentry/testutils/factories.py</a>
</details>
<details>
<summary>test_error_upsampling.py<code>Test error-upsampling helper decisions and transformations</code> <code>+101/-0</code></summary>
<br/>
>Test error-upsampling helper decisions and transformations
>
><pre>
>• Covers complete and partial allowlists, count column rewriting, error-query detection, and dataset-specific eligibility behavior.
></pre>
>
><a href='https://github.com/drape-io/crbench-qodo-2026-09/pull/76/files#diff-769369d5100c2d9570a7c991ab4daf1f70d5358285cf2b9aed4ee8366c6495c7'>tests/sentry/api/helpers/test_error_upsampling.py</a>
</details>
<details>
<summary>test_organization_events_stats.py<code>Verify error upsampling through the events-stats API</code> <code>+171/-0</code></summary>
<br/>
>Verify error upsampling through the events-stats API
>
><pre>
>• Adds integration coverage for weighted error counts across fully allowlisted projects. It also verifies ordinary counts for partial or empty allowlists and transaction-focused Discover queries.
></pre>
>
><a href='https://github.com/drape-io/crbench-qodo-2026-09/pull/76/files#diff-5ed826d8b4107921da1492baa5fc58589930a5d45fcd7cc84d8f841dc859fc59'>tests/snuba/api/endpoints/test_organization_events_stats.py</a>
</details>
</dd>
</dl>
</details>
<details>
<summary>Other (1) <code> +2 / -0 </code></summary>
<dl>
<dd>
<details>
<summary>pyproject.toml<code>Enable stronger typing for error-upsampling modules</code> <code>+2/-0</code></summary>
<br/>
>Enable stronger typing for error-upsampling modules
>
><pre>
>• Adds the new helper and its tests to the stricter mypy override module list.
></pre>
>
><a href='https://github.com/drape-io/crbench-qodo-2026-09/pull/76/files#diff-50c86b7ed8ac2cf95bd48334961bf0530cdc77b5a56f852c5c61b89d735fd711'>pyproject.toml</a>
</details>
</dd>
</dl>
</details>
</dd>
</dl>
</details>
<h3>Code Review by Qodo</h3>
<code>🐞 Bugs (5)</code> <code>📘 Rule violations (0)</code> <code>📜 Skill insights (0)</code>
<img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider">
<br/>
<img src="https://img.shields.io/badge/High-634FD1?style=flat-square" height="20px" alt="Action required">
<details>
<summary> 1. Aliased error counts stay sampled <code>🐞 Bug</code> <code>≡ Correctness</code></summary>
<br/>
> <details open>
><summary>Description</summary>
><br/>
>
><pre>
><b><i>transform_query_columns_for_error_upsampling</i></b> only replaces a column whose complete normalized text
>equals <b><i>count()</i></b>, leaving both explicitly aliased counts such as <b><i>count() as errors</i></b> and counts
>embedded in equations such as <b><i>equation|count() / 100</i></b> unchanged. On fully allowlisted error
>projects, aliases pass through directly while the equation builder auto-adds the unchanged count
>dependency, so both ordinary and equation axes use raw sampled values.
></pre>
></details>
> <details>
><summary>Code</summary>
><br/>
>
><code>[src/sentry/api/helpers/error_upsampling.py[R92-95]](https://github.com/drape-io/crbench-qodo-2026-09/pull/76/files#diff-7575538d865e4fbb8daa743db184daa03cf257ec983da52722392e8886fed761R92-R95)</code>
>
>```diff
>+ if column_lower == "count()":
>+ # Transform to upsampled count - assumes sample_weight column exists
>+ # for all events in allowlisted projects per our data model requirements
>+ transformed_columns.append("upsampled_count() as count")
>```
></details>
> <details>
><summary>Evidence</summary>
><br/>
>
><pre>
>The helper performs an exact-string comparison even though the existing search field parser extracts
>and returns aliases from valid function expressions. The endpoint also transforms only query-column
>strings, while equations are handled separately and the arithmetic resolver auto-adds referenced
>functions that are absent, proving that an embedded <b><i>count()</i></b> can re-enter the query as the original
>aggregate.
></pre>
>
> <code>[src/sentry/api/helpers/error_upsampling.py[89-98]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/api/helpers/error_upsampling.py/#L89-L98)</code>
> <code>[src/sentry/search/events/fields.py[680-689]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/search/events/fields.py/#L680-L689)</code>
> <code>[src/sentry/api/endpoints/organization_events_stats.py[229-256]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/api/endpoints/organization_events_stats.py/#L229-L256)</code>
> <code>[src/sentry/api/bases/organization_events.py[93-106]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/api/bases/organization_events.py/#L93-L106)</code>
> <code>[src/sentry/discover/arithmetic.py[341-364]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/discover/arithmetic.py/#L341-L364)</code>
></details>
> <details>
><summary>Agent prompt</summary>
><br/>
>
>```
>The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
>
>## Issue description
>Error upsampling recognizes only the exact `count()` string, leaving supported aliased counts and counts referenced inside equation axes sampled; equation dependencies are subsequently auto-added as raw aggregates.
>
>## Fix Focus Areas
>- src/sentry/api/helpers/error_upsampling.py[77-100]
>- src/sentry/search/events/fields.py[680-689]
>- src/sentry/api/endpoints/organization_events_stats.py[229-256]
>- src/sentry/discover/arithmetic.py[341-364]
>
>## Recommended Fix
>Parse each function expression with the existing event-search field parser, replace a parsed `count` function with `upsampled_count`, and preserve any explicit alias. Use the existing arithmetic parser to replace count aggregates inside equation expressions, then pass the transformed equations to both standard and top-event query builders. Add tests for aliased, case-insensitive, and whitespace-bearing count expressions, plus endpoint coverage for equation-only and mixed count/equation axes.
>```
> <code>ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools</code>
></details>
<hr/>
</details>
<details>
<summary> 2. Top error series rank by sampled counts <code>🐞 Bug</code> <code>≡ Correctness</code></summary>
<br/>
> <details open>
><summary>Description</summary>
><br/>
>
><pre>
>The top-events branch transforms <b><i>timeseries_columns</i></b> but still passes the original request fields
>as <b><i>selected_columns</i></b> to <b><i>top_events_timeseries</i></b>. That method uses those original aggregates in its
>preliminary ranking query, so projects with different sample weights can select the wrong top groups
>before their displayed series are upsampled.
></pre>
></details>
> <details>
><summary>Code</summary>
><br/>
>
><code>[src/sentry/api/endpoints/organization_events_stats.py[R253-256]](https://github.com/drape-io/crbench-qodo-2026-09/pull/76/files#diff-4aa78da493b2755b89cf27bd682c88915d46aee98920034188d4405d63b260f6R253-R256)</code>
>
>```diff
> return scoped_dataset.top_events_timeseries(
>- timeseries_columns=query_columns,
>+ timeseries_columns=final_columns,
> selected_columns=self.get_field_list(organization, request),
> equations=self.get_equation_list(organization, request),
>```
></details>
> <details>
><summary>Evidence</summary>
><br/>
>
><pre>
>The endpoint passes transformed columns only as <b><i>timeseries_columns</i></b> and leaves <b><i>selected_columns</i></b>
>untouched, while <b><i>top_events_timeseries</i></b> runs its first ranking query directly from those selected
>columns before building the timeseries.
></pre>
>
> <code>[src/sentry/api/endpoints/organization_events_stats.py[253-260]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/api/endpoints/organization_events_stats.py/#L253-L260)</code>
> <code>[src/sentry/snuba/errors.py[252-277]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/snuba/errors.py/#L252-L277)</code>
></details>
> <details>
><summary>Agent prompt</summary>
><br/>
>
>```
>The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
>
>## Issue description
>Top-event timeseries display weighted counts but choose their groups using an earlier raw-count ranking query.
>
>## Fix Focus Areas
>- src/sentry/api/endpoints/organization_events_stats.py[229-270]
>- src/sentry/snuba/errors.py[252-284]
>
>## Recommended Fix
>Transform count aggregates used by the preliminary top-event selection, including the matching ordering expression, while preserving non-aggregate grouping fields. Add a test where differing sample weights change which group belongs in the requested top set.
>```
> <code>ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools</code>
></details>
<hr/>
</details>
<details>
<summary> 3. Dashboard error charts stay sampled <code>🐞 Bug</code> <code>≡ Correctness</code></summary>
<br/>
> <details open>
><summary>Description</summary>
><br/>
>
><pre>
><b><i>_get_event_stats</i></b> determines eligibility from the outer <b><i>dataset</i></b> and the original request rather
>than from its <b><i>scoped_dataset</i></b> and <b><i>query</i></b> arguments. Dashboard splitting generates an error-only
>Discover query by appending <b><i>!event.type:transaction</i></b>, but a request without the literal
><b><i>event.type:error</i></b> disables upsampling for that generated query.
></pre>
></details>
> <details>
><summary>Code</summary>
><br/>
>
><code>[src/sentry/api/endpoints/organization_events_stats.py[R220-222]](https://github.com/drape-io/crbench-qodo-2026-09/pull/76/files#diff-4aa78da493b2755b89cf27bd682c88915d46aee98920034188d4405d63b260f6R220-R222)</code>
>
>```diff
>+ should_upsample = is_errors_query_for_error_upsampled_projects(
>+ snuba_params, organization, dataset, request
>+ )
>```
></details>
> <details>
><summary>Evidence</summary>
><br/>
>
><pre>
>The eligibility call closes over the request and outer dataset, whereas the dashboard path passes a
>different dataset and a newly constructed error-only query into <b><i>_get_event_stats</i></b>; the helper never
>examines those effective arguments.
></pre>
>
> <code>[src/sentry/api/endpoints/organization_events_stats.py[209-222]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/api/endpoints/organization_events_stats.py/#L209-L222)</code>
> <code>[src/sentry/api/endpoints/organization_events_stats.py[355-380]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/api/endpoints/organization_events_stats.py/#L355-L380)</code>
> <code>[src/sentry/api/endpoints/organization_events_stats.py[382-394]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/api/endpoints/organization_events_stats.py/#L382-L394)</code>
> <code>[src/sentry/api/helpers/error_upsampling.py[121-138]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/api/helpers/error_upsampling.py/#L121-L138)</code>
></details>
> <details>
><summary>Agent prompt</summary>
><br/>
>
>```
>The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
>
>## Issue description
>Generated dashboard error queries are classified using the original request and outer dataset, so their counts are not upsampled.
>
>## Fix Focus Areas
>- src/sentry/api/endpoints/organization_events_stats.py[209-233]
>- src/sentry/api/endpoints/organization_events_stats.py[355-394]
>- src/sentry/api/helpers/error_upsampling.py[103-140]
>
>## Recommended Fix
>Pass the actual scoped dataset and executed query string into the eligibility helper. Classify the parsed effective query rather than reading `request.GET`, and add coverage for dashboard split queries whose original request has no event-type filter.
>```
> <code>ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools</code>
></details>
<hr/>
</details>
<details><summary><ins><strong>View high (1)</strong></ins></summary><br/>
<details>
<summary> 4. Non-error queries use error weights <code>🐞 Bug</code> <code>≡ Correctness</code></summary>
<br/>
> <details open>
><summary>Description</summary>
><br/>
>
><pre>
><b><i>_is_error_focused_query</i></b> treats any raw substring occurrence of <b><i>event.type:error</i></b> as an error-only
>query, including <b><i>!event.type:error</i></b>, longer values, and expressions that also select transactions.
>For allowlisted projects these Discover requests are rewritten to sum <b><i>sample_weight</i></b> even though
>the helper documents that transaction rows do not provide that field, producing invalid or
>incorrectly weighted results.
></pre>
></details>
> <details>
><summary>Code</summary>
><br/>
>
><code>[src/sentry/api/helpers/error_upsampling.py[R135-138]](https://github.com/drape-io/crbench-qodo-2026-09/pull/76/files#diff-7575538d865e4fbb8daa743db184daa03cf257ec983da52722392e8886fed761R135-R138)</code>
>
>```diff
>+ query = request.GET.get("query", "").lower()
>+
>+ if "event.type:error" in query:
>+ return True
>```
></details>
> <details>
><summary>Evidence</summary>
><br/>
>
><pre>
>The helper uses an unbounded substring check and directly feeds its result into Discover
>eligibility, after which the transformation replaces count with <b><i>upsampled_count</i></b>; its own dataset
>documentation says sample weight is unavailable for transactions.
></pre>
>
> <code>[src/sentry/api/helpers/error_upsampling.py[103-127]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/api/helpers/error_upsampling.py/#L103-L127)</code>
> <code>[src/sentry/api/helpers/error_upsampling.py[130-140]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/api/helpers/error_upsampling.py/#L130-L140)</code>
> <code>[src/sentry/api/helpers/error_upsampling.py[89-98]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/api/helpers/error_upsampling.py/#L89-L98)</code>
></details>
> <details>
><summary>Agent prompt</summary>
><br/>
>
>```
>The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
>
>## Issue description
>A substring check classifies negated and mixed event-type filters as error-only, enabling an aggregation that is not valid for every selected event type.
>
>## Fix Focus Areas
>- src/sentry/api/helpers/error_upsampling.py[103-140]
>
>## Recommended Fix
>Use the repository's search-query parser to establish that the effective query exclusively selects error events. Reject negated, disjunctive, malformed, and non-error event-type conditions, and add focused tests for each form.
>```
> <code>ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools</code>
></details>
<hr/>
</details>
</details>
<br/>
<img src="https://img.shields.io/badge/Medium-634FD1?style=flat-square" height="20px" alt="Remediation recommended">
<details>
<summary> 5. Tests accept invalid sampling rates <code>🐞 Bug</code> <code>⚙ Maintainability</code></summary>
<br/>
> <details open>
><summary>Description</summary>
><br/>
>
><pre>
><b><i>_set_sample_rate_from_error_sampling</i></b> accepts any truthy value convertible by <b><i>float</i></b>, rather than
>production's numeric <b><i>(0, 1]</i></b> constraint. Factory events can therefore exercise impossible ingestion
>states—for example, <b><i>1.5</i></b> produces a weighted <b><i>times_seen</i></b> value of zero—while numeric strings are
>accepted in tests but ignored in production.
></pre>
></details>
> <details>
><summary>Code</summary>
><br/>
>
><code>[src/sentry/testutils/factories.py[R353-356]](https://github.com/drape-io/crbench-qodo-2026-09/pull/76/files#diff-8548c74110f5b85ee8249c107733efee8e08f3f6d17d1613289fed01b090535aR353-R356)</code>
>
>```diff
>+ if client_sample_rate:
>+ try:
>+ normalized_data["sample_rate"] = float(client_sample_rate)
>+ except Exception:
>```
></details>
> <details>
><summary>Evidence</summary>
><br/>
>
><pre>
>The new factory helper merely checks truthiness and float conversion, whereas production requires an
>original integer or float in <b><i>(0, 1]</i></b>; downstream weighting truncates <b><i>1 / sample_rate</i></b>, making
>above-one factory values observably invalid.
></pre>
>
> <code>[src/sentry/testutils/factories.py[344-357]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/testutils/factories.py/#L344-L357)</code>
> <code>[src/sentry/event_manager.py[774-787]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/event_manager.py/#L774-L787)</code>
> <code>[src/sentry/event_manager.py[1560-1565]](https://github.com/drape-io/crbench-qodo-2026-09/blob/c916859f72774d154befa1b522ec927aacb80024/src/sentry/event_manager.py/#L1560-L1565)</code>
></details>
> <details>
><summary>Agent prompt</summary>
><br/>
>
>```
>The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
>
>## Issue description
>The test factory derives sample rates with looser type and range rules than production ingestion, allowing tests to construct impossible event states.
>
>## Fix Focus Areas
>- src/sentry/testutils/factories.py[344-357]
>- src/sentry/event_manager.py[774-787]
>
>## Recommended Fix
>Extract and reuse one validation helper that accepts only non-boolean numeric values greater than zero and at most one. Add factory tests for strings, zero, negative values, values above one, and valid integer or floating-point rates.
>```
> <code>ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools</code>
></details>
<hr/>
</details>
<img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider">
<!-- qodo-context:start -->
<details><summary><strong>Context sources</strong></summary>
<div>Review mode: <code>⚖️ Balanced</code>: Downgraded extended -> standard: change is below the extended eligibility bar (hunks 15/18, lines 485/200; both must reach the floor). Router rationale: This adds substantial new runtime logic across query transformation, caching, dataset handling, event factories, and endpoint paths, creating multiple independent, behaviorally significant defect opportunities.</div>
<!-- qodo-context:end -->
</details>
<img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider">
<!-- qodo-daily-tip:start -->
<details>
<summary><strong>Tip of the day</strong></summary>
<br/>
<pre>💡 Did you know, you can reply 'qodo' on any finding to push back, ask questions, or dig deeper</pre>
<a href="https://docs.qodo.ai/tips-and-tricks">More tips ↗</a> | <a href="https://app.qodo.ai/configurations?tab=display-preferences">Customize Qodo ↗</a> | <a href="https://docs.qodo.ai">Qodo docs ↗</a>
</details>
<img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider">
<!-- qodo-daily-tip:end -->
<!-- https://github.com/drape-io/crbench-qodo-2026-09/commit/c916859f72774d154befa1b522ec927aacb80024 -->
<a href="https://www.qodo.ai"><img src="https://www.qodo.ai/wp-content/uploads/2025/03/qodo-logo.svg" width="80" alt="Qodo Logo"></a>