-
Notifications
You must be signed in to change notification settings - Fork 53
/
metric_ruleset.go
124 lines (97 loc) · 3.41 KB
/
metric_ruleset.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
package signalfx
import (
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"github.com/signalfx/signalfx-go/metric_ruleset"
)
// MetricRulesetApiURL is the base URL for interacting with metric rulesets.
const MetricRulesetApiURL = "/v2/metricruleset"
// GetMetricRuleset gets a metric ruleset.
func (c *Client) GetMetricRuleset(ctx context.Context, id string) (*metric_ruleset.GetMetricRulesetResponse, error) {
resp, err := c.doRequest(ctx, http.MethodGet, MetricRulesetApiURL+"/"+id, nil, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}
metricRuleset := &metric_ruleset.GetMetricRulesetResponse{}
err = json.NewDecoder(resp.Body).Decode(&metricRuleset)
io.Copy(ioutil.Discard, resp.Body)
return metricRuleset, err
}
// CreateMetricRuleset creates a metric ruleset.
func (c *Client) CreateMetricRuleset(ctx context.Context, metricRuleset *metric_ruleset.CreateMetricRulesetRequest) (*metric_ruleset.CreateMetricRulesetResponse, error) {
payload, err := json.Marshal(metricRuleset)
if err != nil {
return nil, err
}
resp, err := c.doRequest(ctx, http.MethodPost, MetricRulesetApiURL, nil, bytes.NewReader(payload))
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}
createdMetricRuleset := &metric_ruleset.CreateMetricRulesetResponse{}
err = json.NewDecoder(resp.Body).Decode(&createdMetricRuleset)
io.Copy(ioutil.Discard, resp.Body)
return createdMetricRuleset, err
}
// UpdateMetricRuleset updates a metric ruleset.
func (c *Client) UpdateMetricRuleset(ctx context.Context, id string, metricRuleset *metric_ruleset.UpdateMetricRulesetRequest) (*metric_ruleset.UpdateMetricRulesetResponse, error) {
payload, err := json.Marshal(metricRuleset)
if err != nil {
return nil, err
}
resp, err := c.doRequest(ctx, http.MethodPut, MetricRulesetApiURL+"/"+id, nil, bytes.NewReader(payload))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}
updatedMetricRuleset := &metric_ruleset.UpdateMetricRulesetResponse{}
err = json.NewDecoder(resp.Body).Decode(&updatedMetricRuleset)
io.Copy(ioutil.Discard, resp.Body)
return updatedMetricRuleset, err
}
// DeleteMetricRuleset deletes a metric ruleset.
func (c *Client) DeleteMetricRuleset(ctx context.Context, id string) error {
resp, err := c.doRequest(ctx, http.MethodDelete, MetricRulesetApiURL+"/"+id, nil, nil)
if err != nil {
return err
}
defer resp.Body.Close()
if err = newResponseError(resp, http.StatusNoContent); err != nil {
return err
}
io.Copy(ioutil.Discard, resp.Body)
return nil
}
func (c *Client) GenerateAggregationMetricName(ctx context.Context, generateAggregationNameRequest metric_ruleset.GenerateAggregationNameRequest) (string, error) {
payload, err := json.Marshal(generateAggregationNameRequest)
if err != nil {
return "", err
}
resp, err := c.doRequest(ctx, http.MethodPost, MetricRulesetApiURL+"/generateAggregationMetricName", nil, bytes.NewReader(payload))
if err != nil {
return "", err
}
defer resp.Body.Close()
if err = newResponseError(resp, http.StatusOK); err != nil {
return "", err
}
respBody, err := ioutil.ReadAll(resp.Body)
aggregationMetricName := string(respBody)
return aggregationMetricName, err
}