Skip to content

Commit

Permalink
Don't use structured logging for Docker runtime information
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Mar 11, 2024
1 parent fef7faa commit 1bf2f96
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
49 changes: 48 additions & 1 deletion src/Testcontainers/Clients/DockerApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace DotNet.Testcontainers.Clients
using System.Threading;
using System.Threading.Tasks;
using Docker.DotNet;
using Docker.DotNet.Models;
using DotNet.Testcontainers.Configurations;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -74,14 +75,60 @@ await RuntimeInitialized.WaitAsync(ct)

if (!ProcessedHashCodes.Contains(hashCode))
{
await Logger.DockerRuntimeInfoAsync(DockerClient)
await LogDockerRuntimeInformationAsync(ct)
.ConfigureAwait(false);

ProcessedHashCodes.Add(hashCode);
}

RuntimeInitialized.Release();
}

private async Task LogDockerRuntimeInformationAsync(CancellationToken ct)
{
SystemInfoResponse dockerInfo;
VersionResponse dockerVersion;
try
{
dockerInfo = await DockerClient.System.GetSystemInfoAsync(ct)
.ConfigureAwait(false);

dockerVersion = await DockerClient.System.GetVersionAsync(ct)
.ConfigureAwait(false);
}
catch (Exception e)
{
Logger.LogError(e, "Failed to retrieve Docker container runtime information");
return;
}

var runtimeInfo = new StringBuilder();

var byteUnits = new[] { "KB", "MB", "GB" };

runtimeInfo.AppendLine("Connected to Docker:");

runtimeInfo.Append(" Host: ");
runtimeInfo.AppendLine(DockerClient.Configuration.EndpointBaseUri.ToString());

runtimeInfo.Append(" Server Version: ");
runtimeInfo.AppendLine(dockerInfo.ServerVersion);

runtimeInfo.Append(" Kernel Version: ");
runtimeInfo.AppendLine(dockerInfo.KernelVersion);

runtimeInfo.Append(" API Version: ");
runtimeInfo.AppendLine(dockerVersion.APIVersion);

runtimeInfo.Append(" Operating System: ");
runtimeInfo.AppendLine(dockerInfo.OperatingSystem);

runtimeInfo.Append(" Total Memory: ");
runtimeInfo.AppendFormat(CultureInfo.InvariantCulture, "{0:F} {1}", dockerInfo.MemTotal / Math.Pow(1024, byteUnits.Length), byteUnits[byteUnits.Length - 1]);

Logger.LogInformation(runtimeInfo.ToString());
}

private static IDockerClient GetDockerClient(Guid sessionId, IDockerEndpointAuthenticationConfiguration dockerEndpointAuthConfig)
{
using (var dockerClientConfiguration = dockerEndpointAuthConfig.GetDockerClientConfiguration(sessionId))
Expand Down
32 changes: 0 additions & 32 deletions src/Testcontainers/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,13 @@ namespace DotNet.Testcontainers
using System.Collections.Generic;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Docker.DotNet;
using DotNet.Testcontainers.Images;
using Microsoft.Extensions.Logging;

internal static class Logging
{
#pragma warning disable InconsistentNaming, SA1309

private static readonly Action<ILogger, Uri, string, string, string, string, double, Exception> _DockerRuntimeInfo
= LoggerMessage.Define<Uri, string, string, string, string, double>(LogLevel.Information, default,
"Connected to Docker" + Environment.NewLine +
" Host: {Host}" + Environment.NewLine +
" Server Version: {ServerVersion}" + Environment.NewLine +
" Kernel Version: {KernelVersion}" + Environment.NewLine +
" API Version: {APIVersion}" + Environment.NewLine +
" Operating System: {OperatingSystem}" + Environment.NewLine +
" Total Memory: {TotalMemory:F2} GB");

private static readonly Action<ILogger, Exception> _DockerRuntimeError
= LoggerMessage.Define(LogLevel.Error, default, "Failed to retrieve Docker container runtime information");

private static readonly Action<ILogger, Regex, Exception> _IgnorePatternAdded
= LoggerMessage.Define<Regex>(LogLevel.Information, default, "Pattern {IgnorePattern} added to the regex cache");

Expand Down Expand Up @@ -127,23 +112,6 @@ private static readonly Action<ILogger, Exception> _ReusableResourceNotFound

#pragma warning restore InconsistentNaming, SA1309

public static async Task DockerRuntimeInfoAsync(this ILogger logger, IDockerClient dockerClient)
{
try
{
var dockerInfo = await dockerClient.System.GetSystemInfoAsync()
.ConfigureAwait(false);
var dockerVersion = await dockerClient.System.GetVersionAsync()
.ConfigureAwait(false);
var totalMemory = dockerInfo.MemTotal / (1024.0 * 1024 * 1024);
_DockerRuntimeInfo(logger, dockerClient.Configuration.EndpointBaseUri, dockerInfo.ServerVersion, dockerInfo.KernelVersion, dockerVersion.APIVersion, dockerInfo.OperatingSystem, totalMemory, null);
}
catch (Exception exception)
{
_DockerRuntimeError(logger, exception);
}
}

public static void IgnorePatternAdded(this ILogger logger, Regex ignorePattern)
{
_IgnorePatternAdded(logger, ignorePattern, null);
Expand Down

0 comments on commit 1bf2f96

Please sign in to comment.