-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace_decode.go
105 lines (89 loc) · 2.24 KB
/
trace_decode.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
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package channel
import (
"encoding/json"
"fmt"
"reflect"
"sort"
)
type TraceMessageDecoder interface {
Decode(msg *Message) ([]byte, bool)
}
type TraceMessageDecode map[string]interface{}
const DecoderFieldName = "__decoder__"
const MessageFieldName = "__message__"
func NewTraceMessageDecode(decoder, message string) TraceMessageDecode {
meta := make(map[string]interface{})
meta[DecoderFieldName] = decoder
meta[MessageFieldName] = message
return meta
}
func (d TraceMessageDecode) MarshalTraceMessageDecode() ([]byte, error) {
data, err := json.Marshal(d)
if err != nil {
return nil, err
}
return data, nil
}
func (d TraceMessageDecode) MarshalResult() ([]byte, bool) {
data, err := json.Marshal(d)
if err != nil {
return nil, true
}
return data, true
}
func decodeTraceAndFormat(decode []byte) string {
if len(decode) > 0 {
meta := make(map[string]interface{})
err := json.Unmarshal(decode, &meta)
if err != nil {
panic(err)
}
out := fmt.Sprintf("%-24s", fmt.Sprintf("%-8s %s", meta[DecoderFieldName], meta[MessageFieldName]))
if len(meta) > 2 {
keys := make([]string, 0)
for k := range meta {
if k != DecoderFieldName && k != MessageFieldName {
keys = append(keys, k)
}
}
sort.Strings(keys)
out += " {"
for i := 0; i < len(keys); i++ {
k := keys[i]
if i > 0 {
out += " "
}
out += k
out += "=["
v := meta[k]
switch v := v.(type) {
case string:
out += v
case float64:
out += fmt.Sprintf("%0.0f", v)
case bool:
out += fmt.Sprintf("%t", v)
default:
out += fmt.Sprintf("<%s>", reflect.TypeOf(v))
}
out += "]"
}
out += "}"
}
return out
} else {
return ""
}
}