Skip to content

Commit

Permalink
Merge pull request #149 from segmentio/lint-fixes
Browse files Browse the repository at this point in the history
lint fixes
  • Loading branch information
extemporalgenome authored Jul 19, 2023
2 parents 62a2aae + fd74c9f commit 91d974d
Show file tree
Hide file tree
Showing 47 changed files with 212 additions and 180 deletions.
4 changes: 3 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ output:
sort-results: true

linters:
disable:
- depguard # this linter is broken in golangci-lint as of 2023-07-18.

enable:
- depguard
- godot
- gofumpt
- goimports
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type funcMetrics struct {
```go
t := time.Now()
f()
callTime := time.Now().Sub(t)
callTime := time.Since(t)

m := &funcMetrics{}
m.calls.count = 1
Expand Down
2 changes: 1 addition & 1 deletion buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func makeKey(s string) Key {
return Key{Measure: measure, Field: field}
}

func splitMeasureField(s string) (measure string, field string) {
func splitMeasureField(s string) (measure, field string) {
if i := strings.LastIndexByte(s, '.'); i >= 0 {
measure, field = s[:i], s[i+1:]
} else {
Expand Down
6 changes: 1 addition & 5 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ type Serializer interface {
type buffer struct {
lock uint64
data []byte
pad [32]byte // padding to avoid false sharing between threads
_ [32]byte // padding to avoid false sharing between threads
}

func (b *buffer) acquire() bool {
Expand All @@ -155,10 +155,6 @@ func (b *buffer) len() int {
return len(b.data)
}

func (b *buffer) cap() int {
return cap(b.data)
}

func (b *buffer) flush(w io.Writer, n int) {
w.Write(b.data[:n])
n = copy(b.data, b.data[n:])
Expand Down
8 changes: 4 additions & 4 deletions cmd/dogstatsd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ commands:
}

func client(cmd string, args ...string) {
var fset = flag.NewFlagSet("dogstatsd "+cmd+" [options...] metric value [-- args...]", flag.ExitOnError)
fset := flag.NewFlagSet("dogstatsd "+cmd+" [options...] metric value [-- args...]", flag.ExitOnError)
var extra []string
var tags tags
var addr string
Expand Down Expand Up @@ -101,12 +101,12 @@ func client(cmd string, args ...string) {
case "time":
start := time.Now()
run(extra...)
stats.Observe(name, time.Now().Sub(start), tags...)
stats.Observe(name, time.Since(start), tags...)
}
}

func server(args ...string) {
var fset = flag.NewFlagSet("dogstatsd agent [options...]", flag.ExitOnError)
fset := flag.NewFlagSet("dogstatsd agent [options...]", flag.ExitOnError)
var bind string

fset.StringVar(&bind, "bind", ":8125", "The network address to listen on for incoming UDP datagrams")
Expand Down Expand Up @@ -146,7 +146,7 @@ func errorf(msg string, args ...interface{}) {
os.Exit(1)
}

func split(args []string, sep string) (head []string, tail []string) {
func split(args []string, sep string) (head, tail []string) {
if i := indexOf(args, sep); i < 0 {
head = args
} else {
Expand Down
6 changes: 3 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func ContextAddTags(ctx context.Context, tags ...Tag) bool {
}

// ContextTags returns a copy of the tags on the context if they exist and nil
// if they don't exist
// if they don't exist.
func ContextTags(ctx context.Context) []Tag {
if x := getTagSlice(ctx); x != nil {
x.lock.Lock()
Expand Down Expand Up @@ -62,12 +62,12 @@ type tagSlice struct {
// for defining context keys was copied from Go 1.7's new use of context in net/http.
type tagsKey struct{}

// String is Stringer implementation
// String implements the fmt.Stringer interface.
func (k tagsKey) String() string {
return "stats_tags_context_key"
}

// contextKeyReqTags is contextKey for tags
// contextKeyReqTags is contextKey for tags.
var (
contextKeyReqTags = tagsKey{}
)
6 changes: 3 additions & 3 deletions datadog/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
"testing"
"time"

"github.com/segmentio/stats/v4"
"github.com/stretchr/testify/assert"

"github.com/segmentio/stats/v4"
)

func TestClient(t *testing.T) {
Expand Down Expand Up @@ -61,7 +62,6 @@ func TestClientWithDistributionPrefixes(t *testing.T) {
}

func TestClientWithUseDistributions(t *testing.T) {

// Start a goroutine listening for packets and giving them back on packets chan
packets := make(chan []byte)
addr, closer := startUDPListener(t, packets)
Expand Down Expand Up @@ -170,7 +170,7 @@ func BenchmarkClient(b *testing.B) {
}

// startUDPListener starts a goroutine listening for UDP packets on 127.0.0.1 and an available port.
// The address listened to is returned as `addr`. The payloads of packets received are copied to `packets`
// The address listened to is returned as `addr`. The payloads of packets received are copied to `packets`.
func startUDPListener(t *testing.T, packets chan []byte) (addr string, closer io.Closer) {
conn, err := net.ListenPacket("udp", "127.0.0.1:0") // :0 chooses an available port
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion datadog/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
EventAlertTypeSuccess EventAlertType = "success"
)

// Event is a representation of a datadog event
// Event is a representation of a datadog event.
type Event struct {
Title string
Text string
Expand Down
19 changes: 11 additions & 8 deletions datadog/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// Adapted from https://github.com/DataDog/datadog-agent/blob/6789e98a1e41e98700fa1783df62238bb23cb454/pkg/dogstatsd/parser.go#L141
func parseEvent(s string) (e Event, err error) {
var next = strings.TrimSpace(s)
next := strings.TrimSpace(s)
var header string
var rawTitleLen string
var rawTextLen string
Expand Down Expand Up @@ -109,8 +109,9 @@ func parseEvent(s string) (e Event, err error) {

return
}

func parseMetric(s string) (m Metric, err error) {
var next = strings.TrimSpace(s)
next := strings.TrimSpace(s)
var name string
var val string
var typ string
Expand Down Expand Up @@ -201,7 +202,7 @@ func parseMetric(s string) (m Metric, err error) {
return
}

func nextToken(s string, b byte) (token string, next string) {
func nextToken(s string, b byte) (token, next string) {
if off := strings.IndexByte(s, b); off >= 0 {
token, next = s[:off], s[off+1:]
} else {
Expand All @@ -210,7 +211,7 @@ func nextToken(s string, b byte) (token string, next string) {
return
}

func split(s string, b byte) (head string, tail string) {
func split(s string, b byte) (head, tail string) {
if off := strings.LastIndexByte(s, b); off >= 0 {
head, tail = s[:off], s[off+1:]
} else {
Expand All @@ -221,12 +222,14 @@ func split(s string, b byte) (head string, tail string) {

func count(s string, b byte) (n int) {
for {
if off := strings.IndexByte(s, b); off < 0 {
off := strings.IndexByte(s, b)
if off < 0 {
break
} else {
n++
s = s[off+1:]
}

n++
s = s[off+1:]
}

return
}
2 changes: 1 addition & 1 deletion datadog/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (s *serializer) AppendMeasure(b []byte, m stats.Measure) []byte {
// sendDist determines whether to send a metric to datadog as histogram `h` type or
// distribution `d` type. It's a confusing setup because useDistributions and distPrefixes
// are independent implementations of a control mechanism for sending distributions that
// aren't elegantly coordinated
// aren't elegantly coordinated.
func (s *serializer) sendDist(name string) bool {
if s.useDistributions {
return true
Expand Down
86 changes: 42 additions & 44 deletions datadog/serializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,57 @@ import (
"github.com/segmentio/stats/v4"
)

var (
testMeasures = []struct {
m stats.Measure
s string
dp []string
}{
{
m: stats.Measure{
Name: "request",
Fields: []stats.Field{
stats.MakeField("count", 5, stats.Counter),
},
var testMeasures = []struct {
m stats.Measure
s string
dp []string
}{
{
m: stats.Measure{
Name: "request",
Fields: []stats.Field{
stats.MakeField("count", 5, stats.Counter),
},
s: `request.count:5|c
`,
dp: []string{},
},
s: `request.count:5|c
`,
dp: []string{},
},

{
m: stats.Measure{
Name: "request",
Fields: []stats.Field{
stats.MakeField("count", 5, stats.Counter),
stats.MakeField("rtt", 100*time.Millisecond, stats.Histogram),
},
Tags: []stats.Tag{
stats.T("answer", "42"),
stats.T("hello", "world"),
},
{
m: stats.Measure{
Name: "request",
Fields: []stats.Field{
stats.MakeField("count", 5, stats.Counter),
stats.MakeField("rtt", 100*time.Millisecond, stats.Histogram),
},
s: `request.count:5|c|#answer:42,hello:world
Tags: []stats.Tag{
stats.T("answer", "42"),
stats.T("hello", "world"),
},
},
s: `request.count:5|c|#answer:42,hello:world
request.rtt:0.1|h|#answer:42,hello:world
`,
dp: []string{},
},
dp: []string{},
},

{
m: stats.Measure{
Name: "request",
Fields: []stats.Field{
stats.MakeField("dist_rtt", 100*time.Millisecond, stats.Histogram),
},
Tags: []stats.Tag{
stats.T("answer", "42"),
stats.T("hello", "world"),
},
{
m: stats.Measure{
Name: "request",
Fields: []stats.Field{
stats.MakeField("dist_rtt", 100*time.Millisecond, stats.Histogram),
},
Tags: []stats.Tag{
stats.T("answer", "42"),
stats.T("hello", "world"),
},
s: `request.dist_rtt:0.1|d|#answer:42,hello:world
`,
dp: []string{"dist_"},
},
}
)
s: `request.dist_rtt:0.1|d|#answer:42,hello:world
`,
dp: []string{"dist_"},
},
}

func TestAppendMeasure(t *testing.T) {
client := NewClient(DefaultAddress)
Expand Down
Loading

0 comments on commit 91d974d

Please sign in to comment.