Skip to content

Commit

Permalink
Log structured Docker runtime information
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Mar 11, 2024
1 parent 1ee0e3e commit fef7faa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 44 deletions.
48 changes: 4 additions & 44 deletions src/Testcontainers/Clients/DockerApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,54 +72,14 @@ public async Task LogContainerRuntimeInfoAsync(CancellationToken ct = default)
await RuntimeInitialized.WaitAsync(ct)
.ConfigureAwait(false);

try
if (!ProcessedHashCodes.Contains(hashCode))
{
if (ProcessedHashCodes.Contains(hashCode))
{
return;
}

var runtimeInfo = new StringBuilder();

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

var dockerInfo = await DockerClient.System.GetSystemInfoAsync(ct)
.ConfigureAwait(false);

var dockerVersion = await DockerClient.System.GetVersionAsync(ct)
await Logger.DockerRuntimeInfoAsync(DockerClient)
.ConfigureAwait(false);

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());
}
catch(Exception e)
{
Logger.LogError(e, "Failed to retrieve Docker container runtime information.");
}
finally
{
ProcessedHashCodes.Add(hashCode);
RuntimeInitialized.Release();
}

RuntimeInitialized.Release();
}

private static IDockerClient GetDockerClient(Guid sessionId, IDockerEndpointAuthenticationConfiguration dockerEndpointAuthConfig)
Expand Down
32 changes: 32 additions & 0 deletions src/Testcontainers/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@ 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 @@ -112,6 +127,23 @@ 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 fef7faa

Please sign in to comment.