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

[Instrumentation.Http] fall back in case of NET9. update unit tests #2314

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
ec51d6f
change Instrumentation.Http to fall back in case of NET9. update unit…
TimothyMothra Nov 12, 2024
3c11ae7
cleanup
TimothyMothra Nov 12, 2024
1a07eca
Merge branch 'main' into 2029_httpinstrumentation_net9
TimothyMothra Nov 12, 2024
8c01b21
fix line endings
TimothyMothra Nov 12, 2024
c25344f
Merge branch '2029_httpinstrumentation_net9' of https://github.com/Ti…
TimothyMothra Nov 12, 2024
1f76f2b
dotnet format
TimothyMothra Nov 12, 2024
339499b
fix access modifiers
TimothyMothra Nov 12, 2024
5aa0e76
fix: add missing cases to switch expression
TimothyMothra Nov 12, 2024
39330f6
fix: test class must be public
TimothyMothra Nov 12, 2024
6fe41be
fix line endings
TimothyMothra Nov 12, 2024
dd3e7f5
testing downgrading the AnalysisLevel.
TimothyMothra Nov 12, 2024
d8d4748
fix: Use pattern matching
TimothyMothra Nov 12, 2024
35a4082
fix: unused variable
TimothyMothra Nov 12, 2024
37563a9
testing changes for Net9 GA
TimothyMothra Nov 12, 2024
c7a06ac
Revert "testing changes for Net9 GA"
TimothyMothra Nov 13, 2024
228bf66
Revert "fix: unused variable"
TimothyMothra Nov 13, 2024
2ce6d41
Revert "testing downgrading the AnalysisLevel."
TimothyMothra Nov 13, 2024
a5aeac9
merge main and resolve conflicts
TimothyMothra Nov 13, 2024
f706e80
testing update to CI
TimothyMothra Nov 13, 2024
27477b6
fix test
TimothyMothra Nov 13, 2024
60b2f5a
add TODO
TimothyMothra Nov 13, 2024
a35941e
cleanup csproj
TimothyMothra Nov 13, 2024
7075153
cleanup extra whitespace
TimothyMothra Nov 13, 2024
8b339ed
fix line endings
TimothyMothra Nov 13, 2024
3022333
cleanup comments. link to tracking issue
TimothyMothra Nov 14, 2024
f9fa4f3
Merge branch 'main' into 2029_httpinstrumentation_net9
TimothyMothra Nov 14, 2024
ff8ff91
fix line endings
TimothyMothra Nov 14, 2024
3b55505
Merge branch 'main' into 2029_httpinstrumentation_net9
TimothyMothra Nov 14, 2024
ce50ec8
remove net9 framework changes. change was made in different pr
TimothyMothra Nov 14, 2024
e6ff071
cleanup
TimothyMothra Nov 15, 2024
c5dc2c9
update changelog
TimothyMothra Nov 15, 2024
8b3eaab
Merge branch 'main' into 2029_httpinstrumentation_net9
TimothyMothra Nov 15, 2024
292c915
Merge branch 'main' into 2029_httpinstrumentation_net9
TimothyMothra Nov 15, 2024
f6f28f6
cleanup static var
TimothyMothra Nov 15, 2024
8fc7f6b
Merge branch '2029_httpinstrumentation_net9' of https://github.com/Ti…
TimothyMothra Nov 15, 2024
03ad8a9
Update src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md
TimothyMothra Nov 16, 2024
8150955
fix line endings
TimothyMothra Nov 16, 2024
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ jobs:
with:
project-name: Component[OpenTelemetry.Instrumentation.Http]
code-cov-name: Instrumentation.Http
tfm-list: '[ "net462", "net8.0", "net9.0" ]'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to add NET9 to the CI. I'm checking offline to confirm if this is the correct way to make that change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After #2313 gets merged, net9.0 would run for all jobs by default. You won't need to configure for this individual job.


build-test-instrumentation-owin:
needs: detect-changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ internal sealed class HttpHandlerDiagnosticListener : ListenerHandler
private static readonly PropertyFetcher<HttpResponseMessage> StopResponseFetcher = new("Response");
private static readonly PropertyFetcher<Exception> StopExceptionFetcher = new("Exception");
private static readonly PropertyFetcher<TaskStatus> StopRequestStatusFetcher = new("RequestTaskStatus");
private static readonly bool Net9orGreater = Environment.Version.Major >= 9;

private readonly HttpClientTraceInstrumentationOptions options;

public HttpHandlerDiagnosticListener(HttpClientTraceInstrumentationOptions options)
Expand Down Expand Up @@ -135,10 +137,13 @@ public void OnStartActivity(Activity activity, object? payload)
ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Client);
}

// see the spec https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md
HttpTagHelper.RequestDataHelper.SetHttpMethodTag(activity, request.Method.Method);
if (!Net9orGreater)
{
// see the spec https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md
HttpTagHelper.RequestDataHelper.SetHttpMethodTag(activity, request.Method.Method);
}

if (request.RequestUri != null)
if (!Net9orGreater && request.RequestUri != null)
{
activity.SetTag(SemanticConventions.AttributeServerAddress, request.RequestUri.Host);
activity.SetTag(SemanticConventions.AttributeServerPort, request.RequestUri.Port);
Expand Down Expand Up @@ -199,16 +204,19 @@ public void OnStopActivity(Activity activity, object? payload)

if (TryFetchResponse(payload, out var response))
{
if (currentStatusCode == ActivityStatusCode.Unset)
if (!Net9orGreater)
{
activity.SetStatus(SpanHelper.ResolveActivityStatusForHttpStatusCode(activity.Kind, (int)response.StatusCode));
}
if (currentStatusCode == ActivityStatusCode.Unset)
{
activity.SetStatus(SpanHelper.ResolveActivityStatusForHttpStatusCode(activity.Kind, (int)response.StatusCode));
}

activity.SetTag(SemanticConventions.AttributeNetworkProtocolVersion, RequestDataHelper.GetHttpProtocolVersion(response.Version));
activity.SetTag(SemanticConventions.AttributeHttpResponseStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode));
if (activity.Status == ActivityStatusCode.Error)
{
activity.SetTag(SemanticConventions.AttributeErrorType, TelemetryHelper.GetStatusCodeString(response.StatusCode));
activity.SetTag(SemanticConventions.AttributeNetworkProtocolVersion, RequestDataHelper.GetHttpProtocolVersion(response.Version));
activity.SetTag(SemanticConventions.AttributeHttpResponseStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode));
if (activity.Status == ActivityStatusCode.Error)
{
activity.SetTag(SemanticConventions.AttributeErrorType, TelemetryHelper.GetStatusCodeString(response.StatusCode));
}
}

try
Expand Down Expand Up @@ -317,6 +325,7 @@ static bool TryFetchException(object? payload, [NotNullWhen(true)] out Exception
HttpRequestError.ConfigurationLimitExceeded => "configuration_limit_exceeded",

// Fall back to the exception type name in case of HttpRequestError.Unknown
HttpRequestError.Unknown => exc.GetType().FullName,
_ => exc.GetType().FullName,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal sealed class HttpHandlerMetricsDiagnosticListener : ListenerHandler
private static readonly PropertyFetcher<HttpResponseMessage> StopResponseFetcher = new("Response");
private static readonly PropertyFetcher<Exception> StopExceptionFetcher = new("Exception");
private static readonly PropertyFetcher<HttpRequestMessage> RequestFetcher = new("Request");
private static readonly bool Net9orGreater = Environment.Version.Major >= 9;
#if NET
private static readonly HttpRequestOptionsKey<string?> HttpRequestOptionsErrorKey = new(SemanticConventions.AttributeErrorType);
#endif
Expand All @@ -39,7 +40,7 @@ public HttpHandlerMetricsDiagnosticListener(string name)

public static void OnStopEventWritten(Activity activity, object? payload)
{
if (TryFetchRequest(payload, out var request))
if (!Net9orGreater && TryFetchRequest(payload, out var request))
{
// see the spec https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-metrics.md
TagList tags = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,18 @@ public async Task HttpRequestMethodIsSetOnActivityAsPerSpec(string originalMetho
}

Assert.Equal(expectedMethod, activity.GetTagValue(SemanticConventions.AttributeHttpRequestMethod));

#if NET9_0_OR_GREATER
TimothyMothra marked this conversation as resolved.
Show resolved Hide resolved
if (expectedOriginalMethod is not null and not "CUSTOM")
{
// TODO: NEED TO REVIEW THE SPEC.
// "http.request.method_original" is only set when the HTTP Method is not a standard HTTP method.

// Our implementation expects HTTP Methods to be in uppercase. "GET" is known but "Get" is unknown.
// In contrast, NET9 is case-insensitive. "GET" and "Get" are both known.
expectedOriginalMethod = null;
}
#endif
Assert.Equal(expectedOriginalMethod, activity.GetTagValue(SemanticConventions.AttributeHttpRequestMethodOriginal));
}

Expand Down Expand Up @@ -727,7 +739,16 @@ public async Task ValidateUrlQueryRedaction(string urlQuery, string expectedUrlQ
var activity = exportedItems[0];

var expectedUrl = $"{this.url}path{expectedUrlQuery}";

#if NET9_0_OR_GREATER
// TODO: NEED TO UPDATE THIS TEST TO USE .NET'S SETTING TO DISABLE REDACTION.
// CURRENTLY THIS DOESN'T WORK WITH OUR TESTS WHICH RUN IN PARALLEL.
// https://github.com/dotnet/docs/issues/42792
expectedUrl = $"{this.url}path?*";
Assert.Equal(expectedUrl, activity.GetTagValue(SemanticConventions.AttributeUrlFull));
#else
Assert.Equal(expectedUrl, activity.GetTagValue(SemanticConventions.AttributeUrlFull));
#endif
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,22 @@ private static async Task HttpOutCallsAreCollectedSuccessfullyBodyAsync(
Assert.Contains(normalizedAttributes, kvp => kvp.Key == SemanticConventions.AttributeHttpRequestMethod && kvp.Value?.ToString() == normalizedAttributesTestCase[SemanticConventions.AttributeHttpRequestMethod]);
Assert.Contains(normalizedAttributes, kvp => kvp.Key == SemanticConventions.AttributeServerAddress && kvp.Value?.ToString() == normalizedAttributesTestCase[SemanticConventions.AttributeServerAddress]);
Assert.Contains(normalizedAttributes, kvp => kvp.Key == SemanticConventions.AttributeServerPort && kvp.Value?.ToString() == normalizedAttributesTestCase[SemanticConventions.AttributeServerPort]);

#if NET9_0_OR_GREATER
// TODO: NEED TO REVIEW THE SPEC
// NET9 does not record the URL Fragment Identifier.
var expected = normalizedAttributesTestCase[SemanticConventions.AttributeUrlFull];
if (expected.EndsWith("#fragment", StringComparison.Ordinal))
{
// remove fragment from expected value
expected = expected.Substring(0, expected.Length - "#fragment".Length);
}

Assert.Contains(normalizedAttributes, kvp => kvp.Key == SemanticConventions.AttributeUrlFull && kvp.Value?.ToString() == expected);
#else
Assert.Contains(normalizedAttributes, kvp => kvp.Key == SemanticConventions.AttributeUrlFull && kvp.Value?.ToString() == normalizedAttributesTestCase[SemanticConventions.AttributeUrlFull]);
#endif

if (tc.ResponseExpected)
{
Assert.Contains(normalizedAttributes, kvp => kvp.Key == SemanticConventions.AttributeNetworkProtocolVersion && kvp.Value?.ToString() == normalizedAttributesTestCase[SemanticConventions.AttributeNetworkProtocolVersion]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ public void HttpOutCallsAreCollectedSuccessfully(HttpOutTestCase tc)
Assert.Fail($"Tag {tag.Key} was not found in test data.");
}

#if NET9_0_OR_GREATER
// TODO: NEED TO REVIEW THE SPEC
// NET9 does not record the URL Fragment Identifier.
if (value.EndsWith("#fragment", StringComparison.Ordinal))
{
// remove fragment from expected value
value = value.Substring(0, value.Length - "#fragment".Length);
}
#endif

Assert.Equal(value, tagValue);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(SupportedNetTargets)</TargetFrameworks>
<TargetFrameworks>net9.0;$(SupportedNetTargets)</TargetFrameworks>
TimothyMothra marked this conversation as resolved.
Show resolved Hide resolved
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);$(NetFrameworkMinimumSupportedVersion)</TargetFrameworks>
<Description>Unit test project for OpenTelemetry HTTP instrumentations.</Description>
</PropertyGroup>
Expand Down
Loading