Golang -在OpenTelemetry中设置自定义TraceID和SpanID

icnyk63a  于 2023-09-28  发布在  Go
关注(0)|答案(1)|浏览(395)

我有2个异步(非HTTP)作业在不同的进程中运行。我想使用我自己生成的TraceID和SpanID,用于使用GO的OpenTelemetry库的所有跟踪和跨度。(我使用Signoz进行可视化,但这不重要)。
1.)如何为根范围设置TraceID和SpanID 2.)如何设置SpanID并将其指向在(1)中创建的具有TraceId的自定义根范围
下面是(2),但我看到了缺失的痕迹。`

b3 := []byte("9cec1f9bb461a96e023efebc879da11c")
copy(ptid[:], b3)

b4 := []byte("3fc56ecf2453bda8")
var parentSpanCtx = trace.NewSpanContext(trace.SpanContextConfig{
    TraceID:    ptid,
    SpanID:     trace.SpanID(b4),
    TraceFlags: trace.FlagsSampled,
    Remote:     true,
})`
    var parentCtx = trace.ContextWithSpanContext(context.Background(), parentsc)
ctx, span := tracer.Start(parentCtx, stage.Name)

我试着用正确的Parent TraceID和SpanId为childSpans设置SpanContext,但它不起作用。它显示为缺少根的跨距。

bq3bfh9z

bq3bfh9z1#

这里有一个例子,同时回答了(1)和(2)。

package main

import (
    "context"
    "fmt"
    "log"
    "time"

    "go.opentelemetry.io/otel"
    "go.opentelemetry.io/otel/attribute"
    "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
    "go.opentelemetry.io/otel/sdk/resource"
    sdktrace "go.opentelemetry.io/otel/sdk/trace"
    "go.opentelemetry.io/otel/trace"
)

func main() {
    // Create a trace provider with a custom exporter (e.g., stdout exporter for testing)
    exporter, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
    if err != nil {
        log.Fatalf("Error creating exporter: %v", err)
    }
    resources, err := resource.New(
        context.Background(),
        resource.WithAttributes(
            attribute.String("service.name", "serviceName"),
            attribute.String("library.language", "go"),
        ),
    )
    tp := sdktrace.NewTracerProvider(
        sdktrace.WithBatcher(exporter),
        sdktrace.WithResource(resources),
    )
    otel.SetTracerProvider(tp)

    // Manually create a custom TraceID and SpanID for the root span
    traceID := trace.TraceID([16]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10})
    spanID := trace.SpanID([8]byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77})

    ctx := trace.ContextWithRemoteSpanContext(
        context.Background(),
        trace.NewSpanContext(trace.SpanContextConfig{
            TraceID:    traceID,
            SpanID:     spanID,
            TraceFlags: trace.FlagsSampled,
        }),
    )

    // Create and start the root span with the custom TraceID and SpanID
    tracer := otel.GetTracerProvider().Tracer("my-tracer")
    _, rootSpan := tracer.Start(ctx, "root-span")
    defer rootSpan.End()

    // Your code here
    fmt.Println("Hello from root span!")
    createChildSpan(ctx)
    time.Sleep(10*time.Second)
}

// In another part of your code or a different process, you can create child spans
// referencing the custom root span created in step 1.

func createChildSpan(ctx context.Context) {
    tracer := otel.GetTracerProvider().Tracer("my-tracer")
    _, childSpan := tracer.Start(ctx, "child-span")
    defer childSpan.End()

    // Your code here
    fmt.Println("Hello from child span!")
}

相关问题