forked from intelsdi-x/snap
/
metric.go
300 lines (260 loc) · 8.43 KB
/
metric.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
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
http://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 plugin
import (
"bytes"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"time"
log "github.com/Sirupsen/logrus"
"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap/core/cdata"
)
const (
// These are our built-in content types for plugins
// SnapAll the wildcard for accepting all snap content types
SnapAllContentType = "snap.*"
// SnapGOB snap metrics serialized into go binary format
SnapGOBContentType = "snap.gob"
// SnapJSON snap metrics serialized into json
SnapJSONContentType = "snap.json"
// SnapProtoBuff snap metrics serialized into protocol buffers
// SnapProtoBuff = "snap.pb" // TO BE IMPLEMENTED
)
type ConfigType struct {
*cdata.ConfigDataNode
}
func (p *ConfigType) UnmarshalJSON(data []byte) error {
cdn := cdata.NewNode()
dec := json.NewDecoder(bytes.NewReader(data))
dec.UseNumber()
if err := dec.Decode(cdn); err != nil {
return err
}
p.ConfigDataNode = cdn
return nil
}
func (p ConfigType) GobEncode() ([]byte, error) {
w := new(bytes.Buffer)
encoder := gob.NewEncoder(w)
if err := encoder.Encode(p.ConfigDataNode); err != nil {
return nil, err
}
return w.Bytes(), nil
}
func (p *ConfigType) GobDecode(data []byte) error {
cdn := cdata.NewNode()
decoder := gob.NewDecoder(bytes.NewReader(data))
if err := decoder.Decode(cdn); err != nil {
return err
}
p.ConfigDataNode = cdn
return nil
}
func NewPluginConfigType() ConfigType {
return ConfigType{
ConfigDataNode: cdata.NewNode(),
}
}
// Represents a metric type. Only used within plugins and across plugin calls.
// Converted to core.MetricType before being used within modules.
type MetricType struct {
// Namespace is the identifier for a metric.
Namespace_ []core.NamespaceElement `json:"namespace"`
// Last advertised time is the last time the snap agent was told about
// a metric.
LastAdvertisedTime_ time.Time `json:"last_advertised_time"`
// The metric version. It is bound to the Plugin version.
Version_ int `json:"version"`
// The config data needed to collect a metric.
Config_ *cdata.ConfigDataNode `json:"config"`
Data_ interface{} `json:"data"`
// Tags are key value pairs that can be added by the framework or any
// plugin along the collect -> process -> publish pipeline.
Tags_ map[string]string `json:"tags"`
// Unit represents the unit of magnitude of the measured quantity.
// See http://metrics20.org/spec/#units as a guideline for this
// field.
Unit_ string
// A (long) description for the metric. The description is stored on the
// metric catalog and not sent through collect -> process -> publish.
Description_ string `json:"description"`
// The timestamp from when the metric was created.
Timestamp_ time.Time `json:"timestamp"`
}
// NewMetricType returns a Constructor
func NewMetricType(namespace core.Namespace, timestamp time.Time, tags map[string]string, unit string, data interface{}) *MetricType {
return &MetricType{
Namespace_: namespace,
Tags_: tags,
Data_: data,
Timestamp_: timestamp,
LastAdvertisedTime_: timestamp,
Unit_: unit,
}
}
// Returns the namespace.
func (p MetricType) Namespace() core.Namespace {
return p.Namespace_
}
// Returns the last time this metric type was received from the plugin.
func (p MetricType) LastAdvertisedTime() time.Time {
return p.LastAdvertisedTime_
}
// Returns the namespace.
func (p MetricType) Version() int {
return p.Version_
}
// Config returns the map of config data for this metric
func (p MetricType) Config() *cdata.ConfigDataNode {
return p.Config_
}
// Tags returns the map of tags for this metric
func (p MetricType) Tags() map[string]string {
return p.Tags_
}
// returns the timestamp of when the metric was collected
func (p MetricType) Timestamp() time.Time {
return p.Timestamp_
}
// returns the data for the metric
func (p MetricType) Data() interface{} {
return p.Data_
}
// returns the description of the metric
func (p MetricType) Description() string {
return p.Description_
}
// returns the metrics unit
func (p MetricType) Unit() string {
return p.Unit_
}
func (p *MetricType) AddData(data interface{}) {
p.Data_ = data
}
// MarshalMetricTypes returns a []byte containing a serialized version of []MetricType using the content type provided.
func MarshalMetricTypes(contentType string, metrics []MetricType) ([]byte, string, error) {
// If we have an empty slice we return an error
if len(metrics) == 0 {
es := fmt.Sprintf("attempt to marshall empty slice of metrics: %s", contentType)
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "marshal-content-type",
"error": es,
}).Error("error while marshalling")
return nil, "", errors.New(es)
}
// Switch on content type
switch contentType {
case SnapAllContentType, SnapGOBContentType:
// NOTE: A snap All wildcard will result in GOB
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(metrics)
if err != nil {
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "marshal-content-type",
"error": err.Error(),
}).Error("error while marshalling")
return nil, "", err
}
// contentType := SnapGOBContentType
return buf.Bytes(), SnapGOBContentType, nil
case SnapJSONContentType:
// Serialize into JSON
b, err := json.Marshal(metrics)
if err != nil {
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "marshal-content-type",
"error": err.Error(),
}).Error("error while marshalling")
return nil, "", err
}
return b, SnapJSONContentType, nil
default:
// We don't recognize this content type. Log and return error.
es := fmt.Sprintf("invalid snap content type: %s", contentType)
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "marshal-content-type",
"error": es,
}).Error("error while marshalling")
return nil, "", errors.New(es)
}
}
// UnmarshallMetricTypes takes a content type and []byte payload and returns a []MetricType
func UnmarshallMetricTypes(contentType string, payload []byte) ([]MetricType, error) {
switch contentType {
case SnapGOBContentType:
var metrics []MetricType
r := bytes.NewBuffer(payload)
err := gob.NewDecoder(r).Decode(&metrics)
if err != nil {
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "unmarshal-content-type",
"error": err.Error(),
}).Error("error while unmarshalling")
return nil, err
}
return metrics, nil
case SnapJSONContentType:
var metrics []MetricType
err := json.Unmarshal(payload, &metrics)
if err != nil {
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "unmarshal-content-type",
"error": err.Error(),
}).Error("error while unmarshalling")
return nil, err
}
return metrics, nil
default:
// We don't recognize this content type as one we can unmarshal. Log and return error.
es := fmt.Sprintf("invalid snap content type for unmarshalling: %s", contentType)
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "unmarshal-content-type",
"error": es,
}).Error("error while unmarshalling")
return nil, errors.New(es)
}
}
// SwapMetricContentType swaps a payload with one content type to another one.
func SwapMetricContentType(contentType, requestedContentType string, payload []byte) ([]byte, string, error) {
metrics, err1 := UnmarshallMetricTypes(contentType, payload)
if err1 != nil {
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "swap-content-type",
"error": err1.Error(),
}).Error("error while swaping")
return nil, "", err1
}
newPayload, newContentType, err2 := MarshalMetricTypes(requestedContentType, metrics)
if err2 != nil {
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "swap-content-type",
"error": err2.Error(),
}).Error("error while swaping")
return nil, "", err2
}
return newPayload, newContentType, nil
}