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
|
|
|
|
DetachType
|
2020-03-17 14:00:16 -06:00
|
|
|
RecordType
|
2020-03-07 16:02:27 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Event struct {
|
2020-03-22 21:18:37 -06:00
|
|
|
typ eventType
|
2020-03-07 16:02:27 -07:00
|
|
|
At time.Time
|
|
|
|
Message string
|
|
|
|
Error error
|
2020-03-20 06:29:48 -06:00
|
|
|
|
|
|
|
tags []Tag
|
2020-03-07 16:02:27 -07:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
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-03-24 19:11:55 -06:00
|
|
|
fmt.Fprint(f, ev.Message)
|
|
|
|
if ev.Error != nil {
|
2020-03-07 16:02:27 -07:00
|
|
|
if f.Flag('+') {
|
2020-03-24 19:11:55 -06:00
|
|
|
fmt.Fprintf(f, ": %+v", ev.Error)
|
2020-03-07 16:02:27 -07:00
|
|
|
} else {
|
2020-03-24 19:11:55 -06:00
|
|
|
fmt.Fprintf(f, ": %v", ev.Error)
|
2020-03-07 16:02:27 -07:00
|
|
|
}
|
|
|
|
}
|
2020-03-24 19:11:55 -06:00
|
|
|
for it := ev.Tags(); it.Valid(); it.Advance() {
|
2020-03-20 06:29:48 -06:00
|
|
|
tag := it.Tag()
|
2020-03-24 21:29:41 -06:00
|
|
|
fmt.Fprintf(f, "\n\t%v", tag)
|
2020-03-20 06:29:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev Event) Tags() TagIterator {
|
|
|
|
if len(ev.tags) == 0 {
|
|
|
|
return TagIterator{}
|
2020-03-07 16:02:27 -07:00
|
|
|
}
|
2020-03-20 06:29:48 -06:00
|
|
|
return NewTagIterator(ev.tags...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev Event) Map() TagMap {
|
|
|
|
return NewTagMap(ev.tags...)
|
2020-03-07 16:02:27 -07:00
|
|
|
}
|