2024-08-01 17:42:19 +02:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
2024-08-12 14:46:49 +02:00
|
|
|
// Package usermetric provides a container and handler
|
2024-08-01 17:42:19 +02:00
|
|
|
// for user-facing metrics.
|
2024-08-12 14:46:49 +02:00
|
|
|
package usermetric
|
2024-08-01 17:42:19 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"expvar"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"tailscale.com/metrics"
|
|
|
|
"tailscale.com/tsweb/varz"
|
|
|
|
)
|
|
|
|
|
|
|
|
var vars expvar.Map
|
|
|
|
|
|
|
|
// NewMultiLabelMap creates and register a new
|
|
|
|
// MultiLabelMap[T] variable with the given name and returns it.
|
|
|
|
// The variable is registered with the userfacing metrics package.
|
|
|
|
//
|
2024-08-12 14:46:49 +02:00
|
|
|
// Note that usermetric are not protected against duplicate
|
2024-08-01 17:42:19 +02:00
|
|
|
// metrics name. It is the caller's responsibility to ensure that
|
|
|
|
// the name is unique.
|
|
|
|
func NewMultiLabelMap[T comparable](name string, promType, helpText string) *metrics.MultiLabelMap[T] {
|
|
|
|
m := &metrics.MultiLabelMap[T]{
|
|
|
|
Type: promType,
|
|
|
|
Help: helpText,
|
|
|
|
}
|
|
|
|
var zero T
|
|
|
|
_ = metrics.LabelString(zero) // panic early if T is invalid
|
|
|
|
vars.Set(name, m)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler returns a varz.Handler that serves the userfacing expvar contained
|
|
|
|
// in this package.
|
|
|
|
func Handler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
varz.ExpvarDoHandler(vars.Do)(w, r)
|
|
|
|
}
|