-
Notifications
You must be signed in to change notification settings - Fork 262
/
dependency.go
303 lines (272 loc) · 8.78 KB
/
dependency.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Copyright 2020 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dependency
import (
"context"
"errors"
"fmt"
"net"
"os"
"os/signal"
"path/filepath"
"reflect"
"strings"
"syscall"
"time"
"github.com/go-echarts/statsview"
"github.com/go-echarts/statsview/viewer"
"github.com/mitchellh/mapstructure"
"github.com/phayes/freeport"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/jaeger"
"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.7.0"
"gopkg.in/yaml.v3"
"d7y.io/dragonfly/v2/client/config"
"d7y.io/dragonfly/v2/client/util"
"d7y.io/dragonfly/v2/cmd/dependency/base"
logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/pkg/dfnet"
"d7y.io/dragonfly/v2/pkg/dfpath"
"d7y.io/dragonfly/v2/pkg/net/fqdn"
"d7y.io/dragonfly/v2/pkg/net/ip"
"d7y.io/dragonfly/v2/pkg/types"
"d7y.io/dragonfly/v2/pkg/unit"
"d7y.io/dragonfly/v2/version"
)
// InitCommandAndConfig initializes flags binding and common sub cmds.
// config is a pointer to configuration struct.
func InitCommandAndConfig(cmd *cobra.Command, useConfigFile bool, config any) {
rootName := cmd.Root().Name()
cobra.OnInitialize(func() {
initConfig(useConfigFile, rootName, config)
})
if !cmd.HasParent() {
// Add common flags
flags := cmd.PersistentFlags()
flags.Bool("console", false, "whether logger output records to the stdout")
flags.Bool("verbose", false, "whether logger use debug level")
flags.Int("pprof-port", -1, "listen port for pprof, 0 represents random port")
flags.String("jaeger", "", "jaeger endpoint url, like: http://localhost:14250/api/traces")
flags.String("service-name", fmt.Sprintf("%s-%s", "dragonfly", cmd.Name()), "name of the service for tracer")
flags.String("config", "", fmt.Sprintf("the path of configuration file with yaml extension name, default is %s, it can also be set by env var: %s", filepath.Join(dfpath.DefaultConfigDir, rootName+".yaml"), strings.ToUpper(rootName+"_config")))
// Bind common flags
if err := viper.BindPFlags(flags); err != nil {
panic(fmt.Errorf("bind common flags to viper: %w", err))
}
// Config for binding env
viper.SetEnvPrefix(rootName)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
_ = viper.BindEnv("config")
// Add common cmds only on root cmd
cmd.AddCommand(VersionCmd)
cmd.AddCommand(newDocCommand(cmd.Name()))
cmd.AddCommand(PluginCmd)
}
}
// InitMonitor initialize monitor and return final handler
func InitMonitor(pprofPort int, otelOption base.TelemetryOption) func() {
var fc = make(chan func(), 5)
if pprofPort >= 0 {
// Enable go pprof and statsview
go func() {
if pprofPort == 0 {
pprofPort, _ = freeport.GetFreePort()
}
debugAddr := fmt.Sprintf(":%d", pprofPort)
viewer.SetConfiguration(viewer.WithAddr(debugAddr))
logger.With("pprof", fmt.Sprintf("http://%s/debug/pprof", debugAddr),
"statsview", fmt.Sprintf("http://%s/debug/statsview", debugAddr)).
Infof("enable pprof at %s", debugAddr)
vm := statsview.New()
if err := vm.Start(); err != nil {
logger.Warnf("serve pprof error: %v", err)
}
fc <- func() { vm.Stop() }
}()
}
ff, err := initJaegerTracer(otelOption)
if err != nil {
logger.Warnf("init jaeger tracer error: %v", err)
return func() {}
}
fc <- ff
return func() {
logger.Infof("do %d monitor finalizer", len(fc))
for {
select {
case f := <-fc:
f()
default:
return
}
}
}
}
func SetupQuitSignalHandler(handler func()) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
go func() {
var done bool
for {
select {
case sig := <-signals:
logger.Warnf("receive signal: %v", sig)
if !done {
done = true
handler()
logger.Warnf("handle signal: %v finish", sig)
}
}
}
}()
}
// initConfig reads in config file and ENV variables if set.
func initConfig(useConfigFile bool, name string, config any) {
// Use config file and read once.
if useConfigFile {
cfgFile := viper.GetString("config")
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
viper.AddConfigPath(dfpath.DefaultConfigDir)
viper.SetConfigName(name)
viper.SetConfigType("yaml")
}
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
ignoreErr := false
if errors.As(err, &viper.ConfigFileNotFoundError{}) {
if cfgFile == "" {
ignoreErr = true
}
}
if !ignoreErr {
panic(fmt.Errorf("viper read config: %w", err))
}
}
}
if err := viper.Unmarshal(config, initDecoderConfig); err != nil {
panic(fmt.Errorf("unmarshal config to struct: %w", err))
}
}
func LoadConfig(config any) error {
if err := viper.ReadInConfig(); err != nil {
return err
}
return viper.Unmarshal(config, initDecoderConfig)
}
func WatchConfig(interval time.Duration, newConfig func() (cfg any), watcher func(cfg any)) {
var oldData string
file := viper.ConfigFileUsed()
data, err := os.ReadFile(file)
if err != nil {
logger.Errorf("read file %s error: %v", file, err)
}
oldData = string(data)
loop:
for {
select {
case <-time.After(interval):
// for k8s configmap case, the config file is symbol link
// reload file instead use fsnotify
data, err = os.ReadFile(file)
if err != nil {
logger.Errorf("read file %s error: %v", file, err)
continue loop
}
if oldData != string(data) {
cfg := newConfig()
err = LoadConfig(cfg)
if err != nil {
logger.Errorf("load config file %s error: %v", file, err)
continue loop
}
logger.Infof("config file %s changed", file)
watcher(cfg)
oldData = string(data)
}
}
}
}
func initDecoderConfig(dc *mapstructure.DecoderConfig) {
dc.DecodeHook = mapstructure.ComposeDecodeHookFunc(func(from, to reflect.Type, v any) (any, error) {
switch to {
case reflect.TypeOf(unit.B),
reflect.TypeOf(dfnet.NetAddr{}),
reflect.TypeOf(util.RateLimit{}),
reflect.TypeOf(util.Duration{}),
reflect.TypeOf(&config.ProxyOption{}),
reflect.TypeOf(config.TCPListenPortRange{}),
reflect.TypeOf(types.PEMContent("")),
reflect.TypeOf(config.URL{}),
reflect.TypeOf(net.IP{}),
reflect.TypeOf(config.CertPool{}),
reflect.TypeOf(config.Regexp{}):
b, _ := yaml.Marshal(v)
p := reflect.New(to)
if err := yaml.Unmarshal(b, p.Interface()); err != nil {
return nil, err
}
return p.Interface(), nil
default:
return v, nil
}
}, dc.DecodeHook)
}
// initTracer creates a new trace provider instance and registers it as global trace provider.
func initJaegerTracer(otelOption base.TelemetryOption) (func(), error) {
// currently, only support jaeger, otherwise just keeps the context of the caller but does not record spans.
if otelOption.Jaeger == "" {
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.NeverSample()),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return func() {}, nil
}
exp, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(otelOption.Jaeger)))
if err != nil {
return nil, err
}
tp := sdktrace.NewTracerProvider(
// Always be sure to batch in production.
sdktrace.WithBatcher(exp),
sdktrace.WithSampler(sdktrace.AlwaysSample()),
// Record information about this application in an Resource.
sdktrace.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(otelOption.ServiceName),
semconv.ServiceInstanceIDKey.String(fmt.Sprintf("%s|%s", fqdn.FQDNHostname, ip.IPv4.String())),
semconv.ServiceNamespaceKey.String("dragonfly"),
semconv.ServiceVersionKey.String(version.GitVersion))),
)
// Register our TracerProvider as the global so any imported
// instrumentation in the future will default to using it.
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return func() {
// Do not make the application hang when it is shutdown.
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
_ = tp.Shutdown(ctx)
}, nil
}