forked from Equanox/petasos-rewriter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.go
180 lines (165 loc) · 5.12 KB
/
setup.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"fmt"
"github.com/benchkram/errz"
"github.com/getsentry/sentry-go"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/stdout"
"go.opentelemetry.io/otel/exporters/trace/jaeger"
"go.opentelemetry.io/otel/exporters/trace/zipkin"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/semconv"
"go.opentelemetry.io/otel/trace"
"gopkg.in/natefinch/lumberjack.v2"
"io"
"net/http"
"os"
"path"
"strings"
"time"
)
func logging(v *viper.Viper) {
switch v.GetString("level") {
case zerolog.DebugLevel.String():
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case zerolog.InfoLevel.String():
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case zerolog.ErrorLevel.String():
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
default:
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
logType := v.GetString("type")
json := v.GetBool("json")
if logType == "file" {
log.Logger = log.Output(fileAppender(v)).With().Caller().Logger()
} else if !json {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger()
}
}
// printConfig read from cli to stdout
func printConfig() {
fmt.Printf("Config:\n")
for _, s := range viper.AllKeys() {
fmt.Printf(" %s : %s\n", s, viper.Get(s))
}
fmt.Printf("\n\n")
}
func fileAppender(v *viper.Viper) io.Writer {
dir := v.GetString("dir")
if err := os.MkdirAll(dir, 0744); err != nil {
log.Error().Err(err).Str("path", dir).Msg("can't create log directory")
return nil
}
return &lumberjack.Logger{
Filename: path.Join(dir, v.GetString("fileName")),
MaxSize: v.GetInt("maxSize"),
MaxAge: v.GetInt("maxAge"),
MaxBackups: v.GetInt("maxBackups"),
}
}
func ConfigureSentry(v *viper.Viper) {
sentryDsn := v.GetString("dsn")
if sentryDsn == "NA" {
return
}
err := sentry.Init(sentry.ClientOptions{
Dsn: sentryDsn,
Environment: v.GetString("environment"),
Debug: v.GetBool("debug"),
AttachStacktrace: true,
})
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetTags(map[string]string{"app_name": applicationName})
scope.SetLevel(sentry.LevelError)
})
if err != nil {
errz.Fatal(err, "Could not configure sentry, shutting down")
}
sentryEnabled = true
defer sentry.Flush(2 * time.Second)
}
func ConfigureViper(applicationName string) error {
viper.AddConfigPath(fmt.Sprintf("/etc/%s", applicationName))
viper.AddConfigPath(fmt.Sprintf("$HOME/.%s", applicationName))
viper.AddConfigPath(".")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix(applicationName)
viper.AutomaticEnv()
viper.SetConfigName(applicationName)
err := viper.ReadInConfig()
return err
}
// ConfigureTracerProvider creates the TracerProvider based on the configuration
// provided. It has built-in support for jaeger, zipkin, stdout and noop providers.
// A different provider can be used if a constructor for it is provided in the
// config.
// If a provider name is not provided, a stdout tracerProvider will be returned.
func configureTracerProvider(v *viper.Viper, applicationName string) (trace.TracerProvider, error) {
var traceProviderName = v.GetString(traceProviderType)
switch traceProviderName {
case zipkinName:
traceProvider, err := zipkin.NewExportPipeline(v.GetString(traceProviderEndpoint),
zipkin.WithSDKOptions(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(
resource.NewWithAttributes(semconv.ServiceNameKey.String(applicationName)),
),
),
)
return traceProvider, err
case jaegarName:
traceProvider, _, err := jaeger.NewExportPipeline(
jaeger.WithCollectorEndpoint(v.GetString(traceProviderEndpoint)),
jaeger.WithSDKOptions(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(
resource.NewWithAttributes(
semconv.ServiceNameKey.String(applicationName),
attribute.String("exporter", traceProviderName),
),
),
),
)
if err != nil {
return nil, err
}
return traceProvider, nil
case noopName:
return trace.NewNoopTracerProvider(), nil
default:
var skipTraceExport = v.GetBool(traceProviderSkipTraceExport)
var option stdout.Option
if skipTraceExport {
option = stdout.WithoutTraceExport()
} else {
option = stdout.WithPrettyPrint()
}
otExporter, err := stdout.NewExporter(option)
if err != nil {
return nil, err
}
traceProvider := sdktrace.NewTracerProvider(sdktrace.WithSyncer(otExporter))
return traceProvider, nil
}
}
func configureClient(propagators propagation.TextMapPropagator, provider trace.TracerProvider) *http.Client {
var transport http.RoundTripper = &http.Transport{}
transport = otelhttp.NewTransport(transport,
otelhttp.WithPropagators(propagators),
otelhttp.WithTracerProvider(provider),
)
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Transport: transport,
}
return client
}