File: json.go

package info (click to toggle)
golang-collectd 0.5.0%2Bgit20240104.26404de-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 468 kB
  • sloc: ansic: 324; sh: 7; makefile: 3
file content (124 lines) | stat: -rw-r--r-- 3,412 bytes parent folder | download
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
115
116
117
118
119
120
121
122
123
124
package api

import (
	"encoding/json"
	"fmt"

	"collectd.org/cdtime"
	"collectd.org/meta"
)

// jsonValueList represents the format used by collectd's JSON export.
type jsonValueList struct {
	Values         []json.Number `json:"values"`
	DSTypes        []string      `json:"dstypes"`
	DSNames        []string      `json:"dsnames,omitempty"`
	Time           cdtime.Time   `json:"time"`
	Interval       cdtime.Time   `json:"interval"`
	Host           string        `json:"host"`
	Plugin         string        `json:"plugin"`
	PluginInstance string        `json:"plugin_instance,omitempty"`
	Type           string        `json:"type"`
	TypeInstance   string        `json:"type_instance,omitempty"`
	Meta           meta.Data     `json:"meta,omitempty"`
}

// MarshalJSON implements the "encoding/json".Marshaler interface for
// ValueList.
func (vl *ValueList) MarshalJSON() ([]byte, error) {
	jvl := jsonValueList{
		Values:         make([]json.Number, len(vl.Values)),
		DSTypes:        make([]string, len(vl.Values)),
		DSNames:        make([]string, len(vl.Values)),
		Time:           cdtime.New(vl.Time),
		Interval:       cdtime.NewDuration(vl.Interval),
		Host:           vl.Host,
		Plugin:         vl.Plugin,
		PluginInstance: vl.PluginInstance,
		Type:           vl.Type,
		TypeInstance:   vl.TypeInstance,
		Meta:           vl.Meta,
	}

	for i, v := range vl.Values {
		switch v := v.(type) {
		case Gauge:
			jvl.Values[i] = json.Number(fmt.Sprintf("%.15g", v))
		case Derive:
			jvl.Values[i] = json.Number(fmt.Sprintf("%d", v))
		case Counter:
			jvl.Values[i] = json.Number(fmt.Sprintf("%d", v))
		default:
			return nil, fmt.Errorf("unexpected data source type: %T", v)
		}
		jvl.DSTypes[i] = v.Type()
		jvl.DSNames[i] = vl.DSName(i)
	}

	return json.Marshal(jvl)
}

// UnmarshalJSON implements the "encoding/json".Unmarshaler interface for
// ValueList.
//
// Please note that this function is currently not compatible with write_http's
// "StoreRates" setting: if enabled, write_http converts derives and counters
// to a rate (a floating point number), but still puts "derive" or "counter" in
// the "dstypes" array. UnmarshalJSON will try to parse such values as
// integers, which will fail in many cases.
func (vl *ValueList) UnmarshalJSON(data []byte) error {
	var jvl jsonValueList

	if err := json.Unmarshal(data, &jvl); err != nil {
		return err
	}

	vl.Host = jvl.Host
	vl.Plugin = jvl.Plugin
	vl.PluginInstance = jvl.PluginInstance
	vl.Type = jvl.Type
	vl.TypeInstance = jvl.TypeInstance

	vl.Time = jvl.Time.Time()
	vl.Interval = jvl.Interval.Duration()
	vl.Values = make([]Value, len(jvl.Values))

	if len(jvl.Values) != len(jvl.DSTypes) {
		return fmt.Errorf("invalid data: %d value(s), %d data source type(s)",
			len(jvl.Values), len(jvl.DSTypes))
	}

	for i, n := range jvl.Values {
		switch jvl.DSTypes[i] {
		case "gauge":
			v, err := n.Float64()
			if err != nil {
				return err
			}
			vl.Values[i] = Gauge(v)
		case "derive":
			v, err := n.Int64()
			if err != nil {
				return err
			}
			vl.Values[i] = Derive(v)
		case "counter":
			v, err := n.Int64()
			if err != nil {
				return err
			}
			vl.Values[i] = Counter(v)
		default:
			return fmt.Errorf("unexpected data source type: %q", jvl.DSTypes[i])
		}
	}

	if len(jvl.DSNames) >= len(vl.Values) {
		vl.DSNames = make([]string, len(vl.Values))
		copy(vl.DSNames, jvl.DSNames)
	}

	vl.Meta = jvl.Meta

	return nil
}