-
-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathProtobufRequestExtractionDispatcher.cs
More file actions
81 lines (68 loc) · 2.67 KB
/
ProtobufRequestExtractionDispatcher.cs
File metadata and controls
81 lines (68 loc) · 2.67 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using Google.Protobuf;
using Sentry.Extensibility;
using Sentry.Internal.Extensions;
namespace Sentry.AspNetCore.Grpc;
/// <summary>
/// Dispatches request message extractions if enabled and within limits.
/// </summary>
public class ProtobufRequestExtractionDispatcher : IProtobufRequestPayloadExtractor
{
private readonly SentryOptions _options;
private readonly Func<RequestSize> _sizeSwitch;
internal IEnumerable<IProtobufRequestPayloadExtractor> Extractors { get; }
/// <summary>
/// Creates a new instance of <see cref="ProtobufRequestExtractionDispatcher"/>.
/// </summary>
/// <param name="extractors">Extractors to use.</param>
/// <param name="options">Sentry Options.</param>
/// <param name="sizeSwitch">The max request size to capture.</param>
public ProtobufRequestExtractionDispatcher(IEnumerable<IProtobufRequestPayloadExtractor> extractors,
SentryOptions options, Func<RequestSize> sizeSwitch)
{
ArgumentNullException.ThrowIfNull(extractors);
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(sizeSwitch);
Extractors = extractors;
_options = options;
_sizeSwitch = sizeSwitch;
}
/// <summary>
/// Extract the payload using the provided extractors.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>A serializable representation of the payload.</returns>
public IMessage? ExtractPayload<TRequest>(IProtobufRequest<TRequest> request)
where TRequest : class, IMessage
{
// Not to throw on code that ignores nullability warnings.
if (request.IsNull())
{
return null;
}
var size = _sizeSwitch();
switch (size)
{
case RequestSize.Small when request.ContentLength < 4_000:
case RequestSize.Medium when request.ContentLength < 10_000:
case RequestSize.Always:
_options.Log(SentryLevel.Debug,
"Attempting to read request body of size: {0}, configured max: {1}.",
null, request.ContentLength, size);
foreach (var extractor in Extractors)
{
var data = extractor.ExtractPayload(request);
if (data == null)
{
continue;
}
return data;
}
break;
// Request body extraction is opt-in
case RequestSize.None:
_options.Log(SentryLevel.Debug, "Skipping request body extraction.");
return null;
}
return null;
}
}