Skip to content

Commit ebf9e62

Browse files
author
Chris Stockton
committed
chore: move the version code from version.go into separate pkg
I missed that the build-strip Makefile step wrote a new version.go file. Moving this code around is easier then messing with build system right now.
1 parent 3ca1e88 commit ebf9e62

File tree

4 files changed

+129
-141
lines changed

4 files changed

+129
-141
lines changed

internal/observability/metrics.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/sirupsen/logrus"
1212
"github.com/supabase/auth/internal/conf"
1313
"github.com/supabase/auth/internal/utilities"
14+
"github.com/supabase/auth/internal/utilities/version"
1415

1516
"github.com/prometheus/client_golang/prometheus/promhttp"
1617
"go.opentelemetry.io/otel"
@@ -197,7 +198,7 @@ func ConfigureMetrics(ctx context.Context, mc *conf.MetricsConfig) error {
197198
logrus.WithError(err).Error("unable to get gotrue.gotrue_running gague metric")
198199
return
199200
}
200-
if err = utilities.InitVersionMetrics(ctx); err != nil {
201+
if err = version.InitVersionMetrics(ctx, utilities.Version); err != nil {
201202
logrus.WithError(err).Error("unable to configure version metrics")
202203
}
203204
})

internal/utilities/version.go

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,4 @@
11
package utilities
22

