- 添加 go.opentelemetry.io/otel 及相关子包 - 更新 golang.org/x/net、golang.org/x/sync 等依赖版本 - 移除部分旧版本的依赖包
53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package serialize
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/codes"
|
|
)
|
|
|
|
func TestOpenTelemetry(t *testing.T) {
|
|
shutdown := InitOpenTelemetry()
|
|
defer shutdown()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
ctx := context.Background()
|
|
parentMethod(ctx)
|
|
}
|
|
time.Sleep(10 * time.Second)
|
|
}
|
|
|
|
func parentMethod(ctx context.Context) {
|
|
tracer := otel.Tracer("otel-go-tracer")
|
|
ctx, span := tracer.Start(ctx, "parent span")
|
|
fmt.Println(span.SpanContext().TraceID()) // 打印 TraceId
|
|
span.SetAttributes(attribute.String("key", "哈哈哈"))
|
|
span.SetStatus(codes.Ok, "Success")
|
|
childMethod(ctx)
|
|
span.End()
|
|
}
|
|
|
|
func childMethod(ctx context.Context) {
|
|
tracer := otel.Tracer("otel-go-tracer")
|
|
ctx, span := tracer.Start(ctx, "child span")
|
|
span.SetStatus(codes.Ok, "Success")
|
|
grandChildMethod(ctx)
|
|
span.End()
|
|
}
|
|
|
|
func grandChildMethod(ctx context.Context) {
|
|
tracer := otel.Tracer("otel-go-tracer")
|
|
ctx, span := tracer.Start(ctx, "grandchild span")
|
|
span.SetStatus(codes.Error, "error")
|
|
|
|
// 业务代码...
|
|
|
|
span.End()
|
|
}
|