From 4af86cfa7e43c2c0b126e4d7e8342c6bed8cdc3f Mon Sep 17 00:00:00 2001 From: JobaDiniz Date: Tue, 16 Apr 2024 09:47:13 -0300 Subject: [PATCH] fix(http): add diagnostic logs to http requests Signed-off-by: JobaDiniz --- .../Client/LogMessageHandler.cs | 22 +++++++++++++++++++ .../Client/RefreshTokenHttpMessageHandler.cs | 5 +---- .../Client/RpaHttpClientFactory.cs | 21 ++++++++---------- .../Joba.IBM.RPA.Cli.Tests/EndToEndTest.cs | 2 +- 4 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 src/Joba.IBM.RPA.Cli/Client/LogMessageHandler.cs diff --git a/src/Joba.IBM.RPA.Cli/Client/LogMessageHandler.cs b/src/Joba.IBM.RPA.Cli/Client/LogMessageHandler.cs new file mode 100644 index 0000000..a60f696 --- /dev/null +++ b/src/Joba.IBM.RPA.Cli/Client/LogMessageHandler.cs @@ -0,0 +1,22 @@ +using Microsoft.Extensions.Logging; +using System.Diagnostics; + +namespace Joba.IBM.RPA.Cli +{ + internal class LogMessageHandler : DelegatingHandler + { + private readonly ILogger logger; + + public LogMessageHandler(ILogger logger, HttpMessageHandler innerHandler) : base(innerHandler) => this.logger = logger; + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + var stopwatch = Stopwatch.StartNew(); + logger.LogTrace("Request started [{METHOD}] {URI}", request.Method.Method, request.RequestUri); + var response = await base.SendAsync(request, cancellationToken); + stopwatch.Stop(); + logger.LogTrace("Request ended [{METHOD}] {URI} ({TIME})", stopwatch.Elapsed, request.Method.Method, request.RequestUri); + return response; + } + } +} diff --git a/src/Joba.IBM.RPA.Cli/Client/RefreshTokenHttpMessageHandler.cs b/src/Joba.IBM.RPA.Cli/Client/RefreshTokenHttpMessageHandler.cs index 2786314..3912c59 100644 --- a/src/Joba.IBM.RPA.Cli/Client/RefreshTokenHttpMessageHandler.cs +++ b/src/Joba.IBM.RPA.Cli/Client/RefreshTokenHttpMessageHandler.cs @@ -8,10 +8,7 @@ internal class RefreshTokenHttpMessageHandler : DelegatingHandler private readonly IRenewExpiredSession sessionRenewal; public RefreshTokenHttpMessageHandler(IRenewExpiredSession sessionRenewal, HttpMessageHandler innerHandler) - : base(innerHandler) - { - this.sessionRenewal = sessionRenewal; - } + : base(innerHandler) => this.sessionRenewal = sessionRenewal; protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellation) { diff --git a/src/Joba.IBM.RPA.Cli/Client/RpaHttpClientFactory.cs b/src/Joba.IBM.RPA.Cli/Client/RpaHttpClientFactory.cs index 886afa0..2bed96c 100644 --- a/src/Joba.IBM.RPA.Cli/Client/RpaHttpClientFactory.cs +++ b/src/Joba.IBM.RPA.Cli/Client/RpaHttpClientFactory.cs @@ -24,7 +24,6 @@ public HttpClient Create(Uri address) { var handler = new ThrottlingHttpMessageHandler(MaxParallelism, CreateUserAgentHandler(logger)); var client = new HttpClient(handler) { BaseAddress = address }; - ApplyDefaultRequestHeaders(client); return client; } @@ -33,23 +32,19 @@ public HttpClient Create(Uri address, IRenewExpiredSession sessionRenewal) var refreshTokenHandler = new RefreshTokenHttpMessageHandler(sessionRenewal, CreateUserAgentHandler(logger)); var handler = new ThrottlingHttpMessageHandler(MaxParallelism, refreshTokenHandler); var client = new HttpClient(handler) { BaseAddress = address }; - ApplyDefaultRequestHeaders(client); return client; } - private static void ApplyDefaultRequestHeaders(HttpClient client) - { - //TODO: invalid formats - //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - //client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(RpaCommand.CommandName, RpaCommand.AssemblyVersion)); - //client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(System.Environment.OSVersion.ToString())); - //client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(System.Environment.MachineName)); - } - private static HttpMessageHandler CreatePolicyHandler(ILogger logger) { var policy = HttpPolicyExtensions.HandleTransientHttpError() .WaitAndRetryAsync(Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromSeconds(2), 5)); + + return new PolicyHttpMessageHandler(policy) { InnerHandler = CreateLogHandler(logger) }; + } + + private static HttpClientHandler CreateCoreHandler(ILogger logger) + { var coreHandler = new HttpClientHandler { ServerCertificateCustomValidationCallback = (requestMessage, certificate, chain, sslPolicyErrors) => @@ -67,7 +62,7 @@ private static HttpMessageHandler CreatePolicyHandler(ILogger logger) return true; //TODO: add an option to allow users to opt-in to disregard certificate issues. } }; - return new PolicyHttpMessageHandler(policy) { InnerHandler = coreHandler }; + return coreHandler; } private static HttpMessageHandler CreateUserAgentHandler(ILogger logger) @@ -75,5 +70,7 @@ private static HttpMessageHandler CreateUserAgentHandler(ILogger logger) var pollyHandler = CreatePolicyHandler(logger); return new UserAgentHttpMessageHandler(pollyHandler); } + + private static HttpMessageHandler CreateLogHandler(ILogger logger) => new LogMessageHandler(logger, CreateCoreHandler(logger)); } } diff --git a/src/Tests/Joba.IBM.RPA.Cli.Tests/EndToEndTest.cs b/src/Tests/Joba.IBM.RPA.Cli.Tests/EndToEndTest.cs index 9d93bae..923c7c7 100644 --- a/src/Tests/Joba.IBM.RPA.Cli.Tests/EndToEndTest.cs +++ b/src/Tests/Joba.IBM.RPA.Cli.Tests/EndToEndTest.cs @@ -97,7 +97,7 @@ private async Task RunAsync(string arguments, DirectoryInfo workingDirectory) private async Task StartProcessAsync(string arguments, DirectoryInfo workingDirectory) { logger.LogInformation($"rpa {arguments}"); - arguments = $"{arguments} -v Detailed"; + arguments = $"{arguments} -v Diagnostic"; var envVarName = "RPA_EXECUTABLE_PATH"; var path = System.Environment.GetEnvironmentVariable(envVarName) ?? throw new InvalidOperationException($"The environment variable {envVarName} was not set."); var info = new ProcessStartInfo(Path.GetFullPath(path), arguments)