Skip to content

Commit d7b0058

Browse files
committed
version: commit hash crc of first stat should be real
After calculating the crc of the commit hash, we should use real value instead of 0. Also started using info.CommitHash, as it's the same (or better, it may include vcs version, if build tag is missing), and easier to test... Change-Id: I9621732cce6bf9f2c7deebca3e55681af761dacc
1 parent 5ca2806 commit d7b0058

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

version/stats.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ func (info *Info) Stats(cb func(key monkit.SeriesKey, field string, val float64)
3737
crc := atomic.LoadUint32(&info.commitHashCRC)
3838
if crc == 0 {
3939
c := crc32.NewIEEE()
40-
_, err := c.Write([]byte(buildCommitHash))
40+
_, err := c.Write([]byte(info.CommitHash))
4141
if err != nil {
4242
panic(err)
4343
}
44-
atomic.StoreUint32(&info.commitHashCRC, c.Sum32())
44+
crc = c.Sum32()
45+
atomic.StoreUint32(&info.commitHashCRC, crc)
4546
}
4647
cb(key, "commit", float64(crc))
4748
cb(key, "major", float64(info.Version.Major))

version/stats_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,31 @@ package version
55

66
import (
77
"testing"
8+
9+
"github.com/spacemonkeygo/monkit/v3"
10+
"github.com/stretchr/testify/require"
811
)
912

1013
func TestOsVersion(t *testing.T) {
1114
t.Log(osversion())
1215
}
16+
17+
func TestCommitHashCRC(t *testing.T) {
18+
info := Info{
19+
CommitHash: "0e7695f391df6c2ea03d2dec02b9059ccaffb9c9",
20+
}
21+
22+
getCommitHash := func() (ret float64) {
23+
info.Stats(func(key monkit.SeriesKey, field string, val float64) {
24+
if key.Measurement == "version_info" && field == "commit" {
25+
ret = val
26+
}
27+
})
28+
return ret
29+
}
30+
31+
// first time the value is calculated, second time cached value is used.
32+
// Let's test both.
33+
require.Equal(t, float64(868966439), getCommitHash())
34+
require.Equal(t, float64(868966439), getCommitHash())
35+
}

0 commit comments

Comments
 (0)