forked from DataDog/datadog-agent
/
raw.go
66 lines (58 loc) · 1.95 KB
/
raw.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2020 Datadog, Inc.
package telemetry
import (
"encoding/json"
"fmt"
)
// Metrics is a container structure for a list of raw metric values. This allows us to set Metrics of a batch payload as
// a pointer and append more metrics to the structure
type Metrics struct {
Values []RawMetrics
}
// RawMetrics single payload structure
type RawMetrics struct {
Name string `json:"name,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
HostName string `json:"host_name,omitempty"`
Value float64 `json:"value,omitempty"`
Tags []string `json:"tags,omitempty"`
}
// RawMetricsMetaData payload containing meta data for the metric
type RawMetricsMetaData struct {
Hostname string `json:"hostname,omitempty"`
Tags []string `json:"tags,omitempty"`
Type string `json:"type,omitempty"`
}
// ConvertToIntakeMetric Converts RawMetricsCheckData struct to an older v1 metrics structure
func (r RawMetrics) ConvertToIntakeMetric() []interface{} {
data := []interface{}{
r.Name,
r.Timestamp,
r.Value,
RawMetricsMetaData{
Hostname: r.HostName,
Type: "raw",
Tags: r.Tags,
},
}
return data
}
// IntakeMetricJSON Converts RawMetricsCheckData struct to an older v1 metrics structure, parses it to JSON and returns
// it as a interface. This is only used in batcher test assertions.
func (r RawMetrics) IntakeMetricJSON() (jsonObject []interface{}) {
jsonString, _ := json.Marshal(r.ConvertToIntakeMetric())
_ = json.Unmarshal(jsonString, &jsonObject)
return jsonObject
}
// JSONString returns a JSON string of the Component
func (r RawMetrics) JSONString() string {
b, err := json.Marshal(r)
if err != nil {
fmt.Println(err)
return fmt.Sprintf("{\"error\": \"%s\"}", err.Error())
}
return string(b)
}