1
0
mirror of https://github.com/golang/go synced 2024-11-18 08:54:45 -07:00

internal/event: extract keys to their own package

Now key types can be implemented outside the package that holds labels or events, they should be.
This prevents the large list of types from poluting the public interface of the core packages.

Change-Id: I927f31cb5e4e1d0c29619681015962f890623e5c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/229240
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Ian Cottrell 2020-04-20 21:50:02 -04:00
parent c81623a0cb
commit a82abb5396
22 changed files with 680 additions and 681 deletions

View File

@ -9,6 +9,7 @@ import (
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/event/export"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
)
@ -18,12 +19,12 @@ type Hooks struct {
}
var (
aValue = core.NewIntKey("a", "")
bValue = core.NewStringKey("b", "")
aCount = core.NewInt64Key("aCount", "Count of time A is called.")
aStat = core.NewIntKey("aValue", "A value.")
bCount = core.NewInt64Key("B", "Count of time B is called.")
bLength = core.NewIntKey("BLen", "B length.")
aValue = keys.NewInt("a", "")
bValue = keys.NewString("b", "")
aCount = keys.NewInt64("aCount", "Count of time A is called.")
aStat = keys.NewInt("aValue", "A value.")
bCount = keys.NewInt64("B", "Count of time B is called.")
bLength = keys.NewInt("BLen", "B length.")
Baseline = Hooks{
A: func(ctx context.Context, a int) (context.Context, func()) {

View File

@ -55,27 +55,11 @@ func (ev Event) IsDetach() bool { return ev.typ == DetachType }
func (ev Event) IsRecord() bool { return ev.typ == RecordType }
func (ev Event) Format(f fmt.State, r rune) {
lm := label.Map(ev)
if !ev.At.IsZero() {
fmt.Fprint(f, ev.At.Format("2006/01/02 15:04:05 "))
}
msg := Msg.Get(lm)
err := Err.Get(lm)
fmt.Fprint(f, msg)
if err != nil {
if f.Flag('+') {
fmt.Fprintf(f, ": %+v", err)
} else {
fmt.Fprintf(f, ": %v", err)
}
}
for index := 0; ev.Valid(index); index++ {
l := ev.Label(index)
// msg and err were both already printed above, so we skip them to avoid
// double printing
if !l.Valid() || l.Key() == Msg || l.Key() == Err {
continue
}
fmt.Fprintf(f, "\n\t%v", l)
}
}

View File

@ -7,19 +7,20 @@ package core
import (
"context"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
)
// Log1 takes a message and one label delivers a log event to the exporter.
// It is a customized version of Print that is faster and does no allocation.
func Log1(ctx context.Context, message string, t1 label.Label) {
Export(ctx, MakeEvent(LogType, [3]label.Label{Msg.Of(message), t1}, nil))
Export(ctx, MakeEvent(LogType, [3]label.Label{keys.Msg.Of(message), t1}, nil))
}
// Log2 takes a message and two labels and delivers a log event to the exporter.
// It is a customized version of Print that is faster and does no allocation.
func Log2(ctx context.Context, message string, t1 label.Label, t2 label.Label) {
Export(ctx, MakeEvent(LogType, [3]label.Label{Msg.Of(message), t1, t2}, nil))
Export(ctx, MakeEvent(LogType, [3]label.Label{keys.Msg.Of(message), t1, t2}, nil))
}
// Metric1 sends a label event to the exporter with the supplied labels.
@ -42,7 +43,7 @@ func Metric3(ctx context.Context, t1, t2, t3 label.Label) context.Context {
// deferred.
func Start1(ctx context.Context, name string, t1 label.Label) (context.Context, func()) {
return ExportPair(ctx,
MakeEvent(StartSpanType, [3]label.Label{Name.Of(name), t1}, nil),
MakeEvent(StartSpanType, [3]label.Label{keys.Name.Of(name), t1}, nil),
MakeEvent(EndSpanType, [3]label.Label{}, nil))
}
@ -51,6 +52,6 @@ func Start1(ctx context.Context, name string, t1 label.Label) (context.Context,
// deferred.
func Start2(ctx context.Context, name string, t1, t2 label.Label) (context.Context, func()) {
return ExportPair(ctx,
MakeEvent(StartSpanType, [3]label.Label{Name.Of(name), t1, t2}, nil),
MakeEvent(StartSpanType, [3]label.Label{keys.Name.Of(name), t1, t2}, nil),
MakeEvent(EndSpanType, [3]label.Label{}, nil))
}

View File

@ -1,551 +0,0 @@
// 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 core
import (
"fmt"
"io"
"math"
"strconv"
"golang.org/x/tools/internal/event/label"
)
var (
// Msg is a key used to add message strings to label lists.
Msg = NewStringKey("message", "a readable message")
// Name is used for things like traces that have a name.
Name = NewStringKey("name", "an entity name")
// Err is a key used to add error values to label lists.
Err = NewErrorKey("error", "an error that occurred")
)
// ValueKey represents a key for untyped values.
type ValueKey struct {
name string
description string
}
// NewKey creates a new Key for untyped values.
func NewKey(name, description string) *ValueKey {
return &ValueKey{name: name, description: description}
}
func (k *ValueKey) Name() string { return k.name }
func (k *ValueKey) Description() string { return k.description }
func (k *ValueKey) Format(w io.Writer, buf []byte, l label.Label) {
fmt.Fprint(w, k.From(l))
}
// Get can be used to get a label for the key from a label.Map.
func (k *ValueKey) Get(lm label.Map) interface{} {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return nil
}
// From can be used to get a value from a Label.
func (k *ValueKey) From(t label.Label) interface{} { return t.UnpackValue() }
// Of creates a new Label with this key and the supplied value.
func (k *ValueKey) Of(value interface{}) label.Label { return label.OfValue(k, value) }
// IntKey represents a key
type IntKey struct {
name string
description string
}
// NewIntKey creates a new Key for int values.
func NewIntKey(name, description string) *IntKey {
return &IntKey{name: name, description: description}
}
func (k *IntKey) Name() string { return k.name }
func (k *IntKey) Description() string { return k.description }
func (k *IntKey) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *IntKey) Of(v int) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *IntKey) Get(lm label.Map) int {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *IntKey) From(t label.Label) int { return int(t.Unpack64()) }
// Int8Key represents a key
type Int8Key struct {
name string
description string
}
// NewInt8Key creates a new Key for int8 values.
func NewInt8Key(name, description string) *Int8Key {
return &Int8Key{name: name, description: description}
}
func (k *Int8Key) Name() string { return k.name }
func (k *Int8Key) Description() string { return k.description }
func (k *Int8Key) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *Int8Key) Of(v int8) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *Int8Key) Get(lm label.Map) int8 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *Int8Key) From(t label.Label) int8 { return int8(t.Unpack64()) }
// Int16Key represents a key
type Int16Key struct {
name string
description string
}
// NewInt16Key creates a new Key for int16 values.
func NewInt16Key(name, description string) *Int16Key {
return &Int16Key{name: name, description: description}
}
func (k *Int16Key) Name() string { return k.name }
func (k *Int16Key) Description() string { return k.description }
func (k *Int16Key) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *Int16Key) Of(v int16) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *Int16Key) Get(lm label.Map) int16 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *Int16Key) From(t label.Label) int16 { return int16(t.Unpack64()) }
// Int32Key represents a key
type Int32Key struct {
name string
description string
}
// NewInt32Key creates a new Key for int32 values.
func NewInt32Key(name, description string) *Int32Key {
return &Int32Key{name: name, description: description}
}
func (k *Int32Key) Name() string { return k.name }
func (k *Int32Key) Description() string { return k.description }
func (k *Int32Key) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *Int32Key) Of(v int32) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *Int32Key) Get(lm label.Map) int32 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *Int32Key) From(t label.Label) int32 { return int32(t.Unpack64()) }
// Int64Key represents a key
type Int64Key struct {
name string
description string
}
// NewInt64Key creates a new Key for int64 values.
func NewInt64Key(name, description string) *Int64Key {
return &Int64Key{name: name, description: description}
}
func (k *Int64Key) Name() string { return k.name }
func (k *Int64Key) Description() string { return k.description }
func (k *Int64Key) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendInt(buf, k.From(l), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *Int64Key) Of(v int64) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *Int64Key) Get(lm label.Map) int64 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *Int64Key) From(t label.Label) int64 { return int64(t.Unpack64()) }
// UIntKey represents a key
type UIntKey struct {
name string
description string
}
// NewUIntKey creates a new Key for uint values.
func NewUIntKey(name, description string) *UIntKey {
return &UIntKey{name: name, description: description}
}
func (k *UIntKey) Name() string { return k.name }
func (k *UIntKey) Description() string { return k.description }
func (k *UIntKey) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *UIntKey) Of(v uint) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *UIntKey) Get(lm label.Map) uint {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *UIntKey) From(t label.Label) uint { return uint(t.Unpack64()) }
// UInt8Key represents a key
type UInt8Key struct {
name string
description string
}
// NewUInt8Key creates a new Key for uint8 values.
func NewUInt8Key(name, description string) *UInt8Key {
return &UInt8Key{name: name, description: description}
}
func (k *UInt8Key) Name() string { return k.name }
func (k *UInt8Key) Description() string { return k.description }
func (k *UInt8Key) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *UInt8Key) Of(v uint8) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *UInt8Key) Get(lm label.Map) uint8 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *UInt8Key) From(t label.Label) uint8 { return uint8(t.Unpack64()) }
// UInt16Key represents a key
type UInt16Key struct {
name string
description string
}
// NewUInt16Key creates a new Key for uint16 values.
func NewUInt16Key(name, description string) *UInt16Key {
return &UInt16Key{name: name, description: description}
}
func (k *UInt16Key) Name() string { return k.name }
func (k *UInt16Key) Description() string { return k.description }
func (k *UInt16Key) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *UInt16Key) Of(v uint16) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *UInt16Key) Get(lm label.Map) uint16 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *UInt16Key) From(t label.Label) uint16 { return uint16(t.Unpack64()) }
// UInt32Key represents a key
type UInt32Key struct {
name string
description string
}
// NewUInt32Key creates a new Key for uint32 values.
func NewUInt32Key(name, description string) *UInt32Key {
return &UInt32Key{name: name, description: description}
}
func (k *UInt32Key) Name() string { return k.name }
func (k *UInt32Key) Description() string { return k.description }
func (k *UInt32Key) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *UInt32Key) Of(v uint32) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *UInt32Key) Get(lm label.Map) uint32 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *UInt32Key) From(t label.Label) uint32 { return uint32(t.Unpack64()) }
// UInt64Key represents a key
type UInt64Key struct {
name string
description string
}
// NewUInt64Key creates a new Key for uint64 values.
func NewUInt64Key(name, description string) *UInt64Key {
return &UInt64Key{name: name, description: description}
}
func (k *UInt64Key) Name() string { return k.name }
func (k *UInt64Key) Description() string { return k.description }
func (k *UInt64Key) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendUint(buf, k.From(l), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *UInt64Key) Of(v uint64) label.Label { return label.Of64(k, v) }
// Get can be used to get a label for the key from a label.Map.
func (k *UInt64Key) Get(lm label.Map) uint64 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *UInt64Key) From(t label.Label) uint64 { return t.Unpack64() }
// Float32Key represents a key
type Float32Key struct {
name string
description string
}
// NewFloat32Key creates a new Key for float32 values.
func NewFloat32Key(name, description string) *Float32Key {
return &Float32Key{name: name, description: description}
}
func (k *Float32Key) Name() string { return k.name }
func (k *Float32Key) Description() string { return k.description }
func (k *Float32Key) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendFloat(buf, float64(k.From(l)), 'E', -1, 32))
}
// Of creates a new Label with this key and the supplied value.
func (k *Float32Key) Of(v float32) label.Label {
return label.Of64(k, uint64(math.Float32bits(v)))
}
// Get can be used to get a label for the key from a label.Map.
func (k *Float32Key) Get(lm label.Map) float32 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *Float32Key) From(t label.Label) float32 {
return math.Float32frombits(uint32(t.Unpack64()))
}
// Float64Key represents a key
type Float64Key struct {
name string
description string
}
// NewFloat64Key creates a new Key for int64 values.
func NewFloat64Key(name, description string) *Float64Key {
return &Float64Key{name: name, description: description}
}
func (k *Float64Key) Name() string { return k.name }
func (k *Float64Key) Description() string { return k.description }
func (k *Float64Key) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendFloat(buf, k.From(l), 'E', -1, 64))
}
// Of creates a new Label with this key and the supplied value.
func (k *Float64Key) Of(v float64) label.Label {
return label.Of64(k, math.Float64bits(v))
}
// Get can be used to get a label for the key from a label.Map.
func (k *Float64Key) Get(lm label.Map) float64 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *Float64Key) From(t label.Label) float64 {
return math.Float64frombits(t.Unpack64())
}
// StringKey represents a key
type StringKey struct {
name string
description string
}
// NewStringKey creates a new Key for int64 values.
func NewStringKey(name, description string) *StringKey {
return &StringKey{name: name, description: description}
}
func (k *StringKey) Name() string { return k.name }
func (k *StringKey) Description() string { return k.description }
func (k *StringKey) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendQuote(buf, k.From(l)))
}
// Of creates a new Label with this key and the supplied value.
func (k *StringKey) Of(v string) label.Label { return label.OfString(k, v) }
// Get can be used to get a label for the key from a label.Map.
func (k *StringKey) Get(lm label.Map) string {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return ""
}
// From can be used to get a value from a Label.
func (k *StringKey) From(t label.Label) string { return t.UnpackString() }
// BooleanKey represents a key
type BooleanKey struct {
name string
description string
}
// NewBooleanKey creates a new Key for bool values.
func NewBooleanKey(name, description string) *BooleanKey {
return &BooleanKey{name: name, description: description}
}
func (k *BooleanKey) Name() string { return k.name }
func (k *BooleanKey) Description() string { return k.description }
func (k *BooleanKey) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendBool(buf, k.From(l)))
}
// Of creates a new Label with this key and the supplied value.
func (k *BooleanKey) Of(v bool) label.Label {
if v {
return label.Of64(k, 1)
}
return label.Of64(k, 0)
}
// Get can be used to get a label for the key from a label.Map.
func (k *BooleanKey) Get(lm label.Map) bool {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return false
}
// From can be used to get a value from a Label.
func (k *BooleanKey) From(t label.Label) bool { return t.Unpack64() > 0 }
// ErrorKey represents a key
type ErrorKey struct {
name string
description string
}
// NewErrorKey creates a new Key for int64 values.
func NewErrorKey(name, description string) *ErrorKey {
return &ErrorKey{name: name, description: description}
}
func (k *ErrorKey) Name() string { return k.name }
func (k *ErrorKey) Description() string { return k.description }
func (k *ErrorKey) Format(w io.Writer, buf []byte, l label.Label) {
io.WriteString(w, k.From(l).Error())
}
// Of creates a new Label with this key and the supplied value.
func (k *ErrorKey) Of(v error) label.Label { return label.OfValue(k, v) }
// Get can be used to get a label for the key from a label.Map.
func (k *ErrorKey) Get(lm label.Map) error {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return nil
}
// From can be used to get a value from a Label.
func (k *ErrorKey) From(t label.Label) error {
err, _ := t.UnpackValue().(error)
return err
}

