Skip to content

Commit 013f3fa

Browse files
committed
Add operator build info metric
Add a new metric to the tidb-operator that exposes the build info as labels. This allows for monitoring of what versions are deployed across multiple installs. Signed-off-by: SuperQ <superq@gmail.com>
1 parent 44edb93 commit 013f3fa

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

cmd/tidb-operator/main.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/go-logr/logr"
25+
"github.com/prometheus/client_golang/prometheus"
2526
"go.uber.org/zap/zapcore"
2627
appsv1 "k8s.io/api/apps/v1"
2728
batchv1 "k8s.io/api/batch/v1"
@@ -39,6 +40,7 @@ import (
3940
runtimeConfig "sigs.k8s.io/controller-runtime/pkg/config"
4041
"sigs.k8s.io/controller-runtime/pkg/healthz"
4142
"sigs.k8s.io/controller-runtime/pkg/log/zap"
43+
controllermetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
4244
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
4345

4446
brv1alpha1 "github.com/pingcap/tidb-operator/api/v2/br/v1alpha1"
@@ -148,7 +150,28 @@ func main() {
148150

149151
ctx := logr.NewContext(context.Background(), setupLog)
150152

151-
setupLog.Info("start tidb operator", version.Get().KeysAndValues()...)
153+
versionInfo := version.Get()
154+
setupLog.Info("start tidb operator", versionInfo.KeysAndValues()...)
155+
156+
versionConstLabels := prometheus.Labels{
157+
"version": versionInfo.GitVersion,
158+
"revision": versionInfo.GitCommit,
159+
"git_tree_state": versionInfo.GitTreeState,
160+
"build_date": versionInfo.BuildDate,
161+
"goversion": versionInfo.GoVersion,
162+
"compiler": versionInfo.Compiler,
163+
"platform": versionInfo.Platform,
164+
}
165+
166+
versionMetric := prometheus.NewGaugeFunc(
167+
prometheus.GaugeOpts{
168+
Name: "tidb_operator_build_info",
169+
Help: "A metric with a constant '1' value with various version labels from which tidb-operator was built.",
170+
ConstLabels: versionConstLabels,
171+
},
172+
func() float64 { return 1 },
173+
)
174+
controllermetrics.Registry.MustRegister(versionMetric)
152175

153176
utilruntime.PanicHandlers = append(utilruntime.PanicHandlers, func(_ context.Context, _ any) {
154177
metrics.ControllerPanic.WithLabelValues().Inc()

0 commit comments

Comments
 (0)