-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace_decode.go
More file actions
113 lines (96 loc) · 2.8 KB
/
Copy pathtrace_decode.go
File metadata and controls
113 lines (96 loc) · 2.8 KB
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
/*
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"
)
// TraceMessageDecoder decodes a Message into a JSON byte representation for tracing.
type TraceMessageDecoder interface {
Decode(msg *Message) ([]byte, bool)
}
// TraceMessageDecode is a map of trace metadata fields, serializable to JSON.
type TraceMessageDecode map[string]interface{}
// DecoderFieldName is the JSON key for the decoder name in trace output.
const DecoderFieldName = "__decoder__"
// MessageFieldName is the JSON key for the message type name in trace output.
const MessageFieldName = "__message__"
// NewTraceMessageDecode creates a TraceMessageDecode with the decoder and message type pre-populated.
func NewTraceMessageDecode(decoder, message string) TraceMessageDecode {
meta := make(map[string]interface{})
meta[DecoderFieldName] = decoder
meta[MessageFieldName] = message
return meta
}
// MarshalTraceMessageDecode serializes the trace metadata to JSON.
func (d TraceMessageDecode) MarshalTraceMessageDecode() ([]byte, error) {
data, err := json.Marshal(d)
if err != nil {
return nil, err
}
return data, nil
}
// MarshalResult serializes the trace metadata to JSON, returning (nil, true) on error.
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 ""
}
}