-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
108 lines (86 loc) · 2.55 KB
/
example_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package tracer_test
import (
"context"
stdlog "log"
"sync"
"github.com/fredbi/go-trace/log"
"github.com/fredbi/go-trace/otel/tracer"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
)
var (
_ tracer.Traceable = Runtime{}
onceInitTracer sync.Once
tp *sdktrace.TracerProvider
)
// Runtime is Traceable
type Runtime struct {
tracer trace.Tracer
logger log.Factory
}
func (r Runtime) Logger() log.Factory {
return r.logger
}
func (r Runtime) Tracer() trace.Tracer {
return r.tracer
}
func newExampleTracer() (trace.Tracer, func(context.Context) error) {
onceInitTracer.Do(func() {
exp, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
if err != nil {
stdlog.Fatalf("failed to initialize stdouttrace exporter: %v", err)
return
}
bsp := sdktrace.NewBatchSpanProcessor(exp)
tp = sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSpanProcessor(bsp),
)
otel.SetTracerProvider(tp)
})
return otel.Tracer("example_tracer"), tp.ForceFlush
}
func executionContext() (context.Context, Runtime, func(context.Context) error) {
lg := zap.NewExample()
tr, flusher := newExampleTracer()
rt := Runtime{
logger: log.NewFactory(lg, log.WithOTEL(true)),
tracer: tr,
}
ctx := context.Background()
return ctx, rt, flusher
}
func ExampleStartSpan() {
ctx, rt, flusher := executionContext()
// Instantiate a span and its associated span logger.
//
// This span is automatically signed with the current function, annotated with the source file and line
spanCtx, span, logger := tracer.StartSpan(ctx, rt, zap.String("field", "fred"))
defer func() {
span.End()
_ = flusher(spanCtx) // flush the spans, so we get them in the output of this example
}()
logger.Info("test")
// The trace span goes to stderr
// output:
// {"level":"info","msg":"test","function":"tracer_test.ExampleStartSpan","field":"fred"}
}
func ExampleStartNamedSpan() {
ctx, rt, flusher := executionContext()
// StartNamedSpan should be used in anonymous functions like so.
handleFunc := func() {
spanCtx, span, logger := tracer.StartNamedSpan(ctx, rt, "signature", zap.String("field", "fred"))
defer func() {
span.End()
_ = flusher(spanCtx) // flush the spans, so we get them in the output of this example
}()
logger.Info("test")
}
handleFunc()
// The trace span goes to stderr
// output:
// {"level":"info","msg":"test","function":"signature","field":"fred"}
}