From f610fc4cc97ccc63d14649f3089f238106f1377f Mon Sep 17 00:00:00 2001 From: Saar Shen Date: Mon, 10 Jun 2024 10:19:47 -0700 Subject: [PATCH] Update logging a bit (#364) --- .../Containers/ContainerIdHolder.cs | 8 ++++---- .../Containers/ContainerStatusManager.cs | 8 +++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/ApplicationInsights.Kubernetes/Containers/ContainerIdHolder.cs b/src/ApplicationInsights.Kubernetes/Containers/ContainerIdHolder.cs index 225c8806..255f57dc 100644 --- a/src/ApplicationInsights.Kubernetes/Containers/ContainerIdHolder.cs +++ b/src/ApplicationInsights.Kubernetes/Containers/ContainerIdHolder.cs @@ -62,14 +62,14 @@ public bool TryBackFillContainerId(V1Pod pod, out V1ContainerStatus? containerSt _logger.LogDebug(FormattableString.Invariant($"Select container by environment variable containerName: {containerName}")); containerStatus = containerStatuses.FirstOrDefault(c => string.Equals(c.Name, containerName, StringComparison.Ordinal)); if (containerStatus is not null) - { + { _logger.LogDebug(FormattableString.Invariant($"Selected container by container.name property container id: {containerStatus.ContainerID}")); } - + } if (containerStatus is not null) { - _logger.LogInformation(FormattableString.Invariant($"Selected container {containerStatus.Name} container id: {containerStatus.ContainerID}")); + _logger.LogInformation(FormattableString.Invariant($"Selected container id: {containerStatus.ContainerID}, name: {containerStatus.Name}")); using (IServiceScope scope = _serviceScopeFactory.CreateScope()) { @@ -96,7 +96,7 @@ private bool TryGetContainerId(out string? containerId) { if (provider.TryGetMyContainerId(out containerId)) { - _logger.LogInformation(FormattableString.Invariant($"Get container id by provider: {containerId}")); + _logger.LogInformation(FormattableString.Invariant($"Got container id {containerId} by provider: {provider.GetType().Name}")); return true; } } diff --git a/src/ApplicationInsights.Kubernetes/Containers/ContainerStatusManager.cs b/src/ApplicationInsights.Kubernetes/Containers/ContainerStatusManager.cs index 72b2a4c1..b94bea0e 100644 --- a/src/ApplicationInsights.Kubernetes/Containers/ContainerStatusManager.cs +++ b/src/ApplicationInsights.Kubernetes/Containers/ContainerStatusManager.cs @@ -59,15 +59,17 @@ public async Task IsContainerReadyAsync(CancellationToken cancellationToke return null; } - // If there's no container id provided by the container id holder, at this moment, try backfill + // Container Id is null. There's no container id provided by the container id holder, try backfill. + // Give out warnings on Linux in case the auto detect has a bug. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - _logger.LogWarning("Can't fetch container id. Container id info will be missing. Please file an issue at https://github.com/microsoft/ApplicationInsights-Kubernetes/issues."); + _logger.LogWarning("Failed to auto detect container id. Will try back-filling. Please file an issue at https://github.com/microsoft/ApplicationInsights-Kubernetes/issues for investigation."); } if (_containerIdHolder.TryBackFillContainerId(myPod, out V1ContainerStatus? inferredContainerStatus)) { + _logger.LogInformation("Backfill container id succeeded. Container id: {0}", inferredContainerStatus?.ContainerID ?? "Empty"); // Back fill success, return the status. return inferredContainerStatus; } @@ -84,7 +86,7 @@ private bool IsContainerStatusReady(V1ContainerStatus containerStatus) public async Task WaitContainerReadyAsync(CancellationToken cancellationToken) { - while(true) + while (true) { cancellationToken.ThrowIfCancellationRequested();