CodeWithSaar.Extensions.ContainerId 0.0.2

dotnet add package CodeWithSaar.Extensions.ContainerId --version 0.0.2                
NuGet\Install-Package CodeWithSaar.Extensions.ContainerId -Version 0.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CodeWithSaar.Extensions.ContainerId" Version="0.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CodeWithSaar.Extensions.ContainerId --version 0.0.2                
#r "nuget: CodeWithSaar.Extensions.ContainerId, 0.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CodeWithSaar.Extensions.ContainerId as a Cake Addin
#addin nuget:?package=CodeWithSaar.Extensions.ContainerId&version=0.0.2

// Install CodeWithSaar.Extensions.ContainerId as a Cake Tool
#tool nuget:?package=CodeWithSaar.Extensions.ContainerId&version=0.0.2                

Get the container id

Package-Badge Download-Badge

Getting container id is important for diagnostic / logging.

Get Started

  • Add NuGet package
dotnet add package CodeWithSaar.Extensions.ContainerId --prerelease
  • Put container id in your telemetry
string containerId = await ContainerService.GetContainerIdAsync();
// Your logging shows container id now.
_logger.LogInformation("This is some information from container: {containerId}", containerId);
  • Output:
info: TestClass[0] This is some information from container: 01d1ad1a7b528295f2d03619b35f995be9ddddcfd2a83c013df129aa6cc4d332

That's it.

Alternatively, dependency injection is also supported, refer to Use it with dependency injection.

Example utilities

By using the library, it is easy to build some utilities.

  • Public docker image to detect container id:
docker pull saars/cid
docker run saars/cid

Output:

Container Id: 7f1477ac7aa29f3e52d8d4f25632e9df461a3855cc8cd8ad071ad94181381215

  • Inspect logs from the worker in Kubernetes Cluster:

    • Deploy saars/cid-worker into a Kubernetes cluster.

    • Check the logs of the pod: kubectl logs <podName>

      info: CodeWithSaar.Extensions.ContainerId.CGroupV1ContainerIdProvider[0] No container id matched.
      info: CodeWithSaar.Extensions.ContainerId.CGroupV2ContainerIdProvider[0] Matched. Value: 01d1ad1a7b528295f2d03619b35f995be9ddddcfd2a83c013df129aa6cc4d332
      info: CodeWithSaar.ContainerId.Worker.Worker[0] Container Id: 01d1ad1a7b528295f2d03619b35f995be9ddddcfd2a83c013df129aa6cc4d332. Wait for 10 seconds.
      
      • Notice, in that case, the first provider missed the container since it doesn't exist. But the second provider got it.

Use it with dependency injection

You still need to add the NuGet package, follow the instructions above. Then:

  1. Register the services:

    // TryAddContainerIdServices is an extension method provided in CodeWithSaar.Extensions.ContainerId.
    services.TryAddContainerIdServices();
    
  2. Inject it and use the service:

    private readonly IContainerService _containerService;
    class Consumer(IContainerService containerService)
    {
        _containerService = containerService ?? throw new ArgumentNullException(nameof(containerService));
    }
    
    public Task ShowContainerIdAsync()
    {
        Console.WriteLine(await _containerService.GetContainerIdAsync());
    }
    

Implementation details

It has been working by fetching /proc/self/cgroup for container id until lately, cGroupV2 rolls out, and there needs to be new ways to get it.

This repo is a yield of the investigations to aggregate various ways to get container ids for both cGroupV1 or cGroupV2 containers. When needed, it shall be able to be extended to support other technologies as time goes by.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.2 460 6/9/2022
0.0.1 420 5/27/2022