-
Notifications
You must be signed in to change notification settings - Fork 125
/
time_truncate.go
49 lines (40 loc) · 1.81 KB
/
time_truncate.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
package promclient
import (
"context"
"time"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
)
func NewTimeTruncate(a API) *TimeTruncate {
return &TimeTruncate{a}
}
const truncateDuration = time.Millisecond
// TimeTruncate is a workaround to https://github.com/jacksontj/promxy/issues/212
// context: https://github.com/prometheus/prometheus/issues/5972
// For now we need to truncate the time so that prometheus doesn't round up and return no data <= the timestamp
// we requested
type TimeTruncate struct {
API
}
// Query performs a query for the given time.
func (t *TimeTruncate) Query(ctx context.Context, query string, ts time.Time) (model.Value, api.Warnings, error) {
return t.API.Query(ctx, query, ts.Truncate(truncateDuration))
}
// QueryRange performs a query for the given range.
func (t *TimeTruncate) QueryRange(ctx context.Context, query string, r v1.Range) (model.Value, api.Warnings, error) {
return t.API.QueryRange(ctx, query, v1.Range{
Start: r.Start.Truncate(truncateDuration),
End: r.End.Truncate(truncateDuration),
Step: r.Step,
})
}
// Series finds series by label matchers.
func (t *TimeTruncate) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, api.Warnings, error) {
return t.API.Series(ctx, matches, startTime.Truncate(truncateDuration), endTime.Truncate(truncateDuration))
}
// GetValue loads the raw data for a given set of matchers in the time range
func (t *TimeTruncate) GetValue(ctx context.Context, start, end time.Time, matchers []*labels.Matcher) (model.Value, api.Warnings, error) {
return t.API.GetValue(ctx, start.Truncate(truncateDuration), end.Truncate(truncateDuration), matchers)
}