main pico / pkg / metrics / serverinfo.go
Antonio Mika  ·  2026-10-25
 1// Package metrics holds the Prometheus metrics every pico process exports,
 2// including the SSH servers, so it can't depend on pkg/shared.
 3package metrics
 4
 5import (
 6	"os"
 7	"runtime/debug"
 8
 9	"github.com/prometheus/client_golang/prometheus"
10	"github.com/prometheus/client_golang/prometheus/promauto"
11)
12
13// RegisterServerInfo exports pico_server_info for app. Its value is always 1,
14// so a query can join it on instance and app to name the server and commit
15// behind any other series.
16func RegisterServerInfo(reg prometheus.Registerer, app string) {
17	RegisterServerInfoCommit(reg, app, commit())
18}
19
20// RegisterServerInfoCommit is RegisterServerInfo for a binary built without
21// VCS stamping, which sets its commit through -ldflags instead.
22func RegisterServerInfoCommit(reg prometheus.Registerer, app, commit string) {
23	promauto.With(reg).NewGauge(prometheus.GaugeOpts{
24		Namespace: "pico",
25		Name:      "server_info",
26		Help:      "The server and commit a process runs on. Always 1.",
27		ConstLabels: prometheus.Labels{
28			"app":      app,
29			"nodename": os.Getenv("NODENAME"),
30			"commit":   commit,
31		},
32	}).Set(1)
33}
34
35// commit is the VCS revision Go stamped into the binary, with -dirty when the
36// tree had uncommitted changes.
37func commit() string {
38	info, ok := debug.ReadBuildInfo()
39	if !ok {
40		return ""
41	}
42	var revision string
43	var modified bool
44	for _, s := range info.Settings {
45		switch s.Key {
46		case "vcs.revision":
47			revision = s.Value
48		case "vcs.modified":
49			modified = s.Value == "true"
50		}
51	}
52	if revision != "" && modified {
53		revision += "-dirty"
54	}
55	return revision
56}