Skip to content

Conversation

@jasonyuezhang
Copy link
Owner

This allows teams to more easily dashboard and alert on API metrics for the APIs they own.

Impact on cardinality shouldn't be too bad; the set of teams is relatively small, and they should be really strongly correlated with url_name.


Copied from getsentry#102116
Original PR: getsentry#102116

@propel-test-bot propel-test-bot bot changed the title feat(api): Tag view.response counter with the ApiOwner where possible feat(api): Tag view.response metrics with ApiOwner when available Oct 24, 2025
@propel-test-bot
Copy link

Add API owner tag to view.response metric

Adds owner attribution to the view.response counter emitted by RequestTimingMiddleware. The middleware now introspects request.resolver_match.func.cls.owner (when present) and records the owner's enum .value. Test suite extended to cover default, rate-limited, UI, and owner-aware scenarios.

Key Changes

• Updated src/sentry/middleware/stats.py to derive owner tag from Endpoint classes
• Extended tests/sentry/middleware/test_stats.py with new OwnedEndpoint and three additional assertions to validate owner tagging
• Default owner falls back to "unknown" / "unowned" to keep tags consistent when no owner is declared

Affected Areas

RequestTimingMiddleware._record_time
• Unit tests for middleware metrics

This summary was automatically generated by @propel-code-bot

Comment on lines +78 to +79
if owner_enum is not None:
owner = owner_enum.value

Choose a reason for hiding this comment

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

[BestPractice]

Owner extraction logic is missing error handling. If view_class.owner exists but owner_enum.value throws an AttributeError (e.g., if owner isn't an enum), this will cause a runtime exception:

if owner_enum is not None:
    try:
        owner = owner_enum.value
    except AttributeError:
        owner = str(owner_enum)  # Fallback for non-enum values
Context for Agents
[**BestPractice**]

Owner extraction logic is missing error handling. If `view_class.owner` exists but `owner_enum.value` throws an AttributeError (e.g., if `owner` isn't an enum), this will cause a runtime exception:

```python
if owner_enum is not None:
    try:
        owner = owner_enum.value
    except AttributeError:
        owner = str(owner_enum)  # Fallback for non-enum values
```

File: src/sentry/middleware/stats.py
Line: 79

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.

3 participants