Skip to content

Commit

Permalink
use Go standard errors
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Nov 25, 2023
1 parent 62d11d0 commit cad7ecc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 204 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ executors:
# Whenever the Go version is updated here, .promu.yml should also be updated.
golang:
docker:
- image: cimg/go:1.19
- image: cimg/go:1.21
jobs:
test:
executor: golang
Expand Down
10 changes: 8 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 13 additions & 13 deletions cmd/getool/backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ 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"

Check failure on line 32 in cmd/getool/backfill.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/prometheus/graphite_exporter (goimports)
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/tsdb"
Expand Down Expand Up @@ -68,7 +67,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()
Expand Down Expand Up @@ -104,7 +103,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++
Expand All @@ -116,7 +115,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)
Expand All @@ -125,15 +124,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 {

Check failure on line 131 in cmd/getool/backfill.go

View workflow job for this annotation

GitHub Actions / lint

switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors (errorlint)
case 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 {
Expand All @@ -144,18 +143,16 @@ func createBlocks(input reader.DBReader, mint, maxt, blockDuration int64, maxSam
}
case 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) {
Expand Down Expand Up @@ -201,7 +198,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{}

Expand All @@ -214,7 +211,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) {
Expand All @@ -225,7 +225,7 @@ func backfillWhisper(inputDir, outputDir, mappingConfig string, strictMatch, hum
}

if err := os.MkdirAll(outputDir, 0777); err != nil {
return errors.Wrap(err, "create output dir")
return fmt.Errorf("create output dir: %w", err)
}

return backfill(5000, inputDir, outputDir, mappingConfig, strictMatch, humanReadable, blockDuration)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
module github.com/prometheus/graphite_exporter

go 1.17
go 1.21

require (
github.com/alecthomas/kingpin/v2 v2.3.2
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137
github.com/go-graphite/go-whisper v0.0.0-20230221134257-6774e38a461b
github.com/go-kit/log v0.2.1
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.15.1
github.com/prometheus/common v0.44.0
github.com/prometheus/exporter-toolkit v0.10.0
Expand All @@ -30,6 +29,7 @@ require (
github.com/matttproud/golang_protobuf_extensions v1.0.4 // 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.4.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
Expand Down
Loading

0 comments on commit cad7ecc

Please sign in to comment.