Skip to content

purview-dev/purview-telemetry-sourcegenerator

Repository files navigation

Purview Telemetry Source Generator

Generates ActivitySource, ILogger, and Metrics based on interface methods.

This approach allows for:

  • Faster iteration cycles - simply create the method on the interface
  • Easy substitution for testing testing - for an example, check the sample project
  • Built-in dependency injection helper generation

Use the latest version available on NuGet, which supports the following frameworks:

  • .NET Framework 4.7.2, or higher
  • .NET 8 or higher

Reference in your Directory.Build.props or .csproj file:

<PackageReference Include="Purview.Telemetry.SourceGenerator" Version="3.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>

For more information see the wiki.

Example Interface

This is called a multi-target interface because it generates more than one output type: Activities, Logging, and Metrics.

When generating a single target, the generator will automatically infer the necessary attributes. More information about multi-targeting can be found in here.

using Purview.Telemetry.Activities;
using Purview.Telemetry.Logging;
using Purview.Telemetry.Metrics;

/// <summary>
/// Generates an implementation of the methods for each generation type (Activity, Logging, or Metrics)
/// and an extension method to enable easy registration with the IServiceCollection.
/// </summary>
[ActivitySource]
[Logger]
[Meter]
interface IEntityStoreTelemetry
{
    /// <summary>
    /// Creates and starts an Activity and adds the parameters as Tags and Baggage.
    /// </summary>
    [Activity]
    Activity? GettingEntityFromStore(int entityId, [Baggage]string serviceUrl);

    /// <summary>
    /// Adds an ActivityEvent to the Activity with the parameters as Tags.
    /// </summary>
    [Event]
    void GetDuration(Activity? activity, int durationInMS);

    /// <summary>
    /// Adds the parameters as Baggage to the Activity.
    /// </summary>
    [Context]
    void RetrievedEntity(Activity? activity, float totalValue, int lastUpdatedByUserId);

    /// <summary>
    /// Generates a structured log message using an ILogger - defaults to Informational.
    /// </summary>
    [Log]
    void ProcessingEntity(int entityId, string updateState);

    /// <summary>
    /// Generates a structured log message using an ILogger, specifically defined as Informational.
    /// </summary>
    [Info]
    void ProcessingAnotherEntity(int entityId, string updateState);

    /// <summary>
    /// Adds 1 to a Counter<T> with the entityId as a Tag.
    /// </summary>
    [AutoCounter]
    void RetrievingEntity(int entityId);
}

To see the code generated for the IEntityStoreTelemetry interface, see the Generated Output page in the wiki.

Example Project

The .NET Aspire Sample demos the Activities, Logs, and Metrics generation working together with the Aspire Dashboard.

Check the page in the the wiki for information.

This sample project has EmitCompilerGeneratedFiles set to true, so you can easily see the generated output.

Notes on Logging Generation

There are two types of logging generation based on:

  1. Microsoft Logging Extension Packages – Determined by the NuGet packages referenced in your project.
  2. Attribute-based Configuration – Controlled using attributes in your code.

For more details, see the Logging page in the wiki.