-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracer_test.go
More file actions
52 lines (43 loc) · 1.15 KB
/
tracer_test.go
File metadata and controls
52 lines (43 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package dgotel
import (
"context"
"fmt"
"testing"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
)
func TestTracer(t *testing.T) {
ctx := context.Background()
shutdown := InitTracer(ctx, "test-service", NewHTTPExporter(ctx, "localhost:4318", ""))
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", "value"))
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()
}