-
Notifications
You must be signed in to change notification settings - Fork 985
/
main.go
147 lines (132 loc) · 3.92 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"os"
runtimeDebug "runtime/debug"
"time"
joonix "github.com/joonix/log"
"github.com/prysmaticlabs/prysm/v5/cmd"
"github.com/prysmaticlabs/prysm/v5/cmd/client-stats/flags"
"github.com/prysmaticlabs/prysm/v5/io/logs"
"github.com/prysmaticlabs/prysm/v5/monitoring/clientstats"
"github.com/prysmaticlabs/prysm/v5/monitoring/journald"
prefixed "github.com/prysmaticlabs/prysm/v5/runtime/logging/logrus-prefixed-formatter"
"github.com/prysmaticlabs/prysm/v5/runtime/version"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var appFlags = []cli.Flag{
cmd.VerbosityFlag,
cmd.LogFormat,
cmd.LogFileName,
cmd.ConfigFileFlag,
flags.BeaconnodeMetricsURLFlag,
flags.ValidatorMetricsURLFlag,
flags.ClientStatsAPIURLFlag,
flags.ScrapeIntervalFlag,
}
func init() {
appFlags = cmd.WrapFlags(appFlags)
}
func main() {
app := cli.App{}
app.Name = "client-stats"
app.Usage = "daemon to scrape client-stats from prometheus and ship to a remote endpoint"
app.Action = run
app.Version = version.Version()
app.Flags = appFlags
// logging/config setup cargo-culted from beaconchain
app.Before = func(ctx *cli.Context) error {
// Load flags from config file, if specified.
if err := cmd.LoadFlagsFromConfig(ctx, app.Flags); err != nil {
return err
}
verbosity := ctx.String(cmd.VerbosityFlag.Name)
level, err := logrus.ParseLevel(verbosity)
if err != nil {
return err
}
logrus.SetLevel(level)
format := ctx.String(cmd.LogFormat.Name)
switch format {
case "text":
formatter := new(prefixed.TextFormatter)
formatter.TimestampFormat = "2006-01-02 15:04:05"
formatter.FullTimestamp = true
// If persistent log files are written - we disable the log messages coloring because
// the colors are ANSI codes and seen as gibberish in the log files.
formatter.DisableColors = ctx.String(cmd.LogFileName.Name) != ""
logrus.SetFormatter(formatter)
case "fluentd":
f := joonix.NewFormatter()
if err := joonix.DisableTimestampFormat(f); err != nil {
panic(err)
}
logrus.SetFormatter(f)
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{})
case "journald":
if err := journald.Enable(); err != nil {
return err
}
default:
return fmt.Errorf("unknown log format %s", format)
}
logFileName := ctx.String(cmd.LogFileName.Name)
if logFileName != "" {
if err := logs.ConfigurePersistentLogging(logFileName); err != nil {
log.WithError(err).Error("Failed to configuring logging to disk.")
}
}
return cmd.ValidateNoArgs(ctx)
}
defer func() {
if x := recover(); x != nil {
log.Errorf("Runtime panic: %v\n%v", x, string(runtimeDebug.Stack()))
panic(x)
}
}()
if err := app.Run(os.Args); err != nil {
log.Error(err.Error())
}
}
func run(ctx *cli.Context) error {
var upd clientstats.Updater
if ctx.IsSet(flags.ClientStatsAPIURLFlag.Name) {
u := ctx.String(flags.ClientStatsAPIURLFlag.Name)
upd = clientstats.NewClientStatsHTTPPostUpdater(u)
} else {
log.Warn("No --clientstats-api-url flag set, writing to stdout as default metrics sink.")
upd = clientstats.NewGenericClientStatsUpdater(os.Stdout)
}
scrapers := make([]clientstats.Scraper, 0)
if ctx.IsSet(flags.BeaconnodeMetricsURLFlag.Name) {
u := ctx.String(flags.BeaconnodeMetricsURLFlag.Name)
scrapers = append(scrapers, clientstats.NewBeaconNodeScraper(u))
}
if ctx.IsSet(flags.ValidatorMetricsURLFlag.Name) {
u := ctx.String(flags.ValidatorMetricsURLFlag.Name)
scrapers = append(scrapers, clientstats.NewValidatorScraper(u))
}
ticker := time.NewTicker(ctx.Duration(flags.ScrapeIntervalFlag.Name))
for {
select {
case <-ticker.C:
for _, s := range scrapers {
r, err := s.Scrape()
if err != nil {
log.WithError(err).Error("Scraper error")
continue
}
err = upd.Update(r)
if err != nil {
log.WithError(err).Error("client-stats collector error")
continue
}
}
case <-ctx.Done():
ticker.Stop()
return nil
}
}
}