-
Notifications
You must be signed in to change notification settings - Fork 6
/
codec_json.go
112 lines (94 loc) · 2.7 KB
/
codec_json.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
//
// February 2016, cisco
//
// Copyright (c) 2016 by cisco Systems, Inc.
// All rights reserved.
//
//
// Provide JSON codec, such as it is. More effort required here to
// exploit common bit (Telemetry message) and provide better
// implementations exporting metadata. Shipping MDT does not support
// JSON yet, though this is in the works. JSON is largely pass
// through.
//
package main
import (
"fmt"
)
//
// dataMsgJSON dataMsg types are produced when handling JSON streams.
type dataMsgJSON struct {
original []byte
source msgproducer
}
func (m *dataMsgJSON) getDataMsgDescription() string {
_, id := m.getMetaDataIdentifier()
return fmt.Sprintf("JSON message [%s msg len %d]", id, len(m.original))
}
func (m *dataMsgJSON) produceByteStream(streamSpec *dataMsgStreamSpec) (
error, []byte) {
switch streamSpec.streamType {
case dMStreamJSON, dMStreamMsgDefault:
return nil, m.original
}
//
// We only support producing stream in JSON for this message
// for the moment - this is because we have the encoded
// variant at hand.
return fmt.Errorf("JSON CODEC: reformat msg to [%s] is"+
" not supported", dataMsgStreamTypeString(streamSpec.streamType)), nil
}
func (m *dataMsgJSON) produceMetrics(
spec *metricsSpec,
outputHandler metricsOutputHandler,
outputContext metricsOutputContext) error {
return fmt.Errorf("JSON CODEC: metric extraction unsupported")
}
func (m *dataMsgJSON) getDataMsgStreamType() dataMsgStreamType {
return dMStreamJSON
}
func (m *dataMsgJSON) getMetaDataPath() (error, string) {
return fmt.Errorf("JSON CODEC: path extraction is not supported"), ""
}
func (m *dataMsgJSON) getMetaDataIdentifier() (error, string) {
return nil, m.source.String()
}
func (m *dataMsgJSON) getMetaData() *dataMsgMetaData {
return &dataMsgMetaData{
Path: "unsupported",
Identifier: m.source.String(),
}
}
type codecJSON struct {
name string
}
//
// Produce a JSON type codec
func getNewCodecJSON(name string) (error, codec) {
c := &codecJSON{
name: name,
}
return nil, c
}
func (p *codecJSON) dataMsgToBlock(dM dataMsg) (error, []byte) {
return fmt.Errorf("CODEC JSON: only decoding is supported currently"),
nil
}
// nextBlock allows JSON to produce dataMsg
func (p *codecJSON) blockToDataMsgs(source msgproducer, nextBlock []byte) (
error, []dataMsg) {
//
// Count the decoded message against the source, section and
// type
codecMetaMonitor.Decoded.WithLabelValues(
p.name, source.String(), encodingToName(ENCODING_JSON)).Inc()
codecMetaMonitor.DecodedBytes.WithLabelValues(
p.name, source.String(), encodingToName(ENCODING_JSON)).Add(
float64(len(nextBlock)))
dMs := make([]dataMsg, 1)
dMs[0] = &dataMsgJSON{
original: nextBlock,
source: source,
}
return nil, dMs
}