Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CGroupv2 support into Docker Extensions #839

Merged
merged 18 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
15b1b51
Initial changes
anoopbabu29 Dec 6, 2022
4cb146d
Adding cgroupv2 to test and updating the approach such that it checks…
anoopbabu29 Dec 15, 2022
cb8f573
Merge branch 'main' of https://github.com/anoopbabu29/opentelemetry-d…
anoopbabu29 Dec 16, 2022
7ff1941
Merge branch 'open-telemetry:main' into cgroupv2-sup
anoopbabu29 Dec 16, 2022
aad5630
Merge branch 'cgroupv2-sup' of https://github.com/anoopbabu29/opentel…
anoopbabu29 Dec 19, 2022
94be6e9
Respoding to Comments and build checks
anoopbabu29 Dec 19, 2022
856fed7
Merge branch 'main' into cgroupv2-sup
anoopbabu29 Dec 19, 2022
fd0c8e4
Adjusted build such that Detect checks both versions, and BuildResour…
anoopbabu29 Dec 22, 2022
5ba7994
Merge branch 'cgroupv2-sup' of https://github.com/anoopbabu29/opentel…
anoopbabu29 Dec 22, 2022
78a5a62
Merge branch 'main' into cgroupv2-sup
anoopbabu29 Dec 22, 2022
14ce9b1
Adjusting BuildResource at suggestion
anoopbabu29 Dec 23, 2022
492552d
Merge branch 'cgroupv2-sup' of https://github.com/anoopbabu29/opentel…
anoopbabu29 Dec 23, 2022
a3647b3
Added same tests as the go version as well as their regex expression
anoopbabu29 Jan 4, 2023
9e05aba
Merge branch 'main' into cgroupv2-sup
anoopbabu29 Jan 4, 2023
b5857f0
Addressing Comments by adding Enums and cleaning up tests
anoopbabu29 Jan 5, 2023
7b7e580
Merge branch 'main' into cgroupv2-sup
anoopbabu29 Jan 6, 2023
ba6b6ab
Fix parse mode version in test
anoopbabu29 Jan 6, 2023
d043c33
Merge branch 'main' into cgroupv2-sup
utpilla Jan 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace OpenTelemetry.Extensions.Docker.Resources;
public class DockerResourceDetector : IResourceDetector
{
private const string FILEPATH = "/proc/self/cgroup";
private const string FILEPATHV2 = "/proc/self/mountinfo";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any usage of FILEPATHV2 in detector. I'd guess, that ExtractContainerIdV2 should use it, instead of FILEPATH. Proper fix for it will also solve problem mentioned by @alexeypukhov , as that functions should check different file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change has been made to check both versions in the Detect method and assuming the version being used in the BuildResource method. This way, the problem mentioned by @alexeypukhov would be solved as you mentioned since no container id would be able to be found when checking for cgroupv1 if cgroupv2 is being used.

private const string HOSTNAME = "hostname";

/// <summary>
/// Detects the resource attributes from Docker.
Expand All @@ -41,7 +43,7 @@ public Resource Detect()
/// <summary>
/// Builds the resource attributes from Container Id in file path.
/// </summary>
/// <param name="path">File path where container id exists.</param>
/// <param name="path">cgroup path.</param>
/// <returns>Returns Resource with list of key-value pairs of container resource attributes if container id exists else empty resource.</returns>
internal Resource BuildResource(string path)
{
Expand All @@ -60,9 +62,28 @@ internal Resource BuildResource(string path)
/// <summary>
/// Extracts Container Id from path.
/// </summary>
/// /// <param name="path">cgroup path.</param>
/// <returns>Returns Resource with list of key-value pairs of container resource attributes if container id exists else empty resource.</returns>
private string ExtractContainerId(string path)
{
try
{
return this.ExtractContainerIdV1(path) ?? this.ExtractContainerIdV2(path);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just thinking out loud.
If the cgroup path will match the Id V2 (e.g. "13:name=systemd:/pod/d86d75589bf6cc254f3e2cc29debdf85dde404998aa128997a819ff991827356/hostname") the only way ExtractContainerIdV1() won't return you "hostname" is (I assume, please correct me if I'm wrong) because of EncodingUtils.IsValidHexString() that won't treat this string valid.
So my thoughts:

  1. it's a bit too advanced in the V1 logic to finally decide that this is the wrong string (I mean it goes thru a lot of code before realizing this)
  2. it's a bit fragile and is very easy to make a bug because from a IsValidHexString() context it's not clear how important this code for this place here. I know it's covered by unit tests but still

So maybe since the V2 logic puts a very specific requirement on the format of the string we should run it first? It will early out much faster than V1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with that logic. After reviewing it, I believe that the IsValidHexString() is the method that prevents the hostname from being accepted and switching the two would be helpful since, as you mentioned, it has a stricter requirement. One way to make IsValidHexString() less fragile is to perhaps make a more strict requirement like a character limit (64 characters)? But it is also unclear to me how crucial this method is so I definitely do not think that I should change it here.

}
catch (Exception ex)
{
DockerExtensionsEventSource.Log.ExtractResourceAttributesException($"{nameof(DockerResourceDetector)} : Failed to extract Container id from path", ex);
}

return null;
}

/// <summary>
/// Extracts Container Id from path using the cgroupv1 format.
/// </summary>
/// <param name="path">cgroup path.</param>
/// <returns>Container Id, Null if not found or exception being thrown.</returns>
private string ExtractContainerId(string path)
private string ExtractContainerIdV1(string path)
{
try
{
Expand All @@ -88,6 +109,37 @@ private string ExtractContainerId(string path)
return null;
}

/// <summary>
/// Extracts Container Id from path using the cgroupv2 format.
/// </summary>
/// <param name="path">File path where container id exists.</param>
/// <returns>Returns Resource with list of key-value pairs of container resource attributes if container id exists else empty resource.</returns>
private string ExtractContainerIdV2(string path)
{
try
{
if (!File.Exists(path))
{
return null;
}

foreach (string line in File.ReadLines(path))
{
string containerId = (!string.IsNullOrEmpty(line) && line.Contains(HOSTNAME)) ? this.GetIdFromLineV2(line) : null;
if (!string.IsNullOrEmpty(containerId))
{
return containerId;
}
}
}
catch (Exception ex)
{
DockerExtensionsEventSource.Log.ExtractResourceAttributesException($"{nameof(DockerResourceDetector)} : Failed to extract Container id from path", ex);
}

return null;
}

/// <summary>
/// Gets the Container Id from the line after removing the prefix and suffix.
/// </summary>
Expand Down Expand Up @@ -116,6 +168,27 @@ private string GetIdFromLine(string line)
return containerId;
}

private string GetIdFromLineV2(string line)
{
int hostnameIndex = line.LastIndexOf("/" + HOSTNAME);
if (hostnameIndex < 0)
{
return null;
}

string earlierSection = line.Substring(0, hostnameIndex);
int startIndex = earlierSection.LastIndexOf('/');

string containerId = this.RemovePrefixAndSuffixIfneeded(earlierSection, startIndex, hostnameIndex);

if (string.IsNullOrEmpty(containerId) || !EncodingUtils.IsValidHexString(containerId))
{
return null;
}

return containerId;
}

private string RemovePrefixAndSuffixIfneeded(string input, int startIndex, int endIndex)
{
startIndex = (startIndex == -1) ? 0 : startIndex + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public class DockerResourceDetectorTests
private const string CONTAINERID =
"d86d75589bf6cc254f3e2cc29debdf85dde404998aa128997a819ff991827356";

// cgroupv2 line with container Id
private const string CGROUPLINEV2 =
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you also need to check suffix or prefix for cgroup v2?
Also just a thought. Should we check prefix and suffix end to end, I mean with reading from file? If GetContainerId works with extracted cgroup string no matter V1 or V2, maybe we should have a separate test just for this function and do not mix it with reading the line from the file and parsing it depending on V1 or V2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can check for them as they are separate implementations. I figured that most of the functionality was similar enough to warrant not having them but I believe you are correct that they should exist, although the suffix isn't a necessary test in my opinion since its not a real case I believe.

"13:name=systemd:/pod/d86d75589bf6cc254f3e2cc29debdf85dde404998aa128997a819ff991827356/hostname";

// Expected Container Id
private const string CONTAINERIDV2 = "d86d75589bf6cc254f3e2cc29debdf85dde404998aa128997a819ff991827356";


[Fact]
public void TestValidContainer()
{
Expand Down Expand Up @@ -85,6 +93,12 @@ public void TestValidContainer()
tempFile.Write(CGROUPLINE);
Assert.Equal(CONTAINERID, this.GetContainerId(dockerResourceDetector.BuildResource(tempFile.FilePath)));
}

using (TempFile tempFile = new TempFile())
{
tempFile.Write(CGROUPLINEV2);
Assert.Equal(CONTAINERIDV2, this.GetContainerId(dockerResourceDetector.BuildResource(tempFile.FilePath)));
}
}

[Fact]
Expand Down