mirror of
https://github.com/tailscale/tailscale.git
synced 2026-02-11 16:31:54 -05:00
This file was never truly necessary and has never actually been used in the history of Tailscale's open source releases. A Brief History of AUTHORS files --- The AUTHORS file was a pattern developed at Google, originally for Chromium, then adopted by Go and a bunch of other projects. The problem was that Chromium originally had a copyright line only recognizing Google as the copyright holder. Because Google (and most open source projects) do not require copyright assignemnt for contributions, each contributor maintains their copyright. Some large corporate contributors then tried to add their own name to the copyright line in the LICENSE file or in file headers. This quickly becomes unwieldy, and puts a tremendous burden on anyone building on top of Chromium, since the license requires that they keep all copyright lines intact. The compromise was to create an AUTHORS file that would list all of the copyright holders. The LICENSE file and source file headers would then include that list by reference, listing the copyright holder as "The Chromium Authors". This also become cumbersome to simply keep the file up to date with a high rate of new contributors. Plus it's not always obvious who the copyright holder is. Sometimes it is the individual making the contribution, but many times it may be their employer. There is no way for the proejct maintainer to know. Eventually, Google changed their policy to no longer recommend trying to keep the AUTHORS file up to date proactively, and instead to only add to it when requested: https://opensource.google/docs/releasing/authors. They are also clear that: > Adding contributors to the AUTHORS file is entirely within the > project's discretion and has no implications for copyright ownership. It was primarily added to appease a small number of large contributors that insisted that they be recognized as copyright holders (which was entirely their right to do). But it's not truly necessary, and not even the most accurate way of identifying contributors and/or copyright holders. In practice, we've never added anyone to our AUTHORS file. It only lists Tailscale, so it's not really serving any purpose. It also causes confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header in other open source repos which don't actually have an AUTHORS file, so it's ambiguous what that means. Instead, we just acknowledge that the contributors to Tailscale (whoever they are) are copyright holders for their individual contributions. We also have the benefit of using the DCO (developercertificate.org) which provides some additional certification of their right to make the contribution. The source file changes were purely mechanical with: git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g' Updates #cleanup Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d Signed-off-by: Will Norris <will@tailscale.com>
298 lines
8.1 KiB
Go
298 lines
8.1 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package metrics
|
|
|
|
import (
|
|
"expvar"
|
|
"fmt"
|
|
"io"
|
|
"reflect"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
// MultiLabelMap is a struct-value-to-Var map variable that satisfies the
|
|
// [expvar.Var] interface but also allows for multiple Prometheus labels to be
|
|
// associated with each value.
|
|
//
|
|
// T must be a struct type with scalar fields. The struct field names
|
|
// (lowercased) are used as the labels, unless a "prom" struct tag is present.
|
|
// The struct fields must all be strings, and the string values must be valid
|
|
// Prometheus label values without requiring quoting.
|
|
type MultiLabelMap[T comparable] struct {
|
|
Type string // optional Prometheus type ("counter", "gauge")
|
|
Help string // optional Prometheus help string
|
|
|
|
m sync.Map // map[T]expvar.Var
|
|
|
|
mu sync.RWMutex
|
|
sorted []labelsAndValue[T] // by labels string, to match expvar.Map + for aesthetics in output
|
|
}
|
|
|
|
// NewMultiLabelMap creates and publishes (via expvar.Publish) a new
|
|
// MultiLabelMap[T] variable with the given name and returns it.
|
|
func NewMultiLabelMap[T comparable](name string, promType, helpText string) *MultiLabelMap[T] {
|
|
m := &MultiLabelMap[T]{
|
|
Type: promType,
|
|
Help: helpText,
|
|
}
|
|
var zero T
|
|
_ = LabelString(zero) // panic early if T is invalid
|
|
expvar.Publish(name, m)
|
|
return m
|
|
}
|
|
|
|
type labelsAndValue[T comparable] struct {
|
|
key T
|
|
labels string // Prometheus-formatted {label="value",label="value"} string
|
|
val expvar.Var
|
|
}
|
|
|
|
// LabelString returns a Prometheus-formatted label string for the given key.
|
|
// k must be a struct type with scalar fields, as required by MultiLabelMap,
|
|
// if k is not a struct, it will panic.
|
|
func LabelString(k any) string {
|
|
rv := reflect.ValueOf(k)
|
|
t := rv.Type()
|
|
if t.Kind() != reflect.Struct {
|
|
panic(fmt.Sprintf("MultiLabelMap must use keys of type struct; got %v", t))
|
|
}
|
|
|
|
var sb strings.Builder
|
|
sb.WriteString("{")
|
|
|
|
for i := range t.NumField() {
|
|
if i > 0 {
|
|
sb.WriteString(",")
|
|
}
|
|
ft := t.Field(i)
|
|
label := ft.Tag.Get("prom")
|
|
if label == "" {
|
|
label = strings.ToLower(ft.Name)
|
|
}
|
|
fv := rv.Field(i)
|
|
switch fv.Kind() {
|
|
case reflect.String:
|
|
fmt.Fprintf(&sb, "%s=%q", label, fv.String())
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
fmt.Fprintf(&sb, "%s=\"%d\"", label, fv.Int())
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
fmt.Fprintf(&sb, "%s=\"%d\"", label, fv.Uint())
|
|
case reflect.Bool:
|
|
fmt.Fprintf(&sb, "%s=\"%v\"", label, fv.Bool())
|
|
default:
|
|
panic(fmt.Sprintf("MultiLabelMap key field %q has unsupported type %v", ft.Name, fv.Type()))
|
|
}
|
|
}
|
|
sb.WriteString("}")
|
|
return sb.String()
|
|
}
|
|
|
|
// KeyValue represents a single entry in a [MultiLabelMap].
|
|
type KeyValue[T comparable] struct {
|
|
Key T
|
|
Value expvar.Var
|
|
}
|
|
|
|
func (v *MultiLabelMap[T]) String() string {
|
|
// NOTE: This has to be valid JSON because it's used by expvar.
|
|
return `"MultiLabelMap"`
|
|
}
|
|
|
|
// WritePrometheus writes v to w in Prometheus exposition format.
|
|
// The name argument is the metric name.
|
|
func (v *MultiLabelMap[T]) WritePrometheus(w io.Writer, name string) {
|
|
if v.Type != "" {
|
|
io.WriteString(w, "# TYPE ")
|
|
io.WriteString(w, name)
|
|
io.WriteString(w, " ")
|
|
io.WriteString(w, v.Type)
|
|
io.WriteString(w, "\n")
|
|
}
|
|
if v.Help != "" {
|
|
io.WriteString(w, "# HELP ")
|
|
io.WriteString(w, name)
|
|
io.WriteString(w, " ")
|
|
io.WriteString(w, v.Help)
|
|
io.WriteString(w, "\n")
|
|
}
|
|
v.mu.RLock()
|
|
defer v.mu.RUnlock()
|
|
|
|
for _, kv := range v.sorted {
|
|
io.WriteString(w, name)
|
|
io.WriteString(w, kv.labels)
|
|
switch v := kv.val.(type) {
|
|
case *expvar.Int:
|
|
fmt.Fprintf(w, " %d\n", v.Value())
|
|
case *expvar.Float:
|
|
fmt.Fprintf(w, " %v\n", v.Value())
|
|
default:
|
|
fmt.Fprintf(w, " %s\n", kv.val)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Init removes all keys from the map.
|
|
//
|
|
// Think of it as "Reset", but it's named Init to match expvar.Map.Init.
|
|
func (v *MultiLabelMap[T]) Init() *MultiLabelMap[T] {
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
v.sorted = nil
|
|
v.m.Range(func(k, _ any) bool {
|
|
v.m.Delete(k)
|
|
return true
|
|
})
|
|
return v
|
|
}
|
|
|
|
// addKeyLocked updates the sorted list of keys in v.keys.
|
|
//
|
|
// v.mu must be held.
|
|
func (v *MultiLabelMap[T]) addKeyLocked(key T, val expvar.Var) {
|
|
ls := LabelString(key)
|
|
|
|
ent := labelsAndValue[T]{key, ls, val}
|
|
// Using insertion sort to place key into the already-sorted v.keys.
|
|
i := sort.Search(len(v.sorted), func(i int) bool {
|
|
return v.sorted[i].labels >= ls
|
|
})
|
|
if i >= len(v.sorted) {
|
|
v.sorted = append(v.sorted, ent)
|
|
} else if v.sorted[i].key == key {
|
|
v.sorted[i].val = val
|
|
} else {
|
|
var zero labelsAndValue[T]
|
|
v.sorted = append(v.sorted, zero)
|
|
copy(v.sorted[i+1:], v.sorted[i:])
|
|
v.sorted[i] = ent
|
|
}
|
|
}
|
|
|
|
// Get returns the expvar for the given key, or nil if it doesn't exist.
|
|
func (v *MultiLabelMap[T]) Get(key T) expvar.Var {
|
|
i, _ := v.m.Load(key)
|
|
av, _ := i.(expvar.Var)
|
|
return av
|
|
}
|
|
|
|
func newInt() expvar.Var { return new(expvar.Int) }
|
|
func newFloat() expvar.Var { return new(expvar.Float) }
|
|
|
|
// getOrFill returns the expvar.Var for the given key, atomically creating it
|
|
// once (for all callers) with fill if it doesn't exist.
|
|
func (v *MultiLabelMap[T]) getOrFill(key T, fill func() expvar.Var) expvar.Var {
|
|
if v := v.Get(key); v != nil {
|
|
return v
|
|
}
|
|
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
|
|
if v := v.Get(key); v != nil {
|
|
return v
|
|
}
|
|
nv := fill()
|
|
v.addKeyLocked(key, nv)
|
|
v.m.Store(key, nv)
|
|
return nv
|
|
}
|
|
|
|
// Set sets key to val.
|
|
//
|
|
// This is not optimized for highly concurrent usage; it's presumed to only be
|
|
// used rarely, at startup.
|
|
func (v *MultiLabelMap[T]) Set(key T, val expvar.Var) {
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
v.addKeyLocked(key, val)
|
|
v.m.Store(key, val)
|
|
}
|
|
|
|
// SetInt sets val to the *[expvar.Int] value stored under the given map key,
|
|
// creating it if it doesn't exist yet.
|
|
// It does nothing if key exists but is of the wrong type.
|
|
func (v *MultiLabelMap[T]) SetInt(key T, val int64) {
|
|
// Set to Int; ignore otherwise.
|
|
if iv, ok := v.getOrFill(key, newInt).(*expvar.Int); ok {
|
|
iv.Set(val)
|
|
}
|
|
}
|
|
|
|
// SetFloat sets val to the *[expvar.Float] value stored under the given map key,
|
|
// creating it if it doesn't exist yet.
|
|
// It does nothing if key exists but is of the wrong type.
|
|
func (v *MultiLabelMap[T]) SetFloat(key T, val float64) {
|
|
// Set to Float; ignore otherwise.
|
|
if iv, ok := v.getOrFill(key, newFloat).(*expvar.Float); ok {
|
|
iv.Set(val)
|
|
}
|
|
}
|
|
|
|
// Add adds delta to the *[expvar.Int] value stored under the given map key,
|
|
// creating it if it doesn't exist yet.
|
|
// It does nothing if key exists but is of the wrong type.
|
|
func (v *MultiLabelMap[T]) Add(key T, delta int64) {
|
|
// Add to Int; ignore otherwise.
|
|
if iv, ok := v.getOrFill(key, newInt).(*expvar.Int); ok {
|
|
iv.Add(delta)
|
|
}
|
|
}
|
|
|
|
// Add adds delta to the *[expvar.Float] value stored under the given map key,
|
|
// creating it if it doesn't exist yet.
|
|
// It does nothing if key exists but is of the wrong type.
|
|
func (v *MultiLabelMap[T]) AddFloat(key T, delta float64) {
|
|
// Add to Float; ignore otherwise.
|
|
if iv, ok := v.getOrFill(key, newFloat).(*expvar.Float); ok {
|
|
iv.Add(delta)
|
|
}
|
|
}
|
|
|
|
// Delete deletes the given key from the map.
|
|
//
|
|
// This is not optimized for highly concurrent usage; it's presumed to only be
|
|
// used rarely, at startup.
|
|
func (v *MultiLabelMap[T]) Delete(key T) {
|
|
ls := LabelString(key)
|
|
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
|
|
// Using insertion sort to place key into the already-sorted v.keys.
|
|
i := sort.Search(len(v.sorted), func(i int) bool {
|
|
return v.sorted[i].labels >= ls
|
|
})
|
|
if i < len(v.sorted) && v.sorted[i].key == key {
|
|
v.sorted = append(v.sorted[:i], v.sorted[i+1:]...)
|
|
v.m.Delete(key)
|
|
}
|
|
}
|
|
|
|
// Do calls f for each entry in the map.
|
|
// The map is locked during the iteration,
|
|
// but existing entries may be concurrently updated.
|
|
func (v *MultiLabelMap[T]) Do(f func(KeyValue[T])) {
|
|
v.mu.RLock()
|
|
defer v.mu.RUnlock()
|
|
for _, e := range v.sorted {
|
|
f(KeyValue[T]{e.key, e.val})
|
|
}
|
|
}
|
|
|
|
// ResetAllForTest resets all values for metrics to zero.
|
|
// Should only be used in tests.
|
|
func (v *MultiLabelMap[T]) ResetAllForTest() {
|
|
v.Do(func(kv KeyValue[T]) {
|
|
switch v := kv.Value.(type) {
|
|
case *expvar.Int:
|
|
v.Set(0)
|
|
case *expvar.Float:
|
|
v.Set(0)
|
|
}
|
|
})
|
|
}
|