1 year ago
#322079
user1424876
How do I link an azure service bus publisher opentelemetry span to subscriber trace in another process using .net core c#
I have 2 processes running
webapi : publishes message to azure service bus queue
console app: subscribes to message from queue
I am using open telemetry collector with yaeger, .net core 6, c#
I am able to see traces from the .net core webapi that publishes to the queue in yaeger
I am also able to see traces from the .net core console app subscriber in yaeger.
However, I do not see the spans from the subscriber in the webapi trace. Not sure what I am missing. Trying to link the subscriber trace/span to the parent webapi trace so that I can see a complete trace.
I followed these links https://devblogs.microsoft.com/azure-sdk/introducing-experimental-opentelemetry-support-in-the-azure-sdk-for-net/ and https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-end-to-end-tracing?tabs=net-standard-sdk-2
I do see the Diagnostic-Id come across on the subscriber, not able to link it to the trace coming from the publisher webapi
Any advice appreciated.
======== Otel Setup Code on subscriber console app =======
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"));
}```
.net
azure
service
bus
open-telemetry
0 Answers
Your Answer