forked from rai-project/dlframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
113 lines (99 loc) · 2.32 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
package eventflow
import (
"encoding/json"
"time"
"github.com/c3sr/dlframework/evaluation/writer"
"github.com/spf13/cast"
m "github.com/jaegertracing/jaeger/model"
model "github.com/jaegertracing/jaeger/model/json"
)
//easyjson:json
type Event struct {
ID string `json:"EVENT_ID"`
ParentID string `json:"PARENT_ID"`
Name string `json:"EVENT_NAME"`
MetaData map[string]string `json:"META,omitempty"`
TimeStamp time.Time `json:"TS,omitempty"`
Duration uint64 `json:"ELAPSED_MS,omitempty"`
}
type Events []Event
func (Event) Header(opts ...writer.Option) []string {
return []string{
"id",
"parent_id",
"name",
"metadata",
"timestamp",
"duration (us)",
}
}
func (e Event) Row(opts ...writer.Option) []string {
metadata, err := json.Marshal(e.MetaData)
if err != nil {
metadata = []byte{}
}
return []string{
e.ID,
e.ParentID,
e.Name,
string(metadata),
e.TimeStamp.String(),
cast.ToString(e.Duration),
}
}
func (Events) Header(opts ...writer.Option) []string {
return Event{}.Header(opts...)
}
func (s Events) Rows(opts ...writer.Option) [][]string {
rows := [][]string{}
for _, e := range s {
rows = append(rows, e.Row(opts...))
}
return rows
}
func tagsOf(span model.Span) map[string]string {
res := map[string]string{}
for _, lg := range span.Logs {
for _, fld := range lg.Fields {
res[fld.Key] = cast.ToString(fld.Value)
}
}
for _, tag := range span.Tags {
res[tag.Key] = cast.ToString(tag.Value)
}
return res
}
func parentOf(span model.Span) model.SpanID {
if span.ParentSpanID != "" {
return span.ParentSpanID
}
for _, ref := range span.References {
if ref.RefType == model.ChildOf {
return ref.SpanID
}
}
return model.SpanID("")
}
func toTime(t uint64) time.Time {
return m.EpochMicrosecondsAsTime(t)
}
func toDuration(d uint64) time.Duration {
return m.MicrosecondsAsDuration(d)
}
func spanToEvent(span model.Span) Event {
return Event{
ID: string(span.SpanID),
ParentID: string(parentOf(span)),
Name: span.OperationName,
MetaData: tagsOf(span),
TimeStamp: toTime(span.StartTime),
Duration: span.Duration,
}
}
func SpansToEvenFlow(spans []model.Span) Events {
events := make([]Event, len(spans))
for ii, span := range spans {
events[ii] = spanToEvent(span)
}
return events
}