Minor code cleanups

This commit is contained in:
Julius Volz 2017-11-01 23:08:34 +01:00
parent 511c6bcb6a
commit 9b72c10134
11 changed files with 29 additions and 30 deletions

View File

@ -39,7 +39,7 @@ func CheckConfig(args []string) error {
config, _, err := config.LoadFile(arg) config, _, err := config.LoadFile(arg)
if err != nil { if err != nil {
fmt.Printf(" FAILED: %s\n", err) fmt.Printf(" FAILED: %s\n", err)
failed += 1 failed++
} else { } else {
fmt.Printf(" SUCCESS\n") fmt.Printf(" SUCCESS\n")
} }
@ -50,7 +50,7 @@ func CheckConfig(args []string) error {
_, err = template.FromGlobs(config.Templates...) _, err = template.FromGlobs(config.Templates...)
if err != nil { if err != nil {
fmt.Printf(" FAILED: %s\n", err) fmt.Printf(" FAILED: %s\n", err)
failed += 1 failed++
} else { } else {
fmt.Printf(" SUCCESS\n") fmt.Printf(" SUCCESS\n")
} }

View File

@ -43,7 +43,7 @@ type Formatter interface {
} }
// Formatters is a map of cli argument name to formatter inferface object // Formatters is a map of cli argument name to formatter inferface object
var Formatters map[string]Formatter = map[string]Formatter{} var Formatters = map[string]Formatter{}
func FormatDate(input time.Time) string { func FormatDate(input time.Time) string {
dateformat := viper.GetString("date.format") dateformat := viper.GetString("date.format")

View File

@ -9,29 +9,29 @@ import (
"github.com/prometheus/alertmanager/types" "github.com/prometheus/alertmanager/types"
) )
type JsonFormatter struct { type JSONFormatter struct {
writer io.Writer writer io.Writer
} }
func init() { func init() {
Formatters["json"] = &JsonFormatter{writer: os.Stdout} Formatters["json"] = &JSONFormatter{writer: os.Stdout}
} }
func (formatter *JsonFormatter) SetOutput(writer io.Writer) { func (formatter *JSONFormatter) SetOutput(writer io.Writer) {
formatter.writer = writer formatter.writer = writer
} }
func (formatter *JsonFormatter) FormatSilences(silences []types.Silence) error { func (formatter *JSONFormatter) FormatSilences(silences []types.Silence) error {
enc := json.NewEncoder(formatter.writer) enc := json.NewEncoder(formatter.writer)
return enc.Encode(silences) return enc.Encode(silences)
} }
func (formatter *JsonFormatter) FormatAlerts(alerts []*dispatch.APIAlert) error { func (formatter *JSONFormatter) FormatAlerts(alerts []*dispatch.APIAlert) error {
enc := json.NewEncoder(formatter.writer) enc := json.NewEncoder(formatter.writer)
return enc.Encode(alerts) return enc.Encode(alerts)
} }
func (formatter *JsonFormatter) FormatConfig(config Config) error { func (formatter *JSONFormatter) FormatConfig(config Config) error {
enc := json.NewEncoder(formatter.writer) enc := json.NewEncoder(formatter.writer)
return enc.Encode(config) return enc.Encode(config)
} }

View File

@ -88,7 +88,7 @@ func add(cmd *cobra.Command, args []string) error {
return fmt.Errorf("No matchers specified") return fmt.Errorf("No matchers specified")
} }
expire_on, err := addFlags.GetString("expire-on") expireOn, err := addFlags.GetString("expire-on")
if err != nil { if err != nil {
return err return err
} }
@ -96,8 +96,8 @@ func add(cmd *cobra.Command, args []string) error {
expires := viper.GetString("expires") expires := viper.GetString("expires")
var endsAt time.Time var endsAt time.Time
if expire_on != "" { if expireOn != "" {
endsAt, err = time.Parse(time.RFC3339, expire_on) endsAt, err = time.Parse(time.RFC3339, expireOn)
if err != nil { if err != nil {
return err return err
} }
@ -111,9 +111,9 @@ func add(cmd *cobra.Command, args []string) error {
author := viper.GetString("author") author := viper.GetString("author")
comment := viper.GetString("comment") comment := viper.GetString("comment")
comment_required := viper.GetBool("comment_required") commentRequired := viper.GetBool("comment_required")
if comment_required && comment == "" { if commentRequired && comment == "" {
return errors.New("Comment required by config") return errors.New("Comment required by config")
} }
@ -151,7 +151,7 @@ func add(cmd *cobra.Command, args []string) error {
response := addResponse{} response := addResponse{}
err = json.NewDecoder(res.Body).Decode(&response) err = json.NewDecoder(res.Body).Decode(&response)
if err != nil { if err != nil {
return errors.New(fmt.Sprintf("Unable to parse silence json response from %s", u.String())) return fmt.Errorf("Unable to parse silence json response from %s", u.String())
} }
if response.Status == "error" { if response.Status == "error" {

View File

@ -26,9 +26,8 @@ func (s ByAlphabetical) Less(i, j int) bool {
return s[i].Type < s[j].Type return s[i].Type < s[j].Type
} else if s[i].Value != s[j].Value { } else if s[i].Value != s[j].Value {
return s[i].Value < s[j].Value return s[i].Value < s[j].Value
} else {
return false
} }
return false
} }
func GetAlertmanagerURL() (*url.URL, error) { func GetAlertmanagerURL() (*url.URL, error) {

View File

@ -1,8 +1,8 @@
package config package config
import ( import (
"testing"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"testing"
) )
func TestEmailToIsPresent(t *testing.T) { func TestEmailToIsPresent(t *testing.T) {
@ -160,4 +160,4 @@ token: ''
if err.Error() != expected { if err.Error() != expected {
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error()) t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
} }
} }

View File

@ -36,7 +36,7 @@ func (m *Entry) IsResolvedSubset(subset map[uint64]struct{}) bool {
} }
func isSubset(set, subset map[uint64]struct{}) bool { func isSubset(set, subset map[uint64]struct{}) bool {
for k, _ := range subset { for k := range subset {
_, exists := set[k] _, exists := set[k]
if !exists { if !exists {
return false return false

View File

@ -981,12 +981,12 @@ func (n *Pushover) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
} }
parameters.Add("message", message) parameters.Add("message", message)
supplementaryUrl := tmpl(n.conf.URL) supplementaryURL := tmpl(n.conf.URL)
if len(supplementaryUrl) > 512 { if len(supplementaryURL) > 512 {
supplementaryUrl = supplementaryUrl[:509] + "..." supplementaryURL = supplementaryURL[:509] + "..."
level.Debug(n.logger).Log("msg", "Truncated URL due to Pushover url limit", "truncated_url", supplementaryUrl, "incident", key) level.Debug(n.logger).Log("msg", "Truncated URL due to Pushover url limit", "truncated_url", supplementaryURL, "incident", key)
} }
parameters.Add("url", supplementaryUrl) parameters.Add("url", supplementaryURL)
parameters.Add("priority", tmpl(n.conf.Priority)) parameters.Add("priority", tmpl(n.conf.Priority))
parameters.Add("retry", fmt.Sprintf("%d", int64(time.Duration(n.conf.Retry).Seconds()))) parameters.Add("retry", fmt.Sprintf("%d", int64(time.Duration(n.conf.Retry).Seconds())))

View File

@ -536,7 +536,7 @@ func (n *DedupStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Al
case 1: case 1:
entry = entries[0] entry = entries[0]
case 2: case 2:
return ctx, nil, fmt.Errorf("Unexpected entry result size %d", len(entries)) return ctx, nil, fmt.Errorf("unexpected entry result size %d", len(entries))
} }
if ok, err := n.needsUpdate(entry, firingSet, resolvedSet, repeatInterval); err != nil { if ok, err := n.needsUpdate(entry, firingSet, resolvedSet, repeatInterval); err != nil {
return ctx, nil, err return ctx, nil, err
@ -588,7 +588,7 @@ func (r RetryStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Ale
numFailedNotifications.WithLabelValues(r.integration.name).Inc() numFailedNotifications.WithLabelValues(r.integration.name).Inc()
level.Debug(l).Log("msg", "Notify attempt failed", "attempt", i, "integration", r.integration.name, "err", err) level.Debug(l).Log("msg", "Notify attempt failed", "attempt", i, "integration", r.integration.name, "err", err)
if !retry { if !retry {
return ctx, alerts, fmt.Errorf("Cancelling notify retry for %q due to unrecoverable error: %s", r.integration.name, err) return ctx, alerts, fmt.Errorf("cancelling notify retry for %q due to unrecoverable error: %s", r.integration.name, err)
} }
// Save this error to be able to return the last seen error by an // Save this error to be able to return the last seen error by an

View File

@ -112,7 +112,7 @@ route:
group_by: [] group_by: []
group_wait: 1s group_wait: 1s
group_interval: 1s group_interval: 1s
repeat_interval: 0s repeat_interval: 0s
receivers: receivers:
- name: "default" - name: "default"
@ -283,7 +283,7 @@ global:
route: route:
receiver: "default" receiver: "default"
group_by: [alertname] group_by: [alertname]
group_wait: 1s group_wait: 1s
group_interval: 5s group_interval: 5s
receivers: receivers:

View File

@ -82,7 +82,7 @@ func (m *memMarker) Count(states ...AlertState) int {
for _, status := range m.m { for _, status := range m.m {
for _, state := range states { for _, state := range states {
if status.State == state { if status.State == state {
count += 1 count++
} }
} }
} }