Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

engine: fix version parsing #180

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/v5/version"
)

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
32 changes: 32 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
package version

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

const Version = "5.3.0"

var (
vsnOnce sync.Once
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")
}
}
Loading