3-
import (
4-
"context"
5-
"errors"
6-
"fmt"
7-
"math"
8-
"strconv"
9-
"strings"
10-
11-
"github.com/Masterminds/semver/v3"
12-
"go.opentelemetry.io/otel"
13-
"go.opentelemetry.io/otel/metric"
14-
)
15-
163
// Version is git commit or release tag from which this binary was built.
174
var Version string
18-
19-
func InitVersionMetrics(ctx context.Context) error {
20-
return initVersionMetrics(ctx, Version)
21-
}
22-
23-
func initVersionMetrics(ctx context.Context, ver string) error {
24-
vi, err := parseSemver(ver)
25-
if err != nil {
26-
const msg = "initVersionMetrics: unable to parse version %q: %w"
27-
return fmt.Errorf(msg, ver, err)
28-
}
29-
30-
if err := initMetrics(ctx, vi); err != nil {
31-
const msg = "initVersionMetrics: unable to initialize version %q: %w"
32-
return fmt.Errorf(msg, ver, err)
33-
}
34-
return nil
35-
}
36-
37-
func initGauge(
38-
ctx context.Context,
39-
typ string,
40-
val uint64,
41-
gaugeFunc initGaugeFunc,
42-
) error {
43-
name := fmt.Sprintf("global_auth_version_%v", typ)
44-
desc := fmt.Sprintf("Set to this auth servers %v version number.", typ)
45-
46-
g, err := gaugeFunc(name, metric.WithDescription(desc))
47-
if err != nil {
48-
const msg = "initGauge: part %q (%v) otel error: %w"
49-
return fmt.Errorf(msg, typ, val, err)
50-
}
51-
if val > math.MaxInt64 {
52-
const msg = "initGauge: part %q (%v) value > math.MaxInt64"
53-
return fmt.Errorf(msg, typ, val)
54-
}
55-
56-
g.Record(ctx, int64(val))
57-
return nil
58-
}
59-
60-
type initGaugeFunc func(
61-
name string,
62-
options ...metric.Int64GaugeOption,
63-
) (metric.Int64Gauge, error)
64-
65-
func initGaugeOtel(name string, options ...metric.Int64GaugeOption) (metric.Int64Gauge, error) {
66-
return otel.Meter("gotrue").Int64Gauge(name, options...)
67-
}
68-
69-
func initMetrics(ctx context.Context, vi *versionInfo) error {
70-
return errors.Join(
71-
initGauge(ctx, "major", vi.Major, initGaugeOtel),
72-
initGauge(ctx, "minor", vi.Minor, initGaugeOtel),
73-
initGauge(ctx, "patch", vi.Patch, initGaugeOtel),
74-
initGauge(ctx, "rc", vi.RC, initGaugeOtel),
75-
)
76-
}
77-
78-
type versionInfo struct {
79-
Original string
80-
Major uint64
81-
Minor uint64
82-
Patch uint64
83-
RC uint64
84-
}
85-
86-
func parseSemver(ver string) (*versionInfo, error) {
87-
vi := &versionInfo{
88-
Original: ver,
89-
}
90-
91-
ver = normalizeVersion(ver)
92-
sv, err := semver.NewVersion(ver)
93-
if err != nil {
94-
return nil, err
95-
}
96-
97-
pre := sv.Prerelease()
98-
if strings.HasPrefix(pre, "rc") {
99-
pre = strings.TrimPrefix(pre, "rc.")
100-
pre = strings.TrimPrefix(pre, "rc-")
101-
pre = strings.TrimPrefix(pre, "rc")
102-
if i := strings.IndexByte(pre, '-'); i >= 0 {
103-
pre = pre[:i]
104-
}
105-
if i := strings.IndexByte(pre, '.'); i >= 0 {
106-
pre = pre[:i]
107-
}
108-
109-
rc, err := strconv.ParseUint(pre, 10, 64)
110-
if err == nil {
111-
vi.RC = rc
112-
}
113-
}
114-
115-
vi.Major = sv.Major()
116-
vi.Minor = sv.Minor()
117-
vi.Patch = sv.Patch()
118-
return vi, nil
119-
}
120-
121-
func normalizeVersion(ver string) string {
122-
ver = strings.TrimSpace(ver)
123-
if strings.HasPrefix(ver, "v") {
124-
return ver
125-
}
126-
if strings.HasPrefix(ver, "rc") {
127-
return "v" + ver[2:]
128-
}
129-
return "v" + ver
130-
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package version
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"math"
8+
"strconv"
9+
"strings"
10+
11+
"github.com/Masterminds/semver/v3"
12+
"go.opentelemetry.io/otel"
13+
"go.opentelemetry.io/otel/metric"
14+
)
15+
16+
func InitVersionMetrics(ctx context.Context, ver string) error {
17+
vi, err := parseSemver(ver)
18+
if err != nil {
19+
const msg = "initVersionMetrics: unable to parse version %q: %w"
20+
return fmt.Errorf(msg, ver, err)
21+
}
22+
23+
if err := initMetrics(ctx, vi); err != nil {
24+
const msg = "initVersionMetrics: unable to initialize version %q: %w"
25+
return fmt.Errorf(msg, ver, err)
26+
}
27+
return nil
28+
}
29+
30+
func initGauge(
31+
ctx context.Context,
32+
typ string,
33+
val uint64,
34+
gaugeFunc initGaugeFunc,
35+
) error {
36+
name := fmt.Sprintf("global_auth_version_%v", typ)
37+
desc := fmt.Sprintf("Set to this auth servers %v version number.", typ)
38+
39+
g, err := gaugeFunc(name, metric.WithDescription(desc))
40+
if err != nil {
41+
const msg = "initGauge: part %q (%v) otel error: %w"
42+
return fmt.Errorf(msg, typ, val, err)
43+
}
44+
if val > math.MaxInt64 {
45+
const msg = "initGauge: part %q (%v) value > math.MaxInt64"
46+
return fmt.Errorf(msg, typ, val)
47+
}
48+
49+
g.Record(ctx, int64(val))
50+
return nil
51+
}
52+
53+
type initGaugeFunc func(
54+
name string,
55+
options ...metric.Int64GaugeOption,
56+
) (metric.Int64Gauge, error)
57+
58+
func initGaugeOtel(name string, options ...metric.Int64GaugeOption) (metric.Int64Gauge, error) {
59+
return otel.Meter("gotrue").Int64Gauge(name, options...)
60+
}
61+
62+
func initMetrics(ctx context.Context, vi *versionInfo) error {
63+
return errors.Join(
64+
initGauge(ctx, "major", vi.Major, initGaugeOtel),
65+
initGauge(ctx, "minor", vi.Minor, initGaugeOtel),
66+
initGauge(ctx, "patch", vi.Patch, initGaugeOtel),
67+
initGauge(ctx, "rc", vi.RC, initGaugeOtel),
68+
)
69+
}
70+
71+
type versionInfo struct {
72+
Original string
73+
Major uint64
74+
Minor uint64
75+
Patch uint64
76+
RC uint64
77+
}
78+
79+
func parseSemver(ver string) (*versionInfo, error) {
80+
vi := &versionInfo{
81+
Original: ver,
82+
}
83+
84+
ver = normalizeVersion(ver)
85+
sv, err := semver.NewVersion(ver)
86+
if err != nil {
87+
return nil, err
88+
}
89+
90+
pre := sv.Prerelease()
91+
if strings.HasPrefix(pre, "rc") {
92+
pre = strings.TrimPrefix(pre, "rc.")
93+
pre = strings.TrimPrefix(pre, "rc-")
94+
pre = strings.TrimPrefix(pre, "rc")
95+
if i := strings.IndexByte(pre, '-'); i >= 0 {
96+
pre = pre[:i]
97+
}
98+
if i := strings.IndexByte(pre, '.'); i >= 0 {
99+
pre = pre[:i]
100+
}
101+
102+
rc, err := strconv.ParseUint(pre, 10, 64)
103+
if err == nil {
104+
vi.RC = rc
105+
}
106+
}
107+
108+
vi.Major = sv.Major()
109+
vi.Minor = sv.Minor()
110+
vi.Patch = sv.Patch()
111+
return vi, nil
112+
}
113+
114+
func normalizeVersion(ver string) string {
115+
ver = strings.TrimSpace(ver)
116+
if strings.HasPrefix(ver, "v") {
117+
return ver
118+
}
119+
if strings.HasPrefix(ver, "rc") {
120+
return "v" + ver[2:]
121+
}
122+
return "v" + ver
123+
}

