Monitoring and Logging

The Kubernetes Client library utilizes the built-in metrics provided by System.Diagnostics.DiagnosticsSource within the HttpClient to track performance data. These metrics can be consumed and exposed in a variety of ways, but it is the responsibility of the application to handle them, not the library itself.

For more information on how the library leverages System.Diagnostics.DiagnosticsSource for performance metrics, refer to the following resources:

Logging

To ensure efficient integration with your existing logging infrastructure, the library has been designed to work seamlessly with the Microsoft.Extensions.Logging framework. This allows you to leverage your preferred logging provider, such as Serilog, NLog, or the default .NET logging, for capturing and processing log events.

The following examples demonstrate how to integrate with different logging providers.

Example using TestOutputLogger:

The example project examples/logs/logs.csproj demonstrates how to use the TestOutputLogger to log to the output of the current Xunit test. This logger is provided in the k8s.Tests.Logging namespace and allows you to view logs within the test results.

// tests/KubernetesClient.Tests/Logging/TestOutputLogger.cs
          namespace k8s.Tests.Logging
          {
              /// <summary>
              ///     An implementation of ILogger that writes to the output of the current Xunit test.
              /// </summary>
              internal sealed class TestOutputLogger
                  : ILogger
              {
                  /// <summary>
                  /// Initializes a new instance of the TestOutputLogger class.
                  /// </summary>
                  /// <param name="testOutput">The output for the current test.</param>
                  /// <param name="loggerCategory">The logger's category name.</param>
                  /// <param name="minLogLevel">The logger's minimum log level.</param>
                  public TestOutputLogger(ITestOutputHelper testOutput, string loggerCategory, LogLevel minLogLevel)
                  {
                      // ...
                  }
          
                  // ...
          
                  /// <summary>
                  ///     Emit a log entry.
                  /// </summary>
                  /// <typeparam name="TState">Type of state to log.</typeparam>
                  /// <param name="level">The log entry's level.</param>
                  /// <param name="eventId">The log entry's associated event Id.</param>
                  /// <param name="state">The log entry to be written. Can be also an object.</param>
                  /// <param name="exception">The exception (if any) related to the log entry.</param>
                  /// <param name="formatter">A function that creates a string log message from the state and exception.</param>
                  public void Log<TState>(LogLevel level, EventId eventId, TState state, Exception exception,
                      Func<TState, Exception, string> formatter)
                  {
                      // ...
                  }
              }
          }
          

Example using OpenTelemetry:

The example project examples/openTelemetryConsole/openTelemetryConsole.csproj demonstrates how to use OpenTelemetry to capture and export metrics.

Example using Watch:

The example project examples/watch/watch.csproj demonstrates how to use Watch to observe changes in Kubernetes resources.

Example using Metrics:

The example project examples/metrics/metrics.csproj demonstrates how to access the built-in metrics provided by HttpClient.

Other Considerations:

The following project files contain relevant code for understanding logging and monitoring:

  • tests/SkipTestLogger/SkipTestLogger.csproj
  • src/KubernetesClient.Aot/KubernetesClient.Aot.csproj
  • src/KubernetesClient.Classic/KubernetesClient.Classic.csproj
  • src/KubernetesClient/KubernetesClient.csproj
  • tests/KubernetesClient.Tests/KubernetesClient.Tests.csproj
  • src/LibKubernetesGenerator/templates/Operations.cs.template

These files contain code related to logging, metrics, and other aspects of the library’s behavior. Reviewing the code within these files will provide a more in-depth understanding of how monitoring and logging are implemented in the Kubernetes Client library.

Top-Level Directory Explanations

examples/ - This directory contains example projects and usage scenarios for the Kubernetes client library.

examples/logs/ - This subdirectory contains examples of working with logs in Kubernetes.

examples/metrics/ - This subdirectory contains examples of working with metrics in Kubernetes.

examples/openTelemetryConsole/ - This subdirectory contains an example of using OpenTelemetry Console with the Kubernetes client library.

examples/watch/ - This subdirectory contains examples of watching Kubernetes resources for changes.

examples/webApiDependencyInjection/ - This subdirectory contains an example of using dependency injection in a web API using the Kubernetes client library.

examples/workerServiceDependencyInjection/ - This subdirectory contains an example of using dependency injection in a worker service using the Kubernetes client library.

src/ - This directory contains the source code for the project.

src/KubernetesClient/ - This subdirectory contains the main Kubernetes client library source code.

src/KubernetesClient/Models/ - This subdirectory contains model classes for various Kubernetes resources and objects.

tests/ - This directory contains test code for the project.

tests/E2E.Tests/ - This subdirectory contains end-to-end tests for the non-AOT code.

tests/KubernetesClient.Tests/ - This subdirectory contains tests for the main implementation of the Kubernetes client library.

tests/KubernetesClient.Tests/Logging/ - This subdirectory contains tests for the logging functionality.

tests/SkipTestLogger/ - This subdirectory contains a test logger implementation that skips logging.