Skip to content
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

Allow custom processors to be added as a scoped dependency #1979

Merged
merged 9 commits into from
Oct 15, 2022

Conversation

chrisza4
Copy link
Contributor

@chrisza4 chrisza4 commented Oct 8, 2022

Refer from issue #318

We can fixed this by turning SentryMiddleware to be a factory-based middleware. But this also required some interfaces changes and now all custom event processors will be injected using PopulateScope during OnEvaluation.

One concern is that this changes might break behaviour of public method GetAllEventProcessors since now it can only be used after scope is evaluated. I'm not sure if that could be a concern.

Alternatively, we can move out from onEvaluating hook but I don't know if that is more useful so I just put it there first to make it easier to write test in same pattern (testing PopulateScope). Let me know if you have other opinion.

@mattjohnsonpint
Copy link
Contributor

mattjohnsonpint commented Oct 9, 2022

Thanks for this! Looks promising on first glance. I'll review in more detail later this week.

@SimonCropp
Copy link
Contributor

wouldn't this result in: processors explicitly added to options being ignored?

@chrisza4
Copy link
Contributor Author

Few test failed. Let me take a look.

@SimonCropp

wouldn't this result in: processors explicitly added to options being ignored?

As I read into Sentry SDK doc, I thought we can only use processors via dependency injection. If there is other way then let me know where and I can take a look.

@mattjohnsonpint
Copy link
Contributor

During initialization, event processors can be added to the options:

.UseSentry(options =>
{
    options.AddEventProcessor(new MyEventProcessor());
}

They can also be added for a given scope:

SentrySdk.ConfigureScope(scope =>
{
    scope.AddEventProcessor(new MyEventProcessor());
});

Both of those would need to continue to work.

@mattjohnsonpint
Copy link
Contributor

mattjohnsonpint commented Oct 12, 2022

Looking over the PR, the main issue I see is now *all* processors are registered on the scope.

Instead, let's control this based on how the processor was registered with DI.

  • If registered using AddSingleton, then it should be applied globally - as if registered directly on the options during initialization. This is the existing behavior.
  • If registered using AddScoped, then it should be applied for the current scope - as if registered with SentrySdk.ConfigureScope.

@mattjohnsonpint
Copy link
Contributor

Actually, ignore my last comments. There's no need to separate those paths out, as the effect would be the same as the approach you're currently taking. In other words, registering the same instance of an event processor on each scope is identical to registering it once on options - since we gather both at the same time when we run them.

@mattjohnsonpint
Copy link
Contributor

mattjohnsonpint commented Oct 12, 2022

I see we are doing this for event processors and exception processors. Please also do the same for transaction processors added in #1862. Looks like we missed this back then. Thanks.

@mattjohnsonpint
Copy link
Contributor

@SimonCropp

wouldn't this result in: processors explicitly added to options being ignored?

No, because scope.GetAllEventProcessors() returns both:

/// <summary>
/// Invokes all event processor providers available.
/// </summary>
/// <param name="scope">The Scope which holds the processor providers.</param>
public static IEnumerable<ISentryEventProcessor> GetAllEventProcessors(this Scope scope)
{
foreach (var processor in scope.Options.GetAllEventProcessors())
{
yield return processor;
}
foreach (var processor in scope.EventProcessors)
{
yield return processor;
}
}

@mattjohnsonpint
Copy link
Contributor

Needs transaction processors added, and a changelog entry, then LGTM. Thanks!

Co-authored-by: Matt Johnson-Pint <mattjohnsonpint@gmail.com>
@mattjohnsonpint mattjohnsonpint changed the title Allow user to use CustomEventProcessor as Scoped Dependency (AddScoped) Allow custom processors to be added as a scoped dependency Oct 15, 2022
@mattjohnsonpint
Copy link
Contributor

I added the changelog entry. We'll do transaction processors in a separate PR. Thanks for your contribution!

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.

Unable to use ISentryEventExceptionProcessor with Scoped DI
3 participants