Skip to content

Commit

Permalink
Revert move to github.com/efficientgo/core/errors for stdlib
Browse files Browse the repository at this point in the history
Signed-off-by: Saswata Mukherjee <[email protected]>
  • Loading branch information
saswatamcode committed Oct 16, 2023
1 parent 13ff836 commit e090658
Show file tree
Hide file tree
Showing 52 changed files with 205 additions and 171 deletions.
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ endef
lint: check-git deps format $(GOLANGCI_LINT) $(FAILLINT)
$(call require_clean_work_tree,'detected files without copyright, run make lint and commit changes')
@echo ">> verifying modules being imported"
@$(FAILLINT) -paths "errors=github.com/efficientgo/core/errors,\
github.com/pkg/errors=github.com/efficientgo/core/errors,\
github.com/prometheus/tsdb=github.com/prometheus/prometheus/tsdb,\
@$(FAILLINT) -paths "github.com/prometheus/tsdb=github.com/prometheus/prometheus/tsdb,\
github.com/prometheus/prometheus/pkg/testutils=github.com/thanos-io/thanos/pkg/testutil,\
github.com/prometheus/client_golang/prometheus.{DefaultGatherer,DefBuckets,NewUntypedFunc,UntypedFunc},\
github.com/prometheus/client_golang/prometheus.{NewCounter,NewCounterVec,NewCounterVec,NewGauge,NewGaugeVec,NewGaugeFunc,\
Expand All @@ -141,6 +139,6 @@ io/ioutil.{Discard,NopCloser,ReadAll,ReadDir,ReadFile,TempDir,TempFile,Writefile
@echo ">> linting all of the Go files GOGC=${GOGC}"
@$(GOLANGCI_LINT) run
# TODO(saswatamcode): Enable this in a separate commit.
@echo ">> ensuring Copyright headers $(MISS)"
# @echo ">> ensuring Copyright headers"
# @go run ./scripts/copyright
$(call require_clean_work_tree,'detected files without copyright, run make lint and commit changes')
23 changes: 12 additions & 11 deletions collectors/metrics/cmd/metrics-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
"syscall"
"time"

"github.com/efficientgo/core/errors"
"errors"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/oklog/run"
Expand Down Expand Up @@ -310,7 +311,7 @@ func (o *Options) Run() error {
cfg.Metrics = metrics
worker, err := forwarder.New(*cfg)
if err != nil {
return errors.Wrap(err, "failed to configure metrics collector")
return fmt.Errorf("failed to configure metrics collector: %w", err)
}

logger.Log(
Expand Down Expand Up @@ -364,7 +365,7 @@ func (o *Options) Run() error {
handlers.Handle("/federate", serveLastMetrics(o.Logger, worker))
l, err := net.Listen("tcp", o.Listen)
if err != nil {
return errors.Wrap(err, "failed to listen")
return fmt.Errorf("failed to listen: %w", err)
}

{
Expand Down Expand Up @@ -392,7 +393,7 @@ func (o *Options) Run() error {
if len(o.CollectRules) != 0 {
evaluator, err := collectrule.New(*cfg)
if err != nil {
return errors.Wrap(err, "failed to configure collect rule evaluator")
return fmt.Errorf("failed to configure collect rule evaluator: %w", err)
}
ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
Expand Down Expand Up @@ -428,7 +429,7 @@ func runMultiWorkers(o *Options, cfg *forwarder.Config) error {
for _, flag := range o.LabelFlag {
values := strings.SplitN(flag, "=", 2)
if len(values) != 2 {
return errors.Newf("--label must be of the form key=value: %s", flag)
return fmt.Errorf("--label must be of the form key=value: %s", flag)
}
if values[0] == "cluster" {
values[1] += "-" + fmt.Sprint(i)
Expand All @@ -446,7 +447,7 @@ func runMultiWorkers(o *Options, cfg *forwarder.Config) error {
forwardCfg.Metrics = cfg.Metrics
forwardWorker, err := forwarder.New(*forwardCfg)
if err != nil {
return errors.Wrap(err, "failed to configure metrics collector")
return fmt.Errorf("failed to configure metrics collector: %w", err)
}

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -467,7 +468,7 @@ func initConfig(o *Options) (error, *forwarder.Config) {
for _, flag := range o.LabelFlag {
values := strings.SplitN(flag, "=", 2)
if len(values) != 2 {
return errors.Newf("--label must be of the form key=value: %s", flag), nil
return fmt.Errorf("--label must be of the form key=value: %s", flag), nil
}
if o.Labels == nil {
o.Labels = make(map[string]string)
Expand All @@ -481,7 +482,7 @@ func initConfig(o *Options) (error, *forwarder.Config) {
}
values := strings.SplitN(flag, "=", 2)
if len(values) != 2 {
return errors.Newf("--rename must be of the form OLD_NAME=NEW_NAME: %s", flag), nil
return fmt.Errorf("--rename must be of the form OLD_NAME=NEW_NAME: %s", flag), nil
}
if o.Renames == nil {
o.Renames = make(map[string]string)
Expand All @@ -491,7 +492,7 @@ func initConfig(o *Options) (error, *forwarder.Config) {

from, err := url.Parse(o.From)
if err != nil {
return errors.Wrap(err, "--from is not a valid URL"), nil
return fmt.Errorf("--from is not a valid URL: %w", err), nil
}
from.Path = strings.TrimRight(from.Path, "/")
if len(from.Path) == 0 {
Expand All @@ -500,7 +501,7 @@ func initConfig(o *Options) (error, *forwarder.Config) {

fromQuery, err := url.Parse(o.FromQuery)
if err != nil {
return errors.Wrap(err, "--from-query is not a valid URL"), nil
return fmt.Errorf("--from-query is not a valid URL: %w", err), nil
}
fromQuery.Path = strings.TrimRight(fromQuery.Path, "/")
if len(fromQuery.Path) == 0 {
Expand All @@ -511,7 +512,7 @@ func initConfig(o *Options) (error, *forwarder.Config) {
if len(o.ToUpload) > 0 {
toUpload, err = url.Parse(o.ToUpload)
if err != nil {
return errors.Wrap(err, "--to-upload is not a valid URL"), nil
return fmt.Errorf("--to-upload is not a valid URL: %w", err), nil
}
}

Expand Down
7 changes: 3 additions & 4 deletions collectors/metrics/pkg/collectrule/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"sync"
"time"

"github.com/efficientgo/core/errors"
"github.com/go-kit/log"
clientmodel "github.com/prometheus/client_model/go"
"github.com/prometheus/prometheus/model/labels"
Expand Down Expand Up @@ -120,7 +119,7 @@ func New(cfg forwarder.Config) (*Evaluator, error) {
func (e *Evaluator) Reconfigure(cfg forwarder.Config) error {
evaluator, err := New(cfg)
if err != nil {
return errors.Wrap(err, "failed to reconfigure")
return fmt.Errorf("failed to reconfigure: %w", err)
}

e.lock.Lock()
Expand Down Expand Up @@ -205,7 +204,7 @@ func startWorker() error {
var err error
forwardWorker, err = forwarder.New(config)
if err != nil {
return errors.Wrap(err, "failed to configure forwarder for additional metrics")
return fmt.Errorf("failed to configure forwarder for additional metrics: %w", err)
}
var ctx context.Context
ctx, cancel = context.WithCancel(context.Background())
Expand All @@ -216,7 +215,7 @@ func startWorker() error {
} else {
err := forwardWorker.Reconfigure(config)
if err != nil {
return errors.Wrap(err, "failed to reconfigure forwarder for additional metrics")
return fmt.Errorf("failed to reconfigure forwarder for additional metrics: %w", err)
}
}

Expand Down
20 changes: 11 additions & 9 deletions collectors/metrics/pkg/forwarder/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
Expand All @@ -16,7 +17,8 @@ import (

"github.com/go-kit/log"

"github.com/efficientgo/core/errors"
"errors"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
clientmodel "github.com/prometheus/client_model/go"
Expand Down Expand Up @@ -110,11 +112,11 @@ func CreateFromClient(cfg Config, m *workerMetrics, interval time.Duration, name
}
pool, err := x509.SystemCertPool()
if err != nil {
return nil, errors.Wrap(err, "failed to read system certificates")
return nil, fmt.Errorf("failed to read system certificates: %w", err)
}
data, err := os.ReadFile(cfg.FromCAFile)
if err != nil {
return nil, errors.Wrap(err, "failed to read from-ca-file")
return nil, fmt.Errorf("failed to read from-ca-file: %w", err)
}
if !pool.AppendCertsFromPEM(data) {
rlogger.Log(logger, rlogger.Warn, "msg", "no certs found in from-ca-file")
Expand All @@ -138,7 +140,7 @@ func CreateFromClient(cfg Config, m *workerMetrics, interval time.Duration, name
if len(cfg.FromToken) == 0 && len(cfg.FromTokenFile) > 0 {
data, err := os.ReadFile(cfg.FromTokenFile)
if err != nil {
return nil, errors.Wrap(err, "unable to read from-token-file")
return nil, fmt.Errorf("unable to read from-token-file: %w", err)
}
cfg.FromToken = strings.TrimSpace(string(data))
}
Expand All @@ -161,7 +163,7 @@ func createClients(cfg Config, m *metricsclient.ClientMetrics, interval time.Dur
if len(cfg.AnonymizeSalt) == 0 && len(cfg.AnonymizeSaltFile) > 0 {
data, err := os.ReadFile(cfg.AnonymizeSaltFile)
if err != nil {
return nil, nil, transformer, errors.Wrap(err, "failed to read anonymize-salt-file")
return nil, nil, transformer, fmt.Errorf("failed to read anonymize-salt-file: %w", err)
}
anonymizeSalt = strings.TrimSpace(string(data))
}
Expand All @@ -188,7 +190,7 @@ func createClients(cfg Config, m *metricsclient.ClientMetrics, interval time.Dur

toTransport, err := metricsclient.MTLSTransport(logger, cfg.ToUploadCA, cfg.ToUploadCert, cfg.ToUploadKey)
if err != nil {
return nil, nil, transformer, errors.Wrap(err, "failed to create TLS transport")
return nil, nil, transformer, fmt.Errorf("failed to create TLS transport: %w", err)
}
toTransport.Proxy = http.ProxyFromEnvironment
toClient := &http.Client{Transport: toTransport}
Expand Down Expand Up @@ -271,7 +273,7 @@ func New(cfg Config) (*Worker, error) {
if len(cfg.RulesFile) > 0 {
data, err := os.ReadFile(cfg.RulesFile)
if err != nil {
return nil, errors.Wrap(err, "unable to read match-file")
return nil, fmt.Errorf("unable to read match-file: %w", err)
}
rules = append(rules, strings.Split(string(data), "\n")...)
}
Expand Down Expand Up @@ -301,7 +303,7 @@ func New(cfg Config) (*Worker, error) {

s, err := status.New(logger)
if err != nil {
return nil, errors.Wrap(err, "unable to create StatusReport")
return nil, fmt.Errorf("unable to create StatusReport: %w", err)
}
w.status = *s

Expand All @@ -313,7 +315,7 @@ func New(cfg Config) (*Worker, error) {
func (w *Worker) Reconfigure(cfg Config) error {
worker, err := New(cfg)
if err != nil {
return errors.Wrap(err, "failed to reconfigure")
return fmt.Errorf("failed to reconfigure: %w", err)
}

w.lock.Lock()
Expand Down
10 changes: 5 additions & 5 deletions collectors/metrics/pkg/metricfamily/drop_timestamp_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package metricfamily

import (
"fmt"
"testing"

"github.com/efficientgo/core/errors"
clientmodel "github.com/prometheus/client_model/go"
)

Expand All @@ -24,7 +24,7 @@ func TestDropTimestamp(t *testing.T) {
isOK := func(want bool) checkFunc {
return func(_ *clientmodel.MetricFamily, got bool, _ error) error {
if want != got {
return errors.Newf("want ok %t, got %t", want, got)
return fmt.Errorf("want ok %t, got %t", want, got)
}
return nil
}
Expand All @@ -33,7 +33,7 @@ func TestDropTimestamp(t *testing.T) {
hasErr := func(want error) checkFunc {
return func(_ *clientmodel.MetricFamily, _ bool, got error) error {
if want != got {
return errors.Newf("want err %v, got %v", want, got)
return fmt.Errorf("want err %v, got %v", want, got)
}
return nil
}
Expand All @@ -42,7 +42,7 @@ func TestDropTimestamp(t *testing.T) {
hasMetrics := func(want int) checkFunc {
return func(m *clientmodel.MetricFamily, _ bool, _ error) error {
if got := len(m.Metric); want != got {
return errors.Newf("want len(m.Metric)=%v, got %v", want, got)
return fmt.Errorf("want len(m.Metric)=%v, got %v", want, got)
}
return nil
}
Expand All @@ -52,7 +52,7 @@ func TestDropTimestamp(t *testing.T) {
return func(m *clientmodel.MetricFamily, _ bool, _ error) error {
for _, metric := range m.Metric {
if got := metric.TimestampMs != nil; want != got {
return errors.Newf("want metrics to have timestamp %t, got %t", want, got)
return fmt.Errorf("want metrics to have timestamp %t, got %t", want, got)
}
}
return nil
Expand Down
14 changes: 7 additions & 7 deletions collectors/metrics/pkg/metricfamily/elide_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package metricfamily

import (
"fmt"
"testing"

"github.com/efficientgo/core/errors"
"github.com/golang/protobuf/proto"
clientmodel "github.com/prometheus/client_model/go"
)
Expand All @@ -20,7 +20,7 @@ func TestElide(t *testing.T) {
isOK := func(want bool) checkFunc {
return func(_ *clientmodel.MetricFamily, got bool, _ error) error {
if want != got {
return errors.Newf("want ok %t, got %t", want, got)
return fmt.Errorf("want ok %t, got %t", want, got)
}
return nil
}
Expand All @@ -29,7 +29,7 @@ func TestElide(t *testing.T) {
hasErr := func(want error) checkFunc {
return func(_ *clientmodel.MetricFamily, _ bool, got error) error {
if want != got {
return errors.Newf("want err %v, got %v", want, got)
return fmt.Errorf("want err %v, got %v", want, got)
}
return nil
}
Expand All @@ -38,7 +38,7 @@ func TestElide(t *testing.T) {
metricIsNil := func(want bool) checkFunc {
return func(m *clientmodel.MetricFamily, _ bool, _ error) error {
if got := m == nil; want != got {
return errors.Newf("want metric to be nil=%t, got %t", want, got)
return fmt.Errorf("want metric to be nil=%t, got %t", want, got)
}
return nil
}
Expand All @@ -47,7 +47,7 @@ func TestElide(t *testing.T) {
hasMetricCount := func(want int) checkFunc {
return func(m *clientmodel.MetricFamily, _ bool, _ error) error {
if got := len(m.Metric); want != got {
return errors.Newf("want len(m.Metric)=%v, got %v", want, got)
return fmt.Errorf("want len(m.Metric)=%v, got %v", want, got)
}
return nil
}
Expand All @@ -57,7 +57,7 @@ func TestElide(t *testing.T) {
return func(family *clientmodel.MetricFamily, _ bool, _ error) error {
for i := range family.Metric {
if got := len(family.Metric[i].Label); got != want[i] {
return errors.Newf(
return fmt.Errorf(
"want len(m.Metric[%v].Label)=%v, got %v",
i, want[i], got)
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestElide(t *testing.T) {
gots = "isn't"
}

return errors.Newf(
return fmt.Errorf(
"want label %q be %s in metrics, but it %s",
label, wants, gots,
)
Expand Down
6 changes: 4 additions & 2 deletions collectors/metrics/pkg/metricfamily/hypershift_transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package metricfamily

import (
"context"
"fmt"
"os"

"github.com/efficientgo/core/errors"
"errors"

"github.com/go-kit/log"
hyperv1 "github.com/openshift/hypershift/api/v1alpha1"
prom "github.com/prometheus/client_model/go"
Expand Down Expand Up @@ -129,7 +131,7 @@ func getClusterName(h *hypershiftTransformer, id string) (string, error) {
}
clusterName, ok = h.hostedClusters[id]
if !ok {
return "", errors.Newf("failed to find HostedCluster with id: %s", id)
return "", fmt.Errorf("failed to find HostedCluster with id: %s", id)
}
}
return clusterName, nil
Expand Down
Loading

0 comments on commit e090658

Please sign in to comment.