Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 773b420

Browse files
authored
Merge pull request #817 from grafana/mt-index-cat-tags-valid-filter
mt-index-cat tags valid filter
2 parents 29bd120 + 0a21297 commit 773b420

File tree

3 files changed

+59
-20
lines changed

3 files changed

+59
-20
lines changed

cmd/mt-index-cat/main.go

+40-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func main() {
2626
var addr string
2727
var prefix string
2828
var substr string
29+
var tags string
2930
var from string
3031
var maxAge string
3132
var verbose bool
@@ -35,6 +36,7 @@ func main() {
3536
globalFlags.StringVar(&addr, "addr", "http://localhost:6060", "graphite/metrictank address")
3637
globalFlags.StringVar(&prefix, "prefix", "", "only show metrics that have this prefix")
3738
globalFlags.StringVar(&substr, "substr", "", "only show metrics that have this substring")
39+
globalFlags.StringVar(&tags, "tags", "", "tag filter. empty (default), 'some', 'none', 'valid', or 'invalid'")
3840
globalFlags.StringVar(&from, "from", "30min", "for vegeta outputs, will generate requests for data starting from now minus... eg '30min', '5h', '14d', etc. or a unix timestamp")
3941
globalFlags.StringVar(&maxAge, "max-age", "6h30min", "max age (last update diff with now) of metricdefs. use 0 to disable")
4042
globalFlags.IntVar(&limit, "limit", 0, "only show this many metrics. use 0 to disable")
@@ -55,6 +57,13 @@ func main() {
5557
fmt.Printf("global config flags:\n\n")
5658
globalFlags.PrintDefaults()
5759
fmt.Println()
60+
fmt.Println("tags filter:")
61+
fmt.Println(" '' no filtering based on tags")
62+
fmt.Println(" 'none' only show metrics that have no tags")
63+
fmt.Println(" 'some' only show metrics that have one or more tags")
64+
fmt.Println(" 'valid' only show metrics whose tags (if any) are valid")
65+
fmt.Println(" 'invalid' only show metrics that have one or more invalid tags")
66+
fmt.Println()
5867
fmt.Printf("idxtype: only 'cass' supported for now\n\n")
5968
fmt.Printf("cass config flags:\n\n")
6069
cassFlags.PrintDefaults()
@@ -111,6 +120,12 @@ func main() {
111120
os.Exit(1)
112121
}
113122

123+
if tags != "" && tags != "valid" && tags != "invalid" && tags != "some" && tags != "none" {
124+
log.Println("invalid tags filter")
125+
flag.Usage()
126+
os.Exit(1)
127+
}
128+
114129
globalFlags.Parse(os.Args[1:cassI])
115130
cassFlags.Parse(os.Args[cassI+1 : len(os.Args)-1])
116131
cassandra.Enabled = true
@@ -155,15 +170,33 @@ func main() {
155170
shown := 0
156171

157172
for _, d := range defs {
158-
if prefix == "" || strings.HasPrefix(d.Metric, prefix) {
159-
if substr == "" || strings.Contains(d.Metric, substr) {
160-
show(d)
161-
shown += 1
162-
if shown == limit {
163-
break
164-
}
173+
// note that prefix and substr can be "", meaning filter disabled.
174+
// the conditions handle this fine as well.
175+
if !strings.HasPrefix(d.Metric, prefix) {
176+
continue
177+
}
178+
if !strings.Contains(d.Metric, substr) {
179+
continue
180+
}
181+
if tags == "none" && len(d.Tags) != 0 {
182+
continue
183+
}
184+
if tags == "some" && len(d.Tags) == 0 {
185+
continue
186+
}
187+
if tags == "valid" || tags == "invalid" {
188+
valid := schema.ValidateTags(d.Tags)
189+
190+
// skip the metric if the validation result is not what we want
191+
if valid != (tags == "valid") {
192+
continue
165193
}
166194
}
195+
show(d)
196+
shown += 1
197+
if shown == limit {
198+
break
199+
}
167200
}
168201

169202
if verbose {

docs/tools.md

+9
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,18 @@ global config flags:
7070
only show metrics that have this prefix
7171
-substr string
7272
only show metrics that have this substring
73+
-tags string
74+
tag filter. empty (default), 'some', 'none', 'valid', or 'invalid'
7375
-verbose
7476
print stats to stderr
7577
78+
tags filter:
79+
'' no filtering based on tags
80+
'none' only show metrics that have no tags
81+
'some' only show metrics that have one or more tags
82+
'valid' only show metrics whose tags (if any) are valid
83+
'invalid' only show metrics that have one or more invalid tags
84+
7685
idxtype: only 'cass' supported for now
7786
7887
cass config flags:

vendor/gopkg.in/raintank/schema.v1/metric.go

+10-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)