From 74ade626b600b55ff924093da43c00195e045ec3 Mon Sep 17 00:00:00 2001 From: Tuukka Turppo Date: Wed, 29 Mar 2023 08:35:09 +0300 Subject: [PATCH 1/2] added fallback method select container by name --- .../Containers/ContainerIdHolder.cs | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/ApplicationInsights.Kubernetes/Containers/ContainerIdHolder.cs b/src/ApplicationInsights.Kubernetes/Containers/ContainerIdHolder.cs index fda347b3..eb6dbb77 100644 --- a/src/ApplicationInsights.Kubernetes/Containers/ContainerIdHolder.cs +++ b/src/ApplicationInsights.Kubernetes/Containers/ContainerIdHolder.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using k8s.Models; using Microsoft.ApplicationInsights.Kubernetes.Debugging; using Microsoft.Extensions.DependencyInjection; @@ -44,12 +45,26 @@ public bool TryBackFillContainerId(V1Pod pod, out V1ContainerStatus? containerSt } containerStatus = null; - // If there's no container id provided providers, check to see if there's only 1 container inside the pod + // If there's no container id provided providers IList? containerStatuses = pod.Status?.ContainerStatuses; - if (containerStatuses is not null && containerStatuses.Count == 1) + if (containerStatuses is not null) { - containerStatus = containerStatuses[0]; - _logger.LogInformation(FormattableString.Invariant($"Use the only container inside the pod for container id: {containerStatus.ContainerID}")); + // check to see if there's only 1 container inside the pod + // else select container by environment variable. + if (containerStatuses.Count == 1) + { + containerStatus = containerStatuses[0]; + _logger.LogDebug(FormattableString.Invariant($"Use the only container inside the pod for container id: {containerStatus.ContainerID}")); + } + else + { + string? containerName = Environment.GetEnvironmentVariable("ContainerName"); + _logger.LogDebug(FormattableString.Invariant($"Select container by environment variable containerName: {containerName}")); + containerStatus = containerStatuses.FirstOrDefault(c => c.Name == containerName); + _logger.LogDebug(FormattableString.Invariant($"Selected container by container.name property container id: {containerStatus.ContainerID}")); + } + + _logger.LogInformation(FormattableString.Invariant($"Selected container {containerStatus.Name} container id: {containerStatus.ContainerID}")); using (IServiceScope scope = _serviceScopeFactory.CreateScope()) { From 338d9d891c6126208b0240e5f2395cd7c2ade39e Mon Sep 17 00:00:00 2001 From: Tuukka Turppo Date: Thu, 30 Mar 2023 08:01:48 +0300 Subject: [PATCH 2/2] Improved null handling --- .../Containers/ContainerIdHolder.cs | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/ApplicationInsights.Kubernetes/Containers/ContainerIdHolder.cs b/src/ApplicationInsights.Kubernetes/Containers/ContainerIdHolder.cs index eb6dbb77..225c8806 100644 --- a/src/ApplicationInsights.Kubernetes/Containers/ContainerIdHolder.cs +++ b/src/ApplicationInsights.Kubernetes/Containers/ContainerIdHolder.cs @@ -60,23 +60,29 @@ public bool TryBackFillContainerId(V1Pod pod, out V1ContainerStatus? containerSt { string? containerName = Environment.GetEnvironmentVariable("ContainerName"); _logger.LogDebug(FormattableString.Invariant($"Select container by environment variable containerName: {containerName}")); - containerStatus = containerStatuses.FirstOrDefault(c => c.Name == containerName); - _logger.LogDebug(FormattableString.Invariant($"Selected container by container.name property container id: {containerStatus.ContainerID}")); + 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}")); + } + } - - _logger.LogInformation(FormattableString.Invariant($"Selected container {containerStatus.Name} container id: {containerStatus.ContainerID}")); - - using (IServiceScope scope = _serviceScopeFactory.CreateScope()) + if (containerStatus is not null) { - IContainerIdNormalizer normalizer = scope.ServiceProvider.GetRequiredService(); - if (normalizer.TryNormalize(containerStatus.ContainerID, out string? normalizedContainerId)) + _logger.LogInformation(FormattableString.Invariant($"Selected container {containerStatus.Name} container id: {containerStatus.ContainerID}")); + + using (IServiceScope scope = _serviceScopeFactory.CreateScope()) { - _containerId = normalizedContainerId; - return true; + IContainerIdNormalizer normalizer = scope.ServiceProvider.GetRequiredService(); + if (normalizer.TryNormalize(containerStatus.ContainerID, out string? normalizedContainerId)) + { + _containerId = normalizedContainerId; + return true; + } } + _logger.LogError(FormattableString.Invariant($"Normalization failed for container id: {containerStatus.ContainerID}")); } - - _logger.LogError(FormattableString.Invariant($"Normalization failed for container id: {containerStatus.ContainerID}")); + _logger.LogError(FormattableString.Invariant($"Try back fill ContainerId failed")); } return false; }