digitalalchemist
digitalalchemist

Reputation: 19

Opentelemetry: Open span in one process, and close it in another

I'm looking into using OpenTelementry golang library to record a function which is connected together by distributed messaging (NATS). I want to start a span, post a message to the second part of the function, and end the span when that completes.

I can only see examples of spans that close when a function ends.

It would be great if I could create a span and pass in an existing id to use, and get a recording span back, but it doesn't look like this is possible.

Any ideas?

Upvotes: 1

Views: 4937

Answers (1)

Dmitry Harnitski
Dmitry Harnitski

Reputation: 6008

Open span in one process, and close it in another

That is not a very good idea. With a single span, you can see total process execution time, but cannot see time and details of its parts.

It is more helpful if you can create a span for every important step. It could be a local function or function executed in an external process.

I found that nice slide in internet that demonstrates what we can achive.

https://speakerdeck.com/simonz130/distributed-tracing-and-monitoring-with-opentelemetry?slide=26

enter image description here

As @pati-ram-yadav mentioned in comment, you can create child spans using span := trace.SpanFromContext(ctx) inside application.

For external process it is a bit more complicated.

Parent function needs to share context (accountID and traceID) with child function through the wire.

After that, the child process injects these fields into its context and all spans created with that context are connected to the parent.

Every transport implements sending additional data (accountID and traceID) differently. With HTTP, for example, it is implemented as custom HTTP headers.

Many transports including HTTP are supported by opentelemetry out-of-the-box. If some transport is not supported, you have to find a community module or write your own code.

This is an example for HTTP. You need to Inject context into HTTP request in parent and Extract that context from the request in client. Client then adds TraceID and AccountID to context.Context and all spans created using that context are connected to parent span.

Upvotes: 1

Related Questions