Skip to content

Commit 0b30928

Browse files
authored
refactor(finder): add Stats method to Result interface (#315)
* add Stats method to Finder interface * fix linter errors * init metrics for new table
1 parent 4960cc2 commit 0b30928

22 files changed

+173
-86
lines changed

config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,10 @@ func (c *Config) setupGraphiteMetrics() bool {
10121012
metrics.InitQueryMetrics(c.ClickHouse.TaggedTable, &c.Metrics)
10131013
}
10141014

1015+
if c.ClickHouse.TagsCountTable != "" {
1016+
metrics.InitQueryMetrics(c.ClickHouse.TagsCountTable, &c.Metrics)
1017+
}
1018+
10151019
return metrics.Graphite != nil
10161020
}
10171021

find/find.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ func NewCached(config *config.Config, body []byte) *Find {
2828
}
2929
}
3030

31-
func New(config *config.Config, ctx context.Context, query string, stat *finder.FinderStat) (*Find, error) {
32-
res, err := finder.Find(config, ctx, query, 0, 0, stat)
31+
func New(config *config.Config, ctx context.Context, query string) (*Find, error) {
32+
res, err := finder.Find(config, ctx, query, 0, 0)
3333
if err != nil {
3434
return nil, err
3535
}

find/handler.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/go-graphite/carbonapi/pkg/parser"
1212
v3pb "github.com/go-graphite/protocol/carbonapi_v3_pb"
1313
"github.com/lomik/graphite-clickhouse/config"
14-
"github.com/lomik/graphite-clickhouse/finder"
1514
"github.com/lomik/graphite-clickhouse/helper/clickhouse"
1615
"github.com/lomik/graphite-clickhouse/helper/utils"
1716
"github.com/lomik/graphite-clickhouse/logs"
@@ -41,7 +40,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4140

4241
var (
4342
metricsCount int64
44-
stat finder.FinderStat
43+
stat metrics.FinderStat
4544
queueFail bool
4645
queueDuration time.Duration
4746
findCache bool
@@ -195,7 +194,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
195194
}()
196195
}
197196

198-
f, err := New(h.config, r.Context(), query, &stat)
197+
f, err := New(h.config, r.Context(), query)
199198

200199
if entered {
201200
// release early as possible

finder/base.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/lomik/graphite-clickhouse/config"
1111
"github.com/lomik/graphite-clickhouse/helper/clickhouse"
12+
"github.com/lomik/graphite-clickhouse/metrics"
1213
"github.com/lomik/graphite-clickhouse/pkg/scope"
1314
"github.com/lomik/graphite-clickhouse/pkg/where"
1415
)
@@ -20,13 +21,15 @@ type BaseFinder struct {
2021
table string // graphite_tree table
2122
opts clickhouse.Options // timeout, connectTimeout
2223
body []byte // clickhouse response body
24+
stats []metrics.FinderStat
2325
}
2426

2527
func NewBase(url string, table string, opts clickhouse.Options) Finder {
2628
return &BaseFinder{
2729
url: url,
2830
table: table,
2931
opts: opts,
32+
stats: make([]metrics.FinderStat, 0),
3033
}
3134
}
3235

@@ -40,8 +43,12 @@ func (b *BaseFinder) where(query string) *where.Where {
4043
return w
4144
}
4245

43-
func (b *BaseFinder) Execute(ctx context.Context, config *config.Config, query string, from int64, until int64, stat *FinderStat) (err error) {
46+
func (b *BaseFinder) Execute(ctx context.Context, config *config.Config, query string, from int64, until int64) (err error) {
4447
w := b.where(query)
48+
49+
b.stats = append(b.stats, metrics.FinderStat{})
50+
stat := &b.stats[len(b.stats)-1]
51+
4552
b.body, stat.ChReadRows, stat.ChReadBytes, err = clickhouse.Query(
4653
scope.WithTable(ctx, b.table),
4754
b.url,
@@ -101,3 +108,7 @@ func (b *BaseFinder) Abs(v []byte) []byte {
101108
func (b *BaseFinder) Bytes() ([]byte, error) {
102109
return b.body, nil
103110
}
111+
112+
func (b *BaseFinder) Stats() []metrics.FinderStat {
113+
return b.stats
114+
}

finder/blacklist.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"regexp"
66

77
"github.com/lomik/graphite-clickhouse/config"
8+
"github.com/lomik/graphite-clickhouse/metrics"
89
)
910

1011
type BlacklistFinder struct {
@@ -20,15 +21,15 @@ func WrapBlacklist(f Finder, blacklist []*regexp.Regexp) *BlacklistFinder {
2021
}
2122
}
2223

23-
func (p *BlacklistFinder) Execute(ctx context.Context, config *config.Config, query string, from int64, until int64, stat *FinderStat) (err error) {
24+
func (p *BlacklistFinder) Execute(ctx context.Context, config *config.Config, query string, from int64, until int64) (err error) {
2425
for i := 0; i < len(p.blacklist); i++ {
2526
if p.blacklist[i].MatchString(query) {
2627
p.matched = true
2728
return
2829
}
2930
}
3031

31-
return p.wrapped.Execute(ctx, config, query, from, until, stat)
32+
return p.wrapped.Execute(ctx, config, query, from, until)
3233
}
3334

3435
func (p *BlacklistFinder) List() [][]byte {
@@ -55,3 +56,7 @@ func (p *BlacklistFinder) Abs(v []byte) []byte {
5556
func (p *BlacklistFinder) Bytes() ([]byte, error) {
5657
return nil, ErrNotImplemented
5758
}
59+
60+
func (p *BlacklistFinder) Stats() []metrics.FinderStat {
61+
return p.wrapped.Stats()
62+
}

finder/date.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/lomik/graphite-clickhouse/config"
99
"github.com/lomik/graphite-clickhouse/helper/clickhouse"
10+
"github.com/lomik/graphite-clickhouse/metrics"
1011
"github.com/lomik/graphite-clickhouse/pkg/scope"
1112
"github.com/lomik/graphite-clickhouse/pkg/where"
1213
)
@@ -30,7 +31,7 @@ func NewDateFinder(url string, table string, tableVersion int, opts clickhouse.O
3031
return &DateFinder{b, tableVersion}
3132
}
3233

33-
func (b *DateFinder) Execute(ctx context.Context, config *config.Config, query string, from int64, until int64, stat *FinderStat) (err error) {
34+
func (b *DateFinder) Execute(ctx context.Context, config *config.Config, query string, from int64, until int64) (err error) {
3435
w := b.where(query)
3536

3637
dateWhere := where.New()
@@ -40,6 +41,9 @@ func (b *DateFinder) Execute(ctx context.Context, config *config.Config, query s
4041
time.Unix(until, 0).Format("2006-01-02"),
4142
)
4243

44+
b.stats = append(b.stats, metrics.FinderStat{})
45+
stat := &b.stats[len(b.stats)-1]
46+
4347
if b.tableVersion == 2 {
4448
b.body, stat.ChReadRows, stat.ChReadBytes, err = clickhouse.Query(
4549
scope.WithTable(ctx, b.table),

finder/date_reverse.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/lomik/graphite-clickhouse/config"
88
"github.com/lomik/graphite-clickhouse/helper/clickhouse"
99
"github.com/lomik/graphite-clickhouse/helper/date"
10+
"github.com/lomik/graphite-clickhouse/metrics"
1011
"github.com/lomik/graphite-clickhouse/pkg/scope"
1112
"github.com/lomik/graphite-clickhouse/pkg/where"
1213
)
@@ -39,8 +40,12 @@ func (f *DateFinderV3) whereFilter(query string, from int64, until int64) (*wher
3940
return w, dateWhere
4041
}
4142

42-
func (f *DateFinderV3) Execute(ctx context.Context, config *config.Config, query string, from int64, until int64, stat *FinderStat) (err error) {
43+
func (f *DateFinderV3) Execute(ctx context.Context, config *config.Config, query string, from int64, until int64) (err error) {
4344
w, dateWhere := f.whereFilter(query, from, until)
45+
46+
f.stats = append(f.stats, metrics.FinderStat{})
47+
stat := &f.stats[len(f.stats)-1]
48+
4449
f.body, stat.ChReadRows, stat.ChReadBytes, err = clickhouse.Query(
4550
scope.WithTable(ctx, f.table),
4651
f.url,

finder/finder.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
"github.com/lomik/graphite-clickhouse/helper/clickhouse"
8+
"github.com/lomik/graphite-clickhouse/metrics"
89

910
"github.com/lomik/graphite-clickhouse/config"
1011
)
@@ -14,18 +15,11 @@ type Result interface {
1415
Series() [][]byte
1516
Abs([]byte) []byte
1617
Bytes() ([]byte, error)
18+
Stats() []metrics.FinderStat
1719
}
18-
19-
type FinderStat struct {
20-
ReadBytes int64
21-
ChReadRows int64
22-
ChReadBytes int64
23-
Table string
24-
}
25-
2620
type Finder interface {
2721
Result
28-
Execute(ctx context.Context, config *config.Config, query string, from int64, until int64, stat *FinderStat) error
22+
Execute(ctx context.Context, config *config.Config, query string, from int64, until int64) error
2923
}
3024

3125
func newPlainFinder(ctx context.Context, config *config.Config, query string, from int64, until int64, useCache bool) Finder {
@@ -108,15 +102,12 @@ func newPlainFinder(ctx context.Context, config *config.Config, query string, fr
108102
return f
109103
}
110104

111-
func Find(config *config.Config, ctx context.Context, query string, from int64, until int64, stat *FinderStat) (Result, error) {
105+
func Find(config *config.Config, ctx context.Context, query string, from int64, until int64) (Result, error) {
112106
fnd := newPlainFinder(ctx, config, query, from, until, config.Common.FindCache != nil)
113107

114-
err := fnd.Execute(ctx, config, query, from, until, stat)
115-
if err != nil {
116-
return nil, err
117-
}
108+
err := fnd.Execute(ctx, config, query, from, until)
118109

119-
return fnd.(Result), nil
110+
return fnd.(Result), err
120111
}
121112

122113
// Leaf strips last dot and detect IsLeaf
@@ -128,7 +119,7 @@ func Leaf(value []byte) ([]byte, bool) {
128119
return value, true
129120
}
130121

131-
func FindTagged(ctx context.Context, config *config.Config, terms []TaggedTerm, from int64, until int64, stat *FinderStat) (Result, error) {
122+
func FindTagged(ctx context.Context, config *config.Config, terms []TaggedTerm, from int64, until int64) (Result, error) {
132123
opts := clickhouse.Options{
133124
Timeout: config.ClickHouse.IndexTimeout,
134125
ConnectTimeout: config.ClickHouse.ConnectTimeout,
@@ -141,7 +132,7 @@ func FindTagged(ctx context.Context, config *config.Config, terms []TaggedTerm,
141132
if plain != nil {
142133
plain.wrappedPlain = newPlainFinder(ctx, config, plain.Target(), from, until, useCache)
143134

144-
err := plain.Execute(ctx, config, plain.Target(), from, until, stat)
135+
err := plain.Execute(ctx, config, plain.Target(), from, until)
145136
if err != nil {
146137
return nil, err
147138
}
@@ -161,7 +152,7 @@ func FindTagged(ctx context.Context, config *config.Config, terms []TaggedTerm,
161152
config.ClickHouse.TaggedCosts,
162153
)
163154

164-
err := fnd.ExecutePrepared(ctx, terms, from, until, stat)
155+
err := fnd.ExecutePrepared(ctx, terms, from, until)
165156
if err != nil {
166157
return nil, err
167158
}

finder/index.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/lomik/graphite-clickhouse/helper/clickhouse"
1212
"github.com/lomik/graphite-clickhouse/helper/date"
1313
"github.com/lomik/graphite-clickhouse/helper/errs"
14+
"github.com/lomik/graphite-clickhouse/metrics"
1415
"github.com/lomik/graphite-clickhouse/pkg/scope"
1516
"github.com/lomik/graphite-clickhouse/pkg/where"
1617
)
@@ -37,6 +38,7 @@ type IndexFinder struct {
3738
reverse uint8 // calculated in IndexFinder.useReverse only once
3839
body []byte // clickhouse response body
3940
rows [][]byte
41+
stats []metrics.FinderStat
4042
useCache bool // rotate body if needed (for store in cache)
4143
useDaily bool
4244
}
@@ -59,6 +61,7 @@ func NewIndex(url string, table string, dailyEnabled bool, reverse string, rever
5961
dailyEnabled: dailyEnabled,
6062
confReverse: config.IndexReverse[reverse],
6163
confReverses: reverses,
64+
stats: make([]metrics.FinderStat, 0),
6265
useCache: useCache,
6366
}
6467
}
@@ -193,14 +196,17 @@ func validatePlainQuery(query string, wildcardMinDistance int) error {
193196
return nil
194197
}
195198

196-
func (idx *IndexFinder) Execute(ctx context.Context, config *config.Config, query string, from int64, until int64, stat *FinderStat) (err error) {
199+
func (idx *IndexFinder) Execute(ctx context.Context, config *config.Config, query string, from int64, until int64) (err error) {
197200
err = validatePlainQuery(query, config.ClickHouse.WildcardMinDistance)
198201
if err != nil {
199202
return err
200203
}
201204

202205
w := idx.whereFilter(query, from, until)
203206

207+
idx.stats = append(idx.stats, metrics.FinderStat{})
208+
stat := &idx.stats[len(idx.stats)-1]
209+
204210
idx.body, stat.ChReadRows, stat.ChReadBytes, err = clickhouse.Query(
205211
scope.WithTable(ctx, idx.table),
206212
idx.url,
@@ -292,3 +298,7 @@ func (idx *IndexFinder) Series() [][]byte {
292298
func (idx *IndexFinder) Bytes() ([]byte, error) {
293299
return idx.body, nil
294300
}
301+
302+
func (idx *IndexFinder) Stats() []metrics.FinderStat {
303+
return idx.stats
304+
}

finder/mock.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/lomik/graphite-clickhouse/config"
9+
"github.com/lomik/graphite-clickhouse/metrics"
910
)
1011

1112
// MockFinder is used for testing purposes
@@ -29,7 +30,7 @@ func NewMockTagged(result [][]byte) *MockFinder {
2930
}
3031

3132
// Execute assigns given query to the query field
32-
func (m *MockFinder) Execute(ctx context.Context, config *config.Config, query string, from int64, until int64, stat *FinderStat) (err error) {
33+
func (m *MockFinder) Execute(ctx context.Context, config *config.Config, query string, from int64, until int64) (err error) {
3334
m.query = query
3435
return
3536
}
@@ -58,3 +59,7 @@ func (m *MockFinder) Strings() []string {
5859
body, _ := m.fnd.Bytes()
5960
return strings.Split(string(body), "\n")
6061
}
62+
63+
func (m *MockFinder) Stats() []metrics.FinderStat {
64+
return m.fnd.Stats()
65+
}

0 commit comments

Comments
 (0)