Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP request breadcrumb #1113

Merged
merged 9 commits into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

## Unreleased

### Fetures
### Features

- Add HTTP request breadcrumb ([#1113](https://github.com/getsentry/sentry-dotnet/pull/1113))
- Add ClearAttachments to Scope ([#1104](https://github.com/getsentry/sentry-dotnet/pull/1104))

## 3.6.1
Expand Down
16 changes: 14 additions & 2 deletions src/Sentry/SentryHttpMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -63,16 +64,27 @@ protected override async Task<HttpResponseMessage> SendAsync(

// Start a span that tracks this request
// (may be null if transaction is not set on the scope)
var requestMethod = request.Method.Method.ToUpperInvariant();
var url = request.RequestUri?.ToString() ?? string.Empty;

lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
var span = _hub.GetSpan()?.StartChild(
"http.client",
// e.g. "GET https://example.com"
$"{request.Method.Method.ToUpperInvariant()} {request.RequestUri}"
$"{requestMethod} {url}"
);

try
{
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

var breadcrumbData = new Dictionary<string, string>
{
{ "url", url },
{ "method", requestMethod },
{ "status_code", ((int)response.StatusCode).ToString() }
};
_hub.AddBreadcrumb(string.Empty, "http", "http", breadcrumbData);
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved

// This will handle unsuccessful status codes as well
span?.Finish(
SpanStatusConverter.FromHttpStatusCode(response.StatusCode)
Expand Down
46 changes: 45 additions & 1 deletion test/Sentry.Tests/SentryHttpMessageHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
Expand Down Expand Up @@ -120,5 +121,48 @@ public async Task SendAsync_ExceptionThrown_ExceptionLinkedToSpan()
// Assert
hub.Received(1).BindException(exception, Arg.Any<ISpan>()); // second argument is an implicitly created span
}

[Fact]
public async Task SendAsync_Executed_BreadcrumbCreated()
{
// Arrange
var scope = new Scope();
var hub = Substitute.For<IHub>();
hub.When(h => h.ConfigureScope(Arg.Any<Action<Scope>>()))
.Do(c => c.Arg<Action<Scope>>()(scope));

var url = "https://example.com/";

var urlKey = "url";
var methodKey = "method";
var statusKey = "status_code";
var expectedBreadcrumbData = new Dictionary<string, string>
{
{ urlKey, url },
{ methodKey, "GET" },
{ statusKey, "200" }
};
var expectedType = "http";
var expectedCategory = "http";
using var sentryHandler = new SentryHttpMessageHandler(hub);
using var client = new HttpClient(sentryHandler);

// Act
await client.GetAsync(url);
var BreadcrumbGenerated = scope.Breadcrumbs.First();

// Assert
Assert.Equal(expectedType, BreadcrumbGenerated.Type);
Assert.Equal(expectedCategory, BreadcrumbGenerated.Category);

Assert.True(BreadcrumbGenerated.Data.ContainsKey(urlKey));
Assert.Equal(expectedBreadcrumbData[urlKey], BreadcrumbGenerated.Data[urlKey]);

Assert.True(BreadcrumbGenerated.Data.ContainsKey(methodKey));
Assert.Equal(expectedBreadcrumbData[methodKey], BreadcrumbGenerated.Data[methodKey]);

Assert.True(BreadcrumbGenerated.Data.ContainsKey(statusKey));
Assert.Equal(expectedBreadcrumbData[statusKey], BreadcrumbGenerated.Data[statusKey]);
}
}
}