diff --git a/version/version.go b/version/version.go index 8c46f10..1d3b0aa 100644 --- a/version/version.go +++ b/version/version.go @@ -90,10 +90,13 @@ func init() { VERSION = "dev-snapshot" } else { if !strings.HasPrefix(VERSION, "v") { - VERSION = "v" + VERSION + vVersion := "v" + VERSION + if semver.IsValid(vVersion) { + VERSION = vVersion + } } - if !semver.IsValid(VERSION) { - slog.Default().Warn("invalid version number", "version", VERSION) + if !semver.IsValid(VERSION) && VERSION != "dev-snapshot" { + slog.Default().Info("non-semver version", "version", VERSION) } } if bi, ok := debug.ReadBuildInfo(); ok { diff --git a/version/version_test.go b/version/version_test.go index 898b4d8..314477d 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -7,6 +7,7 @@ import ( "github.com/prometheus/client_golang/prometheus" dto "github.com/prometheus/client_model/go" + "golang.org/x/mod/semver" ) func TestCheckVersion(t *testing.T) { @@ -48,9 +49,12 @@ func TestVersionInfo(t *testing.T) { t.Error("VersionInfo().Version should not be empty") } - // Version should start with "v" or be "dev-snapshot" + // Version should start with "v", be "dev-snapshot", or be a non-semver string (e.g. branch name) if !strings.HasPrefix(info.Version, "v") && info.Version != "dev-snapshot" { - t.Errorf("Version should start with 'v' or be 'dev-snapshot', got: %s", info.Version) + // Non-semver versions like branch names ("main") are acceptable + if semver.IsValid("v" + info.Version) { + t.Errorf("Semver-like version should start with 'v', got: %s", info.Version) + } } // GitRevShort should be <= 7 characters if set @@ -398,10 +402,10 @@ func TestParseBuildTimeConsistency(t *testing.T) { func BenchmarkParseBuildTime(b *testing.B) { inputs := []string{ - "1672531200", // Unix epoch - "2023-01-01T00:00:00Z", // RFC3339 - "invalid-timestamp", // Invalid - "", // Empty + "1672531200", // Unix epoch + "2023-01-01T00:00:00Z", // RFC3339 + "invalid-timestamp", // Invalid + "", // Empty } for _, input := range inputs {