R
R
Ruslan2022-02-03 10:41:52
.NET
Ruslan, 2022-02-03 10:41:52

Where to look for TraceId from OpenTelemetry in Jaeger and how to bind microservices?

Hello.
A few questions came up when using OpenTelemetry in .net core applications.
To get traces, I use Jaeger deployed in docker.
Based on the documentation:
https://opentelemetry.io/docs/instrumentation/net/...
https://docs.microsoft.com/en-us/dotnet/core/diagn...

Question one, how to see in Jaeger UI identifier of traces?
Here is an example of my code:

ActivitySource source = new ActivitySource(ServiceName);
Activity span = source.StartActivity(name);
string traceid = span.TraceId.ToHexString();


The trace gets into Jaeger, the name matches, but there is no traceid anywhere in the interface, although if you type in the traceid in the search bar, then the trace is located. the question is, is it possible to find its traceid by opening a trace, or is it not provided for in Jaeger?

The second question is how to link traces from different applications together so that they are displayed together in Jaeger?
There is an article: https://habr.com/ru/company/jugru/blog/505890/
There is such a picture:
mm917vkllqx0yj77f6gxtmdmcou.png

Is it possible to make a trace consisting of spans from different applications be displayed in Jaeger UI or whatever we did, Jaeger will not display information like that?

I already have a method in one application that passes a traceid to another application, in which System.Diagnostics.Activity is created and passed to Jaeger and appears there.
There is also a second method in another application that receives the traceid from the first method, also creates an Activity, sends it to Jaeger and it appears there.

I tried to link the second Activity with the first one like this:

Activity span = source.CreateActivity(name);
span?.SetParentId(parentTraceId);
span?.Start();


As a result, the span.Parent.TraceId field contains the traceid from the first method, a new traceid is generated in the span.TraceId, and two unrelated traces are displayed in the Jaeger UI.

So, the burning question: Is it still possible to make it so that we see in Jaeger one Trace, which would consist of activities from different applications, or is this basically impossible in Jaeger?
Well, if it is possible, then how to establish this connection in the SystemDiagnostics classes?

Thank you for your attention.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan, 2022-02-03
@Razbezhkin

In general, not everything is as simple as it seemed.
I had to download the opentelemetry sources https://github.com/open-telemetry/opentelemetry-dotnet and see how the distribution of trace information in the AspNetCore toolkit works.
Saving, transferring and restoring TraceID (and additional information) is called Propogation.
There is a special class in the OpenTelemetry package:
OpenTelemetry.Context.Propagation.Propagators
Which returns these propagators with which to implement and retrieve context information (tracing information).
To embed trace data somewhere, you can use something like this:

//достаем текстового распространителя
var textMapPropagator = Propagators.DefaultTextMapPropagator;
//формируем контекст распространения
var propContext = new OpenTelemetry.Context.Propagation.PropagationContext(Activity.Current.Context, Baggage.Current);
//этот метод вызывает для каждого необходимого значения их контекста распространения ваш же метод, который сохраняет пару ключ-значения для последующего восстановления
textMapPropagator.Inject(propContext,context, (ctx, name, val) =>
{
    ctx.Headers.Set(name,val);
});

On the other side, you need to restore the trace context. the code is something like this:
//извлекаем из среды распространяемую трейс-информацию
        var propagator = Propagators.DefaultTextMapPropagator;
        PropagationContext propagationContext = propagator.Extract(default, context,
            (ctx, name) => { return new string[] { ctx.Headers.Get<string>(name) }; });

        if (propagationContext.ActivityContext.IsValid())
        {
            //формируем новый спан.
            newOne = new Activity("Consumer");
            newOne.SetParentId(propagationContext.ActivityContext.TraceId,
                propagationContext.ActivityContext.SpanId,
                propagationContext.ActivityContext.TraceFlags);
            newOne.TraceStateString = propagationContext.ActivityContext.TraceState;
            newOne.Start();
            newOne.IsAllDataRequested = false;

            Baggage.Current = propagationContext.Baggage;
        }

as a result, the tracing context is restored and gets to Jaeger in a bound form.
The only problem is that this particular Span is not saved (although all subsequent ones in the chain are) and Warning appears like: "invalid parent span IDs=b3f757a84de75eda; skipping clock skew adjustment"
I don't know what to do with it yet. something is all missing

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question