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

Commit a9fede5

Browse files
authored
Merge pull request #6 from rebuy-de/add-env
add environment variables
2 parents 933ad22 + 27cb941 commit a9fede5

File tree

4 files changed

+146
-33
lines changed

4 files changed

+146
-33
lines changed

Gopkg.lock

Lines changed: 91 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/command.go

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package cmd
33
import (
44
"fmt"
55
"net/http"
6+
"os"
7+
"strings"
68

79
log "github.com/sirupsen/logrus"
810
"github.com/spf13/cobra"
11+
"github.com/spf13/viper"
912
)
1013

1114
func NewRootCommand() *cobra.Command {
@@ -20,36 +23,61 @@ func NewRootCommand() *cobra.Command {
2023
},
2124
}
2225

23-
cmd.PersistentFlags().StringVarP(
24-
&app.configPath, "config-path", "c", "./merger.yaml",
25-
"Path to the configuration file.")
26-
cmd.PersistentFlags().IntVar(
27-
&app.port, "listen-port", 8080,
28-
"Listen port for the HTTP server.")
26+
app.Bind(cmd)
2927

3028
cmd.AddCommand(NewVersionCommand())
3129

3230
return cmd
3331
}
3432

3533
type App struct {
36-
configPath string
37-
port int
34+
viper *viper.Viper
3835
}
3936

40-
func (app *App) run(cmd *cobra.Command, args []string) {
41-
config, err := ReadConfig(app.configPath)
42-
if err != nil {
43-
log.WithField("error", err).Error("failed to load config")
44-
return
45-
}
37+
func (app *App) Bind(cmd *cobra.Command) {
38+
app.viper = viper.New()
39+
app.viper.SetEnvPrefix("MERGER")
40+
app.viper.AutomaticEnv()
41+
42+
configPath := cmd.PersistentFlags().StringP(
43+
"config-path", "c", "",
44+
"Path to the configuration file.")
45+
cobra.OnInitialize(func() {
46+
if configPath != nil && *configPath != "" {
47+
config, err := ReadConfig(*configPath)
48+
if err != nil {
49+
log.WithField("error", err).Errorf("failed to load config file '%s'", *configPath)
50+
os.Exit(1)
51+
return
52+
}
4653

54+
urls := []string{}
55+
for _, e := range config.Exporters {
56+
urls = append(urls, e.URL)
57+
}
58+
app.viper.SetDefault("urls", strings.Join(urls, " "))
59+
}
60+
})
61+
62+
cmd.PersistentFlags().Int(
63+
"listen-port", 8080,
64+
"Listen port for the HTTP server. (ENV:MERGER_PORT)")
65+
app.viper.BindPFlag("port", cmd.PersistentFlags().Lookup("listen-port"))
66+
67+
cmd.PersistentFlags().StringSlice(
68+
"url", nil,
69+
"URL to scrape. Can be speficied multiple times. (ENV:MERGER_URLS,space-seperated)")
70+
app.viper.BindPFlag("urls", cmd.PersistentFlags().Lookup("url"))
71+
}
72+
73+
func (app *App) run(cmd *cobra.Command, args []string) {
4774
http.Handle("/metrics", Handler{
48-
Config: *config,
75+
Exporters: app.viper.GetStringSlice("urls"),
4976
})
5077

51-
log.Infof("starting HTTP server on port %d", app.port)
52-
err = http.ListenAndServe(fmt.Sprintf(":%d", app.port), nil)
78+
port := app.viper.GetInt("port")
79+
log.Infof("starting HTTP server on port %d", port)
80+
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
5381
if err != nil {
5482
log.Fatal(err)
5583
}

cmd/handler.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
type Handler struct {
14-
Config Config
14+
Exporters []string
1515
}
1616

1717
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -30,8 +30,9 @@ func (h Handler) Merge(w io.Writer) error {
3030
mfs := map[string]*prom.MetricFamily{}
3131
tp := new(expfmt.TextParser)
3232

33-
for _, e := range h.Config.Exporters {
34-
resp, err := http.Get(e.URL)
33+
for _, url := range h.Exporters {
34+
log.WithField("url", url).Debug("getting remote metrics")
35+
resp, err := http.Get(url)
3536
if err != nil {
3637
return err
3738
}

cmd/handler_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ import (
1111
log "github.com/sirupsen/logrus"
1212
)
1313

14-
func testExporter(t testing.TB, content string) (cmd.Exporter, func()) {
14+
func testExporter(t testing.TB, content string) (string, func()) {
1515
t.Helper()
1616

1717
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1818
fmt.Fprintln(w, content)
1919
}))
2020

21-
return cmd.Exporter{
22-
URL: ts.URL,
23-
}, ts.Close
21+
return ts.URL, ts.Close
2422
}
2523

2624
func TestHandler(t *testing.T) {
@@ -34,15 +32,13 @@ func TestHandler(t *testing.T) {
3432
"bar{} 4\nconflict 5\nshared{meh=\"b\"} 6")
3533
defer deferrer()
3634

37-
config := cmd.Config{
38-
Exporters: []cmd.Exporter{
39-
te1,
40-
te2,
41-
},
35+
exporters := []string{
36+
te1,
37+
te2,
4238
}
4339

4440
server := httptest.NewServer(cmd.Handler{
45-
Config: config,
41+
Exporters: exporters,
4642
})
4743
defer server.Close()
4844

0 commit comments

Comments
 (0)