Skip to content

Commit

Permalink
refactor: Add lookback_delta query param for TSDB queries
Browse files Browse the repository at this point in the history
* Bump client_golang package to main branch to get `lookback_delta` client option available.

Signed-off-by: Mahendra Paipuri <mahendra.paipuri@gmail.com>
  • Loading branch information
mahendrapaipuri committed Feb 23, 2025
1 parent bf93625 commit 625d87a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/jellydator/ttlcache/v3 v3.3.0
github.com/mahendrapaipuri/perf-utils v0.0.0-20241102115757-6c72709e1c07
github.com/mattn/go-sqlite3 v1.14.24
github.com/prometheus/client_golang v1.20.5
github.com/prometheus/client_golang v1.21.1-0.20250221111557-6b820eb1ff36
github.com/prometheus/common v0.62.0
github.com/prometheus/exporter-toolkit v0.14.0
github.com/prometheus/procfs v0.15.1
Expand Down Expand Up @@ -53,7 +53,7 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mdlayher/socket v0.4.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ github.com/jsimonetti/rtnetlink/v2 v2.0.1 h1:xda7qaHDSVOsADNouv7ukSuicKZO7GgVUCX
github.com/jsimonetti/rtnetlink/v2 v2.0.1/go.mod h1:7MoNYNbb3UaDHtF8udiJo/RH6VsTKP1pqKLUTVCvToE=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down Expand Up @@ -107,8 +107,8 @@ github.com/planetscale/vtprotobuf v0.6.0/go.mod h1:t/avpk3KcrXxUnYOhZhMXJlSEyie6
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_golang v1.21.1-0.20250221111557-6b820eb1ff36 h1:8OQwkTW4kWEnFN6+BJZygwTPnndwmFF8kEy8iKyidxQ=
github.com/prometheus/client_golang v1.21.1-0.20250221111557-6b820eb1ff36/go.mod h1:4Rw4L/1MuIFyBl0yahCFX3GZEkM0RAyx3QT00KJoC9g=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io=
Expand Down
40 changes: 20 additions & 20 deletions pkg/tsdb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,17 @@ func (t *Client) Labels(ctx context.Context, matchers []string, start time.Time,

// Query makes a Client query.
func (t *Client) Query(ctx context.Context, query string, queryTime time.Time) (Metric, error) {
// We need to recommend to do it for whole Prometheus instance
//
// // Get current scrape interval to use as lookback_delta
// // This query parameter is undocumented on Prometheus. If we use
// // default value of 5m, we tend to have metrics 5m **after** compute
// // unit has finished which gives over estimation of energy
// if scrapeInterval := t.Settings(ctx).ScrapeInterval; scrapeInterval > 0 {
// values.Add("lookback_delta", scrapeInterval.String())
// }
// Get current scrape interval to use as lookback_delta
// This query parameter is undocumented on Prometheus. If we use
// default value of 5m, we tend to have metrics 5m **after** compute
// unit has finished which gives over estimation of energy
period := 10 * time.Second
if scrapeInterval := t.Settings(ctx).ScrapeInterval; scrapeInterval > 0 {
period = scrapeInterval
}

// Make API request to execute query
result, warnings, err := t.API.Query(ctx, query, queryTime)
result, warnings, err := t.API.Query(ctx, query, queryTime, v1.WithLookbackDelta(period))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -341,15 +341,15 @@ func (t *Client) RangeQuery(
endTime time.Time,
step time.Duration,
) (RangeMetric, error) {
// We need to recommend to do it for whole Prometheus instance
//
// // Get current scrape interval to use as lookback_delta
// // This query parameter is undocumented on Prometheus. If we use
// // default value of 5m, we tend to have metrics 5m **after** compute
// // unit has finished which gives over estimation of energy
// if scrapeInterval := t.Settings(ctx).ScrapeInterval; scrapeInterval > 0 {
// values.Add("lookback_delta", scrapeInterval.String())
// }
// Get current scrape interval to use as lookback_delta
// This query parameter is undocumented on Prometheus. If we use
// default value of 5m, we tend to have metrics 5m **after** compute
// unit has finished which gives over estimation of energy
period := 10 * time.Second
if scrapeInterval := t.Settings(ctx).ScrapeInterval; scrapeInterval > 0 {
period = scrapeInterval
}

// Make query range
queryRange := v1.Range{
Start: startTime,
Expand All @@ -361,7 +361,7 @@ func (t *Client) RangeQuery(
}

// Make API request to execute query
result, warnings, err := t.API.QueryRange(ctx, query, queryRange)
result, warnings, err := t.API.QueryRange(ctx, query, queryRange, v1.WithLookbackDelta(period))
if err != nil {
return nil, err
}
Expand Down
16 changes: 13 additions & 3 deletions pkg/tsdb/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (
)

var (
expectedSeries Response[[]model.LabelSet]
expectedLabels Response[[]string]
expectedSeries Response[[]model.LabelSet]
expectedLabels Response[[]string]
expectedQueryLookback model.Duration
expectedQueryRangeLookback model.Duration
)

func testTSDBServer(emptyResponse bool) *httptest.Server {
Expand Down Expand Up @@ -164,10 +166,16 @@ func testTSDBServer(emptyResponse bool) *httptest.Server {
w.Write([]byte("KO"))
}
} else if strings.HasSuffix(r.URL.Path, "query") {
_ = r.ParseForm()
expectedQueryLookback, _ = model.ParseDuration(r.Form["lookback_delta"][0])

if err := json.NewEncoder(w).Encode(&expectedQuery); err != nil {
w.Write([]byte("KO"))
}
} else if strings.HasSuffix(r.URL.Path, "query_range") {
_ = r.ParseForm()
expectedQueryRangeLookback, _ = model.ParseDuration(r.Form["lookback_delta"][0])

if err := json.NewEncoder(w).Encode(&expectedQueryRange); err != nil {
w.Write([]byte("KO"))
}
Expand Down Expand Up @@ -344,9 +352,10 @@ func TestTSDBQuerySuccess(t *testing.T) {
require.NoError(t, err)
assert.True(t, tsdb.Available())

m, err := tsdb.Query(context.Background(), "", time.Now())
m, err := tsdb.Query(context.Background(), "foo", time.Now())
require.NoError(t, err)
assert.Equal(t, Metric{"1": 1.1, "2": 2.2}, m)
assert.Equal(t, 15*time.Second, time.Duration(expectedQueryLookback))
}

func TestTSDBQueryFail(t *testing.T) {
Expand Down Expand Up @@ -384,6 +393,7 @@ func TestTSDBQueryRangeSuccess(t *testing.T) {
},
m,
)
assert.Equal(t, 15*time.Second, time.Duration(expectedQueryRangeLookback))
}

func TestTSDBQueryRangeFail(t *testing.T) {
Expand Down

0 comments on commit 625d87a

Please sign in to comment.