-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.go
124 lines (99 loc) · 3.66 KB
/
root.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
/*
Copyright © 2024 cool4pso
*/
package cmd
import (
"fmt"
"log"
"os"
"github.com/coolapso/prometheus-youtube-exporter/internal/collectors"
"github.com/coolapso/prometheus-youtube-exporter/internal/httpServer"
"github.com/coolapso/prometheus-youtube-exporter/internal/slogLogger"
"github.com/prometheus/common/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var rootCmd = &cobra.Command{
Use: "youtube-exporter",
Short: "Prometheus Youtube Exporter",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return checkCoreSettings()
},
Run: func(cmd *cobra.Command, args []string) {
exporter()
},
}
const (
defaultLogLevel = "info"
defaultLogFormat = "text"
defaultMetricsPath = "/metrics"
defaultListenPort = "10020"
defaultAddress = "localhost"
)
var (
settings collectors.Settings
Version = "DEV"
)
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
viper.AutomaticEnv()
viper.SetDefault("LOG_LEVEL", defaultLogLevel)
viper.SetDefault("LOG_FORMAT", defaultLogFormat)
viper.SetDefault("METRICS_PATH", defaultMetricsPath)
viper.SetDefault("LISTEN_PORT", defaultListenPort)
viper.SetDefault("ADDRESS", defaultAddress)
viper.SetDefault("YT_CHANNEL_IDS", nil)
viper.SetDefault("YT_API_KEY", "")
rootCmd.Flags().StringVar(&settings.LogLevel, "log.level", defaultLogLevel, "Exporter log level")
_ = viper.BindPFlag("log.level", rootCmd.Flags().Lookup("LOG_LEVEL"))
rootCmd.Flags().StringVar(&settings.LogFormat, "log.format", defaultLogFormat, "Exporter log format, text or json")
_ = viper.BindPFlag("log.format", rootCmd.Flags().Lookup("LOG_FORMAT"))
rootCmd.Flags().StringVar(&settings.MetricsPath, "metrics.path", defaultMetricsPath, "Path to expose metrics at")
_ = viper.BindPFlag("metrics.path", rootCmd.Flags().Lookup("METRICS_PATH"))
rootCmd.Flags().StringVar(&settings.ListenPort, "listen.port", defaultListenPort, "Port to listen at")
_ = viper.BindPFlag("listen.port", rootCmd.Flags().Lookup("LISTEN_PORT"))
rootCmd.Flags().StringVar(&settings.Address, "address", defaultAddress, "The address to access the exporter used for oauth redirect uri")
_ = viper.BindPFlag("address", rootCmd.Flags().Lookup("ADDRESS"))
rootCmd.Flags().StringVar(&settings.ApiKey, "api.key", defaultAddress, "Youtube API Key")
_ = viper.BindPFlag("api.key", rootCmd.Flags().Lookup("YT_API_KEY"))
rootCmd.Flags().StringSliceVar(&settings.ChannelIds, "channel.ids", nil, "The ids of youttube channels to monitor")
_ = viper.BindPFlag("channel.ids", rootCmd.Flags().Lookup("YT_CHANNEL_IDS"))
settings.LogLevel = viper.GetString("LOG_LEVEL")
settings.LogFormat = viper.GetString("LOG_FORMAT")
settings.MetricsPath = viper.GetString("METRICS_PATH")
settings.ListenPort = viper.GetString("LISTEN_PORT")
settings.Address = viper.GetString("ADDRESS")
settings.ChannelIds = viper.GetStringSlice("YT_CHANNEL_IDS")
settings.ApiKey = viper.GetString("YT_API_KEY")
}
func checkCoreSettings() error {
s := &settings
if s.ApiKey == "" {
return fmt.Errorf("Missing Youtube API Key")
}
if s.ChannelIds == nil {
return fmt.Errorf("Missing Channel ids")
}
return nil
}
func exporter() {
s := &settings
logger, err := slogLogger.NewLogger(s.LogLevel, s.LogFormat)
if err != nil {
logger.Warn(err.Error())
}
logger.Info(fmt.Sprintf("starting prometheus exporter %v %v", Version, version.BuildContext()))
exporter, err := collectors.NewExporter(s, logger)
if err != nil {
logger.Error("Failed to create new exporter", "err", err)
os.Exit(1)
}
srv := httpServer.NewServer(exporter)
logger.Info(fmt.Sprintf("Server ready and listening on port :%v", s.ListenPort))
log.Fatal(srv.ListenAndServe())
}