forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
412 lines (349 loc) · 11.7 KB
/
types.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package cloudmonitoring
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/huandu/xstrings"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/tsdb/cloud-monitoring/kinds/dataquery"
)
type (
cloudMonitoringQueryExecutor interface {
run(ctx context.Context, req *backend.QueryDataRequest, s *Service, dsInfo datasourceInfo, tracer tracing.Tracer) (
*backend.DataResponse, cloudMonitoringResponse, string, error)
parseResponse(dr *backend.DataResponse, data cloudMonitoringResponse, executedQueryString string) error
buildDeepLink() string
getRefID() string
getAliasBy() string
getParameter(i string) string
}
// Plugin API query data request used to generate
// a cloudMonitoringTimeSeriesList or cloudMonitoringTimeSeriesQuery
grafanaQuery struct {
AliasBy string `json:"aliasBy"`
TimeSeriesList *dataquery.TimeSeriesList `json:"timeSeriesList,omitempty"`
TimeSeriesQuery *dataquery.TimeSeriesQuery `json:"timeSeriesQuery,omitempty"`
SloQuery *dataquery.SLOQuery `json:"sloQuery,omitempty"`
}
cloudMonitoringTimeSeriesList struct {
refID string
aliasBy string
logger log.Logger
parameters *dataquery.TimeSeriesList
// Processed properties
params url.Values
}
// cloudMonitoringSLO is used to build time series with a filter but for the SLO case
cloudMonitoringSLO struct {
refID string
aliasBy string
logger log.Logger
parameters *dataquery.SLOQuery
// Processed properties
params url.Values
}
// cloudMonitoringTimeSeriesQuery is used to build MQL queries
cloudMonitoringTimeSeriesQuery struct {
refID string
aliasBy string
logger log.Logger
parameters *dataquery.TimeSeriesQuery
// Processed properties
timeRange backend.TimeRange
IntervalMS int64
}
cloudMonitoringBucketOptions struct {
LinearBuckets *struct {
NumFiniteBuckets int64 `json:"numFiniteBuckets"`
Width float64 `json:"width"`
Offset float64 `json:"offset"`
} `json:"linearBuckets"`
ExponentialBuckets *struct {
NumFiniteBuckets int64 `json:"numFiniteBuckets"`
GrowthFactor float64 `json:"growthFactor"`
Scale float64 `json:"scale"`
} `json:"exponentialBuckets"`
ExplicitBuckets *struct {
Bounds []float64 `json:"bounds"`
} `json:"explicitBuckets"`
}
cloudMonitoringResponse struct {
TimeSeries []timeSeries `json:"timeSeries"`
TimeSeriesDescriptor timeSeriesDescriptor `json:"timeSeriesDescriptor"`
TimeSeriesData []timeSeriesData `json:"timeSeriesData"`
Unit string `json:"unit"`
NextPageToken string `json:"nextPageToken"`
}
)
type pointIterator interface {
length() int
getPoint(index int) point
metricType() string
valueType() string
}
type point interface {
doubleValue(descriptorIndex int) float64
int64Value(descriptorIndex int) string
boolValue(descriptorIndex int) bool
bucketCounts(descriptorIndex int) []string
bucketValue(descriptorIndex int, bucketCountIndex int) string
bucketOptions(descriptorIndex int) cloudMonitoringBucketOptions
endTime() time.Time
}
type timeSeriesDescriptor struct {
LabelDescriptors []LabelDescriptor `json:"labelDescriptors"`
PointDescriptors []timeSeriesPointDescriptor `json:"pointDescriptors"`
}
type LabelDescriptor struct {
Key string `json:"key"`
ValueType string `json:"valueType"`
Description string `json:"description"`
}
type timeSeriesPointDescriptor struct {
Key string `json:"key"`
ValueType string `json:"valueType"`
MetricKind string `json:"metricKind"`
}
func (ts timeSeriesPointDescriptor) metricType() string {
return ts.Key
}
func (ts timeSeriesPointDescriptor) valueType() string {
return ts.ValueType
}
type timeSeriesData struct {
LabelValues []struct {
BoolValue bool `json:"boolValue"`
Int64Value string `json:"int64Value"`
StringValue string `json:"stringValue"`
} `json:"labelValues"`
PointData []timeSeriesPointData `json:"pointData"`
}
func (ts timeSeriesData) length() int {
return len(ts.PointData)
}
func (ts timeSeriesData) getPoint(index int) point {
return &ts.PointData[index]
}
func (ts timeSeriesData) getLabels(labelDescriptors []LabelDescriptor) (data.Labels, string) {
seriesLabels := make(map[string]string)
defaultMetricName := ""
for n, d := range labelDescriptors {
key := xstrings.ToSnakeCase(d.Key)
key = strings.Replace(key, ".", ".label.", 1)
labelValue := ts.LabelValues[n]
switch d.ValueType {
case "BOOL":
strVal := strconv.FormatBool(labelValue.BoolValue)
seriesLabels[key] = strVal
case "INT64":
seriesLabels[key] = labelValue.Int64Value
default:
seriesLabels[key] = labelValue.StringValue
}
if strings.Contains(key, "metric.label") || strings.Contains(key, "resource.label") || strings.Contains(key, "metadata.label") {
defaultMetricName += seriesLabels[key] + " "
}
}
defaultMetricName = strings.Trim(defaultMetricName, " ")
return seriesLabels, defaultMetricName
}
type timeSeriesDataIterator struct {
timeSeriesData
timeSeriesPointDescriptor
}
type timeSeriesPointData struct {
Values []timeSeriesPointValue `json:"values"`
TimeInterval struct {
EndTime time.Time `json:"endTime"`
StartTime time.Time `json:"startTime"`
} `json:"timeInterval"`
}
func (point timeSeriesPointData) doubleValue(descriptorIndex int) float64 {
return point.Values[descriptorIndex].DoubleValue
}
func (point timeSeriesPointData) int64Value(descriptorIndex int) string {
return point.Values[descriptorIndex].IntValue
}
func (point timeSeriesPointData) boolValue(descriptorIndex int) bool {
return point.Values[descriptorIndex].BoolValue
}
func (point timeSeriesPointData) bucketCounts(descriptorIndex int) []string {
return point.Values[descriptorIndex].DistributionValue.BucketCounts
}
func (point timeSeriesPointData) bucketValue(descriptorIndex int, bucketCountIndex int) string {
return point.Values[descriptorIndex].DistributionValue.BucketCounts[bucketCountIndex]
}
func (point timeSeriesPointData) bucketOptions(descriptorIndex int) cloudMonitoringBucketOptions {
return point.Values[descriptorIndex].DistributionValue.BucketOptions
}
func (point timeSeriesPointData) endTime() time.Time {
return point.TimeInterval.EndTime
}
type timeSeries struct {
Metric struct {
Labels map[string]string `json:"labels"`
Type string `json:"type"`
} `json:"metric"`
Resource struct {
Type string `json:"type"`
Labels map[string]string `json:"labels"`
} `json:"resource"`
MetaData map[string]map[string]interface{} `json:"metadata"`
MetricKind string `json:"metricKind"`
ValueType string `json:"valueType"`
Points []timeSeriesPoint `json:"points"`
}
func (ts timeSeries) length() int {
return len(ts.Points)
}
func (ts timeSeries) getPoint(index int) point {
return &ts.Points[index]
}
func (ts timeSeries) metricType() string {
return ts.Metric.Type
}
func (ts timeSeries) valueType() string {
return ts.ValueType
}
func (ts timeSeries) getLabels(groupBys []string) (data.Labels, string) {
seriesLabels := data.Labels{}
defaultMetricName := ts.Metric.Type
seriesLabels["resource.type"] = ts.Resource.Type
groupBysMap := make(map[string]bool)
for _, groupBy := range groupBys {
groupBysMap[groupBy] = true
}
for key, value := range ts.Metric.Labels {
seriesLabels["metric.label."+key] = value
if len(groupBys) == 0 || groupBysMap["metric.label."+key] {
defaultMetricName += " " + value
}
}
for key, value := range ts.Resource.Labels {
seriesLabels["resource.label."+key] = value
if groupBysMap["resource.label."+key] {
defaultMetricName += " " + value
}
}
for labelType, labelTypeValues := range ts.MetaData {
for labelKey, labelValue := range labelTypeValues {
key := xstrings.ToSnakeCase(fmt.Sprintf("metadata.%s.%s", labelType, labelKey))
switch v := labelValue.(type) {
case string:
seriesLabels[key] = v
case bool:
strVal := strconv.FormatBool(v)
seriesLabels[key] = strVal
case []interface{}:
for _, v := range v {
strVal := v.(string)
if len(seriesLabels[key]) > 0 {
strVal = fmt.Sprintf("%s, %s", seriesLabels[key], strVal)
}
seriesLabels[key] = strVal
}
}
}
}
return seriesLabels, defaultMetricName
}
type timeSeriesPoint struct {
Interval struct {
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
} `json:"interval"`
Value timeSeriesPointValue `json:"value"`
}
type timeSeriesPointValue struct {
DoubleValue float64 `json:"doubleValue"`
StringValue string `json:"stringValue"`
BoolValue bool `json:"boolValue"`
IntValue string `json:"int64Value"`
DistributionValue struct {
Count string `json:"count"`
Mean float64 `json:"mean"`
SumOfSquaredDeviation float64 `json:"sumOfSquaredDeviation"`
Range struct {
Min int `json:"min"`
Max int `json:"max"`
} `json:"range"`
BucketOptions cloudMonitoringBucketOptions `json:"bucketOptions"`
BucketCounts []string `json:"bucketCounts"`
Examplars []struct {
Value float64 `json:"value"`
Timestamp string `json:"timestamp"`
// attachments
} `json:"examplars"`
} `json:"distributionValue"`
}
func (point timeSeriesPoint) doubleValue(descriptorIndex int) float64 {
return point.Value.DoubleValue
}
func (point timeSeriesPoint) int64Value(descriptorIndex int) string {
return point.Value.IntValue
}
func (point timeSeriesPoint) boolValue(descriptorIndex int) bool {
return point.Value.BoolValue
}
func (point timeSeriesPoint) bucketCounts(descriptorIndex int) []string {
return point.Value.DistributionValue.BucketCounts
}
func (point timeSeriesPoint) bucketValue(descriptorIndex int, bucketCountIndex int) string {
return point.Value.DistributionValue.BucketCounts[bucketCountIndex]
}
func (point timeSeriesPoint) bucketOptions(descriptorIndex int) cloudMonitoringBucketOptions {
return point.Value.DistributionValue.BucketOptions
}
func (point timeSeriesPoint) endTime() time.Time {
return point.Interval.EndTime
}
type metricDescriptorResponse struct {
Descriptors []metricDescriptor `json:"metricDescriptors"`
Token string `json:"nextPageToken"`
}
type metricDescriptor struct {
ValueType string `json:"valueType"`
MetricKind string `json:"metricKind"`
Type string `json:"type"`
Unit string `json:"unit"`
Service string `json:"service"`
ServiceShortName string `json:"serviceShortName"`
DisplayName string `json:"displayName"`
Description string `json:"description"`
}
type projectResponse struct {
Projects []projectDescription `json:"projects"`
Token string `json:"nextPageToken"`
}
type projectDescription struct {
ProjectID string `json:"projectId"`
Name string `json:"name"`
}
type serviceResponse struct {
Services []serviceDescription `json:"services"`
Token string `json:"nextPageToken"`
}
type serviceDescription struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
}
type sloResponse struct {
SLOs []sloDescription `json:"serviceLevelObjectives"`
Token string `json:"nextPageToken"`
}
type sloDescription struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Goal float64 `json:"goal"`
}
type selectableValue struct {
Value string `json:"value"`
Label string `json:"label"`
Goal float64 `json:"goal,omitempty"`
}