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

Commit

Permalink
Merge pull request #6 from rebuy-de/add-env
Browse files Browse the repository at this point in the history
add environment variables
  • Loading branch information
svenwltr authored Jan 14, 2019
2 parents 933ad22 + 27cb941 commit a9fede5
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 33 deletions.
94 changes: 91 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 45 additions & 17 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package cmd
import (
"fmt"
"net/http"
"os"
"strings"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func NewRootCommand() *cobra.Command {
Expand All @@ -20,36 +23,61 @@ func NewRootCommand() *cobra.Command {
},
}

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

cmd.AddCommand(NewVersionCommand())

return cmd
}

type App struct {
configPath string
port int
viper *viper.Viper
}

func (app *App) run(cmd *cobra.Command, args []string) {
config, err := ReadConfig(app.configPath)
if err != nil {
log.WithField("error", err).Error("failed to load config")
return
}
func (app *App) Bind(cmd *cobra.Command) {
app.viper = viper.New()
app.viper.SetEnvPrefix("MERGER")
app.viper.AutomaticEnv()

configPath := cmd.PersistentFlags().StringP(
"config-path", "c", "",
"Path to the configuration file.")
cobra.OnInitialize(func() {
if configPath != nil && *configPath != "" {
config, err := ReadConfig(*configPath)
if err != nil {
log.WithField("error", err).Errorf("failed to load config file '%s'", *configPath)
os.Exit(1)
return
}

urls := []string{}
for _, e := range config.Exporters {
urls = append(urls, e.URL)
}
app.viper.SetDefault("urls", strings.Join(urls, " "))
}
})

cmd.PersistentFlags().Int(
"listen-port", 8080,
"Listen port for the HTTP server. (ENV:MERGER_PORT)")
app.viper.BindPFlag("port", cmd.PersistentFlags().Lookup("listen-port"))

cmd.PersistentFlags().StringSlice(
"url", nil,
"URL to scrape. Can be speficied multiple times. (ENV:MERGER_URLS,space-seperated)")
app.viper.BindPFlag("urls", cmd.PersistentFlags().Lookup("url"))
}

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

log.Infof("starting HTTP server on port %d", app.port)
err = http.ListenAndServe(fmt.Sprintf(":%d", app.port), nil)
port := app.viper.GetInt("port")
log.Infof("starting HTTP server on port %d", port)
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil {
log.Fatal(err)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type Handler struct {
Config Config
Exporters []string
}

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

for _, e := range h.Config.Exporters {
resp, err := http.Get(e.URL)
for _, url := range h.Exporters {
log.WithField("url", url).Debug("getting remote metrics")
resp, err := http.Get(url)
if err != nil {
return err
}
Expand Down
16 changes: 6 additions & 10 deletions cmd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ import (
log "github.com/sirupsen/logrus"
)

func testExporter(t testing.TB, content string) (cmd.Exporter, func()) {
func testExporter(t testing.TB, content string) (string, func()) {
t.Helper()

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

return cmd.Exporter{
URL: ts.URL,
}, ts.Close
return ts.URL, ts.Close
}

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

config := cmd.Config{
Exporters: []cmd.Exporter{
te1,
te2,
},
exporters := []string{
te1,
te2,
}

server := httptest.NewServer(cmd.Handler{
Config: config,
Exporters: exporters,
})
defer server.Close()

Expand Down

0 comments on commit a9fede5

Please sign in to comment.