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.
|
|
|
|
|
|
|
|
// Package event provides support for event based telemetry.
|
|
|
|
package event
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type eventType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
LogType = eventType(iota)
|
|
|
|
StartSpanType
|
|
|
|
EndSpanType
|
|
|
|
LabelType
|
2020-03-16 08:01:57 -06:00
|
|
|
QueryType
|
2020-03-07 16:02:27 -07:00
|
|
|
DetachType
|
2020-03-17 14:00:16 -06:00
|
|
|
RecordType
|
2020-03-07 16:02:27 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Event struct {
|
|
|
|
Type eventType
|
|
|
|
At time.Time
|
|
|
|
Message string
|
|
|
|
Error error
|
2020-03-10 08:13:00 -06:00
|
|
|
Tags TagSet
|
2020-03-07 16:02:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e Event) IsLog() bool { return e.Type == LogType }
|
|
|
|
func (e Event) IsEndSpan() bool { return e.Type == EndSpanType }
|
|
|
|
func (e Event) IsStartSpan() bool { return e.Type == StartSpanType }
|
2020-03-16 08:01:57 -06:00
|
|
|
func (e Event) IsLabel() bool { return e.Type == LabelType }
|
|
|
|
func (e Event) IsQuery() bool { return e.Type == QueryType }
|
2020-03-07 16:02:27 -07:00
|
|
|
func (e Event) IsDetach() bool { return e.Type == DetachType }
|
2020-03-17 14:00:16 -06:00
|
|
|
func (e Event) IsRecord() bool { return e.Type == RecordType }
|
2020-03-07 16:02:27 -07:00
|
|
|
|
|
|
|
func (e Event) Format(f fmt.State, r rune) {
|
|
|
|
if !e.At.IsZero() {
|
|
|
|
fmt.Fprint(f, e.At.Format("2006/01/02 15:04:05 "))
|
|
|
|
}
|
|
|
|
fmt.Fprint(f, e.Message)
|
|
|
|
if e.Error != nil {
|
|
|
|
if f.Flag('+') {
|
|
|
|
fmt.Fprintf(f, ": %+v", e.Error)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(f, ": %v", e.Error)
|
|
|
|
}
|
|
|
|
}
|
2020-03-10 08:13:00 -06:00
|
|
|
for i := e.Tags.Iterator(); i.Next(); {
|
|
|
|
tag := i.Value()
|
2020-03-10 21:52:14 -06:00
|
|
|
fmt.Fprintf(f, "\n\t%s = %v", tag.key.name, tag.value)
|
2020-03-07 16:02:27 -07:00
|
|
|
}
|
|
|
|
}
|