Skip to content

Commit

Permalink
Update golangci-lint and fix complaints (prometheus#2853)
Browse files Browse the repository at this point in the history
* Copy latest golangci-lint files from Prometheus

Signed-off-by: Matthias Loibl <[email protected]>

* Use grafana/regexp over stdlib regexp

Signed-off-by: Matthias Loibl <[email protected]>

* Fix typos in comments

Signed-off-by: Matthias Loibl <[email protected]>

* Fix goimports complains in import sorting

Signed-off-by: Matthias Loibl <[email protected]>

* gofumpt all Go files

Signed-off-by: Matthias Loibl <[email protected]>

* Update naming to comply with revive linter

Signed-off-by: Matthias Loibl <[email protected]>

* config: Fix error messages to be lower case

Signed-off-by: Matthias Loibl <[email protected]>

* test/cli: Fix error messages to be lower case

Signed-off-by: Matthias Loibl <[email protected]>

* .golangci.yaml: Remove obsolete space

Signed-off-by: Matthias Loibl <[email protected]>

* config: Fix expected victorOps error

Signed-off-by: Matthias Loibl <[email protected]>

* Use stdlib regexp

Signed-off-by: Matthias Loibl <[email protected]>

* Clean up Go modules

Signed-off-by: Matthias Loibl <[email protected]>
  • Loading branch information
metalmatze authored Mar 25, 2022
1 parent 985c45a commit a6d10bd
Show file tree
Hide file tree
Showing 74 changed files with 306 additions and 275 deletions.
49 changes: 48 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,58 @@
---
run:
deadline: 5m
skip-files:
# Skip autogenerated files.
- ^.*\.(pb|y)\.go$

output:
sort-results: true

linters:
enable:
- depguard
- gofumpt
- goimports
- revive
- misspell

issues:
exclude-rules:
- path: _test.go
linters:
- errcheck

linters-settings:
depguard:
list-type: blacklist
include-go-root: true
packages-with-error-message:
- sync/atomic: "Use go.uber.org/atomic instead of sync/atomic"
- github.com/stretchr/testify/assert: "Use github.com/stretchr/testify/require instead of github.com/stretchr/testify/assert"
- github.com/go-kit/kit/log: "Use github.com/go-kit/log instead of github.com/go-kit/kit/log"
errcheck:
exclude: scripts/errcheck_excludes.txt
goimports:
local-prefixes: github.com/prometheus/alertmanager
gofumpt:
extra-rules: true
revive:
rules:
- name: exported
arguments: ["disableStutteringCheck"]
- name: blank-imports
- name: context-as-argument
- name: dot-imports
- name: error-return
- name: error-strings
- name: error-naming
- name: if-return
- name: increment-decrement
- name: var-naming
- name: var-declaration
- name: package-comments
- name: range
- name: receiver-naming
- name: time-naming
- name: unexported-return
- name: indent-error-flow
- name: errorf
11 changes: 5 additions & 6 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import (
"runtime"
"time"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/common/route"

apiv1 "github.com/prometheus/alertmanager/api/v1"
apiv2 "github.com/prometheus/alertmanager/api/v2"
"github.com/prometheus/alertmanager/cluster"
Expand All @@ -28,11 +33,6 @@ import (
"github.com/prometheus/alertmanager/provider"
"github.com/prometheus/alertmanager/silence"
"github.com/prometheus/alertmanager/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/common/route"

"github.com/go-kit/log"
)

// API represents all APIs of Alertmanager.
Expand Down Expand Up @@ -128,7 +128,6 @@ func New(opts Options) (*API, error) {
log.With(l, "version", "v2"),
opts.Registry,
)

if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (api *API) receivers(w http.ResponseWriter, req *http.Request) {
func (api *API) status(w http.ResponseWriter, req *http.Request) {
api.mtx.RLock()

var status = struct {
status := struct {
ConfigYAML string `json:"configYAML"`
ConfigJSON *config.Config `json:"configJSON"`
VersionInfo map[string]string `json:"versionInfo"`
Expand Down Expand Up @@ -618,13 +618,13 @@ func (api *API) listSilences(w http.ResponseWriter, r *http.Request) {
}
}

sort.Slice(active, func(i int, j int) bool {
sort.Slice(active, func(i, j int) bool {
return active[i].EndsAt.Before(active[j].EndsAt)
})
sort.Slice(pending, func(i int, j int) bool {
sort.Slice(pending, func(i, j int) bool {
return pending[i].StartsAt.Before(pending[j].EndsAt)
})
sort.Slice(expired, func(i int, j int) bool {
sort.Slice(expired, func(i, j int) bool {
return expired[i].EndsAt.After(expired[j].EndsAt)
})

Expand Down
11 changes: 6 additions & 5 deletions api/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (f *fakeAlerts) Get(model.Fingerprint) (*types.Alert, error) { return nil,
func (f *fakeAlerts) Put(alerts ...*types.Alert) error {
return f.err
}

func (f *fakeAlerts) GetPending() provider.AlertIterator {
ch := make(chan *types.Alert)
done := make(chan struct{})
Expand Down Expand Up @@ -158,31 +159,31 @@ func TestAddAlerts(t *testing.T) {
func TestListAlerts(t *testing.T) {
now := time.Now()
alerts := []*types.Alert{
&types.Alert{
{
Alert: model.Alert{
Labels: model.LabelSet{"state": "active", "alertname": "alert1"},
StartsAt: now.Add(-time.Minute),
},
},
&types.Alert{
{
Alert: model.Alert{
Labels: model.LabelSet{"state": "unprocessed", "alertname": "alert2"},
StartsAt: now.Add(-time.Minute),
},
},
&types.Alert{
{
Alert: model.Alert{
Labels: model.LabelSet{"state": "suppressed", "silenced_by": "abc", "alertname": "alert3"},
StartsAt: now.Add(-time.Minute),
},
},
&types.Alert{
{
Alert: model.Alert{
Labels: model.LabelSet{"state": "suppressed", "inhibited_by": "abc", "alertname": "alert4"},
StartsAt: now.Add(-time.Minute),
},
},
&types.Alert{
{
Alert: model.Alert{
Labels: model.LabelSet{"alertname": "alert5"},
StartsAt: now.Add(-2 * time.Minute),
Expand Down
20 changes: 10 additions & 10 deletions api/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ type API struct {
Handler http.Handler
}

type groupsFn func(func(*dispatch.Route) bool, func(*types.Alert, time.Time) bool) (dispatch.AlertGroups, map[prometheus_model.Fingerprint][]string)
type getAlertStatusFn func(prometheus_model.Fingerprint) types.AlertStatus
type setAlertStatusFn func(prometheus_model.LabelSet)
type (
groupsFn func(func(*dispatch.Route) bool, func(*types.Alert, time.Time) bool) (dispatch.AlertGroups, map[prometheus_model.Fingerprint][]string)
getAlertStatusFn func(prometheus_model.Fingerprint) types.AlertStatus
setAlertStatusFn func(prometheus_model.LabelSet)
)

// NewAPI returns a new Alertmanager API v2
func NewAPI(
Expand Down Expand Up @@ -524,13 +526,11 @@ func (api *API) getSilencesHandler(params silence_ops.GetSilencesParams) middlew
return silence_ops.NewGetSilencesOK().WithPayload(sils)
}

var (
silenceStateOrder = map[types.SilenceState]int{
types.SilenceStateActive: 1,
types.SilenceStatePending: 2,
types.SilenceStateExpired: 3,
}
)
var silenceStateOrder = map[types.SilenceState]int{
types.SilenceStateActive: 1,
types.SilenceStatePending: 2,
types.SilenceStateExpired: 3,
}

// SortSilences sorts first according to the state "active, pending, expired"
// then by end time or start time depending on the state.
Expand Down
16 changes: 7 additions & 9 deletions api/v2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestGetStatusHandlerWithNilPeer(t *testing.T) {
}
}

func assertEqualStrings(t *testing.T, expected string, actual string) {
func assertEqualStrings(t *testing.T, expected, actual string) {
if expected != actual {
t.Fatal("expected: ", expected, ", actual: ", actual)
}
Expand All @@ -90,10 +90,9 @@ func newSilences(t *testing.T) *silence.Silences {
return silences
}

func gettableSilence(id string, state string,
updatedAt string, start string, end string,
func gettableSilence(id, state string,
updatedAt, start, end string,
) *open_api_models.GettableSilence {

updAt, err := strfmt.ParseDateTime(updatedAt)
if err != nil {
panic(err)
Expand Down Expand Up @@ -122,7 +121,6 @@ func gettableSilence(id string, state string,
}

func TestGetSilencesHandler(t *testing.T) {

updateTime := "2019-01-01T12:00:00+00:00"
silences := []*open_api_models.GettableSilence{
gettableSilence("silence-6-expired", "expired", updateTime,
Expand Down Expand Up @@ -313,15 +311,15 @@ func TestPostSilencesHandler(t *testing.T) {
}
}

func createSilenceMatcher(name string, pattern string, matcherType silencepb.Matcher_Type) *silencepb.Matcher {
func createSilenceMatcher(name, pattern string, matcherType silencepb.Matcher_Type) *silencepb.Matcher {
return &silencepb.Matcher{
Name: name,
Pattern: pattern,
Type: matcherType,
}
}

func createLabelMatcher(name string, value string, matchType labels.MatchType) *labels.Matcher {
func createLabelMatcher(name, value string, matchType labels.MatchType) *labels.Matcher {
matcher, _ := labels.NewMatcher(matchType, name, value)
return matcher
}
Expand Down Expand Up @@ -453,8 +451,8 @@ func TestAlertToOpenAPIAlert(t *testing.T) {
UpdatedAt: convertDateTime(updated),
Fingerprint: &fp,
Receivers: []*open_api_models.Receiver{
&open_api_models.Receiver{Name: &receivers[0]},
&open_api_models.Receiver{Name: &receivers[1]},
{Name: &receivers[0]},
{Name: &receivers[1]},
},
}, openAPIAlert)
}
Expand Down
3 changes: 2 additions & 1 deletion api/v2/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (
"time"

"github.com/go-openapi/strfmt"
prometheus_model "github.com/prometheus/common/model"

open_api_models "github.com/prometheus/alertmanager/api/v2/models"
"github.com/prometheus/alertmanager/silence/silencepb"
"github.com/prometheus/alertmanager/types"
prometheus_model "github.com/prometheus/common/model"
)

// GettableSilenceFromProto converts *silencepb.Silence to open_api_models.GettableSilence.
Expand Down
5 changes: 2 additions & 3 deletions cli/alert_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ package cli

import (
"context"

"fmt"
"time"

"github.com/go-openapi/strfmt"
"gopkg.in/alecthomas/kingpin.v2"

"github.com/prometheus/alertmanager/api/v2/client/alert"
"github.com/prometheus/alertmanager/api/v2/models"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

type alertAddCmd struct {
Expand Down Expand Up @@ -70,7 +70,6 @@ func configureAddAlertCmd(cc *kingpin.CmdClause) {
}

func (a *alertAddCmd) addAlert(ctx context.Context, _ *kingpin.ParseContext) error {

if len(a.labels) > 0 {
// Allow the alertname label to be defined implicitly as the first argument rather
// than explicitly as a key=value pair.
Expand Down
3 changes: 1 addition & 2 deletions cli/alert_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"errors"
"fmt"

kingpin "gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/alecthomas/kingpin.v2"

"github.com/prometheus/alertmanager/api/v2/client/alert"
"github.com/prometheus/alertmanager/cli/format"
Expand Down Expand Up @@ -102,7 +102,6 @@ func (a *alertQueryCmd) queryAlerts(ctx context.Context, _ *kingpin.ParseContext
amclient := NewAlertmanagerClient(alertmanagerURL)

getOk, err := amclient.Alert.GetAlerts(alertParams)

if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cli/check_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (
"fmt"
"os"

"gopkg.in/alecthomas/kingpin.v2"

"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/template"
"gopkg.in/alecthomas/kingpin.v2"
)

// TODO: This can just be a type that is []string, doesn't have to be a struct
Expand Down
8 changes: 3 additions & 5 deletions cli/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ import (
"io"
"time"

kingpin "gopkg.in/alecthomas/kingpin.v2"

"github.com/go-openapi/strfmt"
"gopkg.in/alecthomas/kingpin.v2"

"github.com/prometheus/alertmanager/api/v2/models"
"github.com/prometheus/alertmanager/pkg/labels"
)

const DefaultDateFormat = "2006-01-02 15:04:05 MST"

var (
dateFormat *string
)
var dateFormat *string

func InitFormatFlags(app *kingpin.Application) {
dateFormat = app.Flag("date.format", "Format of date output").Default(DefaultDateFormat).String()
Expand Down
3 changes: 1 addition & 2 deletions cli/format/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func (s ByAddress) Less(i, j int) bool {
p1, _ := strconv.Atoi(port1)
p2, _ := strconv.Atoi(port2)
return p1 < p2
} else {
return bytes.Compare(net.ParseIP(ip1), net.ParseIP(ip2)) < 0
}
return bytes.Compare(net.ParseIP(ip1), net.ParseIP(ip2)) < 0
}
6 changes: 2 additions & 4 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
promconfig "github.com/prometheus/common/config"
"github.com/prometheus/common/version"
"golang.org/x/mod/semver"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/alecthomas/kingpin.v2"

"github.com/prometheus/alertmanager/api/v2/client"
"github.com/prometheus/alertmanager/cli/config"
Expand Down Expand Up @@ -127,9 +127,7 @@ func NewAlertmanagerClient(amURL *url.URL) *client.Alertmanager {

// Execute is the main function for the amtool command
func Execute() {
var (
app = kingpin.New("amtool", helpRoot).UsageWriter(os.Stdout)
)
app := kingpin.New("amtool", helpRoot).UsageWriter(os.Stdout)

format.InitFormatFlags(app)

Expand Down
4 changes: 2 additions & 2 deletions cli/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
"fmt"

"github.com/xlab/treeprint"
"gopkg.in/alecthomas/kingpin.v2"

"github.com/prometheus/alertmanager/api/v2/models"
"github.com/prometheus/alertmanager/dispatch"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

type routingShow struct {
Expand Down Expand Up @@ -75,7 +75,7 @@ func (c *routingShow) routingShowAction(ctx context.Context, _ *kingpin.ParseCon
return nil
}

func getRouteTreeSlug(route *dispatch.Route, showContinue bool, showReceiver bool) string {
func getRouteTreeSlug(route *dispatch.Route, showContinue, showReceiver bool) string {
var branchSlug bytes.Buffer
if route.Matchers.Len() == 0 {
branchSlug.WriteString("default-route")
Expand Down
Loading

0 comments on commit a6d10bd

Please sign in to comment.