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

Export the versions of Go modules linked into the binary #578

Merged
merged 4 commits into from
Nov 1, 2022
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
8 changes: 8 additions & 0 deletions baseplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"fmt"
"io"
"os"
"runtime/debug"
"time"

"github.com/reddit/baseplate.go/batchcloser"
"github.com/reddit/baseplate.go/configbp"
"github.com/reddit/baseplate.go/ecinterface"
"github.com/reddit/baseplate.go/log"
"github.com/reddit/baseplate.go/metricsbp"
"github.com/reddit/baseplate.go/prometheusbp"
"github.com/reddit/baseplate.go/runtimebp"
"github.com/reddit/baseplate.go/secrets"
"github.com/reddit/baseplate.go/tracing"
Expand Down Expand Up @@ -307,6 +309,12 @@ func New(ctx context.Context, args NewArgs) (context.Context, Baseplate, error)
cfg := args.Config.GetConfig()
bp := impl{cfg: cfg, closers: batchcloser.New()}

if info, ok := debug.ReadBuildInfo(); ok {
prometheusbp.RecordModuleVersions(info)
} else {
log.C(ctx).Warn("baseplate.New: unable to read build info to export dependency metrics")
}

runtimebp.InitFromConfig(cfg.Runtime)

ctx, cancel := context.WithCancel(ctx)
Expand Down
38 changes: 38 additions & 0 deletions prometheusbp/modules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package prometheusbp

import (
"runtime/debug"
"strconv"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

"github.com/reddit/baseplate.go/internal/prometheusbpint"
)

var goModules = promauto.With(prometheusbpint.GlobalRegistry).NewGaugeVec(prometheus.GaugeOpts{
Name: "baseplate_go_modules",
Help: "Export the version information for included Go modules, and whether the module is the 'main' module or a 'dependency'. Always 1",
}, []string{"go_module", "module_role", "module_replaced", "module_version"})

// RecordModuleVersions records the modules linked into this binary in the
// baseplate_go_modules prometheus metric.
//
// Users should not need to call this directly, as it is called by baseplate.New.
// This should generally not be called more than once, and it is not safe to call concurrently.
func RecordModuleVersions(info *debug.BuildInfo) {
record := func(role string, mod *debug.Module) {
goModules.With(prometheus.Labels{
"go_module": mod.Path,
"module_role": role,
"module_replaced": strconv.FormatBool(mod.Replace != nil),
"module_version": mod.Version,
}).Set(1)
}

goModules.Reset()
record("main", &info.Main)
for _, dep := range info.Deps {
record("dependency", dep)
}
}
55 changes: 55 additions & 0 deletions prometheusbp/modules_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package prometheusbp

import (
"runtime/debug"
"testing"

"github.com/prometheus/client_golang/prometheus"

"github.com/reddit/baseplate.go/prometheusbp/promtest"
)

func TestBuildInfoMetrics(t *testing.T) {
info := &debug.BuildInfo{
GoVersion: "go1.18.3",
Path: "example.com/path/to/main/module",
Main: debug.Module{
Path: "example.com/path/to/main/module",
Version: "(devel)",
},
Deps: []*debug.Module{{
Path: "github.com/reddit/baseplate.go",
Version: "v1.2.3",
}, {
Path: "github.com/reddit/oldmodule",
Replace: &debug.Module{
Path: "github.com/reddit/newmodule",
Version: "v1.42.0",
},
Version: "v0.1.2",
}},
}

defer promtest.NewPrometheusMetricTest(t, "baseplate_go_modules", goModules, prometheus.Labels{
"go_module": "example.com/path/to/main/module",
"module_role": "main",
"module_replaced": "false",
"module_version": "(devel)",
}).CheckDelta(1)

defer promtest.NewPrometheusMetricTest(t, "baseplate_go_modules", goModules, prometheus.Labels{
"go_module": "github.com/reddit/baseplate.go",
"module_role": "dependency",
"module_replaced": "false",
"module_version": "v1.2.3",
}).CheckDelta(1)

defer promtest.NewPrometheusMetricTest(t, "baseplate_go_modules", goModules, prometheus.Labels{
"go_module": "github.com/reddit/oldmodule",
"module_role": "dependency",
"module_replaced": "true",
"module_version": "v0.1.2",
}).CheckDelta(1)

RecordModuleVersions(info)
}