-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
123 lines (109 loc) · 3.61 KB
/
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
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
// Copyright (c) Thanos Contributors
// Licensed under the Apache License 2.0.
package main
import (
"context"
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/bwplotka/mdox/pkg/clilog"
extflag "github.com/efficientgo/tools/extkingpin"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/oklog/run"
"github.com/pkg/errors"
"github.com/thanos-community/thanos-sd-sidecar/pkg/discovery"
"github.com/thanos-community/thanos-sd-sidecar/pkg/extkingpin"
"github.com/thanos-community/thanos-sd-sidecar/pkg/version"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
logFormatLogfmt = "logfmt"
logFormatJson = "json"
logFormatCLILog = "clilog"
)
func setupLogger(logLevel, logFormat string) log.Logger {
var lvl level.Option
switch logLevel {
case "error":
lvl = level.AllowError()
case "warn":
lvl = level.AllowWarn()
case "info":
lvl = level.AllowInfo()
case "debug":
lvl = level.AllowDebug()
default:
panic("unexpected log level")
}
switch logFormat {
case logFormatJson:
return level.NewFilter(log.NewJSONLogger(log.NewSyncWriter(os.Stderr)), lvl)
case logFormatLogfmt:
return level.NewFilter(log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)), lvl)
case logFormatCLILog:
fallthrough
default:
return level.NewFilter(clilog.New(log.NewSyncWriter(os.Stderr)), lvl)
}
}
func main() {
app := extkingpin.NewApp(kingpin.New(filepath.Base(os.Args[0]), `Thanos Service Discovery Sidecar.`).Version(version.Version))
logLevel := app.Flag("log.level", "Log filtering level.").
Default("info").Enum("error", "warn", "info", "debug")
logFormat := app.Flag("log.format", "Log format to use.").
Default(logFormatCLILog).Enum(logFormatLogfmt, logFormatJson, logFormatCLILog)
ctx, cancel := context.WithCancel(context.Background())
registerCommands(ctx, app)
cmd, runner := app.Parse()
logger := setupLogger(*logLevel, *logFormat)
var g run.Group
g.Add(func() error {
return runner(ctx, logger)
}, func(err error) {
cancel()
})
// Listen for termination signals.
{
cancel := make(chan struct{})
g.Add(func() error {
return interrupt(logger, cancel)
}, func(error) {
close(cancel)
})
}
if err := g.Run(); err != nil {
if *logLevel == "debug" {
// Use %+v for github.com/pkg/errors error to print with stack.
level.Error(logger).Log("err", fmt.Sprintf("%+v", errors.Wrapf(err, "%s command failed", cmd)))
os.Exit(1)
}
level.Error(logger).Log("err", errors.Wrapf(err, "%s command failed", cmd))
os.Exit(1)
}
}
func interrupt(logger log.Logger, cancel <-chan struct{}) error {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
select {
case s := <-c:
level.Info(logger).Log("msg", "caught signal. Exiting.", "signal", s)
return nil
case <-cancel:
return errors.New("canceled")
}
}
func registerCommands(_ context.Context, app *extkingpin.App) {
cmd := app.Command("run", "Launches sidecar for Thanos Service Discovery which generates file_sd output (https://prometheus.io/docs/prometheus/latest/configuration/configuration/#file_sd_config) according to configuration.")
config := extflag.RegisterPathOrContent(cmd, "config", "YAML file for Service Discovery configuration, with spec defined in https://prometheus.io/docs/prometheus/latest/configuration/configuration.", extflag.WithEnvSubstitution(), extflag.WithRequired())
outputPath := cmd.Flag("output.path", "The output path for file_sd compatible files.").Required().String()
cmd.Run(func(ctx context.Context, logger log.Logger) error {
validateConfig, err := config.Content()
if err != nil {
return err
}
return discovery.Discovery(validateConfig, *outputPath)
})
}