-
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
8 changed files
with
174 additions
and
7 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/ApplicationInsights.Kubernetes/ContainerIdProviders/ContainerIdNormalizer.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,57 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.ApplicationInsights.Kubernetes.Debugging; | ||
|
||
namespace Microsoft.ApplicationInsights.Kubernetes.ContainerIdProviders; | ||
|
||
/// <summary> | ||
/// A simple container id normalizer that picks out 64 digits of GUID/UUID from a container id with prefix / suffix. | ||
/// For example: | ||
/// cri-containerd-5146b2bcd77ab4f2624bc1fbd98cf9751741344a80b043dbd77a4e847bff4f06.scope will be normalized to | ||
/// 5146b2bcd77ab4f2624bc1fbd98cf9751741344a80b043dbd77a4e847bff4f06 | ||
/// </summary> | ||
internal class ContainerIdNormalizer : IContainerIdNormalizer | ||
{ | ||
// Simple rule: First 64-characters GUID/UUID. | ||
private const string ContainerIdIdentifierPattern = @"([a-f\d]{64})"; | ||
private readonly Regex _matcher = new Regex(ContainerIdIdentifierPattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant, matchTimeout: TimeSpan.FromSeconds(1)); | ||
private readonly ApplicationInsightsKubernetesDiagnosticSource _logger = ApplicationInsightsKubernetesDiagnosticSource.Instance; | ||
|
||
/// <summary> | ||
/// Gets normalized container id. | ||
/// </summary> | ||
/// <param name="input">The original container id. String.Empty yields string.Empty with true. Null is not accepted.</param> | ||
/// <param name="normalized">The normalized container id.</param> | ||
/// <returns>True when the normalized succeeded. False otherwise.</returns> | ||
public bool TryNormalize(string input, out string? normalized) | ||
{ | ||
// Should not happen. Put here just in case. | ||
if (input is null) | ||
{ | ||
throw new ArgumentNullException(nameof(input)); | ||
} | ||
|
||
// Special case: string.Empty in, string.Empty out. | ||
if (input == string.Empty) | ||
{ | ||
normalized = string.Empty; | ||
return true; | ||
} | ||
|
||
_logger.LogDebug($"Normalize container id: {input}"); | ||
|
||
Match match = _matcher.Match(input); | ||
if (!match.Success) | ||
{ | ||
_logger.LogDebug($"Failed match any container id by pattern: {ContainerIdIdentifierPattern}."); | ||
normalized = null; | ||
return false; | ||
} | ||
normalized = match.Groups[1].Value; | ||
_logger.LogTrace($"Container id normalized to: {normalized}"); | ||
return true; | ||
} | ||
} | ||
|
17 changes: 17 additions & 0 deletions
17
src/ApplicationInsights.Kubernetes/ContainerIdProviders/IContainerIdNormalizer.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> | ||
/// A service to normalize container id. | ||
/// </summary> | ||
internal interface IContainerIdNormalizer | ||
{ | ||
/// <summary> | ||
/// Tries to normalize container id. | ||
/// </summary> | ||
/// <param name="input">The container id.</param> | ||
/// <param name="normalized">The normalized container id.</param> | ||
/// <returns>True when normalized. False otherwise.</returns> | ||
bool TryNormalize(string input, out string? normalized); | ||
} |
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
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
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
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
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,65 @@ | ||
using System; | ||
using Xunit; | ||
|
||
namespace Microsoft.ApplicationInsights.Kubernetes.ContainerIdProviders.Tests; | ||
|
||
public class ContainerIdNormalizerTests | ||
{ | ||
[Theory] | ||
// With prefix and suffix: | ||
[InlineData(@"cri-containerd-5146b2bcd77ab4f2624bc1fbd98cf9751741344a80b043dbd77a4e847bff4f06.scope", "5146b2bcd77ab4f2624bc1fbd98cf9751741344a80b043dbd77a4e847bff4f06")] | ||
// With prefix only: | ||
[InlineData(@"docker://5146b2bcd77ab4f2624bc1fbd98cf9751741344a80b043dbd77a4e847bff4f06", "5146b2bcd77ab4f2624bc1fbd98cf9751741344a80b043dbd77a4e847bff4f06")] | ||
// With suffix only: | ||
[InlineData(@"5146b2bcd77ab4f2624bc1fbd98cf9751741344a80b043dbd77a4e847bff4f06-scope", "5146b2bcd77ab4f2624bc1fbd98cf9751741344a80b043dbd77a4e847bff4f06")] | ||
// Same as normalized: | ||
[InlineData(@"5146b2bcd77ab4f2624bc1fbd98cf9751741344a80b043dbd77a4e847bff4f06", "5146b2bcd77ab4f2624bc1fbd98cf9751741344a80b043dbd77a4e847bff4f06")] | ||
// Longer than 64 digits - notes: so that the match regex is simplified. | ||
[InlineData(@"5146b2bcd77ab4f2624bc1fbd98cf9751741344a80b043dbd77a4e847bff4f06a", "5146b2bcd77ab4f2624bc1fbd98cf9751741344a80b043dbd77a4e847bff4f06")] | ||
public void TryGetNormalizedShouldNormalizeContainerIds(string input, string expected) | ||
{ | ||
ContainerIdNormalizer target = new ContainerIdNormalizer(); | ||
bool result = target.TryNormalize(input, out string actual); | ||
|
||
Assert.True(result); | ||
Assert.Equal(expected, actual); | ||
} | ||
|
||
[Theory] | ||
// Input has no container id | ||
[InlineData("Input has no container id")] | ||
// Short guid is not accepted | ||
[InlineData("f78375b1c487")] | ||
// Shorter than 64 digits | ||
[InlineData("5146b2bcd77ab4f2624bc1fbd98cf9751741344a80b043dbd77a4e847bff4f0")] | ||
// Not a valid guid with character of z | ||
[InlineData("5146b2bcd77ab4f2624bc1fbd98cf9751741344a80b043dbd77a4e847bff4f0z")] | ||
public void TryGetNormalizedShouldNotAcceptInvalidContainerIds(string input) | ||
{ | ||
ContainerIdNormalizer target = new ContainerIdNormalizer(); | ||
bool result = target.TryNormalize(input, out string actual); | ||
|
||
Assert.False(result); | ||
Assert.Null(actual); | ||
} | ||
|
||
[Fact] | ||
public void TryGetNormalizedShouldHandleStringEmpty() | ||
{ | ||
ContainerIdNormalizer target = new ContainerIdNormalizer(); | ||
bool result = target.TryNormalize(string.Empty, out string actual); | ||
|
||
Assert.True(result); | ||
Assert.Equal(string.Empty, actual); | ||
} | ||
|
||
[Fact] | ||
public void TryGetNormalizedShouldDoesNotAcceptNull() | ||
{ | ||
ContainerIdNormalizer target = new ContainerIdNormalizer(); | ||
Assert.Throws<ArgumentNullException>(() => | ||
{ | ||
_ = target.TryNormalize(null, out string actual); | ||
}); | ||
} | ||
} |
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