Skip to content

VerifyTests/Verify.OpenTelemetry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verify.OpenTelemetry

Discussions Build status NuGet Status

Extends Verify to allow verification of OpenTelemetry types including System.Diagnostics.Activity and LogRecord

See Milestones for release notes.

Sponsors

Entity Framework Extensions

Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.

Entity Framework Extensions

NuGet

Usage

[ModuleInitializer]
public static void Initialize() =>
    VerifyOpenTelemetry.Initialize();

snippet source | anchor

Activity Verification

VerifyOpenTelemetry allows, when a method is being tested, for any Activity created as part of that method call to be recorded and verified.

Call Recording.Start() to begin listening. All activities from any ActivitySource will be captured by default.

[Fact]
public Task Usage()
{
    Recording.Start();
    using var source = new ActivitySource("TestSource");

    using (var activity = source.StartActivity("MyOperation"))
    {
        activity!.SetTag("key1", "value1");
        activity.SetTag("key2", 42);
    }

    return Verify("result");
}

snippet source | anchor

Results in:

{
  target: result,
  activity: {
    MyOperation: {
      Tags: {
        key1: value1,
        key2: 42
      }
    }
  }
}

snippet source | anchor

LogRecord Verification

OpenTelemetry LogRecord instances can be captured using InMemoryExporter and verified directly.

[Fact]
public Task LogRecordVerification()
{
    var logRecords = new List<LogRecord>();
    using var loggerFactory = LoggerFactory.Create(builder =>
    {
        builder.AddOpenTelemetry(options =>
        {
            options.AddInMemoryExporter(logRecords);
        });
    });

    var logger = loggerFactory.CreateLogger("TestCategory");
    logger.LogInformation("Hello {Name}", "World");

    return Verify(logRecords);
}

snippet source | anchor

Results in:

[
  {
    CategoryName: TestCategory,
    LogLevel: Information,
    Body: Hello {Name},
    Attributes: {
      {OriginalFormat}: Hello {Name},
      Name: World
    }
  }
]

snippet source | anchor

Serialization

Activities are serialized with the following conventions:

  • OperationName is used as the JSON property key
  • DisplayName only included if different from OperationName
  • Kind only included if not Internal
  • Status and StatusDescription only included if not Unset
  • Tags, Events, Links, and Baggage included when present
  • Non-deterministic values (Id, TraceId, SpanId, ParentSpanId, Duration, StartTimeUtc, Source) are omitted

LogRecords are serialized with the following conventions:

  • Timestamp, TraceId, SpanId are omitted (non-deterministic)
  • CategoryName, LogLevel, Body, FormattedMessage included when present
  • EventId only included if non-default
  • Exception included when present
  • Attributes included when present

Icon

Diagnostic from The Noun Project.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages