1
0
mirror of https://github.com/golang/go synced 2024-11-18 15:14:44 -07:00
go/internal/telemetry/tag.go
Ian Cottrell 5b08f89bfc internal/telemetry: clean up the exporter api
this shuffles things so there a single exporter API rather than an observer
It also removes most of the globals.
per telemetry type.

Change-Id: Iaa82abe2ded1fff9df8e067ed4a55bcbd9d9591f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/190405
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2019-08-15 23:56:12 +00:00

72 lines
1.6 KiB
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package telemetry
import (
"context"
"fmt"
)
// Tag holds a key and value pair.
// It is normally used when passing around lists of tags.
type Tag struct {
Key interface{}
Value interface{}
}
// TagList is a way of passing around a collection of key value pairs.
// It is an alternative to the less efficient and unordered method of using
// maps.
type TagList []Tag
// Format is used for debug printing of tags.
func (t Tag) Format(f fmt.State, r rune) {
fmt.Fprintf(f, `%v="%v"`, t.Key, t.Value)
}
// Get returns the tag unmodified.
// It makes Key conform to the Tagger interface.
func (t Tag) Tag(ctx context.Context) Tag {
return t
}
// Get will get a single key's value from the list.
func (l TagList) Get(k interface{}) interface{} {
for _, t := range l {
if t.Key == k {
return t.Value
}
}
return nil
}
// Format pretty prints a list.
// It is intended only for debugging.
func (l TagList) Format(f fmt.State, r rune) {
printed := false
for _, t := range l {
if t.Value == nil {
continue
}
if printed {
fmt.Fprint(f, ",")
}
fmt.Fprint(f, t)
printed = true
}
}
// Equal returns true if two lists are identical.
func (l TagList) Equal(other TagList) bool {
//TODO: make this more efficient
return fmt.Sprint(l) == fmt.Sprint(other)
}
// Less is intended only for using tag lists as a sorting key.
func (l TagList) Less(other TagList) bool {
//TODO: make this more efficient
return fmt.Sprint(l) < fmt.Sprint(other)
}