forked from go-graphite/carbonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zipper.go
78 lines (58 loc) · 1.62 KB
/
zipper.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
package main
import (
"context"
"errors"
"fmt"
"github.com/go-graphite/carbonapi/expr"
pb "github.com/go-graphite/carbonzipper/carbonzipperpb3"
realZipper "github.com/go-graphite/carbonzipper/zipper"
"go.uber.org/zap"
)
var errNoMetrics = errors.New("no metrics")
type zipper struct {
z *realZipper.Zipper
logger *zap.Logger
statsSender func(*realZipper.Stats)
}
func newZipper(sender func(*realZipper.Stats), config *realZipper.Config, logger *zap.Logger) *zipper {
z := &zipper{
z: realZipper.NewZipper(sender, config),
logger: logger,
statsSender: sender,
}
return z
}
func (z zipper) Find(ctx context.Context, metric string) (pb.GlobResponse, error) {
var pbresp pb.GlobResponse
res, stats, err := z.z.Find(ctx, z.logger, metric)
if err != nil {
return pbresp, err
}
pbresp.Name = metric
pbresp.Matches = res
z.statsSender(stats)
return pbresp, err
}
func (z zipper) Info(ctx context.Context, metric string) (map[string]pb.InfoResponse, error) {
resp, stats, err := z.z.Info(ctx, z.logger, metric)
if err != nil {
return nil, fmt.Errorf("http.Get: %+v", err)
}
z.statsSender(stats)
return resp, nil
}
func (z zipper) Render(ctx context.Context, metric string, from, until int32) ([]*expr.MetricData, error) {
var result []*expr.MetricData
pbresp, stats, err := z.z.Render(ctx, z.logger, metric, from, until)
if err != nil {
return result, err
}
z.statsSender(stats)
if m := pbresp.Metrics; len(m) == 0 {
return result, errNoMetrics
}
for i := range pbresp.Metrics {
result = append(result, &expr.MetricData{FetchResponse: pbresp.Metrics[i]})
}
return result, nil
}