-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop'
- Loading branch information
Showing
16 changed files
with
410 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 4 additions & 36 deletions
40
src/ApplicationInsights.Kubernetes/ContainerIdProviders/CGroupContainerIdProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,21 @@ | ||
#nullable enable | ||
|
||
using Microsoft.ApplicationInsights.Kubernetes.Debugging; | ||
using System; | ||
using System.IO; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Microsoft.ApplicationInsights.Kubernetes.ContainerIdProviders | ||
{ | ||
/// <summary> | ||
/// Gets the current container id by using CGroup | ||
/// </summary> | ||
internal class CGroupContainerIdProvider : IContainerIdProvider | ||
internal class CGroupContainerIdProvider : FileBasedContainerIdProvider | ||
{ | ||
private const string CGroupPath = "/proc/self/cgroup"; | ||
private const string CGroupPathPatternString = "cpu.+/([^/]*)$"; | ||
private static readonly Regex CGroupPathPattern = new Regex(CGroupPathPatternString, RegexOptions.CultureInvariant | RegexOptions.Multiline, TimeSpan.FromSeconds(1)); | ||
|
||
private readonly ApplicationInsightsKubernetesDiagnosticSource _logger = ApplicationInsightsKubernetesDiagnosticSource.Instance; | ||
|
||
public bool TryGetMyContainerId(out string? containerId) | ||
{ | ||
containerId = FetchContainerId(CGroupPath); | ||
return containerId != null; | ||
} | ||
|
||
private string? FetchContainerId(string pathToCGroup) | ||
{ | ||
if (!File.Exists(pathToCGroup)) | ||
{ | ||
_logger.LogWarning("CGroup file doesn't exist. Path: {0}", pathToCGroup); | ||
return null; | ||
} | ||
|
||
string content = File.ReadAllText(pathToCGroup); | ||
return ParseContainerId(content); | ||
} | ||
|
||
internal string? ParseContainerId(string? content) | ||
public CGroupContainerIdProvider( | ||
CGroupV1Matcher lineMatcher) : | ||
base(lineMatcher, CGroupPath, providerName: default) | ||
{ | ||
if (!string.IsNullOrEmpty(content)) | ||
{ | ||
MatchCollection matches = CGroupPathPattern.Matches(content); | ||
if (matches.Count >= 1 && matches[0].Groups.Count >= 2) | ||
{ | ||
return matches[0].Groups[1].Value; | ||
} | ||
} | ||
_logger.LogWarning("Can't figure out container id. Input: {0}. Pattern: {1}", content, CGroupPathPatternString); | ||
return null; | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...icationInsights.Kubernetes/ContainerIdProviders/ContainerDMountInfoContainerIdProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#nullable enable | ||
|
||
namespace Microsoft.ApplicationInsights.Kubernetes.ContainerIdProviders; | ||
|
||
internal class ContainerDMountInfoContainerIdProvider : FileBasedContainerIdProvider | ||
{ | ||
private const string InfoFilePath = "/proc/self/mountinfo"; | ||
|
||
public ContainerDMountInfoContainerIdProvider( | ||
ContainerDMountInfoMatcher matcher) | ||
: base(matcher, InfoFilePath, providerName: default) | ||
{ | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ationInsights.Kubernetes/ContainerIdProviders/DockerEngineMountInfoContainerIdProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#nullable enable | ||
|
||
namespace Microsoft.ApplicationInsights.Kubernetes.ContainerIdProviders; | ||
|
||
internal class DockerEngineMountInfoContainerIdProvider : FileBasedContainerIdProvider | ||
{ | ||
private const string InfoFilePath = "/proc/self/mountinfo"; | ||
|
||
public DockerEngineMountInfoContainerIdProvider( | ||
DockerEngineMountInfoMatcher matcher) | ||
: base(matcher, InfoFilePath, providerName: default) | ||
{ | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...icationInsights.Kubernetes/ContainerIdProviders/EnvironmentVariableContainerIdProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using Microsoft.ApplicationInsights.Kubernetes.Debugging; | ||
|
||
namespace Microsoft.ApplicationInsights.Kubernetes.ContainerIdProviders | ||
{ | ||
internal class EnvironmentVariableContainerIdProvider : IContainerIdProvider | ||
{ | ||
private readonly ApplicationInsightsKubernetesDiagnosticSource _logger = ApplicationInsightsKubernetesDiagnosticSource.Instance; | ||
|
||
private const string EnvironmentVariableName = "ContainerId"; | ||
|
||
/// <summary> | ||
/// Try get the container id by environment variable named "ContainerId". | ||
/// </summary> | ||
public bool TryGetMyContainerId(out string? containerId) | ||
{ | ||
containerId = Environment.GetEnvironmentVariable(EnvironmentVariableName); | ||
_logger.LogWarning($"Getting container id by environment variable {EnvironmentVariableName}. Result: {containerId}."); | ||
return !string.IsNullOrEmpty(containerId); | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/ApplicationInsights.Kubernetes/ContainerIdProviders/FileBasedContainerIdProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.IO; | ||
using Microsoft.ApplicationInsights.Kubernetes.Debugging; | ||
|
||
namespace Microsoft.ApplicationInsights.Kubernetes.ContainerIdProviders; | ||
|
||
/// <summary> | ||
/// A common framework to get container id from a file, providing consistent implementation as well as logging. | ||
/// </summary> | ||
internal abstract class FileBasedContainerIdProvider : IContainerIdProvider | ||
{ | ||
private readonly ApplicationInsightsKubernetesDiagnosticSource _logger = ApplicationInsightsKubernetesDiagnosticSource.Instance; | ||
private readonly IContainerIdMatcher _lineMatcher; | ||
private readonly string _providerName; | ||
private readonly string _targetFile; | ||
|
||
public FileBasedContainerIdProvider( | ||
IContainerIdMatcher lineMatcher, | ||
string filePath, | ||
string? providerName) | ||
{ | ||
_providerName = GetType().Name; | ||
if (string.IsNullOrEmpty(filePath)) | ||
{ | ||
throw new ArgumentException($"'[{_providerName}] {nameof(filePath)}' cannot be null or empty.", nameof(filePath)); | ||
} | ||
_targetFile = filePath; | ||
|
||
_lineMatcher = lineMatcher ?? throw new System.ArgumentNullException(nameof(lineMatcher)); | ||
} | ||
|
||
public bool TryGetMyContainerId(out string? containerId) | ||
{ | ||
containerId = FetchContainerId(_targetFile); | ||
return containerId != null; | ||
} | ||
|
||
private string? FetchContainerId(string filePath) | ||
{ | ||
if (!File.Exists(filePath)) | ||
{ | ||
_logger.LogWarning($"[{_providerName}] {nameof(_targetFile)} doesn't exist at: {filePath}"); | ||
return null; | ||
} | ||
|
||
using StreamReader reader = File.OpenText(_targetFile); | ||
while (!reader.EndOfStream) | ||
{ | ||
string line = reader.ReadLine(); | ||
if(string.IsNullOrEmpty(line)) | ||
{ | ||
continue; | ||
} | ||
|
||
if (_lineMatcher.TryParseContainerId(line, out string containerId)) | ||
{ | ||
_logger.LogDebug($"[{_providerName}] Got container id by: {line}"); | ||
_logger.LogInformation($"[{_providerName}] Got container id: {containerId}"); | ||
return containerId; | ||
} | ||
} | ||
_logger.LogWarning($"[{_providerName}] Can't figure out container id."); | ||
return null; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/ApplicationInsights.Kubernetes/ContainerIdProviders/Matchers/CGroupV1Matcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.ApplicationInsights.Kubernetes.Debugging; | ||
|
||
namespace Microsoft.ApplicationInsights.Kubernetes.ContainerIdProviders; | ||
|
||
internal class CGroupV1Matcher : IContainerIdMatcher | ||
{ | ||
private const string LogCategory = nameof(CGroupV1Matcher); | ||
private readonly ApplicationInsightsKubernetesDiagnosticSource _logger = ApplicationInsightsKubernetesDiagnosticSource.Instance; | ||
|
||
private const string MatchPattern = @"cpu.+/([^/]*)$"; | ||
private static readonly Regex MatchRegex = new Regex(MatchPattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1)); | ||
|
||
public bool TryParseContainerId(string? line, out string containerId) | ||
{ | ||
containerId = string.Empty; | ||
if (string.IsNullOrEmpty(line)) | ||
{ | ||
return false; | ||
} | ||
|
||
Match match = MatchRegex.Match(line); | ||
if (!match.Success) | ||
{ | ||
_logger.LogDebug($"[{LogCategory}] No match for containerId. Input: {line}, pattern: {MatchPattern}"); | ||
return false; | ||
} | ||
_logger.LogTrace($"[{LogCategory}] Matched container id."); | ||
containerId = match.Groups[1].Value; | ||
return true; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...pplicationInsights.Kubernetes/ContainerIdProviders/Matchers/ContainerDMountInfoMatcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.ApplicationInsights.Kubernetes.Debugging; | ||
|
||
namespace Microsoft.ApplicationInsights.Kubernetes.ContainerIdProviders; | ||
|
||
/// <summary> | ||
/// Ths is a heuristic matcher for container id in containers by containerD using the mount info. | ||
/// More info about MountInfo: https://man7.org/linux/man-pages/man5/proc.5.html | ||
/// </summary> | ||
internal class ContainerDMountInfoMatcher : IContainerIdMatcher | ||
{ | ||
private readonly ApplicationInsightsKubernetesDiagnosticSource _logger = ApplicationInsightsKubernetesDiagnosticSource.Instance; | ||
|
||
// An example of container id line: | ||
// 1735 1729 0:37 /kubepods/besteffort/pod3272f253-be44-4a82-a541-9083e68cf99f/a22b3a93bd510bf062765ec5df6608fa6cae186a476b0407bfb5369ff99afdd2 /sys/fs/cgroup/hugetlb ro,nosuid,nodev,noexec,relatime master:19 - cgroup cgroup rw,hugetlb | ||
// See unit tests for more examples. | ||
// This is heuristic, the mount path is not always guaranteed. File issue at https://github.com/microsoft/ApplicationInsights-Kubernetes/issues if/when find it changed. | ||
private const string MatchPattern = @"/kubepods/.*?/.*?/(.*?)[\s|/]"; | ||
private static readonly Regex MatchRegex = new Regex(MatchPattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1)); | ||
|
||
private const string LogCategory = nameof(ContainerDMountInfoMatcher); | ||
|
||
public bool TryParseContainerId(string? line, out string containerId) | ||
{ | ||
containerId = string.Empty; | ||
if (string.IsNullOrEmpty(line)) | ||
{ | ||
return false; | ||
} | ||
|
||
Match match = MatchRegex.Match(line); | ||
if (!match.Success) | ||
{ | ||
_logger.LogDebug($"[{LogCategory}] No match for containerId. Input: {line}, pattern: {MatchPattern}"); | ||
return false; | ||
} | ||
_logger.LogTrace($"[{LogCategory}] Matched container id."); | ||
containerId = match.Groups[1].Value; | ||
return true; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...licationInsights.Kubernetes/ContainerIdProviders/Matchers/DockerEngineMountInfoMatcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.ApplicationInsights.Kubernetes.Debugging; | ||
|
||
namespace Microsoft.ApplicationInsights.Kubernetes.ContainerIdProviders; | ||
|
||
internal class DockerEngineMountInfoMatcher : IContainerIdMatcher | ||
{ | ||
private const string LogCategory = nameof(DockerEngineMountInfoMatcher); | ||
private readonly ApplicationInsightsKubernetesDiagnosticSource _logger = ApplicationInsightsKubernetesDiagnosticSource.Instance; | ||
|
||
private const string MatchPattern = @"/docker/containers/(.*?)/"; | ||
private static readonly Regex MatchRegex = new Regex(MatchPattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1)); | ||
|
||
public bool TryParseContainerId(string? line, out string containerId) | ||
{ | ||
containerId = string.Empty; | ||
if (string.IsNullOrEmpty(line)) | ||
{ | ||
return false; | ||
} | ||
|
||
Match match = MatchRegex.Match(line); | ||
if (!match.Success) | ||
{ | ||
_logger.LogDebug($"[{LogCategory}] No match for containerId. Input: {line}, pattern: {MatchPattern}"); | ||
return false; | ||
} | ||
_logger.LogTrace($"[{LogCategory}] Matched container id."); | ||
containerId = match.Groups[1].Value; | ||
return true; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/ApplicationInsights.Kubernetes/ContainerIdProviders/Matchers/IContainerIdMatcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#nullable enable | ||
|
||
namespace Microsoft.ApplicationInsights.Kubernetes.ContainerIdProviders; | ||
|
||
/// <summary> | ||
/// Matches container id. | ||
/// </summary> | ||
public interface IContainerIdMatcher | ||
{ | ||
/// <summary> | ||
/// Matches the container id. | ||
/// </summary> | ||
/// <param name="value">The value to match.</param> | ||
/// <param name="containerId">The container id when matched.</param> | ||
/// <returns>Returns true when matched. Otherwise, false.</returns> | ||
bool TryParseContainerId(string? value, out string containerId); | ||
} |
Oops, something went wrong.