Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NATS.Net.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Project Path="src/NATS.Client.JetStream/NATS.Client.JetStream.csproj" />
<Project Path="src/NATS.Client.KeyValueStore/NATS.Client.KeyValueStore.csproj" />
<Project Path="src/NATS.Client.ObjectStore/NATS.Client.ObjectStore.csproj" />
<Project Path="src/NATS.Client.OpenTelemetry/NATS.Client.OpenTelemetry.csproj" />
<Project Path="src/NATS.Client.Serializers.Json/NATS.Client.Serializers.Json.csproj" />
<Project Path="src/NATS.Client.Services/NATS.Client.Services.csproj" />
<Project Path="src/NATS.Client.Simplified/NATS.Client.Simplified.csproj" />
Expand Down
12 changes: 12 additions & 0 deletions src/NATS.Client.Core/NatsTelemetry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace NATS.Client.Core;

/// <summary>
/// Telemetry identifiers for NATS .NET. Use these when configuring OpenTelemetry
/// or any other listener directly. The same name is used for both the
/// <see cref="System.Diagnostics.ActivitySource"/> and the
/// <see cref="System.Diagnostics.Metrics.Meter"/>.
Comment thread
mtmk marked this conversation as resolved.
/// </summary>
public static class NatsTelemetry
{
public const string SourceName = "NATS.Net";
}
17 changes: 17 additions & 0 deletions src/NATS.Client.OpenTelemetry/NATS.Client.OpenTelemetry.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!-- NuGet Packaging -->
<PackageTags>opentelemetry;tracing;metrics;observability;nats</PackageTags>
<Description>OpenTelemetry instrumentation for NATS .NET. Adds the ActivitySource and Meter for the client to a TracerProviderBuilder or MeterProviderBuilder.</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Api" Version="1.15.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NATS.Client.Core\NATS.Client.Core.csproj" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions src/NATS.Client.OpenTelemetry/NatsInstrumentationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NATS.Client.Core;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

namespace NATS.Client.OpenTelemetry;

public static class NatsInstrumentationExtensions
{
/// <summary>
/// Adds the NATS .NET client <see cref="System.Diagnostics.ActivitySource"/> to the tracer provider,
/// enabling distributed tracing for publish, subscribe, and request/reply operations.
/// </summary>
public static TracerProviderBuilder AddNatsClientInstrumentation(this TracerProviderBuilder builder) =>
builder.AddSource(NatsTelemetry.SourceName);

/// <summary>
/// Adds the NATS .NET client <see cref="System.Diagnostics.Metrics.Meter"/> to the meter provider,
/// enabling messaging metrics (published/consumed counters, operation duration, and more).
/// </summary>
public static MeterProviderBuilder AddNatsClientInstrumentation(this MeterProviderBuilder builder) =>
builder.AddMeter(NatsTelemetry.SourceName);
Comment thread
mtmk marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="[7,8)" />
<PackageReference Include="OpenTelemetry" Version="1.15.3" />
<PackageReference Include="ProcessX" Version="1.5.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit.v3" Version="1.0.1" />
Expand All @@ -34,6 +35,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\NATS.Client.JetStream\NATS.Client.JetStream.csproj" />
<ProjectReference Include="..\..\src\NATS.Client.OpenTelemetry\NATS.Client.OpenTelemetry.csproj" />
<ProjectReference Include="..\..\src\NATS.Client.Serializers.Json\NATS.Client.Serializers.Json.csproj" />
<ProjectReference Include="..\NATS.Client.TestUtilities\NATS.Client.TestUtilities.csproj" />
<ProjectReference Include="..\..\src\NATS.Client.Core\NATS.Client.Core.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using NATS.Client.OpenTelemetry;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

namespace NATS.Client.Core.Tests;

public class NatsInstrumentationExtensionsTest
{
[Fact]
public void AddNatsClientInstrumentation_builds_tracer_provider()
{
using var provider = Sdk.CreateTracerProviderBuilder()
.AddNatsClientInstrumentation()
.Build();

provider.Should().NotBeNull();
}

[Fact]
public void AddNatsClientInstrumentation_builds_meter_provider()
{
using var provider = Sdk.CreateMeterProviderBuilder()
.AddNatsClientInstrumentation()
.Build();

provider.Should().NotBeNull();
}

[Fact]
public void SourceName_is_NATS_Net()
{
NatsTelemetry.SourceName.Should().Be("NATS.Net");
}
}
Loading