mirror of
https://github.com/tailscale/tailscale.git
synced 2025-01-08 09:07:44 +00:00
expose tailscaled_advertised_routes as a gauge
This commit is contained in:
parent
93277ea2f7
commit
2b01273124
@ -12,7 +12,6 @@
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"expvar"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@ -119,11 +118,8 @@
|
|||||||
"tailscale.com/wgengine/wgcfg/nmcfg"
|
"tailscale.com/wgengine/wgcfg/nmcfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
var metricAdvertisedRoutes expvar.Int
|
var metricAdvertisedRoutes = usermetric.NewGauge(
|
||||||
|
"tailscaled_advertised_routes", "Number of routes advertised by tailscaled")
|
||||||
func init() {
|
|
||||||
usermetric.Publish("tailscaled_advertised_routes", &metricAdvertisedRoutes)
|
|
||||||
}
|
|
||||||
|
|
||||||
var controlDebugFlags = getControlDebugFlags()
|
var controlDebugFlags = getControlDebugFlags()
|
||||||
|
|
||||||
@ -4633,11 +4629,13 @@ func (b *LocalBackend) applyPrefsToHostinfoLocked(hi *tailcfg.Hostinfo, prefs ip
|
|||||||
hi.AllowsUpdate = envknob.AllowsRemoteUpdate() || prefs.AutoUpdate().Apply.EqualBool(true)
|
hi.AllowsUpdate = envknob.AllowsRemoteUpdate() || prefs.AutoUpdate().Apply.EqualBool(true)
|
||||||
|
|
||||||
// count routes without exit node routes
|
// count routes without exit node routes
|
||||||
|
var routes int64
|
||||||
for _, route := range hi.RoutableIPs {
|
for _, route := range hi.RoutableIPs {
|
||||||
if route.Bits() != 0 {
|
if route.Bits() != 0 {
|
||||||
metricAdvertisedRoutes.Add(1)
|
routes++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
metricAdvertisedRoutes.Set(float64(routes))
|
||||||
|
|
||||||
var sshHostKeys []string
|
var sshHostKeys []string
|
||||||
if prefs.RunSSH() && envknob.CanSSHD() {
|
if prefs.RunSSH() && envknob.CanSSHD() {
|
||||||
|
@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"expvar"
|
"expvar"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"tailscale.com/metrics"
|
"tailscale.com/metrics"
|
||||||
@ -33,14 +35,40 @@ func NewMultiLabelMap[T comparable](name string, promType, helpText string) *met
|
|||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
// Publish declares a named exported variable. This should be called from a
|
// Gauge is a gauge metric with no labels.
|
||||||
// package's init function when it creates its Vars.
|
type Gauge struct {
|
||||||
//
|
m *expvar.Float
|
||||||
// Note that usermetric are not protected against duplicate
|
help string
|
||||||
// metrics name. It is the caller's responsibility to ensure that
|
}
|
||||||
// the name is unique.
|
|
||||||
func Publish(name string, v expvar.Var) {
|
// NewGauge creates and register a new gauge metric with the given name and help text.
|
||||||
vars.Set(name, v)
|
func NewGauge(name, help string) *Gauge {
|
||||||
|
m := &expvar.Float{}
|
||||||
|
vars.Set(name, m)
|
||||||
|
return &Gauge{m, help}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set sets the gauge to the given value.
|
||||||
|
func (g *Gauge) Set(v float64) {
|
||||||
|
g.m.Set(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WritePrometheus writes the gauge metric in Prometheus format to the given writer.
|
||||||
|
// This satisfies the varz.PrometheusWriter interface.
|
||||||
|
func (g *Gauge) WritePrometheus(w io.Writer, name string) {
|
||||||
|
io.WriteString(w, "# TYPE ")
|
||||||
|
io.WriteString(w, name)
|
||||||
|
io.WriteString(w, " gauge\n")
|
||||||
|
if g.help != "" {
|
||||||
|
io.WriteString(w, "# HELP ")
|
||||||
|
io.WriteString(w, name)
|
||||||
|
io.WriteString(w, " ")
|
||||||
|
io.WriteString(w, g.help)
|
||||||
|
io.WriteString(w, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
io.WriteString(w, name)
|
||||||
|
fmt.Fprintf(w, " %v\n", g.m.Value())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handler returns a varz.Handler that serves the userfacing expvar contained
|
// Handler returns a varz.Handler that serves the userfacing expvar contained
|
||||||
|
25
util/usermetric/usermetric_test.go
Normal file
25
util/usermetric/usermetric_test.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||||||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
|
||||||
|
package usermetric
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGauge(t *testing.T) {
|
||||||
|
g := NewGauge("test_gauge", "This is a test gauge")
|
||||||
|
g.Set(15)
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
g.WritePrometheus(&buf, "test_gauge")
|
||||||
|
const want = `# TYPE test_gauge gauge
|
||||||
|
# HELP test_gauge This is a test gauge
|
||||||
|
test_gauge 15
|
||||||
|
`
|
||||||
|
if got := buf.String(); got != want {
|
||||||
|
t.Errorf("got %q; want %q", got, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user