Home > Backend Development > Golang > How can I create an OpenTelemetry Span from a string TraceID in Go?

How can I create an OpenTelemetry Span from a string TraceID in Go?

Mary-Kate Olsen
Release: 2024-10-27 20:51:30
Original
845 people have browsed it

How can I create an OpenTelemetry Span from a string TraceID in Go?

Creating an OpenTelemetry Span from a String TraceID

To create a new span on the subscriber side using the traceID, one cannot directly create a span using the received traceID string. Instead, you must construct a [trace.SpanContext](https://pkg.go.dev/go.opentelemetry.io/otel/trace#SpanContext) using the provided traceID.

Construct the SpanContext

To generate the SpanContext, it is recommended to wrap the code in a separate function, such as this:

<code class="go">func constructNewSpanContext(request NewRequest) (spanContext trace.SpanContext, err error) {
    // Assuming the trace and span IDs are provided as strings in request struct.

    var traceID trace.TraceID
    traceID, err = trace.TraceIDFromHex(request.TraceID)
    if err != nil {
        fmt.Println("error:", err)
        return
    }

    var spanID trace.SpanID
    spanID, err = trace.SpanIDFromHex(request.SpanID)
    if err != nil {
        fmt.Println("error:", err)
        return
    }

    spanContextConfig := trace.SpanContextConfig{
        TraceID: traceID,
        SpanID:  spanID,
        // Other TraceFlag bits if desired
    }

    spanContext = trace.NewSpanContext(spanContextConfig)
    return spanContext, nil
}</code>
Copy after login

Enrich a Context with the SpanContext

Once the SpanContext is created, enrich a new context with it:

<code class="go">spanContext, err := constructNewSpanContext(request)
if err != nil {
    fmt.Println("ERROR:", err)
}

fmt.Println("IS VALID?", spanContext.IsValid()) // Check if the `spanContext` is valid

requestContext := context.Background()
requestContext = trace.ContextWithSpanContext(requestContext, spanContext)

// Start a new span within the enriched context
var requestInLoopSpan trace.Span
childContext, requestInLoopSpan := otel.Tracer("inboundmessage").Start(requestContext, "requestInLoopSpan")

requestInLoopSpan.AddEvent("processing....") // Should now work</code>
Copy after login

By performing these steps, you can successfully create a new Span on the subscriber side using the traceID received from the request headers.

The above is the detailed content of How can I create an OpenTelemetry Span from a string TraceID in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template