-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental OpenTelemetry Core (#437)
* Experimental OpenTelemetry Core * Moved OTEL tests * format * Span naming * Fixed test
- Loading branch information
Showing
20 changed files
with
790 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Diagnostics; | ||
using NATS.Client.Core; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace Example.OpenTelemetry; | ||
|
||
public static class ClientApp | ||
{ | ||
public static async Task Run() | ||
{ | ||
var serviceName = "ClientApp"; | ||
var serviceVersion = "1.0.0"; | ||
|
||
using var tracerProvider = Sdk.CreateTracerProviderBuilder() | ||
.AddOtlpExporter() | ||
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName: serviceName, serviceVersion: serviceVersion)) | ||
.AddSource("NATS.Net") | ||
.AddSource("MyClientSource") | ||
.Build(); | ||
|
||
ActivitySource activitySource = new("MyClientSource"); | ||
|
||
Console.WriteLine("Client App is starting..."); | ||
|
||
await using var nats = new NatsConnection(); | ||
|
||
using (var activity = activitySource.StartActivity("SayHi")) | ||
{ | ||
await nats.PublishAsync("greet.presence.client.app", "ClientApp is here!"); | ||
|
||
var response = await nats.RequestAsync<string, string>("greet.hi", "Hi, telemetry!"); | ||
Console.WriteLine($"Response: {response}"); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
sandbox/Example.OpenTelemetry/Example.OpenTelemetry.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="OpenTelemetry" Version="1.7.0" /> | ||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\NATS.Client.Core\NATS.Client.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/**************************************************************************** | ||
OpenTelemetry Example | ||
(1) Run Jaeger locally and then run the client and server apps. | ||
https://www.jaegertracing.io/download/ | ||
https://medium.com/jaegertracing/introducing-native-support-for-opentelemetry-in-jaeger-eb661be8183c | ||
```powershell | ||
> $env:COLLECTOR_OTLP_ENABLED=true | ||
> jaeger-all-in-one.exe | ||
``` | ||
or | ||
```bash | ||
$ COLLECTOR_OTLP_ENABLED=true jaeger-all-in-one | ||
``` | ||
(2) Jaeger UI default URL http://localhost:16686/search | ||
(3) In different terminals run: | ||
``` | ||
nats-server | ||
``` | ||
``` | ||
dotnet run -- service | ||
``` | ||
``` | ||
dotnet run -- client | ||
``` | ||
****************************************************************************/ | ||
|
||
using Example.OpenTelemetry; | ||
|
||
if (args.Length > 0 && args[0] == "client") | ||
{ | ||
await ClientApp.Run(); | ||
} | ||
else if (args.Length > 0 && args[0] == "service") | ||
{ | ||
await ServiceApp.Run(); | ||
} | ||
else | ||
{ | ||
Console.Error.WriteLine("Usage: dotnet run -- <client|service>"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Diagnostics; | ||
using NATS.Client.Core; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace Example.OpenTelemetry; | ||
|
||
public static class ServiceApp | ||
{ | ||
public static async Task Run() | ||
{ | ||
var serviceName = "ServiceApp"; | ||
var serviceVersion = "1.0.0"; | ||
|
||
using var tracerProvider = Sdk.CreateTracerProviderBuilder() | ||
.AddOtlpExporter() | ||
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName: serviceName, serviceVersion: serviceVersion)) | ||
.AddSource("NATS.Net") | ||
.AddSource("MyServiceSource") | ||
.Build(); | ||
|
||
ActivitySource activitySource = new("MyServiceSource"); | ||
|
||
Console.WriteLine("Service App is starting..."); | ||
|
||
await using var nats = new NatsConnection(); | ||
|
||
await foreach (var msg in nats.SubscribeAsync<string>("greet.>")) | ||
{ | ||
using var activity = msg.StartActivity("Greetings"); | ||
|
||
if (msg.Subject.StartsWith("greet.presence")) | ||
{ | ||
Console.WriteLine($"{msg.Data} is here!"); | ||
|
||
activity?.AddEvent(new ActivityEvent("Presence", tags: new() | ||
{ | ||
["subject"] = msg.Subject, | ||
["data"] = msg.Data, | ||
})); | ||
|
||
continue; | ||
} | ||
|
||
await msg.ReplyAsync($"Hi there! {msg.Data}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Threading.Channels; | ||
|
||
namespace NATS.Client.Core.Internal; | ||
|
||
internal sealed class ActivityEndingMsgReader<T> : ChannelReader<NatsMsg<T>> | ||
{ | ||
private readonly ChannelReader<NatsMsg<T>> _inner; | ||
|
||
public ActivityEndingMsgReader(ChannelReader<NatsMsg<T>> inner) => _inner = inner; | ||
|
||
public override bool CanCount => _inner.CanCount; | ||
|
||
public override bool CanPeek => _inner.CanPeek; | ||
|
||
public override int Count => _inner.Count; | ||
|
||
public override Task Completion => _inner.Completion; | ||
|
||
/// <inheritdoc/> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public override bool TryRead(out NatsMsg<T> item) | ||
{ | ||
if (!_inner.TryRead(out item)) | ||
return false; | ||
|
||
item.Headers?.Activity?.Dispose(); | ||
|
||
return true; | ||
} | ||
|
||
/// <inheritdoc/> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public override ValueTask<bool> WaitToReadAsync(CancellationToken cancellationToken = default) => _inner.WaitToReadAsync(cancellationToken); | ||
|
||
public override ValueTask<NatsMsg<T>> ReadAsync(CancellationToken cancellationToken = default) => _inner.ReadAsync(cancellationToken); | ||
|
||
public override bool TryPeek(out NatsMsg<T> item) => _inner.TryPeek(out item); | ||
|
||
public override IAsyncEnumerable<NatsMsg<T>> ReadAllAsync(CancellationToken cancellationToken = default) => _inner.ReadAllAsync(cancellationToken); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.