Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Add flags for exporter http timeout and log level #10

Merged
merged 2 commits into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ func NewRootCommand() *cobra.Command {
Short: "merges Prometheus metrics from multiple sources",
Run: app.run,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
log.SetLevel(log.DebugLevel)
if app.viper.GetBool("verbose") {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
},
}

Expand Down Expand Up @@ -64,6 +68,16 @@ func (app *App) Bind(cmd *cobra.Command) {
"Listen port for the HTTP server. (ENV:MERGER_PORT)")
app.viper.BindPFlag("port", cmd.PersistentFlags().Lookup("listen-port"))

cmd.PersistentFlags().Int(
"exporters-timeout", 10,
"HTTP client timeout for connecting to exporters. (ENV:MERGER_EXPORTERSTIMEOUT)")
app.viper.BindPFlag("exporterstimeout", cmd.PersistentFlags().Lookup("exporters-timeout"))

cmd.PersistentFlags().BoolP(
"verbose", "v", false,
"Include debug messages to output (ENV:MERGER_VERBOSE)")
app.viper.BindPFlag("verbose", cmd.PersistentFlags().Lookup("verbose"))

cmd.PersistentFlags().StringSlice(
"url", nil,
"URL to scrape. Can be speficied multiple times. (ENV:MERGER_URLS,space-seperated)")
Expand All @@ -72,7 +86,8 @@ func (app *App) Bind(cmd *cobra.Command) {

func (app *App) run(cmd *cobra.Command, args []string) {
http.Handle("/metrics", Handler{
Exporters: app.viper.GetStringSlice("urls"),
Exporters: app.viper.GetStringSlice("urls"),
ExportersHTTPTimeout: app.viper.GetInt("exporterstimeout"),
})

port := app.viper.GetInt("port")
Expand Down
6 changes: 3 additions & 3 deletions cmd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
)

type Handler struct {
Exporters []string
Exporters []string
ExportersHTTPTimeout int
}

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand All @@ -29,8 +30,7 @@ func (h Handler) Merge(w io.Writer) {

responses := make([]map[string]*prom.MetricFamily, 1024)
responsesMu := sync.Mutex{}

httpClientTimeout := time.Second * 10
httpClientTimeout := time.Second * time.Duration(h.ExportersHTTPTimeout)

wg := sync.WaitGroup{}
for _, url := range h.Exporters {
Expand Down