Skip to content

chore: Refactor SentryAttributes into a separate class#4936

Merged
jamescrosswell merged 11 commits into
mainfrom
sentry-attributes
May 13, 2026
Merged

chore: Refactor SentryAttributes into a separate class#4936
jamescrosswell merged 11 commits into
mainfrom
sentry-attributes

Conversation

@jamescrosswell
Copy link
Copy Markdown
Collaborator

Before closing #4912 I figured we could extract some of the refactorings that might be useful in other PRs dealing with Attributes. Basically there's some functionality that is only accessible in the SentryMetrics class at the moment, but which will be needed by any other classes that also have Attributes.

#skip-changelog

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Feb 19, 2026

Semver Impact of This PR

None (no version bump detected)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


This PR will not appear in the changelog.


🤖 This preview updates automatically when you update the PR.

@jamescrosswell jamescrosswell changed the title Refactored SentryAttributes into a separate class chore: Refactor SentryAttributes into a separate class Feb 19, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 19, 2026

Codecov Report

❌ Patch coverage is 80.95238% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.12%. Comparing base (0b59060) to head (7699aaa).
⚠️ Report is 14 commits behind head on main.

Files with missing lines Patch % Lines
src/Sentry/Protocol/SentryAttributes.cs 75.55% 10 Missing and 1 partial ⚠️
src/Sentry/SentryMetric.Factory.cs 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4936      +/-   ##
==========================================
+ Coverage   74.08%   74.12%   +0.03%     
==========================================
  Files         506      508       +2     
  Lines       18247    18282      +35     
  Branches     3564     3574      +10     
==========================================
+ Hits        13519    13551      +32     
- Misses       3858     3861       +3     
  Partials      870      870              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jamescrosswell jamescrosswell marked this pull request as ready for review February 20, 2026 04:39
Comment thread test/Sentry.Testing/SentryAttributesExtensions.cs
Comment thread src/Sentry/Protocol/SentryAttributes.cs
Comment thread src/Sentry/SentryMetric.cs
Comment thread src/Sentry/SentryLog.cs
Comment thread src/Sentry/SentryLog.cs Outdated
@jamescrosswell jamescrosswell requested a review from Flash0ver March 5, 2026 22:39
@jamescrosswell
Copy link
Copy Markdown
Collaborator Author

jamescrosswell commented May 6, 2026

@Flash0ver is there any reason not to approve/merge this PR? The changes are internal only (so we can easily refactor again later if we need) and all the tests pass. Can we not just get this out of the way so we can move on to other stuff? It's been waiting for a review for over 2 months now.

Copy link
Copy Markdown
Member

@Flash0ver Flash0ver left a comment

Choose a reason for hiding this comment

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

No reason ... I was just slow ... getting all the reviews done now.

I love the refactoring ❤️.
This removes duplicate code, and gives even more control to testing.

I got a couple of suggestions to make the implementation and usage aligned across SentryMetric and SentryLog.

Comment thread src/Sentry/Protocol/SentryAttributes.cs
Comment thread src/Sentry/Protocol/SentryAttributes.cs
Comment thread src/Sentry/Protocol/SentryAttributes.cs
Comment thread src/Sentry/SentryLog.cs Outdated
Comment thread src/Sentry/SentryLog.cs Outdated
Comment thread src/Sentry/SentryMetric.cs
Comment thread test/Sentry.Tests/SentryMetricTests.cs Outdated
Comment thread test/Sentry.Tests/SentryMetricTests.cs Outdated
Comment thread src/Sentry/SentryLog.cs Outdated
jamescrosswell and others added 2 commits May 12, 2026 13:09
Replace the private `_attributes` field with an `internal SentryAttributes Attributes { get; }` property after `SpanId`, matching the `SentryMetric` pattern. Remove the internal typed overloads (`TryGetAttribute<string>`, `SetAttribute(string/char/int)`) and update all callers to go through `log.Attributes` directly. Update tests to use `ShouldContain`/`ShouldNotContain` extension methods.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…utes

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit c48cbd8. Configure here.

Comment thread src/Sentry/SentryLog.cs
jamescrosswell and others added 2 commits May 12, 2026 13:20
Change ShouldNotContain<string> to ShouldNotContain<object> to match
previous semantics, and replace Attributes.SetAttribute("null", null!)
with the public metric.SetAttribute<object>("null", null!) overload.
Remove the now-unreachable null warning expectation.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Remove the early null-return from SentryAttributes.SetAttribute so null
values are stored in the collection and the existing null warning in
SentryAttributeSerializer fires consistently during serialization for
both SentryLog and SentryMetric.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jamescrosswell jamescrosswell requested a review from Flash0ver May 12, 2026 01:54
Copy link
Copy Markdown
Member

@Flash0ver Flash0ver left a comment

Choose a reason for hiding this comment

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

✅ with some final set of nitpicks about comments and tests/assertions

Comment thread src/Sentry/Protocol/SentryAttributes.cs Outdated
Comment thread src/Sentry/Protocol/SentryAttributes.cs Outdated
Comment thread test/Sentry.Tests/SentryLogTests.cs Outdated
Comment thread test/Sentry.Tests/SentryLogTests.cs Outdated
Comment thread test/Sentry.Tests/SentryLogTests.cs Outdated
Comment thread src/Sentry/Protocol/SentryAttributes.cs Outdated
Comment thread src/Sentry/Protocol/SentryAttributes.cs Outdated
- SentryAttributes.TryGetAttribute: "this" → "this attribute collection"
- SentryAttributes.SetAttribute: rephrase summary doc
- SentryAttributes.SetAttribute<T>: remove unnecessary null-suppression operator
- SentryAttributes.TryGetAttribute: update seealso href to attributes docs
- SentryLogTests: use ShouldContain/ShouldNotContain instead of TryGetAttribute
- SentryLogTests: remove unnecessary null-forgiving operators

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jamescrosswell jamescrosswell merged commit 0f91f7d into main May 13, 2026
44 checks passed
@jamescrosswell jamescrosswell deleted the sentry-attributes branch May 13, 2026 01:10
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