Repository: yarpc/yab
Pull request #356 (2022-04-04 16:37:08)

cactus/go-statsd-client: Upgrade to v5
The version of the go-statsd-client we're using is woefully old.
Upgrade to the latest version (v5).
There's a [changelog here][1], but the only change that affects us is
that the new NewNoopClient constructor was deleted.
A nil `*statsd.Client` is a no-op client instead.

[1]: https://github.com/cactus/go-statsd-client/blob/master/CHANGELOG.md

The deleted ensureStatsd function existed only to ensure that the
`NewNoopClient() (Statter, error)` function's error was handled cleanly.
Since constructing a nil `*statsd.Client` is free and will not fail, the
function is no longer necessary.

Depends on #355



Patch:
diff --git a/go.mod b/go.mod
index 83aec0b..b7590c4 100644
--- a/go.mod
+++ b/go.mod
@@ -4,7 +4,7 @@ go 1.18
 
 require (
 	github.com/apache/thrift v0.0.0-20161221203622-b2a4d4ae21c7
-	github.com/cactus/go-statsd-client v3.1.2-0.20180516050940-5e3c483eb932+incompatible
+	github.com/cactus/go-statsd-client/v5 v5.0.0
 	github.com/casimir/xdg-go v0.0.0-20160329195404-372ccc2180da
 	github.com/ghodss/yaml v1.0.0
 	github.com/golang/protobuf v1.3.2
diff --git a/go.sum b/go.sum
index 5a2c76f..f4752e1 100644
--- a/go.sum
+++ b/go.sum
@@ -18,8 +18,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
 github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
 github.com/bmizerany/perks v0.0.0-20141205001514-d9a9656a3a4b h1:AP/Y7sqYicnjGDfD5VcY4CIfh1hRXBUavxrvELjTiOE=
 github.com/bmizerany/perks v0.0.0-20141205001514-d9a9656a3a4b/go.mod h1:ac9efd0D1fsDb3EJvhqgXRbFx7bs2wqZ10HQPeU8U/Q=
-github.com/cactus/go-statsd-client v3.1.2-0.20180516050940-5e3c483eb932+incompatible h1:1np1y0X7FceoIF7FVo89cgptEpXP5Y9PuH8iRVB+BVM=
-github.com/cactus/go-statsd-client v3.1.2-0.20180516050940-5e3c483eb932+incompatible/go.mod h1:cMRcwZDklk7hXp+Law83urTHUiHMzCev/r4JMYr/zU0=
+github.com/cactus/go-statsd-client/v5 v5.0.0 h1:KqvIQtc9qt34uq+nu4nd1PwingWfBt/IISgtUQ2nSJk=
+github.com/cactus/go-statsd-client/v5 v5.0.0/go.mod h1:COEvJ1E+/E2L4q6QE5CkjWPi4eeDw9maJBMIuMPBZbY=
 github.com/casimir/xdg-go v0.0.0-20160329195404-372ccc2180da h1:hjpZV7G49m1bly++F+Gho1Sbf2+eBW/eTLJWuRkH9Uc=
 github.com/casimir/xdg-go v0.0.0-20160329195404-372ccc2180da/go.mod h1:dywSSi3sMtJn2IjiYfJciP9tjVVeIVRa7AE7N5WLUBo=
 github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
@@ -66,6 +66,7 @@ github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
 github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
 github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
+github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
 github.com/jessevdk/go-flags v1.4.1-0.20180331124232-1c38ed7ad0cc h1:unqembev+qAeQDOhvsahBm2Xla2M/cfQvjl+0MhkpOM=
 github.com/jessevdk/go-flags v1.4.1-0.20180331124232-1c38ed7ad0cc/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
 github.com/jhump/protoreflect v0.0.0-20180908113807-a84568470d8a h1:r9BzTkXPj6nOg6H8Dhd1ToeGirTNXOBqBu2dJaYwub4=
diff --git a/statsd/statsd.go b/statsd/statsd.go
index fa1f6e9..24f6731 100644
--- a/statsd/statsd.go
+++ b/statsd/statsd.go
@@ -26,9 +26,8 @@ import (
 	"regexp"
 	"time"
 
+	"github.com/cactus/go-statsd-client/v5/statsd"
 	"go.uber.org/zap"
-
-	"github.com/cactus/go-statsd-client/statsd"
 )
 
 // Client is the statsd interface that we use.
@@ -38,7 +37,14 @@ type Client interface {
 }
 
 // Noop is a no-op statsd client.
-var Noop Client = client{ensureStatsd(statsd.NewNoopClient())}
+var Noop Client = client{newNoopStatter()}
+
+func newNoopStatter() statsd.Statter {
+	// A nil *statsd.Client is a valid no-op statsd.Statter.
+	// This will convert to a valid non-nil statsd.Statter value.
+	var client *statsd.Client
+	return client
+}
 
 var newStatsD = statsd.NewBufferedClient
 
@@ -54,13 +60,6 @@ func (c client) Timing(stat string, d time.Duration) {
 	c.statter.TimingDuration(stat, d, 1.0)
 }
 
-func ensureStatsd(statter statsd.Statter, err error) statsd.Statter {
-	if err != nil {
-		panic(err)
-	}
-	return statter
-}
-
 func createStatsd(statsdHostPort, service, method string) (statsd.Statter, error) {
 	user := os.Getenv("USER")
 	r := regexp.MustCompile(`[^a-zA-Z0-9]`)


Test patch:
diff --git a/statsd/statsd_test.go b/statsd/statsd_test.go
index e9a0425..ca0a43e 100644
--- a/statsd/statsd_test.go
+++ b/statsd/statsd_test.go
@@ -28,7 +28,7 @@ import (
 
 	"go.uber.org/zap"
 
-	"github.com/cactus/go-statsd-client/statsd"
+	"github.com/cactus/go-statsd-client/v5/statsd"
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
 )
@@ -53,41 +53,13 @@ func TestNewClientEmpty(t *testing.T) {
 	statsd.Timing("t", time.Second)
 }
 
-func TestEnsureStatsd(t *testing.T) {
-	noopClient, err := statsd.NewNoopClient()
-	require.NoError(t, err, "Failed to initialize NooClient")
-
-	tests := []struct {
-		statsd statsd.Statter
-		err    error
-		panic  bool
-	}{
-		{noopClient, nil, false},
-		{noopClient, errors.New("err"), true},
-		{nil, errors.New("err"), true},
-	}
-
-	for _, tt := range tests {
-		if tt.panic {
-			assert.Panics(t, func() {
-				ensureStatsd(tt.statsd, tt.err)
-			})
-			continue
-		}
-
-		got := ensureStatsd(tt.statsd, tt.err)
-		assert.Equal(t, tt.statsd, got, "Got unexpected instance")
-	}
-}
-
 func TestNewClientCreate(t *testing.T) {
 	origNewStatsD := newStatsD
 	defer func() { newStatsD = origNewStatsD }()
 	origUserEnv := os.Getenv("USER")
 	defer os.Setenv("USER", origUserEnv)
 
-	noopClient, err := statsd.NewNoopClient()
-	require.NoError(t, err, "Failed to initialize NooClient")
+	noopClient := newNoopStatter()
 	errFailed := errors.New("failure for test")
 
 	tests := []struct {
diff --git a/statsd/statsdtest/server.go b/statsd/statsdtest/server.go
index 3c08374..1e67867 100644
--- a/statsd/statsdtest/server.go
+++ b/statsd/statsdtest/server.go
@@ -6,7 +6,7 @@ import (
 	"strconv"
 	"testing"
 
-	"github.com/cactus/go-statsd-client/statsd/statsdtest"
+	"github.com/cactus/go-statsd-client/v5/statsd/statsdtest"
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
 )
diff --git a/statsd/statsdtest/server_test.go b/statsd/statsdtest/server_test.go
index c414050..6ae4979 100644
--- a/statsd/statsdtest/server_test.go
+++ b/statsd/statsdtest/server_test.go
@@ -6,8 +6,8 @@ import (
 
 	"github.com/uber/tchannel-go/testutils"
 
-	"github.com/cactus/go-statsd-client/statsd"
-	"github.com/cactus/go-statsd-client/statsd/statsdtest"
+	"github.com/cactus/go-statsd-client/v5/statsd"
+	"github.com/cactus/go-statsd-client/v5/statsd/statsdtest"
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
 	"go.uber.org/multierr"