wgengine/magicsock: add home DERP region usermetric (#18062)

Expose the node's home DERP region ID as a Prometheus gauge via the
usermetrics endpoint.

Fixes #18061

Signed-off-by: Raj Singh <raj@tailscale.com>
This commit is contained in:
Raj Singh
2026-01-09 16:47:56 -05:00
committed by GitHub
parent 5db95ec376
commit aadc4f2ef4
2 changed files with 19 additions and 0 deletions

View File

@@ -216,17 +216,28 @@ func (c *Conn) derpRegionCodeLocked(regionID int) string {
return ""
}
// setHomeDERPGaugeLocked updates the home DERP gauge metric.
//
// c.mu must be held.
func (c *Conn) setHomeDERPGaugeLocked(derpNum int) {
if c.homeDERPGauge != nil {
c.homeDERPGauge.Set(float64(derpNum))
}
}
// c.mu must NOT be held.
func (c *Conn) setNearestDERP(derpNum int) (wantDERP bool) {
c.mu.Lock()
defer c.mu.Unlock()
if !c.wantDerpLocked() {
c.myDerp = 0
c.setHomeDERPGaugeLocked(0)
c.health.SetMagicSockDERPHome(0, c.homeless)
return false
}
if c.homeless {
c.myDerp = 0
c.setHomeDERPGaugeLocked(0)
c.health.SetMagicSockDERPHome(0, c.homeless)
return false
}
@@ -238,6 +249,7 @@ func (c *Conn) setNearestDERP(derpNum int) (wantDERP bool) {
metricDERPHomeChange.Add(1)
}
c.myDerp = derpNum
c.setHomeDERPGaugeLocked(derpNum)
c.health.SetMagicSockDERPHome(derpNum, c.homeless)
if c.privateKey.IsZero() {

View File

@@ -406,6 +406,10 @@ type Conn struct {
// metrics contains the metrics for the magicsock instance.
metrics *metrics
// homeDERPGauge is the usermetric gauge for the home DERP region ID.
// This can be nil when [Options.Metrics] are not enabled.
homeDERPGauge *usermetric.Gauge
}
// SetDebugLoggingEnabled controls whether spammy debug logging is enabled.
@@ -744,6 +748,9 @@ func NewConn(opts Options) (*Conn, error) {
}
c.metrics = registerMetrics(opts.Metrics)
if opts.Metrics != nil {
c.homeDERPGauge = opts.Metrics.NewGauge("tailscaled_home_derp_region_id", "DERP region ID of this node's home relay server")
}
if d4, err := c.listenRawDisco("ip4"); err == nil {
c.logf("[v1] using BPF disco receiver for IPv4")