From 63424aff9b530e330a118c8bbf1dbac8a69eb74b Mon Sep 17 00:00:00 2001 From: Yury Moladau Date: Tue, 18 Jun 2024 16:09:01 +0200 Subject: [PATCH] fix: change time range to start and end query args #22 --- CHANGELOG.md | 2 ++ pkg/plugin/datasource.go | 1 + pkg/plugin/query.go | 10 ++++++++++ src/datasource.ts | 5 ----- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc4189c..aa8646e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## tip +* BUGFIX: change time range limitation from `_time` in the expression to `start` and `end` query args. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/22). + ## v0.2.1 * BUGFIX: change the `metrics` flag from `false` to `true` in `plugin.json` to ensure the plugin appears in the Grafana datasource selection list. diff --git a/pkg/plugin/datasource.go b/pkg/plugin/datasource.go index ee9b72f..0ff4e48 100644 --- a/pkg/plugin/datasource.go +++ b/pkg/plugin/datasource.go @@ -88,6 +88,7 @@ func (d *Datasource) query(ctx context.Context, _ backend.PluginContext, query b settings.HTTPMethod = http.MethodPost } + q.TimeRange = TimeRange(query.TimeRange) reqURL, err := q.getQueryURL(d.settings.URL, settings.QueryParams) if err != nil { err = fmt.Errorf("failed to create request URL: %w", err) diff --git a/pkg/plugin/query.go b/pkg/plugin/query.go index 085aeed..20b8b2f 100644 --- a/pkg/plugin/query.go +++ b/pkg/plugin/query.go @@ -5,6 +5,7 @@ import ( "net/url" "path" "strconv" + "time" ) const ( @@ -18,9 +19,16 @@ type Query struct { Expr string `json:"expr"` LegendFormat string `json:"legendFormat"` MaxLines int `json:"maxLines"` + TimeRange TimeRange url *url.URL } +// TimeRange represents time range backend object +type TimeRange struct { + From time.Time + To time.Time +} + // GetQueryURL calculates step and clear expression from template variables, // and after builds query url depends on query type func (q *Query) getQueryURL(rawURL string, queryParams string) (string, error) { @@ -58,6 +66,8 @@ func (q *Query) queryInstantURL(queryParams url.Values) string { values.Set("query", q.Expr) values.Set("limit", strconv.Itoa(q.MaxLines)) + values.Set("start", strconv.FormatInt(q.TimeRange.From.Unix(), 10)) + values.Set("end", strconv.FormatInt(q.TimeRange.To.Unix(), 10)) q.url.RawQuery = values.Encode() return q.url.String() diff --git a/src/datasource.ts b/src/datasource.ts index f3bd1e4..af2d485 100644 --- a/src/datasource.ts +++ b/src/datasource.ts @@ -37,11 +37,6 @@ export class VictoriaLogsDatasource query(request: DataQueryRequest): Observable { const queries = request.targets.filter(q => q.expr).map((q) => { - // include time range in query if not already present - if (!/_time/.test(q.expr)) { - const timerange = `_time:[${request.range.from.toISOString()}, ${request.range.to.toISOString()}]` - q.expr = `${timerange} AND (${q.expr})`; - } return { ...q, maxLines: q.maxLines ?? this.maxLines } });