graphite-web icon indicating copy to clipboard operation
graphite-web copied to clipboard

Summarize function is doing alignment without timezone ?

Open toni-moreno opened this issue 11 years ago • 14 comments

Hi @guys.

I'm trying to get summarize from 00:00 ot first day of this month a metric, I'm working on spain and I've configured my timezone in /opt/graphite/webapp/graphite/local_settings.py

# needs to be explicitly set to your local timezone.
TIME_ZONE = 'Europe/Madrid'

I did this query.

http://graphite/render?format=json&from=4/01/2015&until=-&target=summarize(my.system.cpu.percent-active,'1day','avg')

And I got that

[{"target": "summarize(my.system.cpu.percent-active, "1day", "avg")", "datapoints": [[49.497208009728958, 1427846400], [37.077833077777775, 1427932800], [42.021989086805604, 1428019200], [49.25920844236115, 1428105600], [63.997593792361165, 1428192000], [81.546292237499998, 1428278400], [73.984663305555543, 1428364800], [73.646580338888811, 1428451200], [48.208400477083359, 1428537600], [38.049111687500016, 1428624000], [32.406861394444419, 1428710400], [32.73222370000007, 1428796800], [34.046655264583308, 1428883200], [34.440842520138879, 1428969600], [60.00367210694445, 1429056000], [87.657549924305584, 1429142400], [75.007352623225813, 1429228800]]}]

If I do tranlation for this timestamps to my current location summarize function seems to align to Standard Time +0000 UTC , and not my local Timezone as I would want

image

image

toni-moreno avatar Apr 17 '15 13:04 toni-moreno

Can you run in a django shell (DJANGO_SETTINGS_MODULE=graphite.settings django-admin.py shell)

from django.utils import timezone
print(timezone.now())

from django.conf import settings
print(settings.TIME_ZONE)

brutasse avatar Apr 17 '15 13:04 brutasse

Hi @brutasse.

It seems like no effect for the settings.TIME_ZONE.

# DJANGO_SETTINGS_MODULE=graphite.settings django-admin.py shell                                                                                                                                 Python 2.6.6 (r266:84292, May 27 2013, 05:35:12)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.utils import timezone
>>> print(timezone.now())
2015-04-20 06:48:17.605693+00:00
>>> from django.conf import settings
>>> print(settings.TIME_ZONE)
Europe/Madrid

date from "date command, is ok ! but not timezone.now() from graphite.

# date
Mon Apr 20 08:48:47 CEST 2015

toni-moreno avatar Apr 20 '15 06:04 toni-moreno

I'm working with 0.10 (commit 8f13d44d446acfaecefb438f1f4b8f56e5164092) .

I've tested forcing "tz" in the query but I got the same result

http://graphite/render?format=json&from=4/01/2015&until=-&target=summarize(my.system.cpu.percent-active,'1day','avg')&tz='Europe/Madrid'

[{"target": "summarize(my.system.cpu.percent-active, "1day", "avg")", "datapoints": [[12.180165230020865, 1427846400], [9.8991865472222234, 1427932800], [8.4890914388888916, 1428019200], [8.545845312499992, 1428105600], [8.5893371986111049, 1428192000], [11.584696202777787, 1428278400], [14.573881442361127, 1428364800], [11.976672966666678, 1428451200], [11.967281797222219, 1428537600], [18.55253356875, 1428624000], [7.5416060506944405, 1428710400], [7.184546814583328, 1428796800], [10.414414825694418, 1428883200], [11.440549245138893, 1428969600], [10.391664684027786, 1429056000], [6.3419650625000017, 1429142400], [9.4203433758765573, 1429228800], [6.7883954076388857, 1429315200], [7.427704547916667, 1429401600], [9.0221278432989589, 1429488000]]}]

toni-moreno avatar Apr 20 '15 08:04 toni-moreno

@toni-moreno do you have pytz installed? It's really strange that timezone.now() gives you a datetime in UTC.

brutasse avatar Apr 20 '15 08:04 brutasse

Yes .. I have pytz

# pip freeze |grep pytz
pytz==2014.10

toni-moreno avatar Apr 20 '15 09:04 toni-moreno

Any workaround ? perhaps force LANG environment in http.conf with SetEnv ??

toni-moreno avatar Apr 20 '15 10:04 toni-moreno

Ok nevermind, timezone.now() always returns a date in UTC. The problem is somewhere else:

https://github.com/graphite-project/graphite-web/blob/master/webapp/graphite/render/functions.py#L3001-L3002

With these lines start points are always multiples of "interval", without taking UTC offset into account.

brutasse avatar Apr 20 '15 13:04 brutasse

Um.. in these two lines the from/until values are already in seconds as timestamp , isn't it.

So , the : [ local date ] to [ timestamp ] conversion seems to be done here.

https://github.com/graphite-project/graphite-web/blob/master/webapp/graphite/render/views.py#L300-L308

