I’m doing Grafana Loki HTTP API queries, specifically query_range
ones which produce stream responses. The parameters listed for such queries are:
query
limit
start
end
since
direction
I’ve got 5 of the 6 working:
const url = new URL('http://localhost:3100/loki/api/v1/query_range');
/* query */
url.searchParams.set('query', '{job="bob_job", branch="master"}');
/* limit */
url.searchParams.set('limit', '500');
/* start */
let start = new Date();
start.setHours(start.getHours() - 4);
url.searchParams.set('start', start.toISOString());
/* end */
let end = new Date();
end.setHours(end.getHours() - 2);
url.searchParams.set('end', end.toISOString());
/* since */
// url.searchParams.set('since', '5m');
/* direction */
url.searchParams.set('direction', 'forward');
console.log(url.toString());
fetch(url.toString(), {
method: 'POST',
})
.then((response) => response.json())
.then((obj) => {
console.log(obj);
})
.catch(console.error);
But since
doesn’t work. (Even if I comment out start
and end
.) Moreover, I’m hard-pressed to find examples of other people’s queries using the since
parameter. What’s up with it?
What sort of error do you get?
Here is an example with query_range and since I jut ran against our cluster:
curl -G -u '<USERNAME>:<PASSWORD>' "https://loki-load-balaner.local/loki/api/v1/query_range" --data-urlencode 'query=count_over_time({SELECTOR} [1m])' --data-urlencode 'since=1h'
There’s no error. It just has no effect. I add since
and nothing changes. The logs I get with it are identical to those I get without it, from the default 1-hour time window.
Why are there two pieces? Both since
, and []
after query?
the []
is the aggregation range, it’s there because i used a metric query for example, not related to since
.
I noticed you are setting limit to 500
, that could be your issue. Try changing that to be a bigger number than what you might get from an hour or two and see if that changes anything.
What’s an aggregation range? How do I use it? What does it do? How does it differ from since
? Why isn’t it mentioned on Grafana Loki HTTP API | Grafana Loki documentation?
-
Aggregation range is used for some functions in metrics query. It is mentioned in the documentation (LogQL: Log query language | Grafana Loki documentation), read through the sections log queries
and metrics queries
.
-
since
is the range to run the query with, aggregation range is the block of time to aggregate metrics with (hence it’s only used with metrics queries, and it is part of
the query). Apologies on confusing you with my previous post, but they are two different things.
Take this query as an example:
count_over_time({SELECTOR} [1m])
What i am essentially doing here is count number of logs over time. But over time doesn’t mean anything if there isn’t a reference, so in this case it’s saying to count number of logs over time, and group the metrics on a 1-minute block. If you were to use since=1h
here, you’d get 60 data points, each one a minute long. Hopefully that makes sense.
- Did you check your
limit
setting?