-
Notifications
You must be signed in to change notification settings - Fork 12.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
$__timeFilter() no longer respects the local time zone #13769
Comments
OK, turns out that using
instead of For the record, the macros Here is how my panel looks like; the screenshot was taken at 13:53 local time. Notice how the last bar of data is from 10:00, instead of from 13:00: For completeness, here is the Grafana query I use for this panel: which translates to the following MySQL query after the resolution of the macros:
Any ideas how to fix this? Or some kind of workaround? I can't use |
Maybe the whole problem occurs because MySQL does not understand the 'Z' date format specification as specifying UTC time? See this. For instance,
results in the same times being displayed:
which is pretty meaningless... |
Grafana SQL data sources only support date column data stored UTC. It was by accident that
They look totally different (the ones you printed). Not sure what you mean. |
@torkelo, you have the horribly annoying habit of closing issues when they are still not resolved! This is simply rude! I am tempted to start re-creating the issue when you close it until it is resolved properly. Please, stop doing this! As it is now, the macros I did find a work-around that is fast enough, because it doesn't require applying a function to
but it is ridiculous what I have to resort to such hacks just because the documented macros do not work properly! Please fix them! Regarding the time - it's a typo; a leftover from a previous redaction. Yes, they are totally different, instead of being the same. This suggests that MySQL simply does not understand the UTC timestamp notation. |
Sorry I must have misunderstood the issue or you have misunderstood something. The viewer does not need to be in UTC but the date stored needs to be stored in UTC. Is your data stored in UTC ? |
It suggest the opposite, if they where same then you would be correct. As they are different it seems to work correctly (being translated as UTC) the reason they are different suggest you are running your MySQL server in a non UTC time environment/ setting. |
Yup, this seems to be the issue for us as well. Our servers are in Los Angeles and their timezones are configured as such (PDT, GMT-7). Our dates are stored as timestamps, so timezone isn't relevant. The format Grafana is sending the date to MySQL (UTC with the 'Z' designation) doesn't seem to be understood by MySQL. These timestamps should, in theory, be the same:
|
If you are storing timestamps in local time then your schema does not handle timezones properly and your data becomes ambiguous if your server is in a timezone with daylight saving time. |
In our case we store data as timestamps. Timestamps contain no timezone info. The server timezone affects how the timestamp is formatted when displayed in a human-readable format, but it does not affect the underlying storage. The server is in Los Angeles and uses the local timezone (PDT, UTC-7):
Note that the underlying epoch timestamp is constant. The only change is in the formatting of the human-readable date. The problem is that when Grafana sends a date to MySQL (e.g. '2018-10-20T10:53:25Z'), MySQL doesn't respect that 'Z' UTC designation. The date is being parsed in the server's configured timezone. One solution that might be easier than trying to fiddle with MySQL timezone parsing would be to issue a |
I got the same problem.so I diff the code before v5.3.0 such as v5.2.4 mysql "__timeFilter" format like this: grafana/pkg/tsdb/mysql/macros.go Line 80 in 0bbac5c
Then at master: grafana/pkg/tsdb/mysql/macros.go Line 63 in 6932306
so if we query with params(UTC+8 Shanghai,China):
before format result:
but the new result:
let's query from mysql :
|
I have replace all 'GetFromAsTimeUTC' with
and 'GetToAsTimeUTC' with
in tsdb.time_range.go. It works fine for me. |
@ArmedChef what do you get in your testcases if you use |
@marefr yes, it is respecting the configured timezone (just a reminder mine is PDT/UTC-7). I think that solution should work.
|
Thanks @ArmedChef looking promising! |
@marefr Thanks for looking into this. Just a follow up since I forgot about indexing. This looks like it will work with indexing on timestamp columns.
|
Thanks for doing that test , we will change this and release a patch next week. Sorry for all the trouble this caused. |
Sorry, folks, I was away for a while and couldn't follow the discussion. Let's see if I could contribute anything useful.
|
@bontchev welcome back. This was fixed 5 days ago using 3) and will be released in upcoming v5.3.3 release. |
@bontchev I tested on v5.3.4, seems still not fixed correctly. ###What datasource are you using? What OS are you running grafana on? What did you do? select from_unixtime(1542211200); -> 2018-11-14 16:00:00 My order_created_at field is timestamp type and stored in mysql with UTC time internally. The problem I think is grafana add extra UTC time conversion from ui date picker to query string. |
@yangliuyu what Grafana version did you use before where this worked for you? select from_unixtime(1542211200); -> 2018-11-14 16:00:00 This tells me that your timezone in mysql session is utc. |
@yangliuyu, my Grafana just updated to version v5.3.4 (69630b9) - missed 5.3.3, for some reason - and, as far as I can see, the issue is resolved there. MySQL always stores |
@marefr You are right, I rechecked and it works now, I tested using a docker mysql 5.7 with utc timezone yesterday, and my timezone in production environment of mysql is UTC+8. |
doesnt work for PostgreSQL either. or has anyone a Workaround? for some tables i cant Change the data and the timestamps are simply not in UTC |
I've fixed the Situation by adding '::TIMESTAMP WITH TIME ZONE AT TIME ZONE 'UTC'' to the SQL Statement e.g.
|
@devsschmidt what postgres version? What data type have |
I'm glad I read the release notes—I must be chopped liver.... #13694. I wish I would have read about that Macro update Git dev version thing back in October though. I came up with a MySQL Event Scheduler and Grafana Variable workaround where each DB has it's own timezone time value records and timezone is of no importance as it adjusts for DST already too. The Grafana SQL I used was https://i.imgur.com/ktEsgRD.png It sounds like the old |
same problem for psql |
$__to() and __from() variables return the time in UTC instead of local time zone of the browser. I am using Grafana 6.0.2 . I need time in local timezone. Adding a GMT difference solves the issue, but the viewers can be in different timezones. Can someone suggest a fix? Additional Information: Grafana Dashboard is set to use "Local Browser Time". |
@shavetasidana that's intentional since the format is epoch milliseconds and epoch is always UTC. |
ok. For that I have used the sql Query, " select from_unixtime($__from/1000,"%Y-%m-%d") as "Start Date", from_unixtime($__to/1000,"%Y-%m-%d") as "End Date" from DUAL" The date should be displayed as it is selected from the time range Panel, but actually I have to add GMT difference. Otherwise, it displays previous date as start date. Preference setting of Timezone to "Local Browser Time" or "UTC" does not make any difference. Can you give an idea how to solve this issue? How do I know local Time Zone (for adding GMT difference to the time returned by __from variable?). Does grafana provide timezone information in someway? |
@marefr, could you help me please on my above query? Thanks in advance!! |
@shavetasidana |
Thanks for your answer marefr, I will check how to do the timeshift. |
For what it's worth - an easy workaround would be to issue set time_zone='+00:00'; as the first line of the query. Although this works perfectly when done natively, trying to do this via Grafana results in Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT
UNIX_TIMESTAMP(timest) DIV 86400 * 86400 as time,
concat(' ',max(date' at line 3 I've tested this works natively by taking the generated SQL (with set line at the front) and run the code in HeidiSQL. I get the expected output, with timestamps in UTC. I assume Grafana is trying to do something helpful with the set line and screwing up the query when it runs it - because it can't be running the code that the Generated SQL shows it to be running as I've proved that code works fine. A much more durable solution would be for Grafana to explicitly set its MySQL session to "+00:00" when connecting. AIUI this would entirely resolve this issue across the board, without any special handling by the user. This was already suggested in #15321 I suggest we need to either allow set time_zone='+00:00'; to work properly, or implement this natively on connect by default (which would be much better)? It's already been suggested in #18120 to make the connect timezone configurable but default to "+00:00" which makes perfect sense to me as it handles default use case and allows special use cases too. |
I think the MSSQL connection plugin have the same issue. MSSQL does not unerstands the Z time notation by simply ignoring the Z at the end of the time string. |
bonjour Hello J'ai un problème: SELECT count(Def_Balance) WHERE Jusque-là ça fonctionne Je souhaite afficher le résultat selon ma sélection date et heure dans le dashboard I want to display the result according to my date and time selection in the dashboard |
This is SUCH a mess. We use MSSQL, and we store LOCAL time in a datetime column, which does NOT store timezone information. Graphana now dictates that we change what we store to be compatible with graphana's insistence that all dates be stored in UCT? |
@errolt I don't think Grafana insists they're stored in UTC just that it will only deal with them in UTC because it expects epoch ms and epoch is UTC. If that's correct then could you write and use a function to convert the datetime to UTC, or directly to epoch ms in UTC. So instead of UNIX_TIMESTAMP(timest) (mysql notation) you'd use MY_UNIX_TIMESTAMP(timest) or whatever you named it. I'm not familiar with MSSQL, but I think this would work in MySQL so probably in MSSQL too. However if you're storing in local with a DST variance then you're going to have a lot of "fun" trying to deal with that. Look on the bright side - my largest data source stores events with datetimes without timezone, in whatever time zone they were generated in (anywhere in the world) and in a separate table holds the UTC offset that applies the the source right now (but not what applied when the data was written, and thus the offset of each event datetime). DST causes a whole bunch of fun with this, for many weeks each year the UTC datetimes cannot be correctly calculated or even assumed. |
It seems the Time Series visualization In my case:
When the Grafana dash on a web browser needs to access data in a different time zone than EST/EDT, I do a few things so it displays in the right time zone on the EST/EDT computer or a CST/CDT computer either/both. I will give you an example of data that is collected on the CST time zone that allows me to see if on a CST or EST computer from its web browser. In Grafana I…
Query A
Query B
Query Note: With the old Graph, I used to NOT have to Other notesI’ve yet to set any Grafana dashes up in this configuration with a time zone other than CST or EST at the moment, so I suspect the interval number in the Grafana SQL queries may need to be adjusted differently here based on time zone difference ( All you have to do to test is adjust the Grafana MySQL query to changes those variables mentioned above, and then change the time zones on your PC and refresh the Grafana dash to see how it looks. I like looking at my data in the server time zone I am on from a web browser, and also on the time zone the timestamp data points are on locally to confirm people in that time zone looking at it sees it right. I’m supposed to be working with some data from another country here soon from Europe too so I’ll be digging in to understand this better myself soon. Also, I use MySQL in my environment but those two equivalents of adding or subtracting an hour from a DATETIME column in MS SQL should apply in your case. This should be a trivial thing to figure out and test for what you will plug into the Grafana Macros. Also in my case, I get the data from the devices that collect with their local time zone time stamp like the example below semicolon separated, but before I put that second value Sample Data (before I transform and put into MySQL tables)
|
I do have similar problems with Grafanas time management. I live in CEST (= UTC+2), timezone for Berlin/Germany. We use Grafana 11.1.4 Windows, local installation (no cloud). Default and browser time are CEST. In my Grafana query, I use: I visualize the data from "today so far" (e.g., now, it is 18:50 CEST). Interestingly, when using the CEST time in Grafana time picker, the above clause is translated to: as shown in the query inspector. But when using UTC in Grafana, the same clause is translated to: SO A SHORTENING OF 2 HOURS (I would expect a shift, but not a shortening?). Something is odd here. Today so far logically means almost 19 hours. The above query gives me that, but shifted, the below query gives me only 17 hours. Therefore, I conclude, the template variables / shortcuts are buggy. While |
What Grafana version are you using?
Grafana v5.3.1 (08c7908)
What datasource are you using?
MySQL
What OS are you running grafana on?
Ubuntu Linux 16.04
What did you do?
I didn't really do anything. I just noticed that one of the panels on my dashboard is sort of broken - it is missing the last 2 hours of data. It was working perfectly in the previous version of Grafana (5.2.x).
The panel is a simple graph, displaying an hourly bar histogram of the data collected during the corresponding hour. So, why were the last two hours missing? I hadn't changed anything on the panel settings or in the MySQL query that feeds it the data.
Examining the generated MySQL query showed that the macro
$__timeFilter(timestamp)
had expanded toHere
timestamp
is a column in a table of the MySQL database with the formatdatetime
. The above query was generated at 22:19 local time. My time zone is UTC+2.Do you see what is happening? The "from" and "to" times passed to the query are in UTC time and not in local time - despite the dashboard being configured to use local time. As a result, the query fetches data for the wrong time period.
Now, I did find a workaround for the problem. Instead of using the simple
$__timeFilter(timestamp)
macro, I have to useThis works with UTC times only and the data is fetched as it should be. It would have been more intuitive to use just
$__unixEpochFilter(UNIX_TIMESTAMP(timestamp))
but, due to the way macros are implemented, you can't pass them the result of a function instead of a simple column name. (I had opened an issue on the subject, but you closed it and didn't fix that problem, although the fix is rather trivial - I managed to figure it out despite that I don't know Go.)Would it be possible to restore the old behavior of the
$__timeFilter()
macro? In fact, the problem probably isn't in the macro itself, but in the format of the times passed to it - they should be in local time, if the dashboard is configured to use the local time of the browser.The text was updated successfully, but these errors were encountered: