Skip to content

Commit

Permalink
Set server.address for HttpRequest spans
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescrosswell committed May 9, 2024
1 parent f8e84bb commit 043cb5f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/Sentry/SentryGraphQLHttpMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ internal SentryGraphQLHttpMessageHandler(IHub? hub, SentryOptions? options,
$"{method} {url}" // e.g. "GET https://example.com"
);
span?.SetExtra(OtelSemanticConventions.AttributeHttpRequestMethod, method);
if (!string.IsNullOrWhiteSpace(request.RequestUri?.Host))
{
span?.SetExtra(OtelSemanticConventions.AttributeServerAddress, request.RequestUri!.Host);
}
return span;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Sentry/SentryHttpMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ internal SentryHttpMessageHandler(IHub? hub, SentryOptions? options, HttpMessage
$"{method} {url}" // e.g. "GET https://example.com"
);
span?.SetExtra(OtelSemanticConventions.AttributeHttpRequestMethod, method);
if (!string.IsNullOrWhiteSpace(request.RequestUri?.Host))
{
span?.SetExtra(OtelSemanticConventions.AttributeServerAddress, request.RequestUri!.Host);
}
return span;
}

Expand Down
6 changes: 4 additions & 2 deletions test/Sentry.Tests/SentryGraphQlHttpMessageHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ public void ProcessRequest_SetsSpanData()
var sut = new SentryGraphQLHttpMessageHandler(hub, null);

var method = "POST";
var url = "http://example.com/graphql";
var host = "example.com";
var url = $"https://{host}/graphql";
var query = ValidQuery;
var request = SentryGraphQlTestHelpers.GetRequestQuery(query);
var request = SentryGraphQlTestHelpers.GetRequestQuery(query, url);

// Act
var returnedSpan = sut.ProcessRequest(request, method, url);
Expand All @@ -69,6 +70,7 @@ public void ProcessRequest_SetsSpanData()
returnedSpan!.Operation.Should().Be("http.client");
returnedSpan.Description.Should().Be($"{method} {url}");
returnedSpan.Received(1).SetExtra(OtelSemanticConventions.AttributeHttpRequestMethod, method);
returnedSpan.Received(1).SetExtra(OtelSemanticConventions.AttributeServerAddress, host);
}

// [Theory]
Expand Down
2 changes: 1 addition & 1 deletion test/Sentry.Tests/SentryGraphQlTestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static HttpRequestMessage GetRequestQuery(string query, string url = "htt
return GetRequest(content, url);
}

public static HttpRequestMessage GetRequest(HttpContent content, string url = "http://foo") => new(HttpMethod.Post, url)
public static HttpRequestMessage GetRequest(HttpContent content, string url = "http://foo") => new(HttpMethod.Post, new Uri(url))
{
Content = content
};
Expand Down
7 changes: 5 additions & 2 deletions test/Sentry.Tests/SentryHttpMessageHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,10 @@ public void ProcessRequest_SetsSpanData()
var sut = new SentryHttpMessageHandler(hub, null);

var method = "GET";
var url = "http://example.com/graphql";
var request = new HttpRequestMessage(HttpMethod.Get, url);
var host = "example.com";
var url = $"https://{host}/graphql";
var uri = new Uri(url);
var request = new HttpRequestMessage(HttpMethod.Get, uri);

// Act
var returnedSpan = sut.ProcessRequest(request, method, url);
Expand All @@ -264,6 +266,7 @@ public void ProcessRequest_SetsSpanData()
returnedSpan!.Operation.Should().Be("http.client");
returnedSpan.Description.Should().Be($"{method} {url}");
returnedSpan.Received(1).SetExtra(OtelSemanticConventions.AttributeHttpRequestMethod, method);
returnedSpan.Received(1).SetExtra(OtelSemanticConventions.AttributeServerAddress, host);
}

[Fact]
Expand Down

0 comments on commit 043cb5f

Please sign in to comment.