This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
schemasbackfill.go
99 lines (90 loc) · 3.68 KB
/
schemasbackfill.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
// Copyright © 2018 Grafana Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"strings"
"sync"
"time"
"github.com/grafana/metrictank/cmd/mt-fakemetrics/policy"
"github.com/grafana/metrictank/conf"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var mpr int
var ignore string
var schemasFile = "/etc/metrictank/storage-schemas.conf"
func init() {
rootCmd.AddCommand(schemasbackfillCmd)
schemasbackfillCmd.Flags().IntVar(&mpr, "mpr", 10, "how many metrics so simulate per rule")
schemasbackfillCmd.Flags().StringVar(&metricBuilder, "metricbuilder", "simple", "the metric builder to use. (simple|tagged)")
schemasbackfillCmd.Flags().StringVar(&schemasFile, "schemas-file", "/etc/metrictank/storage-schemas.conf", "path to storage-schemas.conf file")
schemasbackfillCmd.Flags().StringVar(&ignore, "ignore", "default", "comma separated list of section names to exclude")
schemasbackfillCmd.Flags().DurationVar(&offset, "offset", 0, "offset duration expression. (how far back in time to start. e.g. 1month, 6h, etc). must be a multiple of 1s")
schemasbackfillCmd.Flags().IntVar(&speedup, "speedup", 1, "for each advancement of real time, how many advancements of fake data to simulate")
schemasbackfillCmd.Flags().DurationVar(&flushDur, "flush", time.Second, "how often to flush metrics")
schemasbackfillCmd.Flags().DurationVar(&periodDur, "period", time.Second, "period between metric points (must be a multiple of 1s)")
schemasbackfillCmd.Flags().StringVar(&valuePolicy, "value-policy", "", "a value policy (i.e. \"single:1\" \"multiple:1,2,3,4,5\" \"timestamp\" \"daily-sine:<peak>,<offset>,<stdev>\")")
}
// schemasbackfillCmd represents the schemasbackfill command
var schemasbackfillCmd = &cobra.Command{
Use: "schemasbackfill",
Short: "backfills a sends a set of metrics for each encountered storage-schemas.conf rule. Note: patterns must be a static string + wildcard at the end (e.g. foo.bar.*)!",
Run: func(cmd *cobra.Command, args []string) {
schemas, err := conf.ReadSchemas(schemasFile)
if err != nil {
log.Fatalf("can't read schemas file %q: %s", schemasFile, err.Error())
}
schemasList, _ := schemas.ListRaw()
wg := &sync.WaitGroup{}
initStats(true, "schemasbackfill")
period = int(periodDur.Seconds())
flush = int(flushDur.Nanoseconds() / 1000 / 1000)
out := getOutput()
ignoreList := strings.Split(ignore, ",")
wg.Add(len(schemasList))
for _, schema := range schemasList {
if in(schema.Name, ignoreList) {
continue
}
name := schema.Pattern.String()
if strings.HasSuffix(name, ".*") {
name = name[:len(name)-2]
}
if strings.HasSuffix(name, "^") {
name = name[1:]
}
// probably the default catchall was matched
if name == "" {
name = "default"
}
go func(name string, period int) {
vp, err := policy.ParseValuePolicy(valuePolicy)
if err != nil {
panic(err)
}
dataFeed(out, 1, mpr, period, flush, int(offset.Seconds()), speedup, true, getBuilder(metricBuilder, name), vp)
wg.Done()
}(name, schema.Retentions.Rets[0].SecondsPerPoint)
}
wg.Wait()
},
}
func in(s string, values []string) bool {
for _, val := range values {
if s == val {
return true
}
}
return false
}