From e4b07b5a53674d77a052d3207136a88913b604f6 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Sat, 25 Nov 2023 15:45:47 +0100 Subject: [PATCH] use Go standard errors Signed-off-by: Matthieu MOREL --- .golangci.yml | 10 +++++-- cmd/getool/backfill.go | 38 ++++++++++++++------------- cmd/getool/backfill_test.go | 4 +-- cmd/getool/main_test.go | 6 +++-- cmd/graphite_exporter/main.go | 3 ++- collector/collector_benchmark_test.go | 6 ++--- collector/collector_test.go | 1 - e2e/issue90_test.go | 1 - go.mod | 2 +- 9 files changed, 40 insertions(+), 31 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 55f7c9a8..73090e89 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,6 +5,12 @@ run: # Run only staticcheck and goimports for now. Additional linters will be enabled one-by-one. linters: enable: - - staticcheck - - goimports + - errorlint + - gofumpt + - goimports + - staticcheck disable-all: true + +linters-settings: + goimports: + local-prefixes: github.com/prometheus/graphite_exporter diff --git a/cmd/getool/backfill.go b/cmd/getool/backfill.go index 3fb37456..05ed21d0 100644 --- a/cmd/getool/backfill.go +++ b/cmd/getool/backfill.go @@ -18,6 +18,7 @@ package main import ( "context" + "errors" "fmt" "math" "os" @@ -29,12 +30,12 @@ import ( "github.com/alecthomas/units" "github.com/go-kit/log" "github.com/go-kit/log/level" - "github.com/pkg/errors" - "github.com/prometheus/graphite_exporter/reader" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb" tsdb_errors "github.com/prometheus/prometheus/tsdb/errors" "github.com/prometheus/statsd_exporter/pkg/mapper" + + "github.com/prometheus/graphite_exporter/reader" ) var invalidMetricChars = regexp.MustCompile("[^a-zA-Z0-9_:]") @@ -68,7 +69,7 @@ func createBlocks(input reader.DBReader, mint, maxt, blockDuration int64, maxSam // original interval later. w, err := tsdb.NewBlockWriter(log.NewNopLogger(), outputDir, 2*blockDuration) if err != nil { - return errors.Wrap(err, "block writer") + return fmt.Errorf("block writer: %w", err) } defer func() { err = tsdb_errors.NewMulti(err, w.Close()).Err() @@ -104,7 +105,7 @@ func createBlocks(input reader.DBReader, mint, maxt, blockDuration int64, maxSam } for _, point := range points { if _, err := app.Add(l, point.Timestamp, point.Value); err != nil { - return errors.Wrap(err, "add sample") + return fmt.Errorf("add sample: %w", err) } samplesCount++ @@ -116,7 +117,7 @@ func createBlocks(input reader.DBReader, mint, maxt, blockDuration int64, maxSam // Therefore the old appender is committed and a new one is created. // This prevents keeping too many samples lined up in an appender and thus in RAM. if err := app.Commit(); err != nil { - return errors.Wrap(err, "commit") + return fmt.Errorf("commit: %w", err) } app = w.Appender(ctx) @@ -125,15 +126,15 @@ func createBlocks(input reader.DBReader, mint, maxt, blockDuration int64, maxSam } if err := app.Commit(); err != nil { - return errors.Wrap(err, "commit") + return fmt.Errorf("commit: %w", err) } block, err := w.Flush(ctx) - switch err { - case nil: + switch { + case err == nil: blocks, err := db.Blocks() if err != nil { - return errors.Wrap(err, "get blocks") + return fmt.Errorf("get blocks: %w", err) } for _, b := range blocks { if b.Meta().ULID == block { @@ -142,20 +143,18 @@ func createBlocks(input reader.DBReader, mint, maxt, blockDuration int64, maxSam break } } - case tsdb.ErrNoSeriesAppended: + case errors.Is(err, tsdb.ErrNoSeriesAppended): default: - return errors.Wrap(err, "flush") + return fmt.Errorf("flush: %w", err) } return nil }() - if err != nil { - return errors.Wrap(err, "process blocks") + return fmt.Errorf("process blocks: %w", err) } } return nil - } func printBlocks(blocks []tsdb.BlockReader, writeHeader, humanReadable bool) { @@ -201,7 +200,7 @@ func backfill(maxSamplesInAppender int, inputDir, outputDir, mappingConfig strin wdb := reader.NewReader(inputDir) mint, maxt, err := wdb.GetMinAndMaxTimestamps() if err != nil { - return errors.Wrap(err, "getting min and max timestamp") + return fmt.Errorf("getting min and max timestamp: %w", err) } metricMapper := &mapper.MetricMapper{} @@ -214,7 +213,10 @@ func backfill(maxSamplesInAppender int, inputDir, outputDir, mappingConfig strin } } - return errors.Wrap(createBlocks(wdb, mint, maxt, blockDuration, maxSamplesInAppender, outputDir, metricMapper, strictMatch, humanReadable), "block creation") + if err := createBlocks(wdb, mint, maxt, blockDuration, maxSamplesInAppender, outputDir, metricMapper, strictMatch, humanReadable); err != nil { + return fmt.Errorf("block creation: %w", err) + } + return nil } func backfillWhisper(inputDir, outputDir, mappingConfig string, strictMatch, humanReadable bool, optBlockDuration time.Duration) (err error) { @@ -224,8 +226,8 @@ func backfillWhisper(inputDir, outputDir, mappingConfig string, strictMatch, hum return fmt.Errorf("invalid block duration: %s", optBlockDuration.String()) } - if err := os.MkdirAll(outputDir, 0777); err != nil { - return errors.Wrap(err, "create output dir") + if err := os.MkdirAll(outputDir, 0o777); err != nil { + return fmt.Errorf("create output dir: %w", err) } return backfill(5000, inputDir, outputDir, mappingConfig, strictMatch, humanReadable, blockDuration) diff --git a/cmd/getool/backfill_test.go b/cmd/getool/backfill_test.go index c18da241..ef3b007c 100644 --- a/cmd/getool/backfill_test.go +++ b/cmd/getool/backfill_test.go @@ -43,7 +43,7 @@ func TestBackfill(t *testing.T) { whisperDir = filepath.Join(tmpData, "whisper", "load", "cpu") ) - require.NoError(t, os.MkdirAll(whisperDir, 0777)) + require.NoError(t, os.MkdirAll(whisperDir, 0o777)) retentions, err := whisper.ParseRetentionDefs("1s:3600") require.NoError(t, err) wsp, err := whisper.Create(filepath.Join(whisperDir, "cpu0.wsp"), retentions, whisper.Sum, 0.5) @@ -68,7 +68,7 @@ func TestBackfill(t *testing.T) { err = cmd.Wait() require.NoError(t, err) - require.NoError(t, os.MkdirAll(filepath.Join(tmpData, "data", "wal"), 0777)) + require.NoError(t, os.MkdirAll(filepath.Join(tmpData, "data", "wal"), 0o777)) db, err := tsdb.OpenDBReadOnly(filepath.Join(tmpData, "data"), nil) require.NoError(t, err) diff --git a/cmd/getool/main_test.go b/cmd/getool/main_test.go index 94eb4bc5..437e38c0 100644 --- a/cmd/getool/main_test.go +++ b/cmd/getool/main_test.go @@ -19,8 +19,10 @@ import ( "testing" ) -var testPath = os.Args[0] -var tmpData = filepath.Join(os.TempDir(), "graphite_expoter_test") +var ( + testPath = os.Args[0] + tmpData = filepath.Join(os.TempDir(), "graphite_expoter_test") +) func TestMain(m *testing.M) { for i, arg := range os.Args { diff --git a/cmd/graphite_exporter/main.go b/cmd/graphite_exporter/main.go index 4d05f496..0ef5c65c 100644 --- a/cmd/graphite_exporter/main.go +++ b/cmd/graphite_exporter/main.go @@ -33,10 +33,11 @@ import ( "github.com/prometheus/common/version" "github.com/prometheus/exporter-toolkit/web" "github.com/prometheus/exporter-toolkit/web/kingpinflag" - "github.com/prometheus/graphite_exporter/collector" "github.com/prometheus/statsd_exporter/pkg/mapper" "github.com/prometheus/statsd_exporter/pkg/mappercache/lru" "github.com/prometheus/statsd_exporter/pkg/mappercache/randomreplacement" + + "github.com/prometheus/graphite_exporter/collector" ) var ( diff --git a/collector/collector_benchmark_test.go b/collector/collector_benchmark_test.go index 16a56028..91b3b105 100644 --- a/collector/collector_benchmark_test.go +++ b/collector/collector_benchmark_test.go @@ -65,11 +65,8 @@ type mockMapper struct { } func (m *mockMapper) GetMapping(metricName string, metricType mapper.MetricType) (*mapper.MetricMapping, prometheus.Labels, bool) { - mapping := mapper.MetricMapping{Name: m.name, Action: m.action} - return &mapping, m.labels, m.present - } func (m *mockMapper) InitFromFile(string) error { @@ -106,9 +103,11 @@ func benchmarkProcessLine(b *testing.B, line string) { func BenchmarkProcessLineMixed1(b *testing.B) { benchmarkProcessLines(1, b, input) } + func BenchmarkProcessLineMixed5(b *testing.B) { benchmarkProcessLines(5, b, input) } + func BenchmarkProcessLineMixed50(b *testing.B) { benchmarkProcessLines(50, b, input) } @@ -117,6 +116,7 @@ func BenchmarkProcessLineMixed50(b *testing.B) { func BenchmarkProcessLineUntagged(b *testing.B) { benchmarkProcessLine(b, untaggedLine) } + func BenchmarkProcessLineTagged(b *testing.B) { benchmarkProcessLine(b, taggedLine) } diff --git a/collector/collector_test.go b/collector/collector_test.go index 17d5428e..9e773502 100644 --- a/collector/collector_test.go +++ b/collector/collector_test.go @@ -75,7 +75,6 @@ func TestParseNameAndTags(t *testing.T) { } func TestProcessLine(t *testing.T) { - type testCase struct { line string name string diff --git a/e2e/issue90_test.go b/e2e/issue90_test.go index 80b7b4ab..37545792 100644 --- a/e2e/issue90_test.go +++ b/e2e/issue90_test.go @@ -98,5 +98,4 @@ func TestIssue90(t *testing.T) { if resp.StatusCode != 200 { t.Errorf("unexpected status, want 200, got %v, body: %s", resp.Status, b) } - } diff --git a/go.mod b/go.mod index ec782c4f..60364087 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ require ( github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 github.com/go-graphite/go-whisper v0.0.0-20230526115116-e3110f57c01c github.com/go-kit/log v0.2.1 - github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.19.0 github.com/prometheus/common v0.49.0 github.com/prometheus/exporter-toolkit v0.11.0 @@ -31,6 +30,7 @@ require ( github.com/jpillora/backoff v1.0.0 // indirect github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect github.com/oklog/ulid v1.3.1 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_model v0.6.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect