2020-03-07 16:02:27 -07:00
|
|
|
// 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.
|
|
|
|
|
2020-04-17 07:32:56 -06:00
|
|
|
// Package core provides support for event based telemetry.
|
|
|
|
package core
|
2020-03-07 16:02:27 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2020-04-20 13:44:34 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/event/label"
|
2020-03-07 16:02:27 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type eventType uint8
|
|
|
|
|
|
|
|
const (
|
2020-03-20 10:00:56 -06:00
|
|
|
invalidType = eventType(iota)
|
|
|
|
LogType // an event that should be recorded in a log
|
|
|
|
StartSpanType // the start of a span of time
|
|
|
|
EndSpanType // the end of a span of time
|
|
|
|
LabelType // some values that should be noted for later events
|
|
|
|
DetachType // an event that causes a context to detach
|
|
|
|
RecordType // a value that should be tracked
|
2020-03-07 16:02:27 -07:00
|
|
|
)
|
|
|
|
|
2020-03-20 10:00:56 -06:00
|
|
|
// Event holds the information about an event of note that ocurred.
|
2020-03-07 16:02:27 -07:00
|
|
|
type Event struct {
|
2020-03-20 10:00:56 -06:00
|
|
|
At time.Time
|
2020-03-20 06:29:48 -06:00
|
|
|
|
2020-04-20 13:44:34 -06:00
|
|
|
typ eventType
|
|
|
|
|
|
|
|
// As events are often on the stack, storing the first few labels directly
|
|
|
|
// in the event can avoid an allocation at all for the very common cases of
|
|
|
|
// simple events.
|
|
|
|
// The length needs to be large enough to cope with the majority of events
|
|
|
|
// but no so large as to cause undue stack pressure.
|
|
|
|
// A log message with two values will use 3 labels (one for each value and
|
|
|
|
// one for the message itself).
|
|
|
|
|
|
|
|
static [3]label.Label // inline storage for the first few labels
|
|
|
|
dynamic []label.Label // dynamically sized storage for remaining labels
|
2020-03-07 16:02:27 -07:00
|
|
|
}
|
|
|
|
|
2020-04-20 13:44:34 -06:00
|
|
|
// eventLabelMap implements label.Map for a the labels of an Event.
|
|
|
|
type eventLabelMap struct {
|
2020-03-30 06:09:07 -06:00
|
|
|
event Event
|
|
|
|
}
|
|
|
|
|
2020-03-24 19:11:55 -06:00
|
|
|
func (ev Event) IsLog() bool { return ev.typ == LogType }
|
|
|
|
func (ev Event) IsEndSpan() bool { return ev.typ == EndSpanType }
|
|
|
|
func (ev Event) IsStartSpan() bool { return ev.typ == StartSpanType }
|
|
|
|
func (ev Event) IsLabel() bool { return ev.typ == LabelType }
|
|
|
|
func (ev Event) IsDetach() bool { return ev.typ == DetachType }
|
|
|
|
func (ev Event) IsRecord() bool { return ev.typ == RecordType }
|
2020-03-07 16:02:27 -07:00
|
|
|
|
2020-03-24 19:11:55 -06:00
|
|
|
func (ev Event) Format(f fmt.State, r rune) {
|
2020-04-20 13:44:34 -06:00
|
|
|
lm := label.Map(ev)
|
2020-03-24 19:11:55 -06:00
|
|
|
if !ev.At.IsZero() {
|
|
|
|
fmt.Fprint(f, ev.At.Format("2006/01/02 15:04:05 "))
|
2020-03-07 16:02:27 -07:00
|
|
|
}
|
2020-04-20 13:44:34 -06:00
|
|
|
msg := Msg.Get(lm)
|
|
|
|
err := Err.Get(lm)
|
2020-03-20 10:00:56 -06:00
|
|
|
fmt.Fprint(f, msg)
|
|
|
|
if err != nil {
|
2020-03-07 16:02:27 -07:00
|
|
|
if f.Flag('+') {
|
2020-03-20 10:00:56 -06:00
|
|
|
fmt.Fprintf(f, ": %+v", err)
|
2020-03-07 16:02:27 -07:00
|
|
|
} else {
|
2020-03-20 10:00:56 -06:00
|
|
|
fmt.Fprintf(f, ": %v", err)
|
2020-03-07 16:02:27 -07:00
|
|
|
}
|
|
|
|
}
|
2020-04-03 21:06:57 -06:00
|
|
|
for index := 0; ev.Valid(index); index++ {
|
2020-04-20 13:44:34 -06:00
|
|
|
l := ev.Label(index)
|
2020-04-04 21:16:06 -06:00
|
|
|
// msg and err were both already printed above, so we skip them to avoid
|
|
|
|
// double printing
|
2020-04-20 13:44:34 -06:00
|
|
|
if !l.Valid() || l.Key() == Msg || l.Key() == Err {
|
2020-04-04 21:16:06 -06:00
|
|
|
continue
|
|
|
|
}
|
2020-04-20 13:44:34 -06:00
|
|
|
fmt.Fprintf(f, "\n\t%v", l)
|
2020-03-20 06:29:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 21:06:57 -06:00
|
|
|
func (ev Event) Valid(index int) bool {
|
|
|
|
return index >= 0 && index < len(ev.static)+len(ev.dynamic)
|
|
|
|
}
|
|
|
|
|
2020-04-20 13:44:34 -06:00
|
|
|
func (ev Event) Label(index int) label.Label {
|
2020-04-03 21:06:57 -06:00
|
|
|
if index < len(ev.static) {
|
|
|
|
return ev.static[index]
|
|
|
|
}
|
|
|
|
return ev.dynamic[index-len(ev.static)]
|
2020-03-20 06:29:48 -06:00
|
|
|
}
|
|
|
|
|
2020-04-20 13:44:34 -06:00
|
|
|
func (ev Event) Find(key label.Key) label.Label {
|
|
|
|
for _, l := range ev.static {
|
|
|
|
if l.Key() == key {
|
|
|
|
return l
|
2020-03-30 06:09:07 -06:00
|
|
|
}
|
|
|
|
}
|
2020-04-20 13:44:34 -06:00
|
|
|
for _, l := range ev.dynamic {
|
|
|
|
if l.Key() == key {
|
|
|
|
return l
|
2020-03-30 06:09:07 -06:00
|
|
|
}
|
|
|
|
}
|
2020-04-20 13:44:34 -06:00
|
|
|
return label.Label{}
|
2020-03-20 10:00:56 -06:00
|
|
|
}
|
|
|
|
|
2020-04-20 13:44:34 -06:00
|
|
|
func MakeEvent(typ eventType, static [3]label.Label, labels []label.Label) Event {
|
2020-03-20 10:00:56 -06:00
|
|
|
return Event{
|
|
|
|
typ: typ,
|
|
|
|
static: static,
|
2020-04-20 13:44:34 -06:00
|
|
|
dynamic: labels,
|
2020-03-20 10:00:56 -06:00
|
|
|
}
|
2020-03-07 16:02:27 -07:00
|
|
|
}
|