@@ -21,32 +21,42 @@ func New(metrics []Metric) *Client {
21
21
}
22
22
}
23
23
24
- func (c * Client ) Run (ctx context.Context , wg * sync.WaitGroup ) {
24
+ func (c * Client ) Run (ctx context.Context , outerWG * sync.WaitGroup ) {
25
25
for _ , metric := range c .metrics {
26
- go func (metric Metric , wg * sync.WaitGroup ) {
27
- defer wg .Done ()
26
+ go func (metric Metric , outerWG * sync.WaitGroup ) {
27
+ defer outerWG .Done ()
28
+
29
+ var wg sync.WaitGroup
28
30
29
31
for {
30
32
select {
31
33
case <- ctx .Done ():
34
+ wg .Wait ()
35
+
32
36
return
33
37
case <- time .After (metric .Interval ):
34
- err := c .ZeroMe (ctx , metric )
35
- if err != nil {
36
- slog .ErrorContext (ctx , "Failed to zero metric" , "metric" , metric .Name , "error" , err )
37
- }
38
+ wg .Add (1 )
39
+
40
+ go func () {
41
+ defer wg .Done ()
42
+
43
+ err := c .ZeroMe (ctx , time .Now (), metric )
44
+ if err != nil {
45
+ slog .ErrorContext (ctx , "Failed to zero metric" , "metric" , metric .Name , "error" , err )
46
+ }
47
+ }()
38
48
}
39
49
}
40
- }(metric , wg )
50
+ }(metric , outerWG )
41
51
}
42
52
}
43
53
44
- func (c * Client ) ZeroMe (ctx context.Context , metric Metric ) error {
54
+ func (c * Client ) ZeroMe (ctx context.Context , nowTime time. Time , metric Metric ) error {
45
55
// Query twice the interval to ensure that the metric has a missing data point in the past.
46
56
queryInterval := metric .Interval * 2 //nolint:gomnd,mnd
47
57
48
58
// Add query interval as a delay to cover exporter scrape failures.
49
- ts := time . Now () .Add (- queryInterval )
59
+ ts := nowTime .Add (- queryInterval )
50
60
51
61
vector , err := metric .querier .Query (ctx , ts , metric .Name , queryInterval , metric .UpLabels )
52
62
if err != nil {
@@ -67,9 +77,11 @@ func (c *Client) ZeroMe(ctx context.Context, metric Metric) error {
67
77
return err
68
78
}
69
79
70
- slog .InfoContext (ctx , "Zeroed metric" , "metric" , metric .Name , "vector" , vector )
80
+ for _ , s := range vector {
81
+ slog .InfoContext (ctx , "Zeroed sample" , "metric" , metric .Name , "sample" , s .String ())
82
+ }
71
83
72
- return nil
84
+ return c . ZeroMe ( ctx , nowTime , metric ) // Retry until nothing to zero
73
85
}
74
86
75
87
func (c * Client ) zeroTimeSeries (metric Metric , vector model.Vector ) []prompb.TimeSeries {
0 commit comments