feat: add nixos_flake_info metric with current and remote revisions

Add a new info metric that exposes the current system's flake revision
and the latest remote revision as labels. This makes it easier to see
exactly which revision is deployed vs available.

Also adds version constant to Go code and extracts it in flake.nix,
providing a single source of truth for the version.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-07 00:16:19 +01:00
parent e381038537
commit 9c29505814
5 changed files with 32 additions and 9 deletions

View File

@@ -27,6 +27,7 @@ type FlakeCollector struct {
inputAge *prometheus.Desc
inputInfo *prometheus.Desc
flakeInfo *prometheus.Desc
revisionBehind *prometheus.Desc
mu sync.RWMutex
@@ -75,6 +76,11 @@ func NewFlakeCollector(flakeURL string, checkInterval time.Duration) *FlakeColle
"Info gauge with revision and type labels",
[]string{"input", "rev", "type"}, nil,
),
flakeInfo: prometheus.NewDesc(
"nixos_flake_info",
"Info gauge with current and remote flake revisions",
[]string{"current_rev", "remote_rev"}, nil,
),
revisionBehind: prometheus.NewDesc(
"nixos_flake_revision_behind",
"1 if current system revision differs from remote latest, 0 if match",
@@ -86,6 +92,7 @@ func NewFlakeCollector(flakeURL string, checkInterval time.Duration) *FlakeColle
func (c *FlakeCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.inputAge
ch <- c.inputInfo
ch <- c.flakeInfo
ch <- c.revisionBehind
}
@@ -168,14 +175,17 @@ func (c *FlakeCollector) collectRevisionBehind(ch chan<- prometheus.Metric, data
return
}
remoteRev := data.Revision
if len(remoteRev) > 7 {
remoteRev = remoteRev[:7]
}
// Emit flake info metric with revisions
ch <- prometheus.MustNewConstMetric(c.flakeInfo, prometheus.GaugeValue, 1, currentRev, remoteRev)
behind := 0.0
if currentRev != "" && data.Revision != "" {
// Compare short hashes
remoteShort := data.Revision
if len(remoteShort) > 7 {
remoteShort = remoteShort[:7]
}
if currentRev != remoteShort && !strings.HasPrefix(data.Revision, currentRev) {
if currentRev != remoteRev && !strings.HasPrefix(data.Revision, currentRev) {
behind = 1.0
}
}