forked from JesseObrien/x-ray-echo-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
114 lines (86 loc) · 2.31 KB
/
types.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
package main
import (
"encoding/json"
"fmt"
"github.com/aws/aws-xray-sdk-go/xray"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
type XraySegment xray.Segment
func (xs XraySegment) Display(isSubsegment bool, padding *string) {
xs.DisplayInfo(isSubsegment, padding)
p := *padding
if isSubsegment {
p = fmt.Sprintf("%s ", *padding)
}
if len(xs.Subsegments) > 0 {
xs.DisplaySubsegments(isSubsegment, &p)
}
}
func (xs XraySegment) DisplayInfo(isSubsegment bool, padding *string) {
title := "└─ "
if isSubsegment {
title = fmt.Sprintf("%s└─ subsegment: ", *padding)
}
subPadding := fmt.Sprintf("%s ", *padding)
elapsedTime := fmt.Sprintf("%fs", (xs.EndTime - xs.StartTime))
metaData := log.Fields{
"Elapsed Time": elapsedTime,
}
if len(xs.Metadata) > 0 {
md := traverseMetadata(xs.Metadata, subPadding)
for k, v := range md {
metaData[k] = v
}
}
log.WithFields(metaData).Infof("%s%s", title, xs.Name)
if len(xs.AWS) > 0 {
log.WithFields(getFields(xs.AWS)).Infof("%s└─ data", subPadding)
}
if xs.HTTP != nil {
log.Infof("%s└─ http", subPadding)
if request := xs.HTTP.GetRequest(); request != nil {
log.WithFields(getFields(request)).Infof("%s └─> Request", subPadding)
}
if response := xs.HTTP.GetResponse(); response != nil {
log.WithFields(getFields(response)).Infof("%s └─> Response", subPadding)
}
}
}
func traverseMetadata(metadata map[string]map[string]interface{}, padding string) logrus.Fields {
md := logrus.Fields{}
for _, v := range metadata {
if len(v) > 0 {
for key, val := range v {
if m, ok := val.(map[string]interface{}); ok {
for subKey, subVal := range getFields(m) {
md[subKey] = subVal
}
} else {
md[key] = val
}
}
}
}
return md
}
func (xs XraySegment) DisplaySubsegments(isSubsegment bool, padding *string) {
p := *padding
if isSubsegment {
p = fmt.Sprintf("%s", *padding)
}
for _, subSegment := range xs.Subsegments {
subSeg := &XraySegment{}
err := json.Unmarshal(subSegment, subSeg)
if err != nil {
log.WithError(err).Error("Could not unmarshal subsegment into a segment")
}
subSeg.Display(true, &p)
}
}
func getFields(i interface{}) logrus.Fields {
b, _ := json.Marshal(i)
fields := logrus.Fields{}
json.Unmarshal(b, &fields)
return fields
}