Skip to content

Commit

Permalink
engine: fix version parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinburkesegment committed Jan 2, 2025
1 parent ec25a49 commit 2afe9e4
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 39 deletions.
53 changes: 25 additions & 28 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -169,35 +167,34 @@ func (e *Engine) reportVersionOnce(t time.Time) {
// configure it after creation time with e.g. the Register function. So
// instead we try to do it at the moment you try to send your first metric.
e.once.Do(func() {
vsn := strings.TrimPrefix(runtime.Version(), "go")
parts := strings.Split(vsn, ".")
// We don't want to report weird compiled Go versions like tip.
// len(parts) may equal 2 because an older Go version might be "go1.13"
// instead of "go1.13.1"
if len(parts) == 2 || len(parts) == 3 {
e.Handler.HandleMeasures(t,
Measure{
Name: "go_version",
Fields: []Field{{
Name: "value",
Value: intValue(1),
}},
Tags: []Tag{
{"go_version", vsn},
},
measures := []Measure{
{
Name: "stats_version",
Fields: []Field{{
Name: "value",
Value: intValue(1),
}},
Tags: []Tag{
{"stats_version", version.Version},
},
Measure{
Name: "stats_version",
Fields: []Field{{
Name: "value",
Value: intValue(1),
}},
Tags: []Tag{
{"stats_version", version.Version},
},
},
}
// We don't want to report weird compiled Go versions like "devel" with
// a commit SHA. Splitting on periods does not work as well for
// filtering these
if !version.DevelGoVersion() {
measures = append(measures, Measure{
Name: "go_version",
Fields: []Field{{
Name: "value",
Value: intValue(1),
}},
Tags: []Tag{
{"go_version", version.GoVersion()},
},
)
})
}
e.Handler.HandleMeasures(t, measures...)
})
}

Expand Down
22 changes: 11 additions & 11 deletions netstats/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package netstats
import (
"net"
"reflect"
"runtime"
"strings"
"testing"

stats "github.com/segmentio/stats/v5"
"github.com/segmentio/stats/v5/statstest"
"github.com/segmentio/stats/version"

Check failure on line 10 in netstats/listener_test.go

View workflow job for this annotation

GitHub Actions / test (oldstable, self-hosted, linux, arm64, segment)

no required module provides package github.com/segmentio/stats/version; to add it:

Check failure on line 10 in netstats/listener_test.go

View workflow job for this annotation

GitHub Actions / test (oldstable, ubuntu-latest)

no required module provides package github.com/segmentio/stats/version; to add it:

Check failure on line 10 in netstats/listener_test.go

View workflow job for this annotation

GitHub Actions / test (stable, self-hosted, linux, arm64, segment)

no required module provides package github.com/segmentio/stats/version; to add it:

Check failure on line 10 in netstats/listener_test.go

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

no required module provides package github.com/segmentio/stats/version; to add it:
)

func TestListener(t *testing.T) {
Expand Down Expand Up @@ -63,11 +62,12 @@ func TestListenerError(t *testing.T) {

lstn.Close()

vsn := strings.TrimPrefix(runtime.Version(), "go")
parts := strings.Split(vsn, ".")
measures := h.Measures()
measurePassed := false
if len(parts) == 2 || len(parts) == 3 {
t.Run("CheckGoVersionEmitted", func(t *testing.T) {
if version.DevelGoVersion() {
t.Skip("No metrics emitted if compiled with Go devel version")
}
measurePassed := false
for _, measure := range measures {
if measure.Name != "go_version" {
continue
Expand All @@ -76,15 +76,15 @@ func TestListenerError(t *testing.T) {
if tag.Name != "go_version" {
continue
}
if tag.Value == vsn {
if tag.Value == version.GoVersion() {
measurePassed = true
}
}
}
}
if !measurePassed {
t.Errorf("did not find correct tag for measure: %#v\n", measures)
}
if !measurePassed {
t.Errorf("did not find correct 'go_version' tag for measure: %#v\n", measures)
}
})
var foundMetric stats.Measure
for i := range measures {
if measures[i].Name == "netstats.test.conn.error" {
Expand Down
30 changes: 30 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
package version

import (
"runtime"
"strings"
"sync"
)

const Version = "5.3.0"

var vsnOnce sync.Once
var vsn string

func isDevel(vsn string) bool {
return strings.Count(vsn, " ") > 2 || strings.HasPrefix(vsn, "devel")
}

// GoVersion reports the Go version, in a format that is consumable by metrics
// tools.
func GoVersion() string {
vsnOnce.Do(func() {
vsn = strings.TrimPrefix(runtime.Version(), "go")
})
return vsn
}

// DevelGoVersion reports whether the version of Go that compiled or ran this
// library is a development ("tip") version. This is useful to distinguish
// because tip versions include a commit SHA and change frequently, so are less
// useful for metric reporting.
func DevelGoVersion() bool {
return isDevel(GoVersion())
}
12 changes: 12 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package version

import "testing"

func TestDevel(t *testing.T) {
if isDevel("1.23.4") {
t.Errorf("expected 1.23.4 to return false; got true")
}
if !isDevel("devel go1.24-d1d9312950 Wed Jan 1 21:18:59 2025 -0800 darwin/arm64") {
t.Errorf("expected tip version to return true; got false")
}
}

0 comments on commit 2afe9e4

Please sign in to comment.