|
| 1 | +// Slo types lifted from github.com/grafana/slo/pkg/generated/models/slo/slo_type_gen.go |
| 2 | +package gapi |
| 3 | + |
| 4 | +import ( |
| 5 | + "bytes" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | +) |
| 9 | + |
| 10 | +var sloPath string = "/api/plugins/grafana-slo-app/resources/v1/slo" |
| 11 | + |
| 12 | +type Slos struct { |
| 13 | + Slos []Slo `json:"slos"` |
| 14 | +} |
| 15 | + |
| 16 | +const ( |
| 17 | + QueryTypeFreeform QueryType = "freeform" |
| 18 | + QueryTypeHistogram QueryType = "histogram" |
| 19 | + QueryTypeRatio QueryType = "ratio" |
| 20 | + QueryTypeThreshold QueryType = "threshold" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + ThresholdOperatorEmpty ThresholdOperator = "<" |
| 25 | + ThresholdOperatorEqualEqual ThresholdOperator = "==" |
| 26 | + ThresholdOperatorN1 ThresholdOperator = "<=" |
| 27 | + ThresholdOperatorN2 ThresholdOperator = ">=" |
| 28 | + ThresholdOperatorN3 ThresholdOperator = ">" |
| 29 | +) |
| 30 | + |
| 31 | +type Alerting struct { |
| 32 | + Annotations []Label `json:"annotations,omitempty"` |
| 33 | + FastBurn *AlertingMetadata `json:"fastBurn,omitempty"` |
| 34 | + Labels []Label `json:"labels,omitempty"` |
| 35 | + SlowBurn *AlertingMetadata `json:"slowBurn,omitempty"` |
| 36 | +} |
| 37 | + |
| 38 | +type AlertingMetadata struct { |
| 39 | + Annotations []Label `json:"annotations,omitempty"` |
| 40 | + Labels []Label `json:"labels,omitempty"` |
| 41 | +} |
| 42 | + |
| 43 | +type DashboardRef struct { |
| 44 | + UID string `json:"UID"` |
| 45 | +} |
| 46 | + |
| 47 | +type FreeformQuery struct { |
| 48 | + Query string `json:"query"` |
| 49 | +} |
| 50 | + |
| 51 | +type HistogramQuery struct { |
| 52 | + GroupByLabels []string `json:"groupByLabels,omitempty"` |
| 53 | + Metric MetricDef `json:"metric"` |
| 54 | + Percentile float64 `json:"percentile"` |
| 55 | + Threshold Threshold `json:"threshold"` |
| 56 | +} |
| 57 | + |
| 58 | +type Label struct { |
| 59 | + Key string `json:"key"` |
| 60 | + Value string `json:"value"` |
| 61 | +} |
| 62 | + |
| 63 | +type MetricDef struct { |
| 64 | + PrometheusMetric string `json:"prometheusMetric"` |
| 65 | + Type *string `json:"type,omitempty"` |
| 66 | +} |
| 67 | + |
| 68 | +type Objective struct { |
| 69 | + Value float64 `json:"value"` |
| 70 | + Window string `json:"window"` |
| 71 | +} |
| 72 | + |
| 73 | +type Query struct { |
| 74 | + Freeform *FreeformQuery `json:"freeform,omitempty"` |
| 75 | + Histogram *HistogramQuery `json:"histogram,omitempty"` |
| 76 | + Ratio *RatioQuery `json:"ratio,omitempty"` |
| 77 | + Threshold *ThresholdQuery `json:"threshold,omitempty"` |
| 78 | + Type QueryType `json:"type"` |
| 79 | +} |
| 80 | + |
| 81 | +type QueryType string |
| 82 | + |
| 83 | +type RatioQuery struct { |
| 84 | + GroupByLabels []string `json:"groupByLabels,omitempty"` |
| 85 | + SuccessMetric MetricDef `json:"successMetric"` |
| 86 | + TotalMetric MetricDef `json:"totalMetric"` |
| 87 | +} |
| 88 | + |
| 89 | +type Slo struct { |
| 90 | + Alerting *Alerting `json:"alerting,omitempty"` |
| 91 | + Description string `json:"description"` |
| 92 | + DrillDownDashboardRef *DashboardRef `json:"drillDownDashboardRef,omitempty"` |
| 93 | + Labels []Label `json:"labels,omitempty"` |
| 94 | + Name string `json:"name"` |
| 95 | + Objectives []Objective `json:"objectives"` |
| 96 | + Query Query `json:"query"` |
| 97 | + UUID string `json:"uuid"` |
| 98 | +} |
| 99 | + |
| 100 | +type Threshold struct { |
| 101 | + Operator ThresholdOperator `json:"operator"` |
| 102 | + Value float64 `json:"value"` |
| 103 | +} |
| 104 | + |
| 105 | +type ThresholdOperator string |
| 106 | + |
| 107 | +type ThresholdQuery struct { |
| 108 | + GroupByLabels []string `json:"groupByLabels,omitempty"` |
| 109 | + Metric MetricDef `json:"metric"` |
| 110 | + Threshold Threshold `json:"threshold"` |
| 111 | +} |
| 112 | + |
| 113 | +type CreateSLOResponse struct { |
| 114 | + Message string `json:"message,omitempty"` |
| 115 | + UUID string `json:"uuid,omitempty"` |
| 116 | +} |
| 117 | + |
| 118 | +// ListSlos retrieves a list of all Slos |
| 119 | +func (c *Client) ListSlos() (Slos, error) { |
| 120 | + var slos Slos |
| 121 | + |
| 122 | + if err := c.request("GET", sloPath, nil, nil, &slos); err != nil { |
| 123 | + return Slos{}, err |
| 124 | + } |
| 125 | + |
| 126 | + return slos, nil |
| 127 | +} |
| 128 | + |
| 129 | +// GetSLO returns a single Slo based on its uuid |
| 130 | +func (c *Client) GetSlo(uuid string) (Slo, error) { |
| 131 | + var slo Slo |
| 132 | + path := fmt.Sprintf("%s/%s", sloPath, uuid) |
| 133 | + |
| 134 | + if err := c.request("GET", path, nil, nil, &slo); err != nil { |
| 135 | + return Slo{}, err |
| 136 | + } |
| 137 | + |
| 138 | + return slo, nil |
| 139 | +} |
| 140 | + |
| 141 | +// CreateSLO creates a single Slo |
| 142 | +func (c *Client) CreateSlo(slo Slo) (CreateSLOResponse, error) { |
| 143 | + response := CreateSLOResponse{} |
| 144 | + |
| 145 | + data, err := json.Marshal(slo) |
| 146 | + if err != nil { |
| 147 | + return response, err |
| 148 | + } |
| 149 | + |
| 150 | + if err := c.request("POST", sloPath, nil, bytes.NewBuffer(data), &response); err != nil { |
| 151 | + return CreateSLOResponse{}, err |
| 152 | + } |
| 153 | + |
| 154 | + return response, err |
| 155 | +} |
| 156 | + |
| 157 | +// DeleteSLO deletes the Slo with the passed in UUID |
| 158 | +func (c *Client) DeleteSlo(uuid string) error { |
| 159 | + path := fmt.Sprintf("%s/%s", sloPath, uuid) |
| 160 | + return c.request("DELETE", path, nil, nil, nil) |
| 161 | +} |
| 162 | + |
| 163 | +// UpdateSLO updates the Slo with the passed in UUID and Slo |
| 164 | +func (c *Client) UpdateSlo(uuid string, slo Slo) error { |
| 165 | + path := fmt.Sprintf("%s/%s", sloPath, uuid) |
| 166 | + |
| 167 | + data, err := json.Marshal(slo) |
| 168 | + if err != nil { |
| 169 | + return err |
| 170 | + } |
| 171 | + |
| 172 | + if err := c.request("PUT", path, nil, bytes.NewBuffer(data), nil); err != nil { |
| 173 | + return err |
| 174 | + } |
| 175 | + |
| 176 | + return nil |
| 177 | +} |
0 commit comments