-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathmetrics.go
More file actions
133 lines (113 loc) · 4.84 KB
/
metrics.go
File metadata and controls
133 lines (113 loc) · 4.84 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package mackerel
import (
"context"
"errors"
"fmt"
"net/url"
"strconv"
)
// MetricValue metric value
type MetricValue struct {
Name string `json:"name,omitempty"`
Time int64 `json:"time,omitempty"`
Value interface{} `json:"value,omitempty"`
}
// HostMetricValue host metric value
type HostMetricValue struct {
HostID string `json:"hostId,omitempty"`
*MetricValue
}
// LatestMetricValues latest metric value
type LatestMetricValues map[string]map[string]*MetricValue
// PostHostMetricValues post host metrics
func (c *Client) PostHostMetricValues(metricValues []*HostMetricValue) error {
return c.PostHostMetricValuesContext(context.Background(), metricValues)
}
// PostHostMetricValuesContext post host metrics
func (c *Client) PostHostMetricValuesContext(ctx context.Context, metricValues []*HostMetricValue) error {
_, err := requestPostContext[any](ctx, c, "/api/v0/tsdb", metricValues)
return err
}
// PostHostMetricValuesByHostID post host metrics
func (c *Client) PostHostMetricValuesByHostID(hostID string, metricValues []*MetricValue) error {
return c.PostHostMetricValuesByHostIDContext(context.Background(), hostID, metricValues)
}
// PostHostMetricValuesByHostIDContext post host metrics
func (c *Client) PostHostMetricValuesByHostIDContext(ctx context.Context, hostID string, metricValues []*MetricValue) error {
var hostMetricValues []*HostMetricValue
for _, metricValue := range metricValues {
hostMetricValues = append(hostMetricValues, &HostMetricValue{
HostID: hostID,
MetricValue: metricValue,
})
}
return c.PostHostMetricValuesContext(ctx, hostMetricValues)
}
// PostServiceMetricValues posts service metrics.
func (c *Client) PostServiceMetricValues(serviceName string, metricValues []*MetricValue) error {
return c.PostServiceMetricValuesContext(context.Background(), serviceName, metricValues)
}
// PostServiceMetricValuesContext posts service metrics.
func (c *Client) PostServiceMetricValuesContext(ctx context.Context, serviceName string, metricValues []*MetricValue) error {
path := fmt.Sprintf("/api/v0/services/%s/tsdb", serviceName)
_, err := requestPostContext[any](ctx, c, path, metricValues)
return err
}
// FetchLatestMetricValues fetches latest metrics.
func (c *Client) FetchLatestMetricValues(hostIDs []string, metricNames []string) (LatestMetricValues, error) {
return c.FetchLatestMetricValuesContext(context.Background(), hostIDs, metricNames)
}
// FetchLatestMetricValuesContext fetches latest metrics.
func (c *Client) FetchLatestMetricValuesContext(ctx context.Context, hostIDs []string, metricNames []string) (LatestMetricValues, error) {
params := url.Values{}
for _, hostID := range hostIDs {
params.Add("hostId", hostID)
}
for _, metricName := range metricNames {
params.Add("name", metricName)
}
data, err := requestGetWithParamsContext[struct {
LatestMetricValues LatestMetricValues `json:"tsdbLatest"`
}](ctx, c, "/api/v0/tsdb/latest", params)
if err != nil {
return nil, err
}
return data.LatestMetricValues, nil
}
// FetchHostMetricValues fetches the metric values for a host.
func (c *Client) FetchHostMetricValues(hostID string, metricName string, from int64, to int64) ([]MetricValue, error) {
return c.fetchMetricValues(context.Background(), hostID, "", metricName, from, to)
}
// FetchHostMetricValuesContext fetches the metric values for a host.
func (c *Client) FetchHostMetricValuesContext(ctx context.Context, hostID string, metricName string, from int64, to int64) ([]MetricValue, error) {
return c.fetchMetricValues(ctx, hostID, "", metricName, from, to)
}
// FetchServiceMetricValues fetches the metric values for a service.
func (c *Client) FetchServiceMetricValues(serviceName string, metricName string, from int64, to int64) ([]MetricValue, error) {
return c.fetchMetricValues(context.Background(), "", serviceName, metricName, from, to)
}
// FetchServiceMetricValuesContext fetches the metric values for a service.
func (c *Client) FetchServiceMetricValuesContext(ctx context.Context, serviceName string, metricName string, from int64, to int64) ([]MetricValue, error) {
return c.fetchMetricValues(ctx, "", serviceName, metricName, from, to)
}
func (c *Client) fetchMetricValues(ctx context.Context, hostID string, serviceName string, metricName string, from int64, to int64) ([]MetricValue, error) {
params := url.Values{}
params.Add("name", metricName)
params.Add("from", strconv.FormatInt(from, 10))
params.Add("to", strconv.FormatInt(to, 10))
path := ""
if hostID != "" {
path = fmt.Sprintf("/api/v0/hosts/%s/metrics", hostID)
} else if serviceName != "" {
path = fmt.Sprintf("/api/v0/services/%s/metrics", serviceName)
} else {
return nil, errors.New("specify either host or service")
}
data, err := requestGetWithParamsContext[struct {
MetricValues []MetricValue `json:"metrics"`
}](ctx, c, path, params)
if err != nil {
return nil, err
}
return data.MetricValues, nil
}