[datasource plugin] options.range.from mutation not resetting after every query() invocation since grafana version 11.3.0

Hi!

I’m working with a Grafana datasource plugin that queries data from an external API, and I’m facing an issue for Grafana versions >= 11.3.0

Following scenario:
To optimise the cache hit rate of the queries the from date of the query is rounded down in the query() method before sending the query, depending on a selected interval. To then also display the first returned data point in the timeseries graph, which would fall outside the time interval specified in the options.range, I also round down options.range.from using options.range.from.startOf(momentTimeUnit). This adjustment modifies the graph’s start date accordingly, aligning it with the rounded-down from value making the first data point visible in the graph.
With version < 11.3.0 the options.range.from.startOf(momentTimeUnit) adjustment didn’t persist between query() calls. So the next query(options) invocation would again contain the option.range values corresponding to the date selected in the Grafana date picker.
However and that’s the issue, with Grafana versions >= 11.3.0 the options.range.from.startOf(momentTimeUnit) adjustment now persists between different query() invocations. Values get only reset to the values selected in the Grafana date picker when the datepicker is changed or the refresh button clicked.

Two questions here:

  1. I didn’t find any documentation on this change so I am not sure if this was an intentional behaviour change or would classify as a bug - does anybody have any insights there? The only maybe related bug report I found was this Issue: Dashboard variable, timepicker, and refresh settings not preserved correctly after navigating away and back in versions 11.3.x and 11.4.0 · Issue #97699 · grafana/grafana · GitHub

  2. The method I’m using—options.range.from.startOf(momentTimeUnit)—feels a bit like a hack. Is there a more official or recommended way to adapt the start and end points of a timeseries graph in Grafana?

Thanks!

Hi there.

it seems there might be some regression on the immutability of the options, you are mutating the objects in your code here by calling startOf which mutates the original object. they only get recreated when something changes so you are effectively affecting the hole dashboard by doing that

Can you test using the latest minor of Grafana 11.5?

I’ll pass along the issue to the responsible team but for now instead of using mutating APIs. copy the momentjs objects before mutating them (and use the copies) or use non-mutating APIs.

Hi, thanks for the quick response!
I tested it with 11.5.3 but same problem.
I am also copying the momentjs object now before mutating it as you suggested, but the problem still persists.

you are still modifying the original object

you call the method in a copy but overwrite the original later.

Is there a way to change the optional.range object in a non mutating way? I am doing this to adapt the graph to show all the points, not doing this would result in the graph not showing the first datapoint as it is out of range.

you are effectively changing the time the user selected like this.

What you should do is construct your query in a way that includes the times you want from this query method instead of modifying the range objects.