@@ -15,9 +15,10 @@ import (
15
15
)
16
16
17
17
const (
18
- fieldPrometheusURL = "url"
19
- fieldPrometheusQueries = "queries"
20
- fieldPrometheusSchedule = "schedule"
18
+ fieldPrometheusURL = "url"
19
+ fieldPrometheusQueries = "queries"
20
+ fieldPrometheusSchedule = "schedule"
21
+ fieldPrometheusQueryOffset = "query_offset"
21
22
)
22
23
23
24
func prometheusInputConfig () * service.ConfigSpec {
@@ -42,11 +43,15 @@ func prometheusInputConfig() *service.ConfigSpec {
42
43
Description ("The cron expression to use for the scrape job." ).
43
44
Examples ("0 * * * * *" , "@every 1m" ).
44
45
Default ("0 * * * * *" ),
45
- ).Example ("Basic Configuration" , "Collect Prometheus metrics with a scrape interval of 1 minute." , `
46
+ service .NewDurationField (fieldPrometheusQueryOffset ).
47
+ Description ("Indicates how far back in time the scraping should be done to account for delays in metric availability." ).
48
+ Default ("0s" ),
49
+ ).Example ("Basic Configuration" , "Collect Prometheus metrics with a scrape interval of 1 minute and a scrape offset of 30 seconds to account for delays in metric availability." , `
46
50
input:
47
51
prometheus:
48
52
url: "${PROMETHEUS_URL:http://localhost:9090}"
49
53
schedule: "0 * * * * *"
54
+ query_offset: "1m"
50
55
queries:
51
56
- query:
52
57
name: "node_cpu_usage"
@@ -78,14 +83,15 @@ type QueryResult struct {
78
83
var _ service.BatchInput = (* prometheusInput )(nil )
79
84
80
85
type prometheusInput struct {
81
- logger * service.Logger
82
- client v1.API
83
- queries []PromQuery
84
- interval time.Duration
85
- schedule string
86
- scheduler gocron.Scheduler
87
- store map [time.Time ][]QueryResult
88
- mu sync.Mutex
86
+ logger * service.Logger
87
+ client v1.API
88
+ queries []PromQuery
89
+ interval time.Duration
90
+ schedule string
91
+ queryOffset time.Duration
92
+ scheduler gocron.Scheduler
93
+ store map [time.Time ][]QueryResult
94
+ mu sync.Mutex
89
95
}
90
96
91
97
func newPrometheusInput (conf * service.ParsedConfig , logger * service.Logger ) (* prometheusInput , error ) {
@@ -99,6 +105,11 @@ func newPrometheusInput(conf *service.ParsedConfig, logger *service.Logger) (*pr
99
105
return nil , err
100
106
}
101
107
108
+ queryOffset , err := conf .FieldDuration (fieldPrometheusQueryOffset )
109
+ if err != nil {
110
+ return nil , err
111
+ }
112
+
102
113
// Parse queries
103
114
queriesConf , err := conf .FieldObjectList (fieldPrometheusQueries )
104
115
if err != nil {
@@ -160,14 +171,15 @@ func newPrometheusInput(conf *service.ParsedConfig, logger *service.Logger) (*pr
160
171
}
161
172
162
173
return & prometheusInput {
163
- logger : logger ,
164
- client : v1 .NewAPI (client ),
165
- queries : queries ,
166
- interval : interval ,
167
- schedule : schedule ,
168
- scheduler : scheduler ,
169
- store : make (map [time.Time ][]QueryResult ),
170
- mu : sync.Mutex {},
174
+ logger : logger ,
175
+ client : v1 .NewAPI (client ),
176
+ queries : queries ,
177
+ interval : interval ,
178
+ schedule : schedule ,
179
+ queryOffset : queryOffset ,
180
+ scheduler : scheduler ,
181
+ store : make (map [time.Time ][]QueryResult ),
182
+ mu : sync.Mutex {},
171
183
}, nil
172
184
}
173
185
@@ -176,15 +188,21 @@ func (in *prometheusInput) scrape(ctx context.Context, t time.Time) error {
176
188
// Convert time to UTC
177
189
t = t .UTC ()
178
190
179
- in .logger .Debugf ("executing PromQL queries at %s" , t .Format (time .RFC3339 ))
191
+ // Apply the metrics scrape offset
192
+ queryTime := t .Add (- in .queryOffset )
193
+
194
+ in .logger .Debugf ("executing PromQL queries at %s (using query time %s with offset %s)" ,
195
+ t .Format (time .RFC3339 ),
196
+ queryTime .Format (time .RFC3339 ),
197
+ in .queryOffset )
180
198
181
199
results := make ([]QueryResult , 0 , len (in .queries ))
182
200
183
201
for _ , query := range in .queries {
184
202
in .logger .Tracef ("executing query: %s" , query .PromQL )
185
203
186
- // Execute the PromQL query
187
- result , warnings , err := in .client .Query (ctx , query .PromQL , t )
204
+ // Execute the PromQL query with the offset applied time
205
+ result , warnings , err := in .client .Query (ctx , query .PromQL , queryTime )
188
206
if err != nil {
189
207
in .logger .Errorf ("error executing query %s: %v" , query .PromQL , err )
190
208
return err
@@ -206,7 +224,7 @@ func (in *prometheusInput) scrape(ctx context.Context, t time.Time) error {
206
224
results = append (results , QueryResult {
207
225
Name : query .Name ,
208
226
Query : query .PromQL ,
209
- Timestamp : t ,
227
+ Timestamp : queryTime ,
210
228
Values : vector ,
211
229
})
212
230
}
@@ -269,6 +287,7 @@ func (in *prometheusInput) ReadBatch(ctx context.Context) (service.MessageBatch,
269
287
msg := service .NewMessage (encoded )
270
288
msg .MetaSet ("scrape_time" , t .Format (time .RFC3339 ))
271
289
msg .MetaSet ("scrape_interval" , in .interval .String ())
290
+ msg .MetaSet ("query_offset" , in .queryOffset .String ())
272
291
msg .MetaSet ("query_name" , result .Name )
273
292
batch = append (batch , msg )
274
293
}
0 commit comments