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
|
|
|
)
|
|
|
|
|
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-04-20 21:00:05 -06:00
|
|
|
at time.Time
|
2020-03-20 06:29:48 -06:00
|
|
|
|
2020-04-20 13:44:34 -06:00
|
|
|
// 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-04-20 21:00:05 -06:00
|
|
|
func (ev Event) At() time.Time { return ev.at }
|
|
|
|
|
2020-03-24 19:11:55 -06:00
|
|
|
func (ev Event) Format(f fmt.State, r rune) {
|
2020-04-20 21:00:05 -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-03 21:06:57 -06:00
|
|
|
for index := 0; ev.Valid(index); index++ {
|
2020-04-26 19:29:37 -06:00
|
|
|
if l := ev.Label(index); l.Valid() {
|
|
|
|
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-21 07:41:57 -06:00
|
|
|
func MakeEvent(static [3]label.Label, labels []label.Label) Event {
|
2020-03-20 10:00:56 -06:00
|
|
|
return Event{
|
|
|
|
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
|
|
|
}
|
2020-04-20 21:00:05 -06:00
|
|
|
|
|
|
|
// CloneEvent event returns a copy of the event with the time adjusted to at.
|
|
|
|
func CloneEvent(ev Event, at time.Time) Event {
|
|
|
|
ev.at = at
|
|
|
|
return ev
|
|
|
|
}
|