In my timezone I expect to have from value for "4/01/2015 00:00" = 1427839200 (http://www.epochconverter.com/)

image

but I got 1427846400 ( +120 minutes after)

toni-moreno avatar Apr 20 '15 14:04 toni-moreno

Hi @brutasse

I've tested the parseATTime() method and it is not returning a timestamp.

>>> from graphite.render.attime import parseATTime
>>> import pytz
>>> tzinfo = pytz.timezone(settings.TIME_ZONE)
>>> fromTime = parseATTime('4/01/2015', tzinfo)
>>> print(fromTime)
2015-04-01 00:00:00.554059+02:00

It seems to return a Date with correct TimeZone (+02:00) set.

The timestamp conversion mistake should be place after that... I will continue investigating...

Thank you !

toni-moreno avatar Apr 21 '15 05:04 toni-moreno

I've added two log lines at

https://github.com/graphite-project/graphite-web/blob/master/webapp/graphite/render/functions.py#L2984

to get more detailed info on time conversions.

    timestamps = range( int(series.start), int(series.end), int(series.step) )
    datapoints = zip(timestamps, series)
    log.info("REQUEST_CONTEXT TIME: (%s , %s)" % (requestContext['startTime'],requestContext['endTime']) )
    log.info("SERIES TIMESTAMPS: results for: %s (%s ,%s, %s)" % (series.name,series.start,series.end,series.step))

1) Query with MM/DD/YYYY.

For this query:

http://graphite/render/?format=json&from=04/1/2015&target=summarize(my.system.cpu.percent-user,%271day%27,%27avg%27)

we got this logs:

Tue Apr 21 10:39:37 2015 :: REQUEST_CONTEXT TIME: (2015-04-01 00:00:00.849499+02:00 , 2015-04-21 10:39:37.849448+02:00)
Tue Apr 21 10:39:37 2015 :: SERIES TIMESTAMPS: results for: dwily.pro.c1.s1.wastest.system.cpu.percent-user (1427839260 ,1429605600, 60)

And this output:

"datapoints": [[null, 1427760000]....

timestamp conversion is not an exact value (00:00:00.849499).. when converted to timestamp it got 1427839260 instead of 1427839260, but result is not correct alignement

2) Query with timestamp

when this query is done:

http://graphite/render/?format=json&from=1427839200&target=summarize(my.system.cpu.percent-user,'1day','avg')

We got this log:

Tue Apr 21 10:46:33 2015 :: REQUEST_CONTEXT TIME: (2015-04-01 00:00:00+02:00 , 2015-04-21 10:46:33.341698+02:00)
Tue Apr 21 10:46:33 2015 :: SERIES TIMESTAMPS: results for: dwily.pro.c1.s1.wastest.system.cpu.percent-user (1427839260 ,1429606020, 60)

And this output:

"datapoints": [[null, 1427760000]...

In this case also we got 1427839260 instead of 1427839200 , but final alignment is not done well.

3) Query with timestamp. (- 1 second and AlignedToFrom=true)

when this query is done:

http://graphite/render/?format=json&from=1427839199&target=summarize(my.system.cpu.percent-user,'1day','avg')

we got this log

Tue Apr 21 10:51:48 2015 :: REQUEST_CONTEXT TIME: (2015-03-31 23:59:59+02:00 , 2015-04-21 10:51:48.839702+02:00)
Tue Apr 21 10:51:48 2015 :: SERIES TIMESTAMPS: results for: dwily.pro.c1.s1.wastest.system.cpu.percent-user (1427839200 ,1429606320, 60)

And this output:

"datapoints": [[null, 1427839200]...

Now it seems to be working fine..

toni-moreno avatar Apr 21 '15 09:04 toni-moreno

Right -- see the source I linked in my previous comment. Without aligntofrom, start time is "rounded" to your interval length. But graphite only deals with epoch timestamps at this time in rendering, which is why you went back to UTC without alignToFrom.

brutasse avatar Apr 21 '15 09:04 brutasse

I'm having the same issue, but in my case I don't want to use alignToFrom (cause I want to specify a relative time, like from=-10d, instead of having to specify absolute time), yet I'd like to have Summarize to allign the date correctly to 00:00 and honor the time zone. I currently get +3 hours shift (as my timezone UTC+3) and summarize is aligned to 03:00.

Any workaround ?

zerthimon avatar Jul 27 '15 14:07 zerthimon

I'm in the same situation as @zerthimon. I'd like to summarize by day from 00:00 to 00:00 without using alignToFrom.

Could this issue be re-opened ?

tbillet avatar Oct 20 '15 10:10 tbillet

According to documentation "The alignToFrom boolean parameter has been replaced by alignTo and no longer has any effect. Alignment can be to years, months, weeks, days, hours, and minutes." So, smartSummarize(seriesList, intervalString, func='sum', alignTo='day') should work fine with timezones.

deniszh avatar Dec 24 '20 14:12 deniszh