-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracing_test.go
56 lines (47 loc) · 1.13 KB
/
tracing_test.go
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
53
54
55
56
package trace_ctx_test
import (
"context"
"fmt"
"log/slog"
"os"
"testing"
"time"
"github.com/rzaripov1990/trace_ctx"
)
func TestTracing(t *testing.T) {
log := slog.New(
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: false,
Level: slog.LevelDebug,
}),
)
trace := trace_ctx.NewTrace()
ctx := trace_ctx.WithTrace(context.Background(), trace)
span1 := trace.StartSpan("operation-1", nil)
time.Sleep(100 * time.Millisecond)
span1.End()
span1.AttrAdd(slog.Bool("slow", true))
log.LogAttrs(ctx, slog.LevelDebug, "operation-1", span1.GetAttrs()...)
span2 := trace.StartSpan("operation-2", span1)
time.Sleep(200 * time.Millisecond)
span2.End()
log.LogAttrs(ctx, slog.LevelDebug, "operation-2", span2.GetAttrs()...)
var (
err error
count int
)
log.LogAttrs(
ctx,
slog.LevelDebug,
"operation-3",
trace.WithSpan("operation-3", span2,
func() {
if count == 0 && err == nil {
count = 5
}
time.Sleep(300 * time.Millisecond)
},
).AttrAdd(slog.Int("count", count)).GetAttrs()...)
fmt.Println("Spans logged to standard output")
//fmt.Printf("%#v", trace_ctx.GetTrace(ctx))
}