# Add $unixtime to http variables

**URL:** <https://www.ruby-forum.com/t/add-unixtime-to-http-variables/195264>\
**Category:** NGINX\
**Created:** [September 20, 2010, 7:37pm UTC](https://www.ruby-forum.com/t/add-unixtime-to-http-variables/195264 "2010-09-20T19:37:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roberto\_De\_Ioris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/50afbb/32.png) [@Roberto\_De\_Ioris](https://www.ruby-forum.com/u/Roberto_De_Ioris)\
**Post date:** [September 20, 2010, 7:37pm UTC](https://www.ruby-forum.com/t/add-unixtime-to-http-variables/195264/1 "2010-09-20T19:37:49Z")

</div>

This is a patch to add $unixtime to the http variables.

It simply contains the output of time(NULL)

It could be useful to measure the time between the send  
of a request to an upstream module and the time the application  
server process it.

Example:

uwsgi\_param UWSGI\_TIME $unixtime;

Then in your app:

if int(time.time()) - int(env[‘UWSGI\_TIME’]):  
print “\*\*\* SYSTEM IS RESPONDING SLOWLY \*\*\*”

I do not know if it is better to name it $time instead of $unixtime…

— src/http/ngx\_http\_variables.c 2010-06-23 17:31:33.000000000 +0200  
+++ …/nginx-ok/src/http/ngx\_http\_variables.c 2010-09-20  
19:24:57.000000000 +0200  
@@ -93,6 +93,9 @@  
static ngx\_int\_t ngx\_http\_variable\_pid(ngx\_http\_request\_t \*r,  
ngx\_http\_variable\_value\_t \*v, uintptr\_t data);

+static ngx\_int\_t ngx\_http\_variable\_unixtime(ngx\_http\_request\_t \*r,

- ngx\_http\_variable\_value\_t \*v, uintptr\_t data);
- 

/\*

- TODO:
- 

```
Apache CGI: AUTH_TYPE, PATH_INFO (null), PATH_TRANSLATED

```

@@ -254,6 +257,9 @@  
{ ngx\_string(“pid”), NULL, ngx\_http\_variable\_pid,  
0, 0, 0 },

- { ngx\_string(“unixtime”), NULL, ngx\_http\_variable\_unixtime,
- 

```
 0, 0, 0 },

```

- { ngx\_null\_string, NULL, NULL, 0, 0, 0 }  
};

@@ -1640,6 +1646,26 @@

static ngx\_int\_t  
+ngx\_http\_variable\_unixtime(ngx\_http\_request\_t \*r,

- ngx\_http\_variable\_value\_t \*v, uintptr\_t data)  
+{
- u\_char \*p;
- 
- p = ngx\_pnalloc(r-\>pool, NGX\_INT64\_LEN);
- if (p == NULL) {
- 

```
   return NGX_ERROR;

```

- }
- 
- v-\>len = ngx\_sprintf(p, “%T”, time(NULL)) - p;
- v-\>valid = 1;
- v-\>no\_cacheable = 0;
- v-\>not\_found = 0;
- v-\>data = p;
- 
- return NGX\_OK;  
+}
- 

+static ngx\_int\_t  
ngx\_http\_variable\_pid(ngx\_http\_request\_t \*r,  
ngx\_http\_variable\_value\_t \*v, uintptr\_t data)  
{

–  
Roberto De Ioris  
[http://unbit.it](http://unbit.it)

---

<div class="post-metadata">

**Author:** ![Roberto\_De\_Ioris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/50afbb/32.png) [@Roberto\_De\_Ioris](https://www.ruby-forum.com/u/Roberto_De_Ioris)\
**Post date:** [September 20, 2010, 8:48pm UTC](https://www.ruby-forum.com/t/add-unixtime-to-http-variables/195264/2 "2010-09-20T20:48:06Z")

</div>

> uwsgi\_param UWSGI\_TIME $unixtime;
> 
> Then in your app:
> 
> if int(time.time()) - int(env[‘UWSGI\_TIME’]):  
> print “\*\*\* SYSTEM IS RESPONDING SLOWLY \*\*\*”
> 
> I do not know if it is better to name it $time instead of $unixtime…

Sorry, i think for this purpose it is betetr to use  
r-\>start\_sec instead of time(NULL).

What do you think about it ?

–  
Roberto De Ioris

> **[unbit - Overview](https://github.com/unbit)**
>
> makers of uWSGI and many more open source projects - unbit

---

<div class="post-metadata">

**Author:** ![Roberto\_De\_Ioris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/50afbb/32.png) [@Roberto\_De\_Ioris](https://www.ruby-forum.com/u/Roberto_De_Ioris)\
**Post date:** [September 20, 2010, 9:24pm UTC](https://www.ruby-forum.com/t/add-unixtime-to-http-variables/195264/3 "2010-09-20T21:24:23Z")

</div>

On Mon, Sep 20, 2010 at 10:43 PM, Roberto De Ioris [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Sorry, i think for this purpose it is betetr to use  
> r-\>start\_sec instead of time(NULL).
> 
> What do you think about it ?  
> When I made the same patch nearly 2 years ago, I used  
> ngx\_cached\_time-\>sec.

–  
Boris D..

---

<div class="post-metadata">

**Author:** ![Roberto\_De\_Ioris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/50afbb/32.png) [@Roberto\_De\_Ioris](https://www.ruby-forum.com/u/Roberto_De_Ioris)\
**Post date:** [September 21, 2010, 9:37am UTC](https://www.ruby-forum.com/t/add-unixtime-to-http-variables/195264/4 "2010-09-21T09:37:14Z")

</div>

On Tue, Sep 21, 2010 at 09:00, Roberto De Ioris [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > 
> 
> I would like to have (with the most possible accuracy) the time  
> when the request starts. (this will allow to reliably calculate  
> overload problems)

ngx\_cached\_time try to update every main loop.

> ngx\_cached\_time-\>sec can be different (oh yes we are talking about  
> milliseconds, but why not maintaining accuracy 🙂 )

but as unixtime you mean unix timestamp?

---

<div class="post-metadata">

**Author:** ![Roberto\_De\_Ioris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/50afbb/32.png) [@Roberto\_De\_Ioris](https://www.ruby-forum.com/u/Roberto_De_Ioris)\
**Post date:** [September 21, 2010, 7:05am UTC](https://www.ruby-forum.com/t/add-unixtime-to-http-variables/195264/5 "2010-09-21T07:05:03Z")

</div>

> 

I would like to have (with the most possible accuracy) the time  
when the request starts. (this will allow to reliably calculate  
overload problems)

ngx\_cached\_time-\>sec can be different (oh yes we are talking about  
milliseconds, but why not maintaining accuracy 🙂 )

This is the relevant nginx code:

tp = ngx\_timeofday();  
r-\>start\_sec = tp-\>sec;  
r-\>start\_msec = tp-\>msec;

#define ngx\_timeofday() (ngx\_time\_t \*) ngx\_cached\_time

–  
Roberto De Ioris

> **[unbit - Overview](https://github.com/unbit)**
>
> makers of uWSGI and many more open source projects - unbit

---

<div class="post-metadata">

**Author:** ![Roberto\_De\_Ioris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/50afbb/32.png) [@Roberto\_De\_Ioris](https://www.ruby-forum.com/u/Roberto_De_Ioris)\
**Post date:** [September 21, 2010, 1:26pm UTC](https://www.ruby-forum.com/t/add-unixtime-to-http-variables/195264/6 "2010-09-21T13:26:42Z")

</div>

Hello!

On Mon, Sep 20, 2010 at 07:32:44PM +0200, Roberto De Ioris wrote:

> uwsgi\_param UWSGI\_TIME $unixtime;
> 
> Then in your app:
> 
> if int(time.time()) - int(env[‘UWSGI\_TIME’]):  
> print “\*\*\* SYSTEM IS RESPONDING SLOWLY \*\*\*”

This won’t work as you expect since request to upstream is created  
once and used when sending request to multiple upstream servers  
(due to proxy\_next\_upstream/fastcgi\_next\_upstream/…). Logging  
$upstream\_response\_time would be better idea.

> I do not know if it is better to name it $time instead of $unixtime…

If you just need current system time as a variable - I would  
recommend moving $msec from log module to generic variables  
instead. Or, probably, duplicating it - as log variable will be  
more efficient during logging than generic one.

> - ngx\_http\_variable\_value\_t \*v, uintptr\_t data);
> - 
> - u\_char \*p;
> - 
> - p = ngx\_pnalloc(r-\>pool, NGX\_INT64\_LEN);

NGX\_TIME\_T\_LEN should be here.

> - if (p == NULL) {
> - 
> 
> ```
> return NGX_ERROR;
> 
> ```
> 
> - }
> - 
> - v-\>len = ngx\_sprintf(p, “%T”, time(NULL)) - p;

As you were already said - don’t use time(). Use ngx\_getimeofday()  
instead.

> - v-\>valid = 1;
> - v-\>no\_cacheable = 0;

If you want to print current time - you may want to mark this  
variable as non-cacheable (here and in definition via  
NGX\_HTTP\_VAR\_NOCACHEABLE flag).

> - v-\>not\_found = 0;
> - v-\>data = p;
> - 
> - return NGX\_OK;  
> +}
> - 
> 
> +static ngx\_int\_t  
> ngx\_http\_variable\_pid(ngx\_http\_request\_t \*r,  
> ngx\_http\_variable\_value\_t \*v, uintptr\_t data)  
> {

Maxim D.

---

<div class="post-metadata">

**Author:** ![Roberto\_De\_Ioris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/50afbb/32.png) [@Roberto\_De\_Ioris](https://www.ruby-forum.com/u/Roberto_De_Ioris)\
**Post date:** [September 22, 2010, 7:57am UTC](https://www.ruby-forum.com/t/add-unixtime-to-http-variables/195264/7 "2010-09-22T07:57:53Z")

</div>

> This won’t work as you expect since request to upstream is created  
> once and used when sending request to multiple upstream servers  
> (due to proxy\_next\_upstream/fastcgi\_next\_upstream/…). Logging  
> $upstream\_response\_time would be better idea.

Thanks Maxim, this works perfectly for logging slow-accept() (caused by  
full  
backlog queue or insufficient processes) as the counter starts before  
nginx connection to upstream.

## Bye

Roberto De Ioris

> **[unbit - Overview](https://github.com/unbit)**
>
> makers of uWSGI and many more open source projects - unbit
