-
Notifications
You must be signed in to change notification settings - Fork 2
/
SentryClient.cs
221 lines (172 loc) · 7.58 KB
/
SentryClient.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
namespace SentryDotNet
{
public class SentryClient : ISentryClient
{
private readonly HttpClient _httpClient = new HttpClient();
private readonly Func<HttpRequestMessage, Task<HttpResponseMessage>> _sendHttpRequestFunc;
private readonly SentryEventDefaults _defaults;
private readonly decimal _sampleRate;
private readonly Action<string> _onError;
/// <summary>
/// Creates a client capable of sending events to Sentry.
/// </summary>
/// <param name="dsn">The DSN to use when sending events to Sentry.</param>
/// <param name="defaults">Defaults object that will be used to prepopulate the event builder. Put static data
/// that should always be sent to Sentry here.</param>
/// <param name="sampleRate">The percentage of events that are actually sent to Sentry e.g. 0.26.</param>
/// <param name="sendHttpRequestFunc">Function that invokes a HttpClient with the given request. This may be used to install
/// retry policies or share the HttpClient. If not provided, HttpClient.SendAsync on the internal client will be used.</param>
/// <param name="onError">Error message callback. If null (the default) an exception is thrown instead of logging the error.</param>
public SentryClient(
string dsn,
SentryEventDefaults defaults = null,
decimal sampleRate = 1m,
Func<HttpRequestMessage, Task<HttpResponseMessage>> sendHttpRequestFunc = null,
Action<string> onError = null)
{
Dsn = string.IsNullOrEmpty(dsn) ? null : new Dsn(dsn);
if (sampleRate < 0 || sampleRate > 1)
{
throw new ArgumentException("sample rate must be in the [0,1] interval", nameof(sampleRate));
}
_defaults = defaults ?? new SentryEventDefaults();
_sampleRate = sampleRate;
_onError = onError;
_sendHttpRequestFunc = sendHttpRequestFunc ?? (async r => await _httpClient.SendAsync(r));
}
public Dsn Dsn { get; }
public async Task<string> SendAsync(SentryEvent sentryEvent)
{
if (Dsn == null)
{
return "";
}
if (sentryEvent == null)
{
throw new ArgumentNullException(nameof(sentryEvent));
}
if (_sampleRate < 1 && Convert.ToDecimal(new Random().NextDouble()) > _sampleRate)
{
return "";
}
return await SerializeAndSendAsync(sentryEvent);
}
public async Task<string> CaptureAsync(Exception exception)
{
if (Dsn == null)
{
return "";
}
var builder = CreateEventBuilder();
builder.SetException(exception);
return await builder.CaptureAsync();
}
public async Task<string> CaptureAsync(FormattableString message)
{
if (Dsn == null)
{
return "";
}
var builder = CreateEventBuilder();
builder.SetMessage(message);
return await builder.CaptureAsync();
}
public async Task<string> CaptureAsync(object message)
{
if (Dsn == null)
{
return "";
}
var builder = CreateEventBuilder();
builder.SetMessage(message);
return await builder.CaptureAsync();
}
public SentryEventBuilder CreateEventBuilder()
{
var builder = new SentryEventBuilder(this)
{
Logger = _defaults.Logger,
Level = _defaults.Level,
ServerName = _defaults.ServerName,
Release = _defaults.Release,
Tags = _defaults.Tags.ToDictionary(p => p.Key, p => p.Value),
Environment = _defaults.Environment,
Modules = _defaults.Modules.ToDictionary(p => p.Key, p => p.Value),
Extra = _defaults.Extra,
Contexts = _defaults.Contexts.ToDictionary(p => p.Key, p => p.Value)
};
return builder;
}
private async Task<string> SerializeAndSendAsync(SentryEvent sentryEvent)
{
var request = PrepareRequest(sentryEvent);
var response = await _sendHttpRequestFunc(request);
if (response.StatusCode != HttpStatusCode.OK)
{
var errorInfo = response.Headers.Contains("X-Sentry-Error")
? string.Join("\n", response.Headers.GetValues("X-Sentry-Error"))
: "";
if (_onError == null)
{
throw new SentryClientException(HttpStatusCode.BadRequest, errorInfo);
}
_onError("Error while communicating with sentry API. " + errorInfo);
}
var responseMessage = JsonConvert.DeserializeObject<SentrySuccessResponse>(await response.Content.ReadAsStringAsync());
return responseMessage.Id;
}
private HttpRequestMessage PrepareRequest(SentryEvent sentryEvent)
{
var request = new HttpRequestMessage(HttpMethod.Post, Dsn.ReportApiUri) { Content = Serialize(sentryEvent) };
request.Headers.UserAgent.Add(new ProductInfoHeaderValue(sentryEvent.Sdk.Name, sentryEvent.Sdk.Version));
var unixTimeSeconds = ((DateTimeOffset) sentryEvent.Timestamp).ToUnixTimeSeconds();
var client = $"{sentryEvent.Sdk.Name}/{sentryEvent.Sdk.Version}";
request.Headers.Add(
"X-Sentry-Auth",
$"Sentry sentry_version=7,sentry_timestamp={unixTimeSeconds},sentry_key={Dsn.PublicKey},sentry_secret={Dsn.PrivateKey},sentry_client={client}");
return request;
}
private StreamContent Serialize(SentryEvent sentryEvent)
{
var payload = JsonConvert.SerializeObject(sentryEvent, CreateSerializerSettings());
var payloadBytes = Encoding.UTF8.GetBytes(payload);
var ms = new MemoryStream();
using (var gzip = new GZipStream(ms, CompressionMode.Compress, true))
{
gzip.Write(payloadBytes, 0, payloadBytes.Length);
}
ms.Position = 0;
return new StreamContent(ms)
{
Headers = {ContentType = new MediaTypeHeaderValue("application/json"), ContentEncoding = {"gzip"}}
};
}
private static JsonSerializerSettings CreateSerializerSettings()
{
var serializer = new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy(false, false) }
};
serializer.Converters.Add(new StringEnumConverter(true));
return serializer;
}
// ReSharper disable once ClassNeverInstantiated.Local
private class SentrySuccessResponse
{
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public string Id { get; set; }
}
}
}