metrics: add a NewSet and Set.NewLabelMap helpers
Updates tailscale/corp#31174 Signed-off-by: Anton Tolchanov <anton@tailscale.com>
This commit is contained in:
committed by
Anton Tolchanov
parent
17b0c7bfb3
commit
58042e2de3
@@ -29,6 +29,21 @@ type Set struct {
|
|||||||
expvar.Map
|
expvar.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewSet creates and publishes a new Set with the given name.
|
||||||
|
func NewSet(name string) *Set {
|
||||||
|
s := &Set{}
|
||||||
|
expvar.Publish(name, s)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLabelMap creates a new LabelMap metric with the given
|
||||||
|
// metric name and label name, and adds it to the Set.
|
||||||
|
func (s *Set) NewLabelMap(metric, label string) *LabelMap {
|
||||||
|
m := &LabelMap{Label: label}
|
||||||
|
s.Set(metric, m)
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
// LabelMap is a string-to-Var map variable that satisfies the
|
// LabelMap is a string-to-Var map variable that satisfies the
|
||||||
// expvar.Var interface.
|
// expvar.Var interface.
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ package stunserver
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"expvar"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@@ -20,9 +19,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
stats = new(metrics.Set)
|
stats = metrics.NewSet("stun")
|
||||||
stunDisposition = &metrics.LabelMap{Label: "disposition"}
|
stunDisposition = stats.NewLabelMap("counter_requests", "disposition")
|
||||||
stunAddrFamily = &metrics.LabelMap{Label: "family"}
|
stunAddrFamily = stats.NewLabelMap("counter_addrfamily", "family")
|
||||||
stunReadError = stunDisposition.Get("read_error")
|
stunReadError = stunDisposition.Get("read_error")
|
||||||
stunNotSTUN = stunDisposition.Get("not_stun")
|
stunNotSTUN = stunDisposition.Get("not_stun")
|
||||||
stunWriteError = stunDisposition.Get("write_error")
|
stunWriteError = stunDisposition.Get("write_error")
|
||||||
@@ -32,12 +31,6 @@ var (
|
|||||||
stunIPv6 = stunAddrFamily.Get("ipv6")
|
stunIPv6 = stunAddrFamily.Get("ipv6")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
stats.Set("counter_requests", stunDisposition)
|
|
||||||
stats.Set("counter_addrfamily", stunAddrFamily)
|
|
||||||
expvar.Publish("stun", stats)
|
|
||||||
}
|
|
||||||
|
|
||||||
type STUNServer struct {
|
type STUNServer struct {
|
||||||
ctx context.Context // ctx signals service shutdown
|
ctx context.Context // ctx signals service shutdown
|
||||||
pc *net.UDPConn // pc is the UDP listener
|
pc *net.UDPConn // pc is the UDP listener
|
||||||
|
|||||||
Reference in New Issue
Block a user