Move source-tag to toplevel maglev.source-tag config, defaulting to short hostname, v1.1.2

This commit is contained in:
2026-05-01 15:39:23 +02:00
parent f16cf7cb14
commit dc7599f3ee
5 changed files with 40 additions and 36 deletions
+20 -22
View File
@@ -21,10 +21,9 @@ import (
// BackendInfo holds the health and config state needed by the collector.
type BackendInfo struct {
Health *health.Backend
Enabled bool
HCName string // healthcheck name from config
SourceTag string // nginx source tag; equals backend name when unset in config
Health *health.Backend
Enabled bool
HCName string // healthcheck name from config
}
// StateSource provides read-only access to the running checker state.
@@ -132,10 +131,11 @@ var (
// on each scrape. This avoids stale label sets when backends are added or
// removed by a config reload.
type Collector struct {
src StateSource
vpp VPPSource // optional; nil when VPP integration is disabled
src StateSource
vpp VPPSource // optional; nil when VPP integration is disabled
sourceTag string
backendInfo *prometheus.Desc
maglevInfo *prometheus.Desc
backendState *prometheus.Desc
backendHealth *prometheus.Desc
backendEnabled *prometheus.Desc
@@ -154,14 +154,15 @@ type Collector struct {
// NewCollector creates a Collector backed by the given StateSource. vpp may
// be nil when VPP integration is disabled; in that case vpp_* metrics are
// simply not emitted.
func NewCollector(src StateSource, vpp VPPSource) *Collector {
func NewCollector(src StateSource, vpp VPPSource, sourceTag string) *Collector {
return &Collector{
src: src,
vpp: vpp,
backendInfo: prometheus.NewDesc(
"maglev_backend_info",
"Static backend metadata. Always 1; metadata is conveyed via labels.",
[]string{"backend", "address", "healthcheck", "source_tag"}, nil,
src: src,
vpp: vpp,
sourceTag: sourceTag,
maglevInfo: prometheus.NewDesc(
"maglev_info",
"Static maglevd instance metadata. Always 1; metadata is conveyed via labels.",
[]string{"source_tag"}, nil,
),
backendState: prometheus.NewDesc(
"maglev_backend_state",
@@ -223,7 +224,7 @@ func NewCollector(src StateSource, vpp VPPSource) *Collector {
// Describe implements prometheus.Collector.
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.backendInfo
ch <- c.maglevInfo
ch <- c.backendState
ch <- c.backendHealth
ch <- c.backendEnabled
@@ -239,6 +240,8 @@ func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
// Collect implements prometheus.Collector.
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(c.maglevInfo, prometheus.GaugeValue, 1.0, c.sourceTag)
states := []health.State{
health.StateUnknown,
health.StateUp,
@@ -255,11 +258,6 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
}
addr := info.Health.Address.String()
ch <- prometheus.MustNewConstMetric(
c.backendInfo, prometheus.GaugeValue, 1.0,
name, addr, info.HCName, info.SourceTag,
)
// One time-series per possible state; the current state is 1, rest 0.
for _, s := range states {
val := 0.0
@@ -356,8 +354,8 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
// Register registers all metrics with the given registry. vpp may be nil
// to disable VPP-related metrics.
func Register(reg prometheus.Registerer, src StateSource, vpp VPPSource) *Collector {
coll := NewCollector(src, vpp)
func Register(reg prometheus.Registerer, src StateSource, vpp VPPSource, sourceTag string) *Collector {
coll := NewCollector(src, vpp, sourceTag)
reg.MustRegister(coll)
reg.MustRegister(ProbeTotal)
reg.MustRegister(ProbeDuration)