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

feat: Crons support (Hangfire) #3128

Merged
merged 50 commits into from
Feb 28, 2024
Merged

feat: Crons support (Hangfire) #3128

merged 50 commits into from
Feb 28, 2024

Conversation

bitsandfoxes
Copy link
Contributor

@bitsandfoxes bitsandfoxes commented Feb 7, 2024

Resolves #2239

This PR accomplishes the following things:

  • It adds the SentrySdk.CaptureCheckIn API for manual capture
  • It adds a Hangfire integration

The Hangfire integration works as follows:
It adds an extension to the hangfire config allowing the SDK to add its filter for all jobs.

builder.Services.AddHangfire(configuration => configuration
    .UseSentry() // <- Add Sentry to automatically send check-ins
);

For the integration to work it requires a monitor slug that needs to be present. The monitor slug can be added via our own HangfireAttribute on the job running in the background.

public class SomeJob
{
    [SentryMonitorSlug("first-job")]
    public void Execute() { /* do something */ }
}

with no changes required to the call into Hangfire

BackgroundJob.Enqueue<SomeJob>(job => job.Execute());

Copy link
Contributor

github-actions bot commented Feb 7, 2024

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against bc64124

@bitsandfoxes bitsandfoxes marked this pull request as draft February 7, 2024 12:15
src/Sentry.Hangfire/Sentry.Hangfire.csproj Outdated Show resolved Hide resolved
src/Sentry.Hangfire/Sentry.Hangfire.csproj Outdated Show resolved Hide resolved
src/Sentry.Hangfire/Sentry.Hangfire.csproj Outdated Show resolved Hide resolved
src/Sentry/SentryCheckIn.cs Outdated Show resolved Hide resolved
src/Sentry/SentryCheckIn.cs Outdated Show resolved Hide resolved
src/Sentry/SentryCheckIn.cs Outdated Show resolved Hide resolved
@bitsandfoxes bitsandfoxes marked this pull request as ready for review February 20, 2024 20:56

public class SecondJob
{
[SentryMonitorSlug("job-that-throws")]
Copy link
Member

Choose a reason for hiding this comment

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

can we use the method name by default? so adding this attribute becomes optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would cause the SDK to send a CheckIn with every Hangfire job. Is this something we'd be okay with?

internal SentryServerFilter(IHub hub, IDiagnosticLogger? logger = null)
{
_hub = hub;
_logger = logger ?? _hub.GetSentryOptions()?.DiagnosticLogger;
Copy link
Member

Choose a reason for hiding this comment

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

when do we not get a logger? seems YAGNI?

src/Sentry.Hangfire/SentryServerFilter.cs Outdated Show resolved Hide resolved
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;net6.0</TargetFrameworks>
Copy link
Member

Choose a reason for hiding this comment

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

Not testing the .NET Framework build?

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 forgot to add the explainer: Hangfire is not strong naming their assemblies and CI chokes on Windows on net48. Due to the viral nature and the use of InternalsVisibleTo I could not get it to opt out.

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.

Only thing I'd consider 'blocking' is agreeing if we need 2 public APIs instead of only 1 (with 3 arguments)

Copy link
Collaborator

@jamescrosswell jamescrosswell left a comment

Choose a reason for hiding this comment

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

LGTM - just a couple of comments.

src/Sentry.Hangfire/SentryServerFilter.cs Show resolved Hide resolved
src/Sentry/Internal/Hub.cs Outdated Show resolved Hide resolved
@bitsandfoxes bitsandfoxes merged commit ee3ea36 into main Feb 28, 2024
30 checks passed
@bitsandfoxes bitsandfoxes deleted the feat/hangfire branch February 28, 2024 20:05
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.

Support for Crons on Hangfire
4 participants