Beskar.CodeGeneration.ObserveGenerator 1.2.1

dotnet add package Beskar.CodeGeneration.ObserveGenerator --version 1.2.1
                    
NuGet\Install-Package Beskar.CodeGeneration.ObserveGenerator -Version 1.2.1
                    
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="Beskar.CodeGeneration.ObserveGenerator" Version="1.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Beskar.CodeGeneration.ObserveGenerator" Version="1.2.1" />
                    
Directory.Packages.props
<PackageReference Include="Beskar.CodeGeneration.ObserveGenerator" />
                    
Project file
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 Beskar.CodeGeneration.ObserveGenerator --version 1.2.1
                    
#r "nuget: Beskar.CodeGeneration.ObserveGenerator, 1.2.1"
                    
#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 Beskar.CodeGeneration.ObserveGenerator@1.2.1
                    
#: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=Beskar.CodeGeneration.ObserveGenerator&version=1.2.1
                    
Install as a Cake Addin
#tool nuget:?package=Beskar.CodeGeneration.ObserveGenerator&version=1.2.1
                    
Install as a Cake Tool

Beskar.CodeGeneration.ObserveGenerator

Beskar.CodeGeneration.ObserveGenerator is a source generator designed to simplify OpenTelemetry integration in C#. It automatically generates boilerplate for instruments and activity sources while providing a centralized registry for easy registration.


Features

  • Automatic Field Generation: Generates ActivitySource, Meter, and various instruments (Counter, Histogram, etc.) directly from attributes.
  • Static Separation: Moves implementation details into an internal {Name}Instrumentation class to prevent field duplication in generic classes.
  • Centralized Registry: Automatically populates an ObserveRegistry per assembly for bulk registration of telemetry sources.

Usage Example

By decorating a partial class with [Observe] and specific instrument attributes, the generator creates all necessary plumbing.

Source Code

[Observe] // Marks this class for generation
[ObserveActivity] // Default name is ClassName
[ObserveMeter("Test.Meters", "2.0.0")]
[ObserveInstrument("Counter", InstrumentKind.Counter, typeof(int))]
[ObserveInstrument("Histogram", InstrumentKind.Histogram, typeof(double))]
[ObserveInstrument("Gauge", InstrumentKind.Gauge, typeof(int))]
[ObserveInstrument("TestTest", InstrumentKind.UpDownCounter, typeof(int))]
public partial class Test
{
   public void TestMethod()
   {
      using var activity = ActivitySource.StartActivity();

      Counter.Add(1);
      Histogram.Record(20d);
   }
}

Generated Code

The generator produces a companion instrumentation class and links it back to your partial class:

public partial class Test
{
   private static readonly ActivitySource ActivitySource = TestInstrumentation.ActivitySource;
   private static readonly Meter MeterSource = TestInstrumentation.MeterSource;

   private static readonly Counter<int> Counter = TestInstrumentation.Counter;
   private static readonly Histogram<double> Histogram = TestInstrumentation.Histogram;
   private static readonly Gauge<int> Gauge = TestInstrumentation.Gauge;
   private static readonly UpDownCounter<int> TestTest = TestInstrumentation.TestTest;
}

internal static class TestInstrumentation
{
   public static readonly ActivitySource ActivitySource = new("Test", "1.0.0");
   public static readonly Meter MeterSource = new("Test.Meters", "2.0.0");

   public static readonly Counter<int> Counter = MeterSource.CreateCounter<int>("Counter", null, null);
   public static readonly Histogram<double> Histogram = MeterSource.CreateHistogram<double>("Histogram", null, null);
   public static readonly Gauge<int> Gauge = MeterSource.CreateGauge<int>("Gauge", null, null);
   public static readonly UpDownCounter<int> TestTest = MeterSource.CreateUpDownCounter<int>("TestTest", null, null);
}

Registration

One ObserveRegistry is generated per assembly. This allows you to register all telemetry sources in a single line during your application startup.

The Generated Registry

internal static class ObserveRegistry
{
   public static readonly ImmutableArray<string> GeneratedSourceNames = ImmutableArray.CreateRange<string>([
      "Test.Activities",
      "TestTwo",
   ]);

   public static readonly ImmutableArray<string> GeneratedMeterNames = ImmutableArray.CreateRange<string>([
      "Test.Meters",
      "TestTwo",
   ]);
}

You can use the registry to create clean extension methods for your TracerProviderBuilder and MeterProviderBuilder:

public static class GeneratedTelemetryExtensions
{
   public static TracerProviderBuilder AddGeneratedSources(this TracerProviderBuilder builder)
   {
      foreach (var activityName in ObserveRegistry.GeneratedSourceNames)
      {
         builder.AddSource(activityName);
      }
      
      return builder;
   }

   public static MeterProviderBuilder AddGeneratedMeters(this MeterProviderBuilder builder)
   {
      foreach (var meterName in ObserveRegistry.GeneratedMeterNames)
      {
         builder.AddMeter(meterName);
      }
      
      return builder;
   }
}
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.

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
1.2.1 103 4/29/2026
1.2.0 100 4/26/2026
1.1.9 102 4/25/2026
1.1.8 98 4/8/2026
1.1.7 92 4/7/2026
1.1.6 112 4/5/2026
1.1.5 93 4/5/2026
1.1.4 98 4/4/2026
1.1.3 102 4/4/2026