如何使用 .net core c# 将 azure 服务总线发布者 opentelemetry 范围链接到另一个进程中的订阅者跟踪

use*_*876 5 .net service azure bus open-telemetry

我有 2 个进程正在运行

webapi :将消息发布到azure服务总线队列

控制台应用程序:订阅队列中的消息

我正在使用带有 yaeger、.net core 6、c# 的开放式遥测收集器

我能够看到发布到 yaeger 队列的 .net core webapi 的痕迹

我还可以在 yaeger 中看到来自 .net core 控制台应用程序订阅者的痕迹。

但是,我在 webapi 跟踪中没有看到订阅者的跨度。不知道我错过了什么。尝试将订阅者跟踪/跨度链接到父 webapi 跟踪,以便我可以看到完整的跟踪。

我关注了这些链接 https://devblogs.microsoft.com/azure-sdk/introducing-experimental-opentelemetry-support-in-the-azure-sdk-for-net/https://learn.microsoft.com/en -us/azure/service-bus-messaging/service-bus-end-to-end-tracing?tabs=net-standard-sdk-2

我确实看到订阅者遇到了 Diagnostic-Id,无法将其链接到来自发布者 webapi 的跟踪

任何建议表示赞赏。

======== 用户控制台应用程序上的酒店设置代码 =======


        static async Task Main()
        {
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true);

            //--------------  Tracing --------------------
            var env = "Development";
            var applicationName = "ConsoleAppQueueProcessor";
            var assemblyVersion = Assembly
                .GetExecutingAssembly()
                .GetCustomAttribute<AssemblyFileVersionAttribute>()!
                .Version;

            meter = Measures.GetMeter(Measures.InstrumentationName);
            ConsoleAppQueueProcessorCounter = meter.CreateCounter<int>("ConsoleAppQueueProcessor");



            using var openTelemetry = Sdk.CreateTracerProviderBuilder()
                .SetResourceBuilder(
                    ResourceBuilder
                        .CreateDefault()
                        //.CreateEmpty()
                        .AddService(serviceName: applicationName, serviceVersion: applicationName)
                        .AddAttributes(
                            new KeyValuePair<string, object>[]
                            {
                                new("deployment.environment",env),
                                new("host.name", Environment.MachineName),
                                new("application.name", applicationName),
                                new("application.assemblyversion",assemblyVersion)
                            })
                        .AddEnvironmentVariableDetector() //Set using powershell: $env:OTEL_RESOURCE_ATTRIBUTES = 'key1=value1,key2=value2'
                )

                .SetSampler(new AlwaysOnSampler())
                .AddAspNetCoreInstrumentation(
                    options =>
                    {
                        //options.Enrich = Enrich;
                        options.RecordException = true;
                    })
                .AddHttpClientInstrumentation()
                    .AddOtlpExporter(options =>
                    {
                        options.Endpoint = new Uri("http://localhost:4317");
                        //options.ExportProcessorType = ExportProcessorType.Simple;
                    })
                    .AddConsoleExporter(options => options.Targets = ConsoleExporterOutputTargets.Console)
                    .AddInstrumentation<MyTraces>()
                    .AddSource(Measures.InstrumentationName)
                .AddSource("Azure.*")
                .Build();





            // The Service Bus client types are safe to cache and use as a singleton for the lifetime
            // of the application, which is best practice when messages are being published or read
            // regularly.
            //
            // Create the clients that we'll use for sending and processing messages.
            client = new ServiceBusClient(connectionString);
            //sender = client.CreateSender(queueName);
            await Receiver();
            Console.WriteLine("Press any key to end the application");
            Console.ReadKey();
        }```



============      Message Subscriber Handler Code ========

```        static async Task MessageHandler(ProcessMessageEventArgs args)
        {
            ServiceBusReceivedMessage message = args.Message;
            
            ConsoleAppQueueProcessorCounter.Add(1);
            using var activity = _myTraces.Source.StartActivity(); // Can add listeners to activity.Events()
            if (message.ApplicationProperties.TryGetValue("Diagnostic-Id", out var objectId) && objectId is string diagnosticId)
            {
                Console.WriteLine($"----- Diagnostic-Id = {diagnosticId}");
                
                activity.SetParentId(diagnosticId);

            }
            activity?.AddEvent(new ActivityEvent("MessageHandler started"));

            activity?.AddTag("ConsoleAppQueueProcessorCounter", "Foo()");

            string body = args.Message.Body.ToString();
            Console.WriteLine($"Received: {body}");

            // complete the message. message is deleted from the queue. 
            await args.CompleteMessageAsync(args.Message);
            activity?.AddEvent(new ActivityEvent("MessageHandler completed"));

        }```

Run Code Online (Sandbox Code Playgroud)