-
Notifications
You must be signed in to change notification settings - Fork 29
/
protobuf_group.go
340 lines (289 loc) · 8.9 KB
/
protobuf_group.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
package v2
import (
"context"
"math"
"net"
"net/http"
"net/url"
"strconv"
"github.com/go-graphite/carbonzipper/limiter"
"github.com/go-graphite/carbonzipper/zipper/errors"
"github.com/go-graphite/carbonzipper/zipper/helper"
"github.com/go-graphite/carbonzipper/zipper/httpHeaders"
"github.com/go-graphite/carbonzipper/zipper/metadata"
"github.com/go-graphite/carbonzipper/zipper/types"
protov2 "github.com/go-graphite/protocol/carbonapi_v2_pb"
protov3 "github.com/go-graphite/protocol/carbonapi_v3_pb"
"go.uber.org/zap"
)
const (
format = "protobuf"
)
func init() {
aliases := []string{"carbonapi_v2_pb", "proto_v2_pb", "v2_pb", "pb", "pb3", "protobuf", "protobuf3"}
metadata.Metadata.Lock()
for _, name := range aliases {
metadata.Metadata.SupportedProtocols[name] = struct{}{}
metadata.Metadata.ProtocolInits[name] = New
metadata.Metadata.ProtocolInitsWithLimiter[name] = NewWithLimiter
}
defer metadata.Metadata.Unlock()
}
// RoundRobin is used to connect to backends inside clientGroups, implements ServerClient interface
type ClientProtoV2Group struct {
groupName string
servers []string
client *http.Client
counter uint64
maxIdleConnsPerHost int
limiter *limiter.ServerLimiter
logger *zap.Logger
timeout types.Timeouts
maxTries int
maxMetricsPerRequest int
httpQuery *helper.HttpQuery
}
func NewWithLimiter(logger *zap.Logger, config types.BackendV2, limiter *limiter.ServerLimiter) (types.ServerClient, *errors.Errors) {
logger = logger.With(zap.String("type", "protoV2Group"), zap.String("name", config.GroupName))
httpClient := &http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: *config.MaxIdleConnsPerHost,
DialContext: (&net.Dialer{
Timeout: config.Timeouts.Connect,
KeepAlive: *config.KeepAliveInterval,
DualStack: true,
}).DialContext,
},
}
httpQuery := helper.NewHttpQuery(logger, config.GroupName, config.Servers, *config.MaxTries, limiter, httpClient, httpHeaders.ContentTypeCarbonAPIv2PB)
c := &ClientProtoV2Group{
groupName: config.GroupName,
servers: config.Servers,
timeout: *config.Timeouts,
maxTries: *config.MaxTries,
maxMetricsPerRequest: config.MaxGlobs,
client: httpClient,
limiter: limiter,
logger: logger,
httpQuery: httpQuery,
}
return c, nil
}
func New(logger *zap.Logger, config types.BackendV2) (types.ServerClient, *errors.Errors) {
if config.ConcurrencyLimit == nil {
return nil, errors.Fatal("concurency limit is not set")
}
if len(config.Servers) == 0 {
return nil, errors.Fatal("no servers specified")
}
limiter := limiter.NewServerLimiter([]string{config.GroupName}, *config.ConcurrencyLimit)
return NewWithLimiter(logger, config, limiter)
}
func (c ClientProtoV2Group) MaxMetricsPerRequest() int {
return c.maxMetricsPerRequest
}
func (c ClientProtoV2Group) Name() string {
return c.groupName
}
func (c ClientProtoV2Group) Backends() []string {
return c.servers
}
type queryBatch struct {
pathExpression string
from int64
until int64
}
func (c *ClientProtoV2Group) Fetch(ctx context.Context, request *protov3.MultiFetchRequest) (*protov3.MultiFetchResponse, *types.Stats, *errors.Errors) {
stats := &types.Stats{}
rewrite, _ := url.Parse("http://127.0.0.1/render/")
batches := make(map[queryBatch][]string)
for _, m := range request.Metrics {
b := queryBatch{
pathExpression: m.PathExpression,
from: m.StartTime,
until: m.StopTime,
}
batches[b] = append(batches[b], m.Name)
}
var r protov3.MultiFetchResponse
for batch, targets := range batches {
v := url.Values{
"target": targets,
"format": []string{format},
"from": []string{strconv.Itoa(int(batch.from))},
"until": []string{strconv.Itoa(int(batch.until))},
}
rewrite.RawQuery = v.Encode()
res, err := c.httpQuery.DoQuery(ctx, rewrite.RequestURI(), nil)
if err == nil {
err = &errors.Errors{}
}
if err.HaveFatalErrors {
err.HaveFatalErrors = false
return nil, stats, err
}
var metrics protov2.MultiFetchResponse
err.AddFatal(metrics.Unmarshal(res.Response))
if err.HaveFatalErrors {
return nil, stats, err
}
for _, m := range metrics.Metrics {
for i, v := range m.IsAbsent {
if v {
m.Values[i] = math.NaN()
}
}
r.Metrics = append(r.Metrics, protov3.FetchResponse{
Name: m.Name,
PathExpression: batch.pathExpression,
ConsolidationFunc: "Average",
StopTime: int64(m.StopTime),
StartTime: int64(m.StartTime),
StepTime: int64(m.StepTime),
Values: m.Values,
XFilesFactor: 0.0,
RequestStartTime: batch.from,
RequestStopTime: batch.until,
})
}
}
return &r, stats, nil
}
func (c *ClientProtoV2Group) Find(ctx context.Context, request *protov3.MultiGlobRequest) (*protov3.MultiGlobResponse, *types.Stats, *errors.Errors) {
logger := c.logger.With(zap.String("type", "find"), zap.Strings("request", request.Metrics))
stats := &types.Stats{}
rewrite, _ := url.Parse("http://127.0.0.1/metrics/find/")
var r protov3.MultiGlobResponse
r.Metrics = make([]protov3.GlobResponse, 0)
var e errors.Errors
for _, query := range request.Metrics {
v := url.Values{
"query": []string{query},
"format": []string{format},
}
rewrite.RawQuery = v.Encode()
res, err := c.httpQuery.DoQuery(ctx, rewrite.RequestURI(), nil)
if err != nil {
e.Merge(err)
continue
}
var globs protov2.GlobResponse
marshalErr := globs.Unmarshal(res.Response)
if marshalErr != nil {
e.Add(marshalErr)
continue
}
stats.Servers = append(stats.Servers, res.Server)
matches := make([]protov3.GlobMatch, 0, len(globs.Matches))
for _, m := range globs.Matches {
matches = append(matches, protov3.GlobMatch{
Path: m.Path,
IsLeaf: m.IsLeaf,
})
}
r.Metrics = append(r.Metrics, protov3.GlobResponse{
Name: globs.Name,
Matches: matches,
})
}
if len(e.Errors) != 0 {
logger.Error("errors occurred while getting results",
zap.Any("errors", e.Errors),
)
}
if len(r.Metrics) == 0 {
return nil, stats, errors.FromErr(types.ErrNoResponseFetched)
}
return &r, stats, nil
}
func (c *ClientProtoV2Group) Info(ctx context.Context, request *protov3.MultiMetricsInfoRequest) (*protov3.ZipperInfoResponse, *types.Stats, *errors.Errors) {
logger := c.logger.With(zap.String("type", "info"))
stats := &types.Stats{}
rewrite, _ := url.Parse("http://127.0.0.1/info/")
var r protov3.ZipperInfoResponse
var e errors.Errors
r.Info = make(map[string]protov3.MultiMetricsInfoResponse)
data := protov3.MultiMetricsInfoResponse{}
server := c.groupName
if len(c.servers) == 1 {
server = c.servers[0]
}
for _, query := range request.Names {
v := url.Values{
"target": []string{query},
"format": []string{format},
}
rewrite.RawQuery = v.Encode()
res, e2 := c.httpQuery.DoQuery(ctx, rewrite.RequestURI(), nil)
if e2 != nil {
e.Merge(e2)
continue
}
var info protov2.InfoResponse
err := info.Unmarshal(res.Response)
if err != nil {
e.Add(err)
continue
}
stats.Servers = append(stats.Servers, res.Server)
if info.AggregationMethod == "" {
info.AggregationMethod = "average"
}
infoV3 := protov3.MetricsInfoResponse{
Name: info.Name,
ConsolidationFunc: info.AggregationMethod,
XFilesFactor: info.XFilesFactor,
MaxRetention: int64(info.MaxRetention),
}
for _, r := range info.Retentions {
newR := protov3.Retention{
SecondsPerPoint: int64(r.SecondsPerPoint),
NumberOfPoints: int64(r.NumberOfPoints),
}
infoV3.Retentions = append(infoV3.Retentions, newR)
}
data.Metrics = append(data.Metrics, infoV3)
}
r.Info[server] = data
if len(e.Errors) != 0 {
logger.Error("errors occurred while getting results",
zap.Any("errors", e.Errors),
)
}
if len(r.Info[server].Metrics) == 0 {
return nil, stats, errors.FromErr(types.ErrNoResponseFetched)
}
logger.Debug("got client response",
zap.Any("r", r),
)
return &r, stats, nil
}
func (c *ClientProtoV2Group) List(ctx context.Context) (*protov3.ListMetricsResponse, *types.Stats, *errors.Errors) {
return nil, nil, errors.FromErr(types.ErrNotImplementedYet)
}
func (c *ClientProtoV2Group) Stats(ctx context.Context) (*protov3.MetricDetailsResponse, *types.Stats, *errors.Errors) {
return nil, nil, errors.FromErr(types.ErrNotImplementedYet)
}
func (c *ClientProtoV2Group) ProbeTLDs(ctx context.Context) ([]string, *errors.Errors) {
logger := c.logger.With(zap.String("function", "prober"))
req := &protov3.MultiGlobRequest{
Metrics: []string{"*"},
}
logger.Debug("doing request",
zap.Strings("request", req.Metrics),
)
res, _, err := c.Find(ctx, req)
if err != nil {
return nil, err
}
var tlds []string
for _, m := range res.Metrics {
for _, v := range m.Matches {
tlds = append(tlds, v.Path)
}
}
logger.Debug("will return data",
zap.Strings("tlds", tlds),
)
return tlds, nil
}