|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using OpenTelemetry; |
| 6 | +using OpenTelemetry.Resources; |
| 7 | +using OpenTelemetry.Trace; |
| 8 | +using Proto; |
| 9 | +using Proto.OpenTelemetry; |
| 10 | +using Proto.Remote; |
| 11 | +using Proto.Remote.GrpcNet; |
| 12 | + |
| 13 | +var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); |
| 14 | +Log.SetLoggerFactory(loggerFactory); |
| 15 | + |
| 16 | +using var tracerProvider = Sdk.CreateTracerProviderBuilder() |
| 17 | + .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("OpenTelemetryTracingSample")) |
| 18 | + .AddProtoActorInstrumentation() |
| 19 | + .AddJaegerExporter() |
| 20 | + .Build(); |
| 21 | + |
| 22 | +var system1 = new ActorSystem().WithRemote(GrpcNetRemoteConfig.BindToLocalhost(12000)); |
| 23 | +var system2 = new ActorSystem().WithRemote(GrpcNetRemoteConfig.BindToLocalhost(12001)); |
| 24 | + |
| 25 | +await system1.Remote().StartAsync(); |
| 26 | +await system2.Remote().StartAsync(); |
| 27 | + |
| 28 | +var remoteProps = Props.FromFunc(ctx => |
| 29 | +{ |
| 30 | + if (ctx.Message is string msg) |
| 31 | + { |
| 32 | + Console.WriteLine($"[remote] received '{msg}' traceId={Activity.Current?.TraceId}"); |
| 33 | + ctx.Respond("pong"); |
| 34 | + } |
| 35 | + return Task.CompletedTask; |
| 36 | +}).WithTracing(); |
| 37 | + |
| 38 | +system2.Root.SpawnNamed(remoteProps, "remote"); |
| 39 | + |
| 40 | +var localProps = Props.FromFunc(async ctx => |
| 41 | +{ |
| 42 | + if (ctx.Message is string msg) |
| 43 | + { |
| 44 | + Console.WriteLine($"[local] received '{msg}' traceId={Activity.Current?.TraceId}"); |
| 45 | + var remotePid = PID.FromAddress("127.0.0.1:12001", "remote"); |
| 46 | + var res = await ctx.RequestAsync<string>(remotePid, "ping"); |
| 47 | + Console.WriteLine($"[local] got reply '{res}' traceId={Activity.Current?.TraceId}"); |
| 48 | + } |
| 49 | +}).WithTracing(); |
| 50 | + |
| 51 | +var localPid = system1.Root.Spawn(localProps); |
| 52 | + |
| 53 | +var tracedRoot = system1.Root.WithTracing(); |
| 54 | + |
| 55 | +using var rootActivity = new ActivitySource(Proto.OpenTelemetry.ProtoTags.ActivitySourceName).StartActivity("root"); |
| 56 | +tracedRoot.Send(localPid, "start"); |
| 57 | + |
| 58 | +Console.WriteLine("Press enter to exit"); |
| 59 | +Console.ReadLine(); |
0 commit comments