/
ClientReportRecorder.cs
65 lines (54 loc) · 2.05 KB
/
ClientReportRecorder.cs
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
65
using System.Collections.Generic;
using System.Linq;
using Sentry.Infrastructure;
namespace Sentry.Internal
{
internal class ClientReportRecorder : IClientReportRecorder
{
private readonly SentryOptions _options;
private readonly ISystemClock _clock;
private readonly ThreadsafeCounterDictionary<DiscardReasonWithCategory> _discardedEvents = new();
// discarded events are exposed internally for testing
internal IReadOnlyDictionary<DiscardReasonWithCategory, int> DiscardedEvents => _discardedEvents;
public ClientReportRecorder(SentryOptions options, ISystemClock? clock = default)
{
_options = options;
_clock = clock ?? SystemClock.Clock;
}
public void RecordDiscardedEvent(DiscardReason reason, DataCategory category)
{
// Don't count discarded events if we're not going to be sending them.
if (!_options.SendClientReports)
{
return;
}
// Increment the counter for the discarded event.
_discardedEvents.Increment(reason.WithCategory(category));
}
public ClientReport? GenerateClientReport()
{
// Don't attach a client report if we've turned them off.
if (!_options.SendClientReports)
{
return null;
}
// Read and reset discarded events.
var discardedEvents = _discardedEvents.ReadAllAndReset();
// Don't attach a client report if there's nothing to report.
if (!discardedEvents.Any(x => x.Value > 0))
{
return null;
}
// Generate the client report.
var timestamp = _clock.GetUtcNow();
return new ClientReport(timestamp, discardedEvents);
}
public void Load(ClientReport clientReport)
{
foreach (var kvp in clientReport.DiscardedEvents)
{
_discardedEvents.Add(kvp.Key, kvp.Value);
}
}
}
}