Skip to content

Conversation

k-fish
Copy link
Member

@k-fish k-fish commented Oct 8, 2025

Summary

Similar to getsentry/sentry-javascript#17883, this allows the py sdk to send in new trace metric protocol items, although this code is experimental since the schema may still change. Most of this code has been copied from logs (eg. log batcher -> metrics batcher) in order to dogfood, once we're more sure of our approach we can refactor.

Closes LOGS-367

Similar to getsentry/sentry-javascript#17883, this allows the py sdk to send in new trace metric protocol items, although this code is experimental since the schema may still change. Most of this code has been copied from logs (eg. log batcher -> metrics batcher) in order to dogfood, once we're more sure of our approach we can refactor.
@k-fish k-fish requested a review from a team as a code owner October 8, 2025 22:16
Copy link

linear bot commented Oct 8, 2025

cursor[bot]

This comment was marked as outdated.

@k-fish k-fish changed the title feat(metrics): Add experimental trace metrics behind an experiments flag feat(metrics): Add trace metrics behind an experiments flag Oct 8, 2025
def _metric_to_transport_format(metric):
# type: (TraceMetric) -> Any
def format_attribute(val):
# type: (int | float | str | bool) -> Any
Copy link

Choose a reason for hiding this comment

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

Potential bug: The type hint int | float | str | bool in _trace_metrics_batcher.py is incompatible with supported Python versions older than 3.10, causing a SyntaxError on import.
  • Description: The type annotation for the format_attribute function uses the int | float | str | bool union syntax. This syntax was introduced in Python 3.10. However, the package's setup.py declares support for Python versions 3.6 and newer. As a result, importing the _trace_metrics_batcher module on Python versions 3.6 through 3.9 will raise an immediate SyntaxError, preventing the application from starting and causing the trace metrics feature to be completely non-functional on these supported versions.

  • Suggested fix: Replace the int | float | str | bool syntax with Union[int, float, str, bool] from the typing module. This will ensure the type hint is compatible with all supported Python versions from 3.6 onwards.
    severity: 0.9, confidence: 1.0

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

…ce trace metric is an internal dataset detail
Comment on lines +16 to +17
from sentry_sdk.api import get_client, get_current_scope, get_current_span
from sentry_sdk.utils import safe_repr
Copy link
Contributor

Choose a reason for hiding this comment

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

The imports should be at the top unless there's a good reason not to have them there.

See my other comment regarding avoiding circular imports.

@@ -1,4 +1,5 @@
from sentry_sdk import profiler
from sentry_sdk import trace_metrics
Copy link
Contributor

Choose a reason for hiding this comment

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

We should try to avoid circular imports. See the other comment I left. We also don't re-export many other modules. Can we remove the re-export?

To my knowledge the only downside is that it wouldn't be included in from sentry_sdk import *. If there's a good reason to keep it we should still try to avoid the circular import when importing at the top of a file.

Note that most other modules are not re-exported here.

sentrivana added a commit that referenced this pull request Oct 9, 2025
### Description
Remove old metrics code to make way for
#4898

Metrics was always an experimental feature and Sentry stopped accepting
metrics a year ago.

#### Issues
<!--
* resolves: #1234
* resolves: LIN-1234
-->

#### Reminders
- Please add tests to validate your changes, and lint your code using
`tox -e linters`.
- Add GH Issue ID _&_ Linear ID (if applicable)
- PR title should use [conventional
commit](https://develop.sentry.dev/engineering-practices/commit-messages/#type)
style (`feat:`, `fix:`, `ref:`, `meta:`)
- For external contributors:
[CONTRIBUTING.md](https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md),
[Sentry SDK development docs](https://develop.sentry.dev/sdk/), [Discord
community](https://discord.gg/Ww9hbqr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants