Skip to content

chore: Refactor SentryAttributes into a separate class#4936

Open
jamescrosswell wants to merge 10 commits into
mainfrom
sentry-attributes
Open

chore: Refactor SentryAttributes into a separate class#4936
jamescrosswell wants to merge 10 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.25%. Comparing base (0b59060) to head (7d0a860).
⚠️ 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.25%   +0.16%     
==========================================
  Files         506      508       +2     
  Lines       18247    18282      +35     
  Branches     3564     3574      +10     
==========================================
+ Hits        13519    13575      +56     
+ Misses       3858     3835      -23     
- Partials      870      872       +2     

☔ 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
}

writer.WriteEndObject();
Attributes.WriteTo(writer, logger);
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.

suggestion: revert

see also https://github.com/getsentry/sentry-dotnet/pull/4936/changes#r3217641003

As this abstraction of SentryAttributes is only used in SentryMetric, but not in SentryLog, I personally find this abstraction a bit "hasty".

Copy link
Copy Markdown
Collaborator Author

@jamescrosswell jamescrosswell May 12, 2026

Choose a reason for hiding this comment

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

It was originally used in these places as well:

_attributes.WriteTo(writer, logger);

_attributes.WriteTo(writer, logger);

So it seemed like a logical refactoring... This whole refactoring would be premature if we hadn't already tried to use it in #4912.

We can revert this if you really want, but I'm pretty sure we're just going to bring it back in again as soon as we build the next thing that has attributes. I think it just makes the code a bit more readable - one descriptively named method call instead of 9 lines of code (whose purpose you have to reverse engineer).

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
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