Shared.K8.Common
1.0.3
dotnet add package Shared.K8.Common --version 1.0.3
NuGet\Install-Package Shared.K8.Common -Version 1.0.3
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="Shared.K8.Common" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Shared.K8.Common" Version="1.0.3" />
<PackageReference Include="Shared.K8.Common" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Shared.K8.Common --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Shared.K8.Common, 1.0.3"
#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.
#:package Shared.K8.Common@1.0.3
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Shared.K8.Common&version=1.0.3
#tool nuget:?package=Shared.K8.Common&version=1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Shared.K8.Common
A comprehensive .NET 9 library for configuring OpenTelemetry observability (logging, metrics, and tracing) in ASP.NET Core applications using standard OpenTelemetry environment variables.
Features
- Simple API: Just two extension methods -
AddObservability()andUseObservability() - Zero Configuration: Works out-of-the-box with sensible defaults
- Standard Compliant: Uses official OpenTelemetry environment variables
- Comprehensive Telemetry: Logging, metrics, and distributed tracing included
- Error Resilient: Gracefully falls back to console when OTLP endpoints are unavailable
- Production Ready: Built on OpenTelemetry 1.14.0 with .NET 9 support
Quick Start
using Shared.K8.Common.OpenTelemetry;
var builder = WebApplication.CreateBuilder(args);
// Add OpenTelemetry observability
builder.AddObservability();
var app = builder.Build();
// Configure observability middleware
app.UseObservability();
app.Run();
That's it! Your application now has comprehensive observability configured with:
- Structured logging with OpenTelemetry
- ASP.NET Core and HTTP client metrics and tracing
- Prometheus metrics endpoint at
/metrics - Automatic fallback to console when no OTLP endpoint is configured
Configuration
Configure the library using standard OpenTelemetry environment variables:
# Basic configuration
OTEL_SERVICE_NAME=my-web-api
OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4317
OTEL_EXPORTER_OTLP_PROTOCOL=grpc
# With authentication
OTEL_SERVICE_NAME=my-web-api
OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp.company.com:4317
OTEL_EXPORTER_OTLP_HEADERS="authorization=Bearer token123"
| Environment Variable | Description | Default |
|---|---|---|
OTEL_SERVICE_NAME |
Service name for telemetry | Application name |
OTEL_EXPORTER_OTLP_ENDPOINT |
OTLP endpoint URL | None (uses console) |
OTEL_EXPORTER_OTLP_PROTOCOL |
Protocol (grpc or http/protobuf) |
grpc |
OTEL_EXPORTER_OTLP_HEADERS |
Headers (e.g., auth) | None |
What's Included
Automatic Instrumentation
- ASP.NET Core: HTTP request/response metrics and tracing
- HTTP Client: Outbound HTTP call metrics and tracing
- Prometheus:
/metricsendpoint for scraping
Built-in Meters
Microsoft.AspNetCore.Hosting- Request metricsMicrosoft.AspNetCore.Server.Kestrel- Server metricsSystem.Net.Http- HTTP client metricsSystem.Net.NameResolution- DNS metrics- Custom service meter (matches your service name)
Resource Attributes
service.name- From environment or application nameservice.version- Set to "1.0.0"service.instance.id- Machine namedeployment.environment- Environment nameservice.namespace- Set to "mockery"
Smart Fallbacks
- OTLP available: Exports to your configured endpoint
- OTLP unavailable: Falls back to console output
- No configuration: Uses console for development
Usage Examples
Docker Deployment
FROM mcr.microsoft.com/dotnet/aspnet:9.0
WORKDIR /app
COPY . .
ENV OTEL_SERVICE_NAME=my-web-api
ENV OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4317
ENTRYPOINT ["dotnet", "MyApp.dll"]
Kubernetes
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-web-api
spec:
template:
spec:
containers:
- name: my-web-api
image: my-web-api:latest
env:
- name: OTEL_SERVICE_NAME
value: "my-web-api"
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: "http://otel-collector:4317"
With Custom Telemetry
using System.Diagnostics;
using System.Diagnostics.Metrics;
var builder = WebApplication.CreateBuilder(args);
// Create custom telemetry
using var activitySource = new ActivitySource("MyApp.Orders");
using var meter = new Meter("MyApp.BusinessMetrics");
var orderCounter = meter.CreateCounter<int>("orders_total");
builder.AddObservability();
var app = builder.Build();
app.UseObservability();
app.MapPost("/orders", (Order order) =>
{
using var activity = activitySource.StartActivity("ProcessOrder");
activity?.SetTag("order.id", order.Id);
orderCounter.Add(1, new("status", "processed"));
return Results.Ok();
});
app.Run();
Requirements
- .NET 9.0 or later
- ASP.NET Core applications
Links
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- OpenTelemetry.Exporter.Console (>= 1.14.0)
- OpenTelemetry.Exporter.OpenTelemetryProtocol (>= 1.14.0)
- OpenTelemetry.Exporter.Prometheus.AspNetCore (>= 1.9.0-beta.2)
- OpenTelemetry.Extensions.Hosting (>= 1.14.0)
- OpenTelemetry.Instrumentation.AspNetCore (>= 1.14.0)
- OpenTelemetry.Instrumentation.Http (>= 1.14.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.