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 Dec 6, 2023
1 parent 8e5d197 commit 8bab0c5
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 1,309 deletions.
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
38 changes: 20 additions & 18 deletions cmd/getool/backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"errors"
"fmt"
"math"
"os"
Expand All @@ -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_:]")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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++
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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{}

Expand All @@ -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) {
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/getool/backfill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions cmd/getool/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion cmd/graphite_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,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 (
Expand Down
6 changes: 3 additions & 3 deletions collector/collector_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
1 change: 0 additions & 1 deletion collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func TestParseNameAndTags(t *testing.T) {
}

func TestProcessLine(t *testing.T) {

type testCase struct {
line string
name string
Expand Down
1 change: 0 additions & 1 deletion e2e/issue90_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
27 changes: 13 additions & 14 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.4.0
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9
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.17.0
github.com/prometheus/common v0.45.0
github.com/prometheus/exporter-toolkit v0.10.0
Expand All @@ -21,28 +20,28 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/kit v0.10.0 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/golang/snappy v0.0.2 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.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.5.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 8bab0c5

Please sign in to comment.