Skip to content

Improve SentryEvent public API #2273

Closed
Closed

Description

@mattjohnsonpint

Problem Statement

The SentryEvent is generally allowed to be mutated. Most properties have setters, or other mechanisms for modification. This is useful when creating a new event to capture manually, and also when modifying an event via BeforeSend.

However, we have some inconsistencies with regards to treatment of dictionaries.

Specifically:

  • Breadcrumbs can only have items added via AddBreadcrumb. The API doesn't provide a direct way to modify or remove breadcrumbs once they are on the event. (Breadcrumb objects are immutable.)
    • BeforeBreadcrumb can be used to keep them off the event, but there may be need to modify in BeforeSend as well, based on other criteria specific to the event.
    • One could cast to List<Breadcrumb> or IList<Breadcrumb> manually, but that is brittle and shouldn't be required.
  • Extra can only have items added via SetExtra.
    • You can set null to overwrite an existing item, but the null will still end up in the data.
    • One could cast to Dictionary<string, object?> or IDictionary<string, object?> manually, but that is brittle and shouldn't be required.
  • Tags has both SetTag and UnsetTag methods, so it is mutable for all practical reasons. However, it's not clear why these methods are needed, rather than just exposing IDictionary<string, string>. It also makes it cumbersome to clear all entries at once.
    • One can cast to Dictionary<string, string> or IDictionary<string, string> manually, but that is brittle and shouldn't be required.

Solution Brainstorm

In next major release (4.0.0), change the public API of SentryEvent to expose all collection and dictionary properties on SentryEvent as IList<T> or IDictionary<TKey, TValue>.

We can then remove the AddBreadcrumb, SetExtra, SetTag, and UnsetTag methods.

The only other non-mutable properties would be:

  • EventId, Timestamp, and Sdk - which should probably remain immutable.
  • Exception, which we might consider adding a setter for.

Investigation

On the Scope, Breadcrumbs are stored internally in a ConcurrentQueue<Breadcrumb>... presumably because once we hit SentryOptions.MaxBreadcrumbs we start throwing away the old ones. For this reason, I think it makes sense to leave Breadcrumbs as they are.

Tags is feasible. The only complication is that we have an IScopeObserver to sync the scope to iOS. So I've had to wrap the ConcurrentDictionary that was being used to store tags internally in an InterceptingDictionary class that let's us intercept the settter/getter on the index property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Feature

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions