3

I'm aware of the $msec logging parameter which gives the request time as seconds since Unix epoch with millisecond granularity. However, the format of this includes a period '.' e.g. '1407233265.472' and this is a problem for the import function that I'm using to parse the log files.

Is there a way (log parameter or plugin) that will allow the request time to be recorded in total milliseconds? e.g. '1407233265472'. Am happy to compile nginx as required.

1
  • 4
    Before using a nonstandard (as in not coming from the distro repos) web server, I would consider to send the logs through sed before importing them to your tool.
    – Sven
    Aug 6, 2014 at 10:00

2 Answers 2

5

There are 2 ways to achieve this. The first one is dirty and I wont recommend it - it's the fastest way though:

if ($msec ~ "(.*)\.(.*)") {
    set $epoch_millis = "$1$2"
}

This is much cleaner but requires lua:

set_by_lua_block $epoch_millis { return string.gsub(ngx.var.msec, "%.", "") }

then just log $epoch_millis

2
  • 1
    What will happen when the value is 1407233265.4? The resulting value will be 14072332654. So it is a wrong answer.
    – sanigo
    Sep 26, 2018 at 0:48
  • 2
    nginx pads the decimal spot with zeroes, so won't emit anything more/less than 3 decimal places
    – xref
    Oct 24, 2019 at 6:23
2

Extending Sirk's answer you can also use a map to convert from the 10.3 millisecond format to the 13 digit millisecond epoch format which has no decimal.

map $msec $msec_no_decimal { ~(.*)\.(.*) $1$2; }

Specifically I needed to put this map inside the http { ... } block of nginx.conf because DataDog doesn't automatically parse the 10.3 format, but does parse the 13 digit format.

(to address sanigo's concern; nginx pads zeroes on the decimal, so this will always convert to 13 digits.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .