Skip to content

Commit

Permalink
Merge pull request #9 from promhippie/health-check
Browse files Browse the repository at this point in the history
Add simple healthcheck command
  • Loading branch information
tboerger authored Mar 27, 2019
2 parents 746668c + 4614e46 commit 154af26
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 137 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

* Add support for server tags
* Support multiple credentials and config file
* Add simple healthcheck command

### Removed

Expand Down
77 changes: 77 additions & 0 deletions cmd/prometheus-scw-sd/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"fmt"
"net/http"

"github.com/go-kit/kit/log/level"
"github.com/promhippie/prometheus-scw-sd/pkg/config"
"gopkg.in/urfave/cli.v2"
)

// Health provides the sub-command to perform a health check.
func Health(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "health",
Usage: "Perform health checks",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "web.address",
Value: "0.0.0.0:9000",
Usage: "Address to bind the metrics server",
EnvVars: []string{"PROMETHEUS_SCW_WEB_ADDRESS"},
Destination: &cfg.Server.Addr,
},
&cli.StringFlag{
Name: "scw.config",
Value: "",
Usage: "Path to Scaleway configuration file",
EnvVars: []string{"PROMETHEUS_SCW_CONFIG"},
},
},
Action: func(c *cli.Context) error {
logger := setupLogger(cfg)

if c.IsSet("scw.config") {
if err := readConfig(c.String("scw.config"), cfg); err != nil {
level.Error(logger).Log(
"msg", "Failed to read config",
"err", err,
)

return err
}
}

resp, err := http.Get(
fmt.Sprintf(
"http://%s/healthz",
cfg.Server.Addr,
),
)

if err != nil {
level.Error(logger).Log(
"msg", "Failed to request health check",
"err", err,
)

return err
}

defer resp.Body.Close()

if resp.StatusCode != 200 {
level.Error(logger).Log(
"msg", "Health check seems to be in bad state",
"err", err,
"code", resp.StatusCode,
)

return err
}

return nil
},
}
}
139 changes: 2 additions & 137 deletions cmd/prometheus-scw-sd/main.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
package main

import (
"errors"
"os"

"github.com/go-kit/kit/log/level"
"github.com/joho/godotenv"
"github.com/promhippie/prometheus-scw-sd/pkg/action"
"github.com/promhippie/prometheus-scw-sd/pkg/config"
"github.com/promhippie/prometheus-scw-sd/pkg/version"
"gopkg.in/urfave/cli.v2"
)

var (
// ErrMissingOutputFile defines the error if output.file is empty.
ErrMissingOutputFile = errors.New("Missing path for output.file")

// ErrMissingScwToken defines the error if scw.token is empty.
ErrMissingScwToken = errors.New("Missing required scw.token")

// ErrMissingScwOrg defines the error if scw.org is empty.
ErrMissingScwOrg = errors.New("Missing required scw.org")

// ErrMissingAnyCredentials defines the error if no credentials are provided.
ErrMissingAnyCredentials = errors.New("Missing any credentials")
)

func main() {
cfg := config.Load()

Expand Down Expand Up @@ -60,126 +43,8 @@ func main() {
},
},
Commands: []*cli.Command{
{
Name: "server",
Usage: "start integrated server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "web.address",
Value: "0.0.0.0:9000",
Usage: "Address to bind the metrics server",
EnvVars: []string{"PROMETHEUS_SCW_WEB_ADDRESS"},
Destination: &cfg.Server.Addr,
},
&cli.StringFlag{
Name: "web.path",
Value: "/metrics",
Usage: "Path to bind the metrics server",
EnvVars: []string{"PROMETHEUS_SCW_WEB_PATH"},
Destination: &cfg.Server.Path,
},
&cli.StringFlag{
Name: "output.file",
Value: "/etc/prometheus/scw.json",
Usage: "Path to write the file_sd config",
EnvVars: []string{"PROMETHEUS_SCW_OUTPUT_FILE"},
Destination: &cfg.Target.File,
},
&cli.IntFlag{
Name: "output.refresh",
Value: 30,
Usage: "Discovery refresh interval in seconds",
EnvVars: []string{"PROMETHEUS_SCW_OUTPUT_REFRESH"},
Destination: &cfg.Target.Refresh,
},
&cli.StringFlag{
Name: "scw.token",
Value: "",
Usage: "Access token for the Scaleway API",
EnvVars: []string{"PROMETHEUS_SCW_TOKEN"},
},
&cli.StringFlag{
Name: "scw.org",
Value: "",
Usage: "Organization for the Scaleway API",
EnvVars: []string{"PROMETHEUS_SCW_ORG"},
},
&cli.StringFlag{
Name: "scw.region",
Value: "",
Usage: "Region for the Scaleway API",
EnvVars: []string{"PROMETHEUS_SCW_REGION"},
},
&cli.StringFlag{
Name: "scw.config",
Value: "",
Usage: "Path to Scaleway configuration file",
EnvVars: []string{"PROMETHEUS_SCW_CONFIG"},
},
},
Action: func(c *cli.Context) error {
logger := setupLogger(cfg)

if c.IsSet("scw.config") {
if err := readConfig(c.String("scw.config"), cfg); err != nil {
level.Error(logger).Log(
"msg", "Failed to read config",
"err", err,
)

return err
}
}

if cfg.Target.File == "" {
level.Error(logger).Log(
"msg", ErrMissingOutputFile,
)

return ErrMissingOutputFile
}

if c.IsSet("scw.token") && c.IsSet("scw.org") && c.IsSet("scw.region") {
credentials := config.Credential{
Project: "default",
Token: c.String("scw.token"),
Org: c.String("scw.org"),
Region: c.String("scw.region"),
}

cfg.Target.Credentials = append(
cfg.Target.Credentials,
credentials,
)

if credentials.Token == "" {
level.Error(logger).Log(
"msg", ErrMissingScwToken,
)

return ErrMissingScwToken
}

if credentials.Org == "" {
level.Error(logger).Log(
"msg", ErrMissingScwOrg,
)

return ErrMissingScwOrg
}
}

if len(cfg.Target.Credentials) == 0 {
level.Error(logger).Log(
"msg", ErrMissingAnyCredentials,
)

return ErrMissingAnyCredentials
}

return action.Server(cfg, logger)
},
},
Health(cfg),
Server(cfg),
},
}

Expand Down
148 changes: 148 additions & 0 deletions cmd/prometheus-scw-sd/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package main

import (
"errors"

"github.com/go-kit/kit/log/level"
"github.com/promhippie/prometheus-scw-sd/pkg/action"
"github.com/promhippie/prometheus-scw-sd/pkg/config"
"gopkg.in/urfave/cli.v2"
)

var (
// ErrMissingOutputFile defines the error if output.file is empty.
ErrMissingOutputFile = errors.New("Missing path for output.file")

// ErrMissingScwToken defines the error if scw.token is empty.
ErrMissingScwToken = errors.New("Missing required scw.token")

// ErrMissingScwOrg defines the error if scw.org is empty.
ErrMissingScwOrg = errors.New("Missing required scw.org")

// ErrMissingAnyCredentials defines the error if no credentials are provided.
ErrMissingAnyCredentials = errors.New("Missing any credentials")
)

// Server provides the sub-command to start the server.
func Server(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "server",
Usage: "Start integrated server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "web.address",
Value: "0.0.0.0:9000",
Usage: "Address to bind the metrics server",
EnvVars: []string{"PROMETHEUS_SCW_WEB_ADDRESS"},
Destination: &cfg.Server.Addr,
},
&cli.StringFlag{
Name: "web.path",
Value: "/metrics",
Usage: "Path to bind the metrics server",
EnvVars: []string{"PROMETHEUS_SCW_WEB_PATH"},
Destination: &cfg.Server.Path,
},
&cli.StringFlag{
Name: "output.file",
Value: "/etc/prometheus/scw.json",
Usage: "Path to write the file_sd config",
EnvVars: []string{"PROMETHEUS_SCW_OUTPUT_FILE"},
Destination: &cfg.Target.File,
},
&cli.IntFlag{
Name: "output.refresh",
Value: 30,
Usage: "Discovery refresh interval in seconds",
EnvVars: []string{"PROMETHEUS_SCW_OUTPUT_REFRESH"},
Destination: &cfg.Target.Refresh,
},
&cli.StringFlag{
Name: "scw.token",
Value: "",
Usage: "Access token for the Scaleway API",
EnvVars: []string{"PROMETHEUS_SCW_TOKEN"},
},
&cli.StringFlag{
Name: "scw.org",
Value: "",
Usage: "Organization for the Scaleway API",
EnvVars: []string{"PROMETHEUS_SCW_ORG"},
},
&cli.StringFlag{
Name: "scw.region",
Value: "",
Usage: "Region for the Scaleway API",
EnvVars: []string{"PROMETHEUS_SCW_REGION"},
},
&cli.StringFlag{
Name: "scw.config",
Value: "",
Usage: "Path to Scaleway configuration file",
EnvVars: []string{"PROMETHEUS_SCW_CONFIG"},
},
},
Action: func(c *cli.Context) error {
logger := setupLogger(cfg)

if c.IsSet("scw.config") {
if err := readConfig(c.String("scw.config"), cfg); err != nil {
level.Error(logger).Log(
"msg", "Failed to read config",
"err", err,
)

return err
}
}

if cfg.Target.File == "" {
level.Error(logger).Log(
"msg", ErrMissingOutputFile,
)

return ErrMissingOutputFile
}

if c.IsSet("scw.token") && c.IsSet("scw.org") && c.IsSet("scw.region") {
credentials := config.Credential{
Project: "default",
Token: c.String("scw.token"),
Org: c.String("scw.org"),
Region: c.String("scw.region"),
}

cfg.Target.Credentials = append(
cfg.Target.Credentials,
credentials,
)

if credentials.Token == "" {
level.Error(logger).Log(
"msg", ErrMissingScwToken,
)

return ErrMissingScwToken
}

if credentials.Org == "" {
level.Error(logger).Log(
"msg", ErrMissingScwOrg,
)

return ErrMissingScwOrg
}
}

if len(cfg.Target.Credentials) == 0 {
level.Error(logger).Log(
"msg", ErrMissingAnyCredentials,
)

return ErrMissingAnyCredentials
}

return action.Server(cfg, logger)
},
}
}

0 comments on commit 154af26

Please sign in to comment.