-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
SentryMessageHandler.cs
175 lines (153 loc) · 6.28 KB
/
SentryMessageHandler.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using Sentry.Extensibility;
using Sentry.Internal.Extensions;
namespace Sentry;
/// <summary>
/// Special HTTP message handler that can be used to propagate Sentry headers and other contextual information.
/// </summary>
public abstract class SentryMessageHandler : DelegatingHandler
{
private readonly IHub _hub;
private readonly SentryOptions? _options;
/// <summary>
/// Constructs an instance of <see cref="SentryMessageHandler"/>.
/// </summary>
protected SentryMessageHandler()
: this(default, default, default) { }
/// <summary>
/// Constructs an instance of <see cref="SentryMessageHandler"/>.
/// </summary>
/// <param name="innerHandler">An inner message handler to delegate calls to.</param>
protected SentryMessageHandler(HttpMessageHandler innerHandler)
: this(default, default, innerHandler) { }
/// <summary>
/// Constructs an instance of <see cref="SentryMessageHandler"/>.
/// </summary>
/// <param name="hub">The Sentry hub.</param>
protected SentryMessageHandler(IHub hub)
: this(hub, default)
{
}
/// <summary>
/// Constructs an instance of <see cref="SentryMessageHandler"/>.
/// </summary>
/// <param name="innerHandler">An inner message handler to delegate calls to.</param>
/// <param name="hub">The Sentry hub.</param>
protected SentryMessageHandler(HttpMessageHandler innerHandler, IHub hub)
: this(hub, default, innerHandler)
{
}
internal SentryMessageHandler(IHub? hub, SentryOptions? options, HttpMessageHandler? innerHandler = default)
{
_hub = hub ?? HubAdapter.Instance;
_options = options ?? _hub.GetSentryOptions();
// Only assign the inner handler if it is supplied. We can't assign null or it will throw.
// We also cannot assign a default value here, or it will throw when used with HttpMessageHandlerBuilderFilter.
if (innerHandler is not null)
{
InnerHandler = innerHandler;
}
}
/// <summary>
/// Starts a span for a request
/// </summary>
/// <param name="request">The <see cref="HttpRequestMessage"/></param>
/// <param name="method">The request method (e.g. "GET")</param>
/// <param name="url">The request URL</param>
/// <returns>An <see cref="ISpan"/></returns>
protected internal abstract ISpan? ProcessRequest(HttpRequestMessage request, string method, string url);
/// <summary>
/// Provides an opportunity for further processing of the span once a response is received.
/// </summary>
/// <param name="response">The <see cref="HttpResponseMessage"/></param>
/// <param name="span">The <see cref="ISpan"/> created in <see cref="ProcessRequest"/></param>
/// <param name="method">The request method (e.g. "GET")</param>
/// <param name="url">The request URL</param>
protected internal abstract void HandleResponse(HttpResponseMessage response, ISpan? span, string method, string url);
/// <inheritdoc />
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
var method = request.Method.Method.ToUpperInvariant();
var url = request.RequestUri?.ToString() ?? string.Empty;
PropagateTraceHeaders(request, url);
var span = ProcessRequest(request, method, url);
try
{
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
HandleResponse(response, span, method, url);
return response;
}
catch (Exception ex)
{
span?.Finish(ex);
throw;
}
}
#if NET5_0_OR_GREATER
/// <inheritdoc />
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
{
var method = request.Method.Method.ToUpperInvariant();
var url = request.RequestUri?.ToString() ?? string.Empty;
PropagateTraceHeaders(request, url);
var span = ProcessRequest(request, method, url);
try
{
var response = base.Send(request, cancellationToken);
HandleResponse(response, span, method, url);
return response;
}
catch (Exception ex)
{
span?.Finish(ex);
throw;
}
}
#endif
private void PropagateTraceHeaders(HttpRequestMessage request, string url)
{
// Assign a default inner handler for convenience the first time this is used.
// We can't do this in a constructor, or it will throw when used with HttpMessageHandlerBuilderFilter.
InnerHandler ??= new HttpClientHandler();
if (_options?.TracePropagationTargets.ContainsMatch(url) is true or null)
{
AddSentryTraceHeader(request);
AddBaggageHeader(request);
}
}
private void AddSentryTraceHeader(HttpRequestMessage request)
{
// Set trace header if it hasn't already been set
if (!request.Headers.Contains(SentryTraceHeader.HttpHeaderName) && _hub.GetTraceHeader() is { } traceHeader)
{
request.Headers.Add(SentryTraceHeader.HttpHeaderName, traceHeader.ToString());
}
}
private void AddBaggageHeader(HttpRequestMessage request)
{
var baggage = _hub.GetBaggage();
if (baggage is null)
{
return;
}
if (request.Headers.TryGetValues(BaggageHeader.HttpHeaderName, out var baggageHeaders))
{
var headers = baggageHeaders.ToList();
if (headers.Any(h => h.StartsWith(BaggageHeader.SentryKeyPrefix)))
{
// The Sentry headers have already been added to this request. Do nothing.
return;
}
// Merge existing baggage headers with ours.
var allBaggage = headers
.Select(s => BaggageHeader.TryParse(s)).ExceptNulls()
.Append(baggage);
baggage = BaggageHeader.Merge(allBaggage);
// Remove the existing header so we can replace it with the merged one.
request.Headers.Remove(BaggageHeader.HttpHeaderName);
}
// Set the baggage header
request.Headers.Add(BaggageHeader.HttpHeaderName, baggage.ToString());
}
}