Intility.OpenTelemetry.Instrumentation.Assembly
0.2.1
dotnet add package Intility.OpenTelemetry.Instrumentation.Assembly --version 0.2.1
NuGet\Install-Package Intility.OpenTelemetry.Instrumentation.Assembly -Version 0.2.1
<PackageReference Include="Intility.OpenTelemetry.Instrumentation.Assembly" Version="0.2.1" />
<PackageVersion Include="Intility.OpenTelemetry.Instrumentation.Assembly" Version="0.2.1" />
<PackageReference Include="Intility.OpenTelemetry.Instrumentation.Assembly" />
paket add Intility.OpenTelemetry.Instrumentation.Assembly --version 0.2.1
#r "nuget: Intility.OpenTelemetry.Instrumentation.Assembly, 0.2.1"
#addin nuget:?package=Intility.OpenTelemetry.Instrumentation.Assembly&version=0.2.1
#tool nuget:?package=Intility.OpenTelemetry.Instrumentation.Assembly&version=0.2.1
OpenTelemetry.Instrumentation.Assembly
A .NET library for automatically instrumenting assemblies with OpenTelemetry traces. This package enables automatic instrumentation of controllers and services within a specified assembly, tracing method calls and capturing method parameters.
Features
- Automatically traces ASP.NET Core controller actions
- Automatically wraps and traces interface implementations
- Captures method arguments as trace attributes
- Traces exceptions and adds them to spans
- Works with both standard and keyed services in ASP.NET Core dependency injection
Installation
Install the package from NuGet:
dotnet add package Intility.OpenTelemetry.Instrumentation.Assembly
Usage
Basic Configuration
Add the following packages to your .csproj
:
<ItemGroup>
<PackageReference Include="Intility.OpenTelemetry.Instrumentation.Assembly" Version="0.1.2" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.11.2" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.11.2" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.11.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.11.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.11.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.11.0-beta.2" />
</ItemGroup>
Add assembly instrumentation to your OpenTelemetry configuration:
using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
// Add OpenTelemetry
var otel = builder.Services.AddOpenTelemetry();
otel.ConfigureResource(r => r
.AddService(serviceName: "MyService")
// Optional, add serviceVersion and environment
// .AddAttributes(new Dictionary<string, string>(){
// { "deployment.environment.name", "Development" },
// { "service.version", "1.0.0" }
// } )
);
// Configure tracing
otel.WithTracing(tracing =>
{
// Add standard instrumentations
tracing.AddAspNetCoreInstrumentation();
tracing.AddHttpClientInstrumentation();
// Optional, dependent om your system
// tracing.AddEntityFrameworkCoreInstrumentation();
// tracing.AddRedisInstrumentation();
// Add exporter of your choice
tracing.AddConsoleExporter();
// or
tracing.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri("https://<url>/v1/traces");
// Enable Http for default tracing to logfire. When using port 4317,grpc is default.
otlpOptions.Protocol = OtlpExportProtocol.HttpProtobuf;
// Set the token if authentication is required
//otlpOptions.Headers = "Authorization=<Token>";
});
});
// Register your services
builder.Services.AddSingleton<IMyService, MyService>();
builder.Services.AddControllers();
// Add assembly instrumentation LAST
otel.AddAssemblyInstrumentation(typeof(Program).Assembly.FullName!);
Important Notes
Always add the assembly instrumentation after registering all services you want to instrument.
The assembly instrumentation will automatically:
- Add trace spans for controller actions in the specified assembly
- Wrap interfaces defined or implemented in the specified assembly with a tracing proxy
- Capture method parameters as span attributes
The assembly name parameter should be the fully qualified assembly name:
typeof(Program).Assembly.FullName!
How It Works
The library works by:
- Adding an
ActionFilter
to trace ASP.NET Core controller actions - Using the .NET
DispatchProxy
class to create dynamic proxies for interface implementations - Creating spans for method calls and capturing method arguments as span attributes
- Handling exceptions and adding them to spans
Example
In the sample application, you can see how the library automatically instruments:
- Controller actions in
WeatherForecastController
- Service method calls in
WeatherForecastService
Each method call becomes a span in the trace, with method parameters captured as attributes.
Requirements
- .NET 9.0 or higher
- OpenTelemetry SDK 1.11.0 or higher
- OpenTelemetry.Extensions.Hosting
- ASP.NET Core for Web API scenarios
License
This project is licensed under the terms specified in the repository.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. net9.0 was computed. 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. |
-
net8.0
- Microsoft.AspNetCore.Mvc.Core (>= 2.3.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.3)
- OpenTelemetry (>= 1.11.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.