diff --git a/go.mod b/go.mod index d7430c7..4b190f7 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 550ad56..6734290 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/pkg/tsdb/client.go b/pkg/tsdb/client.go index 3509923..12c7ae6 100644 --- a/pkg/tsdb/client.go +++ b/pkg/tsdb/client.go @@ -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 } @@ -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, @@ -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 } diff --git a/pkg/tsdb/client_test.go b/pkg/tsdb/client_test.go index f96a1af..9213910 100644 --- a/pkg/tsdb/client_test.go +++ b/pkg/tsdb/client_test.go @@ -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 { @@ -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")) } @@ -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) { @@ -384,6 +393,7 @@ func TestTSDBQueryRangeSuccess(t *testing.T) { }, m, ) + assert.Equal(t, 15*time.Second, time.Duration(expectedQueryRangeLookback)) } func TestTSDBQueryRangeFail(t *testing.T) {