View File

@ -8,6 +8,7 @@ import (
"context"
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
)
@ -26,7 +27,7 @@ func SetExporter(e Exporter) {
// before delivering them to the exporter.
func Log(ctx context.Context, message string, labels ...label.Label) {
core.Export(ctx, core.MakeEvent(core.LogType, [3]label.Label{
core.Msg.Of(message),
keys.Msg.Of(message),
}, labels))
}
@ -35,8 +36,8 @@ func Log(ctx context.Context, message string, labels ...label.Label) {
// delivered event.
func Error(ctx context.Context, message string, err error, labels ...label.Label) {
core.Export(ctx, core.MakeEvent(core.LogType, [3]label.Label{
core.Msg.Of(message),
core.Err.Of(err),
keys.Msg.Of(message),
keys.Err.Of(err),
}, labels))
}
@ -56,7 +57,7 @@ func Label(ctx context.Context, labels ...label.Label) context.Context {
func Start(ctx context.Context, name string, labels ...label.Label) (context.Context, func()) {
return core.ExportPair(ctx,
core.MakeEvent(core.StartSpanType, [3]label.Label{
core.Name.Of(name),
keys.Name.Of(name),
}, labels),
core.MakeEvent(core.EndSpanType, [3]label.Label{}, nil))
}

View File

@ -12,6 +12,7 @@ import (
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
)
@ -34,7 +35,7 @@ type logWriter struct {
func (w *logWriter) ProcessEvent(ctx context.Context, ev core.Event, lm label.Map) context.Context {
switch {
case ev.IsLog():
if w.onlyErrors && core.Err.Get(lm) == nil {
if w.onlyErrors && keys.Err.Get(lm) == nil {
return ctx
}
w.mu.Lock()
@ -44,15 +45,15 @@ func (w *logWriter) ProcessEvent(ctx context.Context, ev core.Event, lm label.Ma
if !ev.At.IsZero() {
w.writer.Write(ev.At.AppendFormat(buf, "2006/01/02 15:04:05 "))
}
msg := core.Msg.Get(lm)
msg := keys.Msg.Get(lm)
io.WriteString(w.writer, msg)
if err := core.Err.Get(lm); err != nil {
if err := keys.Err.Get(lm); err != nil {
io.WriteString(w.writer, ": ")
io.WriteString(w.writer, err.Error())
}
for index := 0; ev.Valid(index); index++ {
l := ev.Label(index)
if !l.Valid() || l.Key() == core.Msg || l.Key() == core.Err {
if !l.Valid() || l.Key() == keys.Msg || l.Key() == keys.Err {
continue
}
io.WriteString(w.writer, "\n\t")

View File

@ -13,14 +13,15 @@ import (
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/event/export"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
)
func ExampleLog() {
ctx := context.Background()
event.SetExporter(timeFixer(export.LogWriter(os.Stdout, false)))
anInt := core.NewIntKey("myInt", "an integer")
aString := core.NewStringKey("myString", "a string")
anInt := keys.NewInt("myInt", "an integer")
aString := keys.NewString("myString", "a string")
event.Log(ctx, "my event", anInt.Of(6))
event.Error(ctx, "error event", errors.New("an error"), aString.Of("some string value"))
// Output:

View File

@ -9,7 +9,7 @@ import (
"sort"
"time"
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
)
@ -38,7 +38,7 @@ type Int64Data struct {
EndTime time.Time
groups [][]label.Label
key *core.Int64Key
key *keys.Int64
}
// Float64Data is a concrete implementation of Data for float64 scalar metrics.
@ -53,7 +53,7 @@ type Float64Data struct {
EndTime time.Time
groups [][]label.Label
key *core.Float64Key
key *keys.Float64
}
// HistogramInt64Data is a concrete implementation of Data for int64 histogram metrics.
@ -66,7 +66,7 @@ type HistogramInt64Data struct {
EndTime time.Time
groups [][]label.Label
key *core.Int64Key
key *keys.Int64
}
// HistogramInt64Row holds the values for a single row of a HistogramInt64Data.
@ -93,7 +93,7 @@ type HistogramFloat64Data struct {
EndTime time.Time
groups [][]label.Label
key *core.Float64Key
key *keys.Float64
}
// HistogramFloat64Row holds the values for a single row of a HistogramFloat64Data.

View File

@ -12,10 +12,11 @@ import (
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
)
var Entries = core.NewKey("metric_entries", "The set of metrics calculated for an event")
var Entries = keys.New("metric_entries", "The set of metrics calculated for an event")
type Config struct {
subscribers map[interface{}][]subscriber

View File

@ -5,7 +5,7 @@
package metric
import (
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
)
@ -54,7 +54,7 @@ func (info Scalar) Count(e *Config, key label.Key) {
// SumInt64 creates a new metric based on the Scalar information that sums all
// the values recorded on the int64 measure.
// Metrics of this type will use Int64Data.
func (info Scalar) SumInt64(e *Config, key *core.Int64Key) {
func (info Scalar) SumInt64(e *Config, key *keys.Int64) {
data := &Int64Data{Info: &info, key: key}
e.subscribe(key, data.sum)
}
@ -62,7 +62,7 @@ func (info Scalar) SumInt64(e *Config, key *core.Int64Key) {
// LatestInt64 creates a new metric based on the Scalar information that tracks
// the most recent value recorded on the int64 measure.
// Metrics of this type will use Int64Data.
func (info Scalar) LatestInt64(e *Config, key *core.Int64Key) {
func (info Scalar) LatestInt64(e *Config, key *keys.Int64) {
data := &Int64Data{Info: &info, IsGauge: true, key: key}
e.subscribe(key, data.latest)
}
@ -70,7 +70,7 @@ func (info Scalar) LatestInt64(e *Config, key *core.Int64Key) {
// SumFloat64 creates a new metric based on the Scalar information that sums all
// the values recorded on the float64 measure.
// Metrics of this type will use Float64Data.
func (info Scalar) SumFloat64(e *Config, key *core.Float64Key) {
func (info Scalar) SumFloat64(e *Config, key *keys.Float64) {
data := &Float64Data{Info: &info, key: key}
e.subscribe(key, data.sum)
}
@ -78,7 +78,7 @@ func (info Scalar) SumFloat64(e *Config, key *core.Float64Key) {
// LatestFloat64 creates a new metric based on the Scalar information that tracks
// the most recent value recorded on the float64 measure.
// Metrics of this type will use Float64Data.
func (info Scalar) LatestFloat64(e *Config, key *core.Float64Key) {
func (info Scalar) LatestFloat64(e *Config, key *keys.Float64) {
data := &Float64Data{Info: &info, IsGauge: true, key: key}
e.subscribe(key, data.latest)
}
@ -86,7 +86,7 @@ func (info Scalar) LatestFloat64(e *Config, key *core.Float64Key) {
// Record creates a new metric based on the HistogramInt64 information that
// tracks the bucketized counts of values recorded on the int64 measure.
// Metrics of this type will use HistogramInt64Data.
func (info HistogramInt64) Record(e *Config, key *core.Int64Key) {
func (info HistogramInt64) Record(e *Config, key *keys.Int64) {
data := &HistogramInt64Data{Info: &info, key: key}
e.subscribe(key, data.record)
}
@ -94,7 +94,7 @@ func (info HistogramInt64) Record(e *Config, key *core.Int64Key) {
// Record creates a new metric based on the HistogramFloat64 information that
// tracks the bucketized counts of values recorded on the float64 measure.
// Metrics of this type will use HistogramFloat64Data.
func (info HistogramFloat64) Record(e *Config, key *core.Float64Key) {
func (info HistogramFloat64) Record(e *Config, key *keys.Float64) {
data := &HistogramFloat64Data{Info: &info, key: key}
e.subscribe(key, data.record)
}

View File

@ -6,7 +6,7 @@ import (
"testing"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/event/keys"
)
func TestEncodeMetric(t *testing.T) {
@ -24,7 +24,7 @@ func TestEncodeMetric(t *testing.T) {
run: func(ctx context.Context) {
ctx = event.Label(ctx, keyMethod.Of("godoc.ServeHTTP"))
event.Metric(ctx, latencyMs.Of(96.58))
ctx = event.Label(ctx, core.Err.Of(errors.New("panic: fatal signal")))
ctx = event.Label(ctx, keys.Err.Of(errors.New("panic: fatal signal")))
event.Metric(ctx, bytesIn.Of(97e2))
},
want: prefix + `

View File

@ -22,6 +22,7 @@ import (
"golang.org/x/tools/internal/event/export"
"golang.org/x/tools/internal/event/export/metric"
"golang.org/x/tools/internal/event/export/ocagent/wire"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
)
@ -201,7 +202,7 @@ func convertSpan(span *export.Span) *wire.Span {
Kind: wire.UnspecifiedSpanKind,
StartTime: convertTimestamp(span.Start().At),
EndTime: convertTimestamp(span.Finish().At),
Attributes: convertAttributes(label.Filter(span.Start(), core.Name)),
Attributes: convertAttributes(label.Filter(span.Start(), keys.Name)),
TimeEvents: convertEvents(span.Events()),
SameProcessAsParentSpan: true,
//TODO: StackTrace?
@ -258,37 +259,37 @@ func convertAttributes(list label.List) *wire.Attributes {
func convertAttribute(l label.Label) wire.Attribute {
switch key := l.Key().(type) {
case *core.IntKey:
case *keys.Int:
return wire.IntAttribute{IntValue: int64(key.From(l))}
case *core.Int8Key:
case *keys.Int8:
return wire.IntAttribute{IntValue: int64(key.From(l))}
case *core.Int16Key:
case *keys.Int16:
return wire.IntAttribute{IntValue: int64(key.From(l))}
case *core.Int32Key:
case *keys.Int32:
return wire.IntAttribute{IntValue: int64(key.From(l))}
case *core.Int64Key:
case *keys.Int64:
return wire.IntAttribute{IntValue: int64(key.From(l))}
case *core.UIntKey:
case *keys.UInt:
return wire.IntAttribute{IntValue: int64(key.From(l))}
case *core.UInt8Key:
case *keys.UInt8:
return wire.IntAttribute{IntValue: int64(key.From(l))}
case *core.UInt16Key:
case *keys.UInt16:
return wire.IntAttribute{IntValue: int64(key.From(l))}
case *core.UInt32Key:
case *keys.UInt32:
return wire.IntAttribute{IntValue: int64(key.From(l))}
case *core.UInt64Key:
case *keys.UInt64:
return wire.IntAttribute{IntValue: int64(key.From(l))}
case *core.Float32Key:
case *keys.Float32:
return wire.DoubleAttribute{DoubleValue: float64(key.From(l))}
case *core.Float64Key:
case *keys.Float64:
return wire.DoubleAttribute{DoubleValue: key.From(l)}
case *core.BooleanKey:
case *keys.Boolean:
return wire.BoolAttribute{BoolValue: key.From(l)}
case *core.StringKey:
case *keys.String:
return wire.StringAttribute{StringValue: toTruncatableString(key.From(l))}
case *core.ErrorKey:
case *keys.Error:
return wire.StringAttribute{StringValue: toTruncatableString(key.From(l).Error())}
case *core.ValueKey:
case *keys.Value:
return wire.StringAttribute{StringValue: toTruncatableString(fmt.Sprint(key.From(l)))}
default:
return wire.StringAttribute{StringValue: toTruncatableString(fmt.Sprintf("%T", key))}
@ -316,11 +317,11 @@ func convertAnnotation(ev core.Event) *wire.Annotation {
return nil
}
lm := label.Map(ev)
description := core.Msg.Get(lm)
labels := label.Filter(ev, core.Msg)
description := keys.Msg.Get(lm)
labels := label.Filter(ev, keys.Msg)
if description == "" {
err := core.Err.Get(lm)
labels = label.Filter(labels, core.Err)
err := keys.Err.Get(lm)
labels = label.Filter(labels, keys.Err)
if err != nil {
description = err.Error()
}

View File

@ -20,6 +20,7 @@ import (
"golang.org/x/tools/internal/event/export"
"golang.org/x/tools/internal/event/export/metric"
"golang.org/x/tools/internal/event/export/ocagent"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
)
@ -41,34 +42,34 @@ const testNodeStr = `{
},`
var (
keyDB = core.NewStringKey("db", "the database name")
keyMethod = core.NewStringKey("method", "a metric grouping key")
keyRoute = core.NewStringKey("route", "another metric grouping key")
keyDB = keys.NewString("db", "the database name")
keyMethod = keys.NewString("method", "a metric grouping key")
keyRoute = keys.NewString("route", "another metric grouping key")
key1DB = core.NewStringKey("1_db", "A test string key")
key1DB = keys.NewString("1_db", "A test string key")
key2aAge = core.NewFloat64Key("2a_age", "A test float64 key")
key2bTTL = core.NewFloat32Key("2b_ttl", "A test float32 key")
key2cExpiryMS = core.NewFloat64Key("2c_expiry_ms", "A test float64 key")
key2aAge = keys.NewFloat64("2a_age", "A test float64 key")
key2bTTL = keys.NewFloat32("2b_ttl", "A test float32 key")
key2cExpiryMS = keys.NewFloat64("2c_expiry_ms", "A test float64 key")
key3aRetry = core.NewBooleanKey("3a_retry", "A test boolean key")
key3bStale = core.NewBooleanKey("3b_stale", "Another test boolean key")
key3aRetry = keys.NewBoolean("3a_retry", "A test boolean key")
key3bStale = keys.NewBoolean("3b_stale", "Another test boolean key")
key4aMax = core.NewIntKey("4a_max", "A test int key")
key4bOpcode = core.NewInt8Key("4b_opcode", "A test int8 key")
key4cBase = core.NewInt16Key("4c_base", "A test int16 key")
key4eChecksum = core.NewInt32Key("4e_checksum", "A test int32 key")
key4fMode = core.NewInt64Key("4f_mode", "A test int64 key")
key4aMax = keys.NewInt("4a_max", "A test int key")
key4bOpcode = keys.NewInt8("4b_opcode", "A test int8 key")
key4cBase = keys.NewInt16("4c_base", "A test int16 key")
key4eChecksum = keys.NewInt32("4e_checksum", "A test int32 key")
key4fMode = keys.NewInt64("4f_mode", "A test int64 key")
key5aMin = core.NewUIntKey("5a_min", "A test uint key")
key5bMix = core.NewUInt8Key("5b_mix", "A test uint8 key")
key5cPort = core.NewUInt16Key("5c_port", "A test uint16 key")
key5dMinHops = core.NewUInt32Key("5d_min_hops", "A test uint32 key")
key5eMaxHops = core.NewUInt64Key("5e_max_hops", "A test uint64 key")
key5aMin = keys.NewUInt("5a_min", "A test uint key")
key5bMix = keys.NewUInt8("5b_mix", "A test uint8 key")
key5cPort = keys.NewUInt16("5c_port", "A test uint16 key")
key5dMinHops = keys.NewUInt32("5d_min_hops", "A test uint32 key")
key5eMaxHops = keys.NewUInt64("5e_max_hops", "A test uint64 key")
recursiveCalls = core.NewInt64Key("recursive_calls", "Number of recursive calls")
bytesIn = core.NewInt64Key("bytes_in", "Number of bytes in") //, unit.Bytes)
latencyMs = core.NewFloat64Key("latency", "The latency in milliseconds") //, unit.Milliseconds)
recursiveCalls = keys.NewInt64("recursive_calls", "Number of recursive calls")
bytesIn = keys.NewInt64("bytes_in", "Number of bytes in") //, unit.Bytes)
latencyMs = keys.NewFloat64("latency", "The latency in milliseconds") //, unit.Milliseconds)
metricLatency = metric.HistogramFloat64{
Name: "latency_ms",

View File

@ -11,6 +11,7 @@ import (
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
)
@ -60,7 +61,7 @@ func Spans(output event.Exporter) event.Exporter {
}
case ev.IsStartSpan():
span := &Span{
Name: core.Name.Get(lm),
Name: keys.Name.Get(lm),
start: ev,
}
if parent := GetSpan(ctx); parent != nil {

542
internal/event/keys/keys.go Normal file
View File

@ -0,0 +1,542 @@
// 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 keys
import (
"fmt"
"io"
"math"
"strconv"
"golang.org/x/tools/internal/event/label"
)
// Value represents a key for untyped values.
type Value struct {
name string
description string
}
// New creates a new Key for untyped values.
func New(name, description string) *Value {
return &Value{name: name, description: description}
}
func (k *Value) Name() string { return k.name }
func (k *Value) Description() string { return k.description }
func (k *Value) Format(w io.Writer, buf []byte, l label.Label) {
fmt.Fprint(w, k.From(l))
}
// Get can be used to get a label for the key from a label.Map.
func (k *Value) Get(lm label.Map) interface{} {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return nil
}
// From can be used to get a value from a Label.
func (k *Value) From(t label.Label) interface{} { return t.UnpackValue() }
// Of creates a new Label with this key and the supplied value.
func (k *Value) Of(value interface{}) label.Label { return label.OfValue(k, value) }
// Int represents a key
type Int struct {
name string
description string
}
// NewInt creates a new Key for int values.
func NewInt(name, description string) *Int {
return &Int{name: name, description: description}
}
func (k *Int) Name() string { return k.name }
func (k *Int) Description() string { return k.description }
func (k *Int) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *Int) Of(v int) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *Int) Get(lm label.Map) int {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *Int) From(t label.Label) int { return int(t.Unpack64()) }
// Int8 represents a key
type Int8 struct {
name string
description string
}
// NewInt8 creates a new Key for int8 values.
func NewInt8(name, description string) *Int8 {
return &Int8{name: name, description: description}
}
func (k *Int8) Name() string { return k.name }
func (k *Int8) Description() string { return k.description }
func (k *Int8) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *Int8) Of(v int8) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *Int8) Get(lm label.Map) int8 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *Int8) From(t label.Label) int8 { return int8(t.Unpack64()) }
// Int16 represents a key
type Int16 struct {
name string
description string
}
// NewInt16 creates a new Key for int16 values.
func NewInt16(name, description string) *Int16 {
return &Int16{name: name, description: description}
}
func (k *Int16) Name() string { return k.name }
func (k *Int16) Description() string { return k.description }
func (k *Int16) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *Int16) Of(v int16) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *Int16) Get(lm label.Map) int16 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *Int16) From(t label.Label) int16 { return int16(t.Unpack64()) }
// Int32 represents a key
type Int32 struct {
name string
description string
}
// NewInt32 creates a new Key for int32 values.
func NewInt32(name, description string) *Int32 {
return &Int32{name: name, description: description}
}
func (k *Int32) Name() string { return k.name }
func (k *Int32) Description() string { return k.description }
func (k *Int32) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendInt(buf, int64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *Int32) Of(v int32) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *Int32) Get(lm label.Map) int32 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *Int32) From(t label.Label) int32 { return int32(t.Unpack64()) }
// Int64 represents a key
type Int64 struct {
name string
description string
}
// NewInt64 creates a new Key for int64 values.
func NewInt64(name, description string) *Int64 {
return &Int64{name: name, description: description}
}
func (k *Int64) Name() string { return k.name }
func (k *Int64) Description() string { return k.description }
func (k *Int64) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendInt(buf, k.From(l), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *Int64) Of(v int64) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *Int64) Get(lm label.Map) int64 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *Int64) From(t label.Label) int64 { return int64(t.Unpack64()) }
// UInt represents a key
type UInt struct {
name string
description string
}
// NewUInt creates a new Key for uint values.
func NewUInt(name, description string) *UInt {
return &UInt{name: name, description: description}
}
func (k *UInt) Name() string { return k.name }
func (k *UInt) Description() string { return k.description }
func (k *UInt) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *UInt) Of(v uint) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *UInt) Get(lm label.Map) uint {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *UInt) From(t label.Label) uint { return uint(t.Unpack64()) }
// UInt8 represents a key
type UInt8 struct {
name string
description string
}
// NewUInt8 creates a new Key for uint8 values.
func NewUInt8(name, description string) *UInt8 {
return &UInt8{name: name, description: description}
}
func (k *UInt8) Name() string { return k.name }
func (k *UInt8) Description() string { return k.description }
func (k *UInt8) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *UInt8) Of(v uint8) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *UInt8) Get(lm label.Map) uint8 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *UInt8) From(t label.Label) uint8 { return uint8(t.Unpack64()) }
// UInt16 represents a key
type UInt16 struct {
name string
description string
}
// NewUInt16 creates a new Key for uint16 values.
func NewUInt16(name, description string) *UInt16 {
return &UInt16{name: name, description: description}
}
func (k *UInt16) Name() string { return k.name }
func (k *UInt16) Description() string { return k.description }
func (k *UInt16) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *UInt16) Of(v uint16) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *UInt16) Get(lm label.Map) uint16 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *UInt16) From(t label.Label) uint16 { return uint16(t.Unpack64()) }
// UInt32 represents a key
type UInt32 struct {
name string
description string
}
// NewUInt32 creates a new Key for uint32 values.
func NewUInt32(name, description string) *UInt32 {
return &UInt32{name: name, description: description}
}
func (k *UInt32) Name() string { return k.name }
func (k *UInt32) Description() string { return k.description }
func (k *UInt32) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendUint(buf, uint64(k.From(l)), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *UInt32) Of(v uint32) label.Label { return label.Of64(k, uint64(v)) }
// Get can be used to get a label for the key from a label.Map.
func (k *UInt32) Get(lm label.Map) uint32 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *UInt32) From(t label.Label) uint32 { return uint32(t.Unpack64()) }
// UInt64 represents a key
type UInt64 struct {
name string
description string
}
// NewUInt64 creates a new Key for uint64 values.
func NewUInt64(name, description string) *UInt64 {
return &UInt64{name: name, description: description}
}
func (k *UInt64) Name() string { return k.name }
func (k *UInt64) Description() string { return k.description }
func (k *UInt64) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendUint(buf, k.From(l), 10))
}
// Of creates a new Label with this key and the supplied value.
func (k *UInt64) Of(v uint64) label.Label { return label.Of64(k, v) }
// Get can be used to get a label for the key from a label.Map.
func (k *UInt64) Get(lm label.Map) uint64 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *UInt64) From(t label.Label) uint64 { return t.Unpack64() }
// Float32 represents a key
type Float32 struct {
name string
description string
}
// NewFloat32 creates a new Key for float32 values.
func NewFloat32(name, description string) *Float32 {
return &Float32{name: name, description: description}
}
func (k *Float32) Name() string { return k.name }
func (k *Float32) Description() string { return k.description }
func (k *Float32) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendFloat(buf, float64(k.From(l)), 'E', -1, 32))
}
// Of creates a new Label with this key and the supplied value.
func (k *Float32) Of(v float32) label.Label {
return label.Of64(k, uint64(math.Float32bits(v)))
}
// Get can be used to get a label for the key from a label.Map.
func (k *Float32) Get(lm label.Map) float32 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *Float32) From(t label.Label) float32 {
return math.Float32frombits(uint32(t.Unpack64()))
}
// Float64 represents a key
type Float64 struct {
name string
description string
}
// NewFloat64 creates a new Key for int64 values.
func NewFloat64(name, description string) *Float64 {
return &Float64{name: name, description: description}
}
func (k *Float64) Name() string { return k.name }
func (k *Float64) Description() string { return k.description }
func (k *Float64) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendFloat(buf, k.From(l), 'E', -1, 64))
}
// Of creates a new Label with this key and the supplied value.
func (k *Float64) Of(v float64) label.Label {
return label.Of64(k, math.Float64bits(v))
}
// Get can be used to get a label for the key from a label.Map.
func (k *Float64) Get(lm label.Map) float64 {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return 0
}
// From can be used to get a value from a Label.
func (k *Float64) From(t label.Label) float64 {
return math.Float64frombits(t.Unpack64())
}
// String represents a key
type String struct {
name string
description string
}
// NewString creates a new Key for int64 values.
func NewString(name, description string) *String {
return &String{name: name, description: description}
}
func (k *String) Name() string { return k.name }
func (k *String) Description() string { return k.description }
func (k *String) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendQuote(buf, k.From(l)))
}
// Of creates a new Label with this key and the supplied value.
func (k *String) Of(v string) label.Label { return label.OfString(k, v) }
// Get can be used to get a label for the key from a label.Map.
func (k *String) Get(lm label.Map) string {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return ""
}
// From can be used to get a value from a Label.
func (k *String) From(t label.Label) string { return t.UnpackString() }
// Boolean represents a key
type Boolean struct {
name string
description string
}
// NewBoolean creates a new Key for bool values.
func NewBoolean(name, description string) *Boolean {
return &Boolean{name: name, description: description}
}
func (k *Boolean) Name() string { return k.name }
func (k *Boolean) Description() string { return k.description }
func (k *Boolean) Format(w io.Writer, buf []byte, l label.Label) {
w.Write(strconv.AppendBool(buf, k.From(l)))
}
// Of creates a new Label with this key and the supplied value.
func (k *Boolean) Of(v bool) label.Label {
if v {
return label.Of64(k, 1)
}
return label.Of64(k, 0)
}
// Get can be used to get a label for the key from a label.Map.
func (k *Boolean) Get(lm label.Map) bool {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return false
}
// From can be used to get a value from a Label.
func (k *Boolean) From(t label.Label) bool { return t.Unpack64() > 0 }
// Error represents a key
type Error struct {
name string
description string
}
// NewError creates a new Key for int64 values.
func NewError(name, description string) *Error {
return &Error{name: name, description: description}
}
func (k *Error) Name() string { return k.name }
func (k *Error) Description() string { return k.description }
func (k *Error) Format(w io.Writer, buf []byte, l label.Label) {
io.WriteString(w, k.From(l).Error())
}
// Of creates a new Label with this key and the supplied value.
func (k *Error) Of(v error) label.Label { return label.OfValue(k, v) }
// Get can be used to get a label for the key from a label.Map.
func (k *Error) Get(lm label.Map) error {
if t := lm.Find(k); t.Valid() {
return k.From(t)
}
return nil
}
// From can be used to get a value from a Label.
func (k *Error) From(t label.Label) error {
err, _ := t.UnpackValue().(error)
return err
}

View File

@ -0,0 +1,14 @@
// Copyright 2020 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 keys
var (
// Msg is a key used to add message strings to label lists.
Msg = NewString("message", "a readable message")
// Name is used for things like traces that have a name.
Name = NewString("name", "an entity name")
// Err is a key used to add error values to label lists.
Err = NewError("error", "an error that occurred")
)

View File

@ -9,14 +9,14 @@ import (
"fmt"
"testing"
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
)
var (
AKey = core.NewStringKey("A", "")
BKey = core.NewStringKey("B", "")
CKey = core.NewStringKey("C", "")
AKey = keys.NewString("A", "")
BKey = keys.NewString("B", "")
CKey = keys.NewString("C", "")
A = AKey.Of("a")
B = BKey.Of("b")
C = CKey.Of("c")

View File

@ -20,7 +20,7 @@ import (
"time"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/gocommand"
"golang.org/x/tools/internal/imports"
"golang.org/x/tools/internal/lsp/debug"
@ -345,7 +345,7 @@ func (v *view) refreshProcessEnv() {
// We don't have a context handy to use for logging, so use the stdlib for now.
event.Log(v.baseCtx, "background imports cache refresh starting")
err := imports.PrimeCache(context.Background(), env)
event.Log(v.baseCtx, fmt.Sprintf("background refresh finished after %v", time.Since(start)), core.Err.Of(err))
event.Log(v.baseCtx, fmt.Sprintf("background refresh finished after %v", time.Since(start)), keys.Err.Of(err))
v.importsMu.Lock()
v.cacheRefreshDuration = time.Since(start)

View File

@ -32,6 +32,7 @@ import (
"golang.org/x/tools/internal/event/export/metric"
"golang.org/x/tools/internal/event/export/ocagent"
"golang.org/x/tools/internal/event/export/prometheus"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
"golang.org/x/tools/internal/lsp/debug/tag"
"golang.org/x/tools/internal/lsp/protocol"
@ -549,7 +550,7 @@ func makeGlobalExporter(stderr io.Writer) event.Exporter {
if ev.IsLog() {
// Don't log context cancellation errors.
if err := core.Err.Get(ev); xerrors.Is(err, context.Canceled) {
if err := keys.Err.Get(ev); xerrors.Is(err, context.Canceled) {
return ctx
}
// Make sure any log messages without an instance go to stderr.

View File

@ -6,40 +6,40 @@
package tag
import (
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/event/keys"
)
var (
// create the label keys we use
Method = core.NewStringKey("method", "")
StatusCode = core.NewStringKey("status.code", "")
StatusMessage = core.NewStringKey("status.message", "")
RPCID = core.NewStringKey("id", "")
RPCDirection = core.NewStringKey("direction", "")
File = core.NewStringKey("file", "")
Directory = core.NewKey("directory", "")
URI = core.NewKey("URI", "")
Package = core.NewStringKey("package", "")
PackagePath = core.NewStringKey("package_path", "")
Query = core.NewKey("query", "")
Snapshot = core.NewUInt64Key("snapshot", "")
Operation = core.NewStringKey("operation", "")
Method = keys.NewString("method", "")
StatusCode = keys.NewString("status.code", "")
StatusMessage = keys.NewString("status.message", "")
RPCID = keys.NewString("id", "")
RPCDirection = keys.NewString("direction", "")
File = keys.NewString("file", "")
Directory = keys.New("directory", "")
URI = keys.New("URI", "")
Package = keys.NewString("package", "")
PackagePath = keys.NewString("package_path", "")
Query = keys.New("query", "")
Snapshot = keys.NewUInt64("snapshot", "")
Operation = keys.NewString("operation", "")
Position = core.NewKey("position", "")
Category = core.NewStringKey("category", "")
PackageCount = core.NewIntKey("packages", "")
Files = core.NewKey("files", "")
Port = core.NewIntKey("port", "")
Type = core.NewKey("type", "")
HoverKind = core.NewStringKey("hoverkind", "")
Position = keys.New("position", "")
Category = keys.NewString("category", "")
PackageCount = keys.NewInt("packages", "")
Files = keys.New("files", "")
Port = keys.NewInt("port", "")
Type = keys.New("type", "")
HoverKind = keys.NewString("hoverkind", "")
)
var (
// create the stats we measure
Started = core.NewInt64Key("started", "Count of started RPCs.")
ReceivedBytes = core.NewInt64Key("received_bytes", "Bytes received.") //, unit.Bytes)
SentBytes = core.NewInt64Key("sent_bytes", "Bytes sent.") //, unit.Bytes)
Latency = core.NewFloat64Key("latency_ms", "Elapsed time in milliseconds") //, unit.Milliseconds)
Started = keys.NewInt64("started", "Count of started RPCs.")
ReceivedBytes = keys.NewInt64("received_bytes", "Bytes received.") //, unit.Bytes)
SentBytes = keys.NewInt64("sent_bytes", "Bytes sent.") //, unit.Bytes)
Latency = keys.NewFloat64("latency_ms", "Elapsed time in milliseconds") //, unit.Milliseconds)
)
const (

View File

@ -5,6 +5,7 @@ import (
"fmt"
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
"golang.org/x/tools/internal/xcontext"
)
@ -28,7 +29,7 @@ func LogEvent(ctx context.Context, ev core.Event, tags label.Map) context.Contex
return ctx
}
msg := &LogMessageParams{Type: Info, Message: fmt.Sprint(ev)}
if core.Err.Get(tags) != nil {
if keys.Err.Get(tags) != nil {
msg.Type = Error
}
go client.LogMessage(xcontext.Detach(ctx), msg)

View File

@ -8,7 +8,6 @@ import (
"context"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/lsp/debug/tag"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
@ -21,7 +20,7 @@ func (s *Server) signatureHelp(ctx context.Context, params *protocol.SignatureHe
}
info, activeParameter, err := source.SignatureHelp(ctx, snapshot, fh, params.Position)
if err != nil {
event.Log(ctx, "no signature help", tag.Position.Of(params.Position), core.Err.Of(err))
event.Error(ctx, "no signature help", err, tag.Position.Of(params.Position))
return nil, nil
}
return &protocol.SignatureHelp{