-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (38 loc) · 931 Bytes
/
main.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
package main
import (
"context"
"errors"
"sync"
"go.opentelemetry.io/otel"
"go.uber.org/zap"
"github.com/uptrace/opentelemetry-go-extra/otelplay"
"github.com/uptrace/opentelemetry-go-extra/otelzap"
)
func main() {
ctx := context.Background()
shutdown := otelplay.ConfigureOpentelemetry(ctx)
defer shutdown()
tracer := otel.Tracer("app_or_package_name")
ctx, span := tracer.Start(ctx, "root")
defer span.End()
// Use Ctx to propagate the active span.
Logger(ctx).Error("hello from zap",
zap.Error(errors.New("hello world")),
zap.String("foo", "bar"))
otelplay.PrintTraceID(ctx)
}
var (
once sync.Once
logger *otelzap.Logger
)
// Logger ensures that the caller does not forget to pass the context.
func Logger(ctx context.Context) otelzap.LoggerWithCtx {
once.Do(func() {
l, err := zap.NewDevelopment()
if err != nil {
panic(err)
}
logger = otelzap.New(l)
})
return logger.Ctx(ctx)
}