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 authored and matthiasr committed Mar 3, 2024
1 parent 8001a1c commit e4b07b5
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 31 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 @@ -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 (
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)
}

}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e4b07b5

Please sign in to comment.