Skip to content

Commit 3ac0e0e

Browse files
Document HTTP Client Errors (enableCaptureFailedRequests) (#6987)
1 parent 04068f1 commit 3ac0e0e

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

src/platforms/common/configuration/options.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ _(New in version 6.6.0)_
577577
578578
</ConfigKey>
579579
580-
<ConfigKey name="capture-failed-requests" supported={["apple"]}>
580+
<ConfigKey name="capture-failed-requests" supported={["apple","dotnet"]}>
581581
582582
Once enabled, this feature automatically captures HTTP client errors, like bad response codes, as error events and reports them to Sentry.
583583
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
title: HTTP Client Errors
3+
sidebar_order: 25
4+
description: "This feature, once enabled, automatically captures HTTP client errors, like bad response codes, as error events and reports them to Sentry"
5+
---
6+
7+
Once enabled, this feature automatically captures HTTP client errors, like bad response codes, as error events and reports them to Sentry. The error event will contain information from the HTTP request and response, such as `url`, `status_code`, and so on.
8+
9+
To enable it:
10+
11+
```csharp
12+
// Add this to the SDK initialization callback
13+
options.CaptureFailedRequests = true;
14+
```
15+
16+
```fsharp
17+
// Add this to the SDK initialization callback
18+
options.CaptureFailedRequests <- true
19+
```
20+
21+
HTTP client errors will then be captured for any requests from the Sentry SDK. To capture HTTP client errors for outbound requests from your own application, pass `SentryHttpMessageHandler` as the inner handler when creating your `HttpClient`:
22+
23+
```csharp
24+
using var httpClient = new HttpClient(new SentryHttpMessageHandler());
25+
```
26+
27+
```fsharp
28+
let httpClient = new HttpClient(new SentryHttpMessageHandler())
29+
```
30+
31+
<Alert title="Tip">
32+
33+
You can skip the above step when using any of the Sentry .NET SDKs that are registered via dependency injection (`Senry.AspNetCore`, `Sentry.Maui`, `Sentry.Extensions.Logging`, etc.).
34+
35+
Instead, create your `HttpClient` using the `IHttpClientFactory` pattern, as described in [the documentation from Microsoft](https://learn.microsoft.com/aspnet/core/fundamentals/http-requests). The `SentryHttpMessageHandler` will automatically be added to the factory during initialization, so no additional wire-up is needed.
36+
37+
</Alert>
38+
39+
By default, only HTTP client errors with a response code between `500` and `599` are captured as error events, but you can change this behavior by modifying the `FailedRequestStatusCodes` option:
40+
41+
```csharp
42+
options.FailedRequestStatusCodes.Add((400, 499));
43+
```
44+
45+
```fsharp
46+
options.FailedRequestStatusCodes.Add(HttpStatusCodeRange(400, 599))
47+
```
48+
49+
HTTP client errors from every target (`.*` regular expression) are automatically captured, but you can change this behavior by updating the `FailedRequestTargets` option with either regular expressions or plain strings. A plain string must contain at least one of the items from the list. Plain strings don't have to be full matches, meaning the URL of a request is matched when it contains a substring provided through the option:
50+
51+
```csharp
52+
options.FailedRequestTargets.Add("foo"); // substring
53+
options.FailedRequestTargets.Add("foo.*bar"); // regex
54+
```
55+
56+
```fsharp
57+
options.FailedRequestTargets.Add("foo") // substring
58+
options.FailedRequestTargets.Add("foo.*bar") // regex
59+
```
60+
61+
When the `SendDefaultPii` option is enabled, error events may contain PII data such as `Headers` and `Cookies`. Sentry already does data scrubbing by default, but you can also scrub any data before it is sent. Learn more in [Scrubbing Sensitive Data](/platforms/dotnet/data-management/sensitive-data/).
62+
63+
These events are searchable and you can set alerts on them if you use the `http.url` and `http.status_code` properties. Learn more in our full [Searchable Properties](/product/sentry-basics/search/searchable-properties/) documentation.
64+
65+
### Customize or Drop the Error Event
66+
67+
The captured error event can be customized or dropped with a `beforeSend`:
68+
69+
```csharp
70+
options.SetBeforeSend((@event, hint) =>
71+
{
72+
if (hint.Items.TryGetValue(HintTypes.HttpResponseMessage, out var responseHint))
73+
{
74+
var response = (HttpResponseMessage)responseHint!;
75+
var request = response.RequestMessage!;
76+
77+
// Do something with the request and/or response... for example, you could add
78+
// some custom context to the event for specific scenarios
79+
var statusCode = response.StatusCode;
80+
if (statusCode == HttpStatusCode.Unauthorized)
81+
{
82+
@event.Contexts["Unauthorized"] = request.RequestUri.GetComponents(
83+
UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped
84+
);
85+
}
86+
}
87+
88+
// return the modified event
89+
return @event;
90+
});
91+
```
92+
93+
```fsharp
94+
options.SetBeforeSend(fun event hint ->
95+
let (isHint, responseHint) = hint.Items.TryGetValue(HintTypes.HttpResponseMessage)
96+
if isHint then
97+
let response = responseHint :?> HttpResponseMessage
98+
let request = response.RequestMessage :> HttpRequestMessage
99+
100+
// Do something with the request and/or response... for example, you could add
101+
// some custom context to the event for specific scenarios
102+
let statusCode = response.StatusCode
103+
if statusCode = HttpStatusCode.Unauthorized then
104+
event.Contexts.["Unauthorized"] <- request.RequestUri.GetComponents(UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped)
105+
106+
// return the modified event
107+
event
108+
)
109+
```

0 commit comments

Comments
 (0)