Skip to content

Replace BaseScope with IScope #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Nov 21, 2020
Merged

Replace BaseScope with IScope #590

merged 15 commits into from
Nov 21, 2020

Conversation

Tyrrrz
Copy link
Contributor

@Tyrrrz Tyrrrz commented Nov 17, 2020

Rearranging scope data and cleaning up the code. Required for implementing transactions.
Some side effects of the changes:

  • Changed Breadcrumbs from ConcurrentQueue<T> to a lock-synchronized List<T>. This is because ConcurrentQueue<T> doesn't implement ICollection<T> or any other common abstraction that could be used in IScope. Synchronized list is less efficient, but for small collections that don't get updated on a hot path it's actually really good.
  • Removed null defaults for collections. As I understand it, this was only used to avoid serializing additional properties if they are unset. Replaced this behavior with a special attribute and a custom contract resolver instead. Previous workaround was also inefficient (serializer still dynamically read those properties, forcing them to initialize) and lead to unnecessary nulls appearing in the code base. After this change, the collections are now null-safe as an additional benefit. I didn't extend this on objects like Contexts and Request because it's more difficult and probably not worth it. Ultimately, manual serialization fixes all those problems in a much more elegant way.

wip

Verified

This commit was signed with the committer’s verified signature. The key has expired.
philipphofmann Philipp Hofmann
@Tyrrrz Tyrrrz added Feature New feature or request Sentry labels Nov 17, 2020
@Tyrrrz Tyrrrz marked this pull request as ready for review November 18, 2020 14:35
@Tyrrrz Tyrrrz requested a review from bruno-garcia as a code owner November 18, 2020 14:35
@Tyrrrz
Copy link
Contributor Author

Tyrrrz commented Nov 18, 2020

@bruno-garcia

@codecov-io
Copy link

codecov-io commented Nov 18, 2020

Codecov Report

Merging #590 (be32c17) into main (bea2086) will decrease coverage by 0.00%.
The diff coverage is 77.84%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #590      +/-   ##
==========================================
- Coverage   81.58%   81.58%   -0.01%     
==========================================
  Files         151      149       -2     
  Lines        3986     4023      +37     
  Branches      879      885       +6     
==========================================
+ Hits         3252     3282      +30     
- Misses        490      494       +4     
- Partials      244      247       +3     
Impacted Files Coverage Δ
src/Sentry.AspNetCore.Grpc/ScopeExtensions.cs 0.00% <ø> (ø)
src/Sentry.AspNetCore/ScopeExtensions.cs 86.56% <ø> (ø)
...entry/Internal/Extensions/CollectionsExtensions.cs 66.66% <50.00%> (-33.34%) ⬇️
src/Sentry/ScopeExtensions.cs 68.61% <72.03%> (+21.24%) ⬆️
src/Sentry/Internal/Json.cs 94.23% <88.23%> (-3.00%) ⬇️
src/Sentry/Scope.cs 86.44% <94.11%> (+4.62%) ⬆️
src/Sentry/Protocol/SentryEvent.cs 100.00% <100.00%> (ø)
src/Sentry/SentryOptions.cs 94.26% <0.00%> (+0.81%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bea2086...be32c17. Read the comment docs.

Copy link
Member

@bruno-garcia bruno-garcia left a comment

Choose a reason for hiding this comment

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

Left a few notes

/// <inheritdoc />
[DataMember(Name = "breadcrumbs", EmitDefaultValue = false)]
[DontSerializeEmpty]
public ICollection<Breadcrumb> Breadcrumbs { get; } = new SynchronizedList<Breadcrumb>();
Copy link
Member

Choose a reason for hiding this comment

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

I'm not a fan of this custom "synchronized list". Doesnt' ConcurrentBag and List both implement IEnumerable?

The extension method is internal and can type cast to the type we know we support in both implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IEnumerable doesn't have .Add().
ConcurrentBag implements ICollection but doesn't guarantee order.

Copy link
Member

Choose a reason for hiding this comment

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

We don't want Add exposed to the user. We need Add ourselves on the AddBreadcrumb method (before we just typecast to the right type)

Copy link
Member

Choose a reason for hiding this comment

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

Right about the order. I don't see the reason to change the way this was done before tbqh

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to avoid leaky abstraction from IScope, because then AddBreadcrumb would be coupled to both SentryEvent and Scope at runtime without a way to validate its assumptions statically.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another approach is to add AddBreadcrumb directly to IScope, which would eliminate the abstraction issues and would probably be the cleanest solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Anyway, I don't really care about this too much and I guess it's safer to just keep things how they were. Reverted it back to ConcurrentQueue on Scope.

Copy link
Member

Choose a reason for hiding this comment

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

OK adding to the interface sounds OK

/// <inheritdoc />
[DataMember(Name = "extra", EmitDefaultValue = false)]
[DontSerializeEmpty]
public IDictionary<string, object?> Extra { get; } = new ConcurrentDictionary<string, object?>();
Copy link
Member

Choose a reason for hiding this comment

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

In the old implementation these lists were not allocated at all. Now we get these two lists instatiated for each new Scope pushed/poped even if nothing is ever added

@Tyrrrz Tyrrrz requested a review from bruno-garcia November 18, 2020 20:11
@Tyrrrz Tyrrrz requested a review from bruno-garcia November 20, 2020 20:30
@bruno-garcia bruno-garcia merged commit f416d15 into main Nov 21, 2020
@bruno-garcia bruno-garcia deleted the remove-base-scope branch November 21, 2020 00:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants