-
Notifications
You must be signed in to change notification settings - Fork 451
/
storage.go
194 lines (166 loc) · 5.32 KB
/
storage.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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package validator
import (
"context"
"errors"
"fmt"
"math"
"strconv"
"time"
"github.com/m3db/m3/src/query/api/v1/handler/prometheus"
"github.com/m3db/m3/src/query/block"
"github.com/m3db/m3/src/query/models"
"github.com/m3db/m3/src/query/storage"
"github.com/m3db/m3/src/query/ts"
)
type debugStorage struct {
seriesList []*ts.Series
lookbackDuration time.Duration
}
// NewStorage creates a new debug storage instance.
func NewStorage(promReadResp prometheus.PromResp, lookbackDuration time.Duration) (storage.Storage, error) {
seriesList, err := PromResultToSeriesList(promReadResp, models.NewTagOptions())
if err != nil {
return nil, err
}
return &debugStorage{
seriesList: seriesList,
lookbackDuration: lookbackDuration,
}, nil
}
func (s *debugStorage) Fetch(
ctx context.Context,
query *storage.FetchQuery,
options *storage.FetchOptions,
) (*storage.FetchResult, error) {
return &storage.FetchResult{
SeriesList: s.seriesList,
}, nil
}
func (s *debugStorage) FetchBlocks(
ctx context.Context,
query *storage.FetchQuery,
options *storage.FetchOptions,
) (block.Result, error) {
fetchResult, err := s.Fetch(ctx, query, options)
if err != nil {
return block.Result{}, err
}
return storage.FetchResultToBlockResult(fetchResult, query, s.lookbackDuration, options.Enforcer)
}
// PromResultToSeriesList converts a prom result to a series list
func PromResultToSeriesList(promReadResp prometheus.PromResp, tagOptions models.TagOptions) ([]*ts.Series, error) {
if promReadResp.Data.ResultType != "matrix" {
return nil, fmt.Errorf("unsupported result type found: %s", promReadResp.Data.ResultType)
}
results := promReadResp.Data.Result
seriesList := make([]*ts.Series, len(results))
for i, result := range results {
dps := make(ts.Datapoints, len(result.Values))
for i, dp := range result.Values {
if len(dp) != 2 {
return nil, errors.New("misformatted datapoint found")
}
dpTimeFloat, ok := dp[0].(float64)
if !ok {
return nil, fmt.Errorf("unable to cast to float: %v", dp[0])
}
dps[i].Timestamp = time.Unix(0, int64(dpTimeFloat)*int64(time.Second))
dpValStr, ok := dp[1].(string)
if !ok {
return nil, fmt.Errorf("unable to cast to string: %v", dp[1])
}
if dpValStr == "NaN" {
dps[i].Value = math.NaN()
} else {
s, err := strconv.ParseFloat(dpValStr, 64)
if err != nil {
return nil, err
}
dps[i].Value = s
}
}
metricName := string(tagOptions.MetricName())
bucketName := string(tagOptions.BucketName())
tags := models.NewTags(len(result.Metric), tagOptions)
for name, val := range result.Metric {
if name == metricName {
tags = tags.SetName([]byte(val))
} else if name == bucketName {
tags = tags.SetBucket([]byte(val))
} else {
tags = tags.AddTag(models.Tag{
Name: []byte(name),
Value: []byte(val),
})
}
}
// NB: if there is no tag for series name here, the input is a Prometheus
// query result for a function that mutates the output tags and drops `name`
// which is a valid case.
//
// It's safe to set ts.Series.Name() here to a default value, as this field
// is used as a minor optimization for presenting grafana output, and as
// such, series names are not validated for equality.
name, exists := tags.Name()
if !exists {
name = []byte("default")
}
seriesList[i] = ts.NewSeries(
name,
dps,
tags,
)
}
return seriesList, nil
}
func (s *debugStorage) Type() storage.Type {
return storage.TypeDebug
}
func (s *debugStorage) ErrorBehavior() storage.ErrorBehavior {
return storage.BehaviorWarn
}
func (s *debugStorage) Name() string {
return "debug_store"
}
func (s *debugStorage) SearchSeries(
ctx context.Context,
query *storage.FetchQuery,
_ *storage.FetchOptions,
) (*storage.SearchResults, error) {
return nil, errors.New("SearchSeries not implemented")
}
func (s *debugStorage) CompleteTags(
ctx context.Context,
query *storage.CompleteTagsQuery,
_ *storage.FetchOptions,
) (*storage.CompleteTagsResult, error) {
return nil, errors.New("CompleteTags not implemented")
}
func (s *debugStorage) Close() error {
return nil
}
func (s *debugStorage) Write(
ctx context.Context,
query *storage.WriteQuery,
) error {
return errors.New("write not implemented")
}