Skip to content

Commit

Permalink
dh: Add ServicePointOnExceptionLogging handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rmandvikar committed Apr 17, 2024
1 parent 1a82a40 commit 06cd589
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 36 deletions.
37 changes: 1 addition & 36 deletions src/rm.DelegatingHandlers/ServicePointLoggingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Threading;
using System.Threading.Tasks;
using Serilog;
using Serilog.Core;
using Serilog.Core.Enrichers;

namespace rm.DelegatingHandlers;

Expand Down Expand Up @@ -36,40 +34,7 @@ protected override async Task<HttpResponseMessage> SendAsync(
finally
{
var servicePoint = ServicePointManager.FindServicePoint(request.RequestUri);

var props =
new ILogEventEnricher[]
{
// global
new PropertyEnricher("CheckCertificateRevocationList", ServicePointManager.CheckCertificateRevocationList),
new PropertyEnricher("DefaultConnectionLimit", ServicePointManager.DefaultConnectionLimit),
new PropertyEnricher("DefaultNonPersistentConnectionLimit", ServicePointManager.DefaultNonPersistentConnectionLimit),
new PropertyEnricher("DefaultPersistentConnectionLimit", ServicePointManager.DefaultPersistentConnectionLimit),
new PropertyEnricher("DnsRefreshTimeout", ServicePointManager.DnsRefreshTimeout),
new PropertyEnricher("EnableDnsRoundRobin", ServicePointManager.EnableDnsRoundRobin),
new PropertyEnricher("EncryptionPolicy", ServicePointManager.EncryptionPolicy),
new PropertyEnricher("Expect100Continue", ServicePointManager.Expect100Continue),
new PropertyEnricher("MaxServicePointIdleTime", ServicePointManager.MaxServicePointIdleTime),
new PropertyEnricher("MaxServicePoints", ServicePointManager.MaxServicePoints),
new PropertyEnricher("ReusePort", ServicePointManager.ReusePort),
new PropertyEnricher("SecurityProtocol", ServicePointManager.SecurityProtocol),
new PropertyEnricher("UseNagleAlgorithm", ServicePointManager.UseNagleAlgorithm),

// servicePoint
new PropertyEnricher("host.Key", $"{servicePoint.Address.Scheme}://{servicePoint.Address.Host}"),
new PropertyEnricher("host.Address", servicePoint.Address),
new PropertyEnricher("host.ConnectionName", servicePoint.ConnectionName),
new PropertyEnricher("host.ProtocolVersion", servicePoint.ProtocolVersion),
new PropertyEnricher("host.Expect100Continue", servicePoint.Expect100Continue),
new PropertyEnricher("host.UseNagleAlgorithm", servicePoint.UseNagleAlgorithm),
new PropertyEnricher("host.SupportsPipelining", servicePoint.SupportsPipelining),
new PropertyEnricher("host.ConnectionLimit", servicePoint.ConnectionLimit),
new PropertyEnricher("host.CurrentConnections", servicePoint.CurrentConnections),
new PropertyEnricher("host.ConnectionLeaseTimeout", servicePoint.ConnectionLeaseTimeout),
new PropertyEnricher("host.IdleSince", servicePoint.IdleSince),
new PropertyEnricher("host.MaxIdleTime", servicePoint.MaxIdleTime),
new PropertyEnricher("host.ReceiveBufferSize", servicePoint.ReceiveBufferSize),
};
var props = ServicePointHelpers.GetServicePointEnrichers(servicePoint);

logger.ForContext(props)
.Information("ServicePoint stats");
Expand Down
45 changes: 45 additions & 0 deletions src/rm.DelegatingHandlers/ServicePointOnExceptionLoggingHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Serilog;

namespace rm.DelegatingHandlers;

/// <summary>
/// Logs <see cref="ServicePoint"/> stats for <see cref="HttpRequestMessage.RequestUri"/> with exception.
/// </summary>
public class ServicePointOnExceptionLoggingHandler : DelegatingHandler
{
private readonly ILogger logger;

/// <inheritdoc cref="ServicePointOnExceptionLoggingHandler" />
public ServicePointOnExceptionLoggingHandler(
ILogger logger)
{
this.logger = logger?.ForContext(GetType())
?? throw new ArgumentNullException(nameof(logger));
}

protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
try
{
return await base.SendAsync(request, cancellationToken)
.ConfigureAwait(false);
}
catch (Exception ex)
{
var servicePoint = ServicePointManager.FindServicePoint(request.RequestUri);
var props = ServicePointHelpers.GetServicePointEnrichers(servicePoint);

logger.ForContext(props)
.Error(ex, "ServicePoint stats");

throw;
}
}
}
46 changes: 46 additions & 0 deletions src/rm.DelegatingHandlers/misc/ServicePointHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Net;
using Serilog.Core;
using Serilog.Core.Enrichers;

namespace rm.DelegatingHandlers;

public static class ServicePointHelpers
{
public static ILogEventEnricher[] GetServicePointEnrichers(ServicePoint servicePoint)
{
return
new[]
{
// global
new PropertyEnricher("CheckCertificateRevocationList", ServicePointManager.CheckCertificateRevocationList),
new PropertyEnricher("DefaultConnectionLimit", ServicePointManager.DefaultConnectionLimit),
new PropertyEnricher("DefaultNonPersistentConnectionLimit", ServicePointManager.DefaultNonPersistentConnectionLimit),
new PropertyEnricher("DefaultPersistentConnectionLimit", ServicePointManager.DefaultPersistentConnectionLimit),
new PropertyEnricher("DnsRefreshTimeout", ServicePointManager.DnsRefreshTimeout),
new PropertyEnricher("EnableDnsRoundRobin", ServicePointManager.EnableDnsRoundRobin),
new PropertyEnricher("EncryptionPolicy", ServicePointManager.EncryptionPolicy),
new PropertyEnricher("Expect100Continue", ServicePointManager.Expect100Continue),
new PropertyEnricher("MaxServicePointIdleTime", ServicePointManager.MaxServicePointIdleTime),
new PropertyEnricher("MaxServicePoints", ServicePointManager.MaxServicePoints),
new PropertyEnricher("ReusePort", ServicePointManager.ReusePort),
new PropertyEnricher("SecurityProtocol", ServicePointManager.SecurityProtocol),
new PropertyEnricher("UseNagleAlgorithm", ServicePointManager.UseNagleAlgorithm),

// servicePoint
// see https://stackoverflow.com/questions/75168666/should-i-pass-the-full-url-or-just-the-domain-to-servicepointmanager-findservice
new PropertyEnricher("host.Key", $"{servicePoint.Address.Scheme}://{servicePoint.Address.DnsSafeHost}"),
new PropertyEnricher("host.Address", servicePoint.Address),
new PropertyEnricher("host.ConnectionName", servicePoint.ConnectionName),
new PropertyEnricher("host.ProtocolVersion", servicePoint.ProtocolVersion),
new PropertyEnricher("host.Expect100Continue", servicePoint.Expect100Continue),
new PropertyEnricher("host.UseNagleAlgorithm", servicePoint.UseNagleAlgorithm),
new PropertyEnricher("host.SupportsPipelining", servicePoint.SupportsPipelining),
new PropertyEnricher("host.ConnectionLimit", servicePoint.ConnectionLimit),
new PropertyEnricher("host.CurrentConnections", servicePoint.CurrentConnections),
new PropertyEnricher("host.ConnectionLeaseTimeout", servicePoint.ConnectionLeaseTimeout),
new PropertyEnricher("host.IdleSince", servicePoint.IdleSince),
new PropertyEnricher("host.MaxIdleTime", servicePoint.MaxIdleTime),
new PropertyEnricher("host.ReceiveBufferSize", servicePoint.ReceiveBufferSize),
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Net.Http;
using AutoFixture;
using AutoFixture.AutoMoq;
using Moq;
using NUnit.Framework;
using rm.DelegatingHandlers;
using Serilog;
using Serilog.Core;

namespace rm.DelegatingHandlersTest;

[TestFixture]
public class ServicePointOnExceptionLoggingHandlerTests
{
[Test]
public async Task Does_Not_Log_ServicePoint_Stats()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());

var loggerMock = fixture.Freeze<Mock<ILogger>>();
loggerMock.Setup(x => x.ForContext(It.IsAny<Type>())).Returns(loggerMock.Object);
loggerMock.Setup(x => x.ForContext(It.IsAny<IEnumerable<ILogEventEnricher>>())).Returns(loggerMock.Object);
var servicePointOnExceptionLoggingHandler = new ServicePointOnExceptionLoggingHandler(loggerMock.Object);

using var invoker = HttpMessageInvokerFactory.Create(
fixture.Create<HttpMessageHandler>(), servicePointOnExceptionLoggingHandler);

using var requestMessage = fixture.Create<HttpRequestMessage>();
using var _ = await invoker.SendAsync(requestMessage, CancellationToken.None);

loggerMock.Verify((x) =>
x.Error(It.IsAny<string>()),
Times.Never);
}

[Test]
public void Logs_ServicePoint_Stats_During_Exception()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());

var loggerMock = fixture.Freeze<Mock<ILogger>>();
loggerMock.Setup(x => x.ForContext(It.IsAny<Type>())).Returns(loggerMock.Object);
loggerMock.Setup(x => x.ForContext(It.IsAny<IEnumerable<ILogEventEnricher>>())).Returns(loggerMock.Object);
var servicePointOnExceptionLoggingHandler = new ServicePointOnExceptionLoggingHandler(loggerMock.Object);
var throwingHandler = new ThrowingHandler(new TurnDownForWhatException());

using var invoker = HttpMessageInvokerFactory.Create(
fixture.Create<HttpMessageHandler>(), servicePointOnExceptionLoggingHandler, throwingHandler);

using var requestMessage = fixture.Create<HttpRequestMessage>();
var ex = Assert.ThrowsAsync<TurnDownForWhatException>(async () =>
{
using var _ = await invoker.SendAsync(requestMessage, CancellationToken.None);
});

loggerMock.Verify((x) =>
x.Error(ex, "ServicePoint stats"),
Times.Once);
}
}

0 comments on commit 06cd589

Please sign in to comment.