forked from sourcegraph/appdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
158 lines (132 loc) · 4.09 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package appdash
import (
"fmt"
"reflect"
"time"
)
// An Event is a record of the occurrence of something.
type Event interface {
Schema() string
}
// ImportantEvent is an event that can describe in particular which annotation
// keys it finds important. Only important annotation keys are displayed in the
// web UI by default.
type ImportantEvent interface {
Important() []string
}
// EventMarshaler is the interface implemented by an event that can
// marshal a representation of itself into annotations.
//
// TODO(sqs): implement this in MarshalEvent
type EventMarshaler interface {
MarshalEvent() ([]*Annotation, error)
}
// EventUnmarshaler is the interface implemented by an event that can
// unmarshal an annotation representation of itself.
//
// TODO(sqs): implement this in UnmarshalEvent
type EventUnmarshaler interface {
UnmarshalEvent([]*Annotation) error
}
const schemaPrefix = "_schema:"
// MarshalEvent marshals an event into annotations.
func MarshalEvent(e Event) (Annotations, error) {
var as Annotations
flattenValue("", reflect.ValueOf(e), func(k, v string) {
as = append(as, Annotation{Key: k, Value: []byte(v)})
})
as = append(as, Annotation{Key: schemaPrefix + e.Schema()})
return as, nil
}
// An EventSchemaUnmarshalError is when annotations are attempted to
// be unmarshaled into an event object that does not match any of the
// schemas in the annotations.
type EventSchemaUnmarshalError struct {
Found []string // schemas found in the annotations
Target string // schema of the target event
}
func (e *EventSchemaUnmarshalError) Error() string {
return fmt.Sprintf("event: can't unmarshal annotations with schemas %v into event of schema %s", e.Found, e.Target)
}
// UnmarshalEvent unmarshals annotations into an event.
func UnmarshalEvent(as Annotations, e Event) error {
aSchemas := as.schemas()
schemaOK := false
for _, s := range aSchemas {
if s == e.Schema() {
schemaOK = true
break
}
}
if !schemaOK {
return &EventSchemaUnmarshalError{Found: aSchemas, Target: e.Schema()}
}
unflattenValue("", reflect.ValueOf(&e), reflect.TypeOf(&e), mapToKVs(as.StringMap()))
return nil
}
// RegisterEvent registers an event type for use with UnmarshalEvents.
func RegisterEvent(e Event) {
if _, present := registeredEvents[e.Schema()]; present {
panic("event schema is already registered: " + e.Schema())
}
if e.Schema() == "" {
panic("event schema is empty")
}
registeredEvents[e.Schema()] = e
}
var registeredEvents = map[string]Event{} // event schema -> event type
func init() {
RegisterEvent(spanName{})
RegisterEvent(logEvent{})
RegisterEvent(msgEvent{})
}
// UnmarshalEvents unmarshals all events found in anns into
// events. Any schemas found in anns that were not registered (using
// RegisterEvent) are ignored; missing a schema is not an error.
func UnmarshalEvents(anns Annotations, events *[]Event) error {
schemas := anns.schemas()
for _, schema := range schemas {
ev := registeredEvents[schema]
if ev == nil {
continue
}
evv := reflect.New(reflect.TypeOf(ev))
if err := UnmarshalEvent(anns, evv.Interface().(Event)); err != nil {
return err
}
*events = append(*events, evv.Elem().Interface().(Event))
}
return nil
}
// A spanName event sets a span's name.
type spanName struct{ Name string }
func (spanName) Schema() string { return "name" }
// Msg returns an Event that contains only a human-readable message.
func Msg(msg string) Event {
return msgEvent{Msg: msg}
}
type msgEvent struct {
Msg string
}
func (msgEvent) Schema() string { return "msg" }
// A TimespanEvent is an Event with a start and an end time.
type TimespanEvent interface {
Event
Start() time.Time
End() time.Time
}
// A TimestampedEvent is an Event with a timestamp.
type TimestampedEvent interface {
Timestamp() time.Time
}
// Log returns an Event whose timestamp is the current time that
// contains only a human-readable message.
func Log(msg string) Event {
return logEvent{Msg: msg, Time: time.Now()}
}
type logEvent struct {
Msg string
Time time.Time
}
func (logEvent) Schema() string { return "log" }
func (e *logEvent) Timestamp() time.Time { return e.Time }