internal/utilities/version_test.go renamed to internal/utilities/version/version_test.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utilities
1+
package version
22

33
import (
44
"context"
@@ -42,12 +42,12 @@ func TestVersionInitVersionMetrics(t *testing.T) {
4242
}
4343

4444
{
45-
err := initVersionMetrics(ctx, validVer)
45+
err := InitVersionMetrics(ctx, validVer)
4646
require.NoError(t, err)
4747
}
4848

4949
{
50-
err := initVersionMetrics(ctx, "invalid")
50+
err := InitVersionMetrics(ctx, "invalid")
5151
require.Error(t, err)
5252
const exp = "initVersionMetrics: unable to parse version"
5353
if got := err.Error(); !strings.Contains(got, exp) {
@@ -58,7 +58,7 @@ func TestVersionInitVersionMetrics(t *testing.T) {
5858
{
5959
max := strconv.AppendUint(nil, math.MaxUint64, 10)
6060
ver := fmt.Sprintf("%v.%v.%s", 2, 187, max)
61-
err := initVersionMetrics(ctx, ver)
61+
err := InitVersionMetrics(ctx, ver)
6262
require.Error(t, err)
6363
if exp, got := "math.MaxInt64", err.Error(); !strings.Contains(got, exp) {
6464
t.Fatalf("exp err %q to contain %q", got, exp)
@@ -77,16 +77,6 @@ func TestVersionInitVersionMetrics(t *testing.T) {
7777
t.Fatalf("exp err %q to contain %q", got, exp)
7878
}
7979
}
80-
81-
func() {
82-
prev := Version
83-
defer func() { Version = prev }()
84-
Version = validVer
85-
86-
err := InitVersionMetrics(ctx)
87-
require.NoError(t, err)
88-
}()
89-
9080
}
9181

9282
func TestVersionParseSemver(t *testing.T) {

0 commit comments

Comments
 (0)