This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
slo.go
209 lines (171 loc) · 5.8 KB
/
slo.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
// Slo types lifted from github.com/grafana/slo/pkg/generated/models/slo/slo_type_gen.go
package gapi
import (
"encoding/json"
"fmt"
)
var sloPath = "/api/plugins/grafana-slo-app/resources/v1/slo"
type Slos struct {
Slos []Slo `json:"slos"`
}
// Defines values for QueryType.
const (
QueryTypeFreeform QueryType = "freeform"
QueryTypeHistogram QueryType = "histogram"
QueryTypeRatio QueryType = "ratio"
QueryTypeThreshold QueryType = "threshold"
)
// Defines values for ThresholdOperator.
const (
ThresholdOperatorEmpty ThresholdOperator = "<"
ThresholdOperatorEqualEqual ThresholdOperator = "=="
ThresholdOperatorN1 ThresholdOperator = "<="
ThresholdOperatorN2 ThresholdOperator = ">="
ThresholdOperatorN3 ThresholdOperator = ">"
)
// Alerting defines model for Alerting.
type Alerting struct {
Annotations []Label `json:"annotations,omitempty"`
FastBurn *AlertingMetadata `json:"fastBurn,omitempty"`
Labels []Label `json:"labels,omitempty"`
SlowBurn *AlertingMetadata `json:"slowBurn,omitempty"`
}
// AlertingMetadata defines model for AlertingMetadata.
type AlertingMetadata struct {
Annotations []Label `json:"annotations,omitempty"`
Labels []Label `json:"labels,omitempty"`
}
// DashboardRef defines model for DashboardRef.
type DashboardRef struct {
UID string `json:"UID"`
}
// DestinationDatasource defines model for DestinationDatasource.
type DestinationDatasource struct {
Type string `json:"type,omitempty"`
UID string `json:"uid,omitempty"`
}
// FreeformQuery defines model for FreeformQuery.
type FreeformQuery struct {
Query string `json:"query"`
}
// HistogramQuery defines model for HistogramQuery.
type HistogramQuery struct {
GroupByLabels []string `json:"groupByLabels,omitempty"`
Metric MetricDef `json:"metric"`
Percentile float64 `json:"percentile"`
Threshold Threshold `json:"threshold"`
}
// Label defines model for Label.
type Label struct {
Key string `json:"key"`
Value string `json:"value"`
}
// MetricDef defines model for MetricDef.
type MetricDef struct {
PrometheusMetric string `json:"prometheusMetric"`
Type string `json:"type,omitempty"`
}
// Objective defines model for Objective.
type Objective struct {
Value float64 `json:"value"`
Window string `json:"window"`
}
// Query defines model for Query.
type Query struct {
Freeform *FreeformQuery `json:"freeform,omitempty"`
Histogram *HistogramQuery `json:"histogram,omitempty"`
Ratio *RatioQuery `json:"ratio,omitempty"`
Threshold *ThresholdQuery `json:"threshold,omitempty"`
Type QueryType `json:"type"`
}
// QueryType defines model for Query.Type.
type QueryType string
// RatioQuery defines model for RatioQuery.
type RatioQuery struct {
GroupByLabels []string `json:"groupByLabels,omitempty"`
SuccessMetric MetricDef `json:"successMetric"`
TotalMetric MetricDef `json:"totalMetric"`
}
// ReadOnly defines model for ReadOnly.
type ReadOnly struct {
DrillDownDashboardRef *DashboardRef `json:"drillDownDashboardRef,omitempty"`
Provenance string `json:"provenance,omitempty"`
Status *Status `json:"status,omitempty"`
}
// Slo defines model for Slo.
type Slo struct {
Alerting *Alerting `json:"alerting,omitempty"`
Description string `json:"description"`
DestinationDatasource *DestinationDatasource `json:"destinationDatasource,omitempty"`
Labels []Label `json:"labels,omitempty"`
Name string `json:"name"`
Objectives []Objective `json:"objectives"`
Query Query `json:"query"`
ReadOnly *ReadOnly `json:"readOnly,omitempty"`
UUID string `json:"uuid"`
}
// Status defines model for Status.
type Status struct {
Message string `json:"message,omitempty"`
Type string `json:"type"`
}
// Threshold defines model for Threshold.
type Threshold struct {
Operator ThresholdOperator `json:"operator"`
Value float64 `json:"value"`
}
// ThresholdOperator defines model for Threshold.Operator.
type ThresholdOperator string
// ThresholdQuery defines model for ThresholdQuery.
type ThresholdQuery struct {
GroupByLabels []string `json:"groupByLabels,omitempty"`
Metric MetricDef `json:"metric"`
Threshold Threshold `json:"threshold"`
}
type CreateSLOResponse struct {
Message string `json:"message,omitempty"`
UUID string `json:"uuid,omitempty"`
}
// ListSlos retrieves a list of all Slos
func (c *Client) ListSlos() (Slos, error) {
var slos Slos
if err := c.request("GET", sloPath, nil, nil, &slos); err != nil {
return Slos{}, err
}
return slos, nil
}
// GetSLO returns a single Slo based on its uuid
func (c *Client) GetSlo(uuid string) (Slo, error) {
var slo Slo
path := fmt.Sprintf("%s/%s", sloPath, uuid)
if err := c.request("GET", path, nil, nil, &slo); err != nil {
return Slo{}, err
}
return slo, nil
}
// CreateSLO creates a single Slo
func (c *Client) CreateSlo(slo Slo) (CreateSLOResponse, error) {
response := CreateSLOResponse{}
data, err := json.Marshal(slo)
if err != nil {
return response, err
}
if err := c.request("POST", sloPath, nil, data, &response); err != nil {
return CreateSLOResponse{}, err
}
return response, err
}
// DeleteSLO deletes the Slo with the passed in UUID
func (c *Client) DeleteSlo(uuid string) error {
path := fmt.Sprintf("%s/%s", sloPath, uuid)
return c.request("DELETE", path, nil, nil, nil)
}
// UpdateSLO updates the Slo with the passed in UUID and Slo
func (c *Client) UpdateSlo(uuid string, slo Slo) error {
path := fmt.Sprintf("%s/%s", sloPath, uuid)
data, err := json.Marshal(slo)
if err != nil {
return err
}
return c.request("PUT", path, nil, data, nil)
}