import ( "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" ) func useOtel() (ctx context.Context) { // 堆代码 duidaima.com // 创建一个 Tracer 对象,用于记录此次请求的 Span // 方式1: // tp := otel.GetTracerProvider() // tracer := tp.Tracer("my-service") // 方式2: tracer := otel.Tracer("my-service") // 创建一个 SpanContext,用于跟踪请求的父 Span ctx = context.Background() spanName := "useOtel" // 创建一个新的 Span,用于记录此次请求 curCtx, span := tracer.Start( ctx, spanName, trace.WithSpanKind(trace.SpanKindInternal), trace.WithAttributes( attribute.String("http.method", "GET"), attribute.String("http.host", "www.test.com"), ), ) defer span.End() // do something callOtherFunc(curCtx) return } func callOtherFunc(parentCtr context.Context) { tracer := otel.Tracer("my-service") spanName := "callOtherFunc" _, span := tracer.Start( parentCtr, spanName, ) defer span.End() // do something }