Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ dependencies = [
"statsd>=3.3.0",
"structlog>=22.1.0",
"symbolic>=12.14.1",
"taskbroker-client>=0.1.7",
"taskbroker-client>=0.1.8",
"tiktoken>=0.8.0",
"tokenizers>=0.22.0",
"tldextract>=5.1.2",
Expand Down
20 changes: 20 additions & 0 deletions src/sentry/taskworker/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ def incr(
sample_rate = settings.SENTRY_METRICS_SAMPLE_RATE
sentry_metrics.incr(name, amount=int(value), tags=tags, sample_rate=sample_rate)

def gauge(
self,
key: str,
value: float,
instance: str | None = None,
tags: Tags | None = None,
sample_rate: float = 1,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gauge bypasses global metrics sample rate setting

Low Severity

The new gauge method defaults sample_rate to 1, unlike incr, distribution, and timer which all default to None and fall back to settings.SENTRY_METRICS_SAMPLE_RATE. In deployments that override SENTRY_METRICS_SAMPLE_RATE to a value other than 1.0, gauge metrics will always be emitted at 100% while all other metric types respect the configured rate.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit dd3bb67. Configure here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a valid bug.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method definition in taskbroker-client also has sample_rate defaulted to 1.

unit: str | None = None,
stacklevel: int = 0,
) -> None:
sentry_metrics.gauge(
key,
value,
instance=instance,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The gauge method's sample_rate defaults to 1, unlike other metrics methods which respect the global SENTRY_METRICS_SAMPLE_RATE setting.
Severity: MEDIUM

Suggested Fix

Change the signature of the gauge method to sample_rate: float | None = None. Inside the method, apply the global settings.SENTRY_METRICS_SAMPLE_RATE if sample_rate is None, mirroring the implementation of the incr, distribution, and timer methods in the same class.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: src/sentry/taskworker/adapters.py#L75

Potential issue: The `gauge` method in `SentryMetricsBackend` is defined with a default
`sample_rate` of `1`. This is inconsistent with other methods in the same class (`incr`,
`distribution`, `timer`), which accept `None` and then apply the global
`settings.SENTRY_METRICS_SAMPLE_RATE`. As a result, any calls to `gauge` from the
`taskbroker-client` library that do not explicitly provide a `sample_rate` will bypass
the global configuration and always sample at 100%. This could lead to an unexpected
increase in metric volume if a lower global sample rate is configured.

Did we get this right? 👍 / 👎 to inform future reviews.

tags=tags,
sample_rate=sample_rate,
unit=unit,
stacklevel=stacklevel,
)

def distribution(
self,
name: str,
Expand Down
6 changes: 3 additions & 3 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading