-
-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathSentryServerFilter.cs
More file actions
64 lines (52 loc) · 2.44 KB
/
SentryServerFilter.cs
File metadata and controls
64 lines (52 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using Hangfire.Server;
using Sentry.Extensibility;
namespace Sentry.Hangfire;
internal class SentryServerFilter : IServerFilter
{
internal const string SentryMonitorSlugKey = "SentryMonitorSlug";
internal const string SentryCheckInIdKey = "SentryCheckInIdKey";
private readonly IHub _hub;
private readonly IDiagnosticLogger? _logger;
public SentryServerFilter() : this(null, null)
{ }
internal SentryServerFilter(IHub? hub, IDiagnosticLogger? logger)
{
_hub = hub ?? HubAdapter.Instance;
#pragma warning disable CS0618 // Type or member is obsolete
_logger = logger ?? _hub.GetInternalSentryOptions()?.DiagnosticLogger;
#pragma warning restore CS0618 // Type or member is obsolete
}
public void OnPerforming(PerformingContext context)
{
var monitorSlug = context.GetJobParameter<string>(SentryMonitorSlugKey);
if (monitorSlug is null)
{
var jobType = context.BackgroundJob.Job.Type;
var jobMethod = context.BackgroundJob.Job.Method;
_logger?.LogDebug("Skipping creating a check-in for '{0}.{1}'. " +
"Failed to find Monitor Slug for the job. You can set the monitor slug " +
"by setting the 'SentryMonitorSlug' attribute.", jobType, jobMethod);
return;
}
var checkInId = _hub.CaptureCheckIn(monitorSlug, CheckInStatus.InProgress);
// Note that we may be overwriting context.Items[SentryCheckInIdKey] here, which is intentional. If that happens
// then implicitly OnPerforming was called previously with the same context, but we never made it to OnPerformed
// This might happen if a Hangfire job failed at least once, with automatic retries configured.
context.Items[SentryCheckInIdKey] = checkInId;
}
public void OnPerformed(PerformedContext context)
{
var monitorSlug = context.GetJobParameter<string>(SentryMonitorSlugKey);
if (monitorSlug is null)
{
return;
}
if (!context.Items.TryGetValue(SentryCheckInIdKey, out var checkInIdObject) || checkInIdObject is not SentryId checkInId)
{
return;
}
var status = context.Exception is null ? CheckInStatus.Ok : CheckInStatus.Error;
var duration = DateTime.UtcNow - context.BackgroundJob.CreatedAt;
_ = _hub.CaptureCheckIn(monitorSlug, status, checkInId, duration: duration);
}
}