-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.go
132 lines (121 loc) · 3.8 KB
/
init.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//go:build go1.8
// +build go1.8
package main
import (
"config-service/db"
"config-service/db/mongo"
"config-service/utils"
"context"
"log"
"os"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
jaegerexporter "go.opentelemetry.io/otel/exporters/jaeger"
stdout "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var zapLogger *zap.Logger
var zapInfoLevelLogger *zap.Logger
func initialize() (shutdown func()) {
conf := utils.GetConfig()
//init logger
initLogger(conf.LoggerConfig)
//init tracer
tracer := initTracer(conf.Telemetry)
//connect db
mongo.MustConnect(conf.Mongo)
//init db library
db.Init()
//shutdown function
shutdown = func() {
mongo.Disconnect()
if err := tracer.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
zapLogger.Sync()
}
return shutdown
}
func initLogger(config utils.LoggerConfig) {
var err error
lvl := zap.NewAtomicLevel()
if config.Level == "" {
config.Level = "warn"
}
if err := lvl.UnmarshalText([]byte(config.Level)); err != nil {
panic(err)
}
// empty string means the output is stdout
zapConf := newZapConf(lvl, config)
zapLogger, err = zapConf.Build()
if err != nil {
panic(err)
}
zap.ReplaceGlobals(zapLogger)
zap.RedirectStdLog(zapLogger)
if lvl.Level() == zapcore.InfoLevel {
zapInfoLevelLogger = zapLogger
} else {
zapInfoLevelLogger, err = newZapConf(zap.NewAtomicLevelAt(zapcore.InfoLevel), config).Build()
if err != nil {
panic(err)
}
}
}
func newZapConf(lvl zap.AtomicLevel, config utils.LoggerConfig) zap.Config {
ec := zap.NewProductionEncoderConfig()
ec.EncodeTime = zapcore.RFC3339NanoTimeEncoder
zapConf := zap.Config{DisableCaller: true, DisableStacktrace: true, Level: lvl,
Encoding: "json", EncoderConfig: ec,
OutputPaths: []string{"stdout"}, ErrorOutputPaths: []string{"stderr"}}
if config.LogFileName != "" {
zapConf.OutputPaths = []string{config.LogFileName}
zapConf.ErrorOutputPaths = append(zapConf.ErrorOutputPaths, config.LogFileName)
}
return zapConf
}
// initTracer used to initialize tracer
func initTracer(config utils.TelemetryConfig) *sdktrace.TracerProvider {
serviceName := "config-service"
hostName, _ := os.Hostname()
var err error
var exporter sdktrace.SpanExporter
if config.JaegerAgentHost != "" && config.JaegerAgentPort != "" {
if config.JaegerAgentHost == "stdout" {
exporter, err = stdout.New(stdout.WithPrettyPrint())
if err != nil {
log.Fatal(err)
}
} else {
host := jaegerexporter.WithAgentHost(config.JaegerAgentHost)
port := jaegerexporter.WithAgentPort(config.JaegerAgentPort)
// jaegerexporter.With
exporter, err = jaegerexporter.New(jaegerexporter.WithAgentEndpoint(host, port))
if err != nil {
zapLogger.Fatal("failed to initialize jaeger exporter", zap.Error(err))
}
}
}
var tp *sdktrace.TracerProvider
// For the demonstration, use sdktrace.AlwaysSample sampler to sample all traces.
// In a production application, use sdktrace.ProbabilitySampler with a desired probability.
opts := []sdktrace.TracerProviderOption{
sdktrace.WithSampler(sdktrace.TraceIDRatioBased(1.0)),
sdktrace.WithResource(resource.NewWithAttributes(semconv.SchemaURL,
semconv.ServiceNameKey.String(serviceName),
attribute.String("pod", hostName),
semconv.K8SPodNameKey.String(hostName))),
}
if exporter != nil {
opts = append(opts, sdktrace.WithBatcher(exporter))
}
tp = sdktrace.NewTracerProvider(opts...)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return tp
}