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

HttpClient logging based on new .NET 8 runtime APIs #4224

Merged
merged 39 commits into from
Aug 21, 2023

Conversation

xakep139
Copy link
Contributor

@xakep139 xakep139 commented Jul 28, 2023

Fixes #4211

Microsoft Reviewers: Open in CodeFlow

@xakep139 xakep139 requested a review from geeknoid July 28, 2023 15:41
@xakep139 xakep139 self-assigned this Jul 28, 2023
/// <param name="exception">An optional <see cref="Exception"/> that was thrown within the outgoing HTTP request processing.</param>
// TODO: adding a new parameter to the interface method is a breaking change. Pay additional attention to this during review.
// Alternatively, we can decide to add another overload instead.
void Enrich(IEnrichmentPropertyBag enrichmentBag, HttpRequestMessage request, HttpResponseMessage? response = null, Exception? exception = null);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to discuss this one

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it breaking if it's defaulted to null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, the breaking change here is that I added Exception? exception - which means that existing users of this API would need to add the new parameter into their implementation of the interface

}

// Recommendation is to swallow the exception (logger shouldn't throw), so we don't re-throw here:
return null;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As @CarnaViire pointed, a logger shouldn't re-throw (but it will change our current behavior)


public object? LogRequestStart(HttpRequestMessage request)
{
throw new NotSupportedException();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As of now the best we can offer for sync overloads - is to call their async counterparts with .GetAwaiter().GetResult(), that's because there's no sync API of reading request/response message body

}

propertyBag = LogMethodHelper.GetHelper();
FillLogRecord(logRecord, propertyBag, in elapsed, request, response, exception);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't use TimeProvider here since elapsed is provided out-of-the-box, but we can consider ignoring it and using the old logic

var httpMethod = request[startIndex].Value;
var httpHost = request[startIndex + 1].Value;
var httpPath = request[startIndex + 2].Value;
return FormattableString.Invariant($"{httpMethod} {httpHost}/{httpPath}");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is to align with current behavior in internal library


private static DelegatingHandler CreateInternalBclLoggingHandler(IHttpClientLogger logger, HttpMessageHandler? innerHandler)
{
var handlerType = typeof(IHttpClientFactory).Assembly.GetType("Microsoft.Extensions.Http.Logging.HttpClientLoggerHandler");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was taken from Natalia's PoC branch main...CarnaViire:extensions:poc-hcf-custom-logging

Copy link
Member

@CarnaViire CarnaViire Jul 28, 2023

Choose a reason for hiding this comment

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

Are you sure you need this? The intent on my side was just to show that our HttpClientLoggerHandler passes all the tests you had for your logging handler. Like, "you can trust our handler" 😄

So I believe the set of tests that were verifying the handler itself can be removed from dotnet/extensions repo (I haven't looked at the PR yet, I don't know how exactly you've transformed the test cases).

Or if you do need a handler for some other tests, create it "normal way" via IHttpMessageHandlerFactory.

@RussKie
Copy link
Member

RussKie commented Jul 30, 2023

MC

/// <param name="exception">An optional <see cref="Exception"/> that was thrown within the outgoing HTTP request processing.</param>
// TODO: adding a new parameter to the interface method is a breaking change. Pay additional attention to this during review.
// Alternatively, we can decide to add another overload instead.
void Enrich(IEnrichmentPropertyBag enrichmentBag, HttpRequestMessage request, HttpResponseMessage? response = null, Exception? exception = null);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it breaking if it's defaulted to null?

@xakep139 xakep139 marked this pull request as ready for review August 14, 2023 11:25
@RussKie
Copy link
Member

RussKie commented Aug 16, 2023

Please re-target to release/8.0, if this is necessary for .NET 8.

@xakep139
Copy link
Contributor Author

xakep139 commented Aug 16, 2023

Please re-target to release/8.0, if this is necessary for .NET 8.

I would rather merge it to main first, and then cherry-pick the commit into release/8.0.
Right now, if I target release/8.0, the PR brings a few unrelated changes from main, e.g. .github/fabricbot.json, etc.

@xakep139 xakep139 changed the base branch from main to release/8.0 August 16, 2023 08:59
@xakep139 xakep139 changed the base branch from release/8.0 to main August 16, 2023 09:03
@RussKie
Copy link
Member

RussKie commented Aug 17, 2023

MC

@RussKie RussKie added the waiting-author-feedback 📭 The author of this issue needs to respond in order for us to continue investigating this issue. label Aug 17, 2023
@ghost ghost removed the waiting-author-feedback 📭 The author of this issue needs to respond in order for us to continue investigating this issue. label Aug 17, 2023
@xakep139
Copy link
Contributor Author

MC

Resolved

@xakep139 xakep139 enabled auto-merge (squash) August 21, 2023 08:45
@xakep139 xakep139 merged commit 16b51f2 into main Aug 21, 2023
6 checks passed
@xakep139 xakep139 deleted the xakep139/outgoing-http-logging branch August 21, 2023 09:11
@ghost ghost added this to the 9.0-preview1 milestone Aug 21, 2023
xakep139 added a commit that referenced this pull request Aug 21, 2023
* use new .NET 8 runtime APIs in HttpClient logging

* Leverage KeyedServices feature in HttpClient logging (#4253)

Co-authored-by: Nikita Balabaev <nbalabaev@microsoft.com>
geeknoid pushed a commit that referenced this pull request Aug 21, 2023
* use new .NET 8 runtime APIs in HttpClient logging

* Leverage KeyedServices feature in HttpClient logging (#4253)

Co-authored-by: Nikita Balabaev <nbalabaev@microsoft.com>
@ghost ghost locked as resolved and limited conversation to collaborators Sep 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HttpClient logging based on .net8
7 participants