Skip to content

Commit

Permalink
get usec time
Browse files Browse the repository at this point in the history
  • Loading branch information
jianhao.dai committed May 1, 2018
1 parent ddb4d68 commit c2b2427
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 5 deletions.
90 changes: 87 additions & 3 deletions lib/resty/influxdb/point.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
-- Copyright (C) by Jianhao Dai (Toruneko)


local setmetatable = setmetatable
local shared = ngx.shared
local str_format = string.format
local concat = table.concat
local tonumber = tonumber
local ngx_time = ngx.time
local pairs = pairs
local error = error
local type = type

local ffi = require "ffi"
local ffi_new = ffi.new
local ffi_null = ffi.null
local C = ffi.C
ffi.cdef [[
struct timeval {
long int tv_sec;
long int tv_usec;
};

int gettimeofday(struct timeval *tv, void *tz);
]]

local _M = { _VERSION = '0.01' }
local mt = { __index = _M }
Expand All @@ -14,6 +30,63 @@ if not ok then
new_tab = function(narr, nrec) return {} end
end

-- escape value " ", ",", "=" => "\\ ", "\\,", "\\="
local function escape(value)
return value
end

local function concatenated_tags(tags)
local tab = new_tab(20, 0)

for key, value in pairs(tags) do
tab[#tab + 1] = ","
tab[#tab + 1] = escape(key)
tab[#tab + 1] = "="
tab[#tab + 1] = escape(value)
end
tab[#tab + 1] = " "

return concat(tab, "")
end

--[[
NumberFormat numberFormat = NumberFormat.getInstance(Locale.ENGLISH);
numberFormat.setMaximumFractionDigits(340);
numberFormat.setGroupingUsed(false);
numberFormat.setMinimumFractionDigits(1);
]]
local function concatenate_metrics(metrics)
local tab = new_tab(0, 100)

for key, value in pairs(metrics) do
tab[#tab + 1] = escape(key)
tab[#tab + 1] = "="
if type(value) == "string" then
tab[#tab + 1] = "\""
tab[#tab + 1] = escape(value)
tab[#tab + 1] = "\""
elseif type(value) == "number" then
tab[#tab + 1] = tonumber(value)
else
tab[#tab + 1] = value
end
tab[#tab + 1] = ","
end
tab[#tab] = " "

return concat(tab, "")
end

local function formated_time()
if ngx.usec_time then
return (tonumber(ngx_time()) + (tonumber(ngx.usec_time()) / 1000 / 1000)) * 1000 * 1000 * 1000
else
local tm = ffi_new("struct timeval")
C.gettimeofday(tm, ffi_null)
return (tonumber(tm.tv_sec) + (tonumber(tm.tv_usec) / 1000 / 1000)) * 1000 * 1000 * 1000
end
end

function _M.new(name)
return setmetatable({
name = name,
Expand All @@ -32,7 +105,18 @@ function _M.add(self, name, metrics)
self.metrics[name] = metrics
end

function _M.lineProtocol()
function _M.lineProtocol(self)
local tab = new_tab(4, 0)
-- measurement
tab[#tab + 1] = escape(self.name)
-- tags
tab[#tab + 1] = concatenated_tags(self.tags)
-- metrics
tab[#tab + 1] = concatenate_metrics(self.metrics)
-- time
tab[#tab + 1] = formated_time()

return concat(tab, "")
end

return _M
2 changes: 0 additions & 2 deletions lib/resty/influxdb/reporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ local point = require "resty.influxdb.point"
local http = require "resty.http"

local setmetatable = setmetatable
local escape_uri = ngx.escape_uri
local concat = table.concat
local pairs = pairs

local DEFAULT_RETENTION_POLICY = "default"
Expand Down
41 changes: 41 additions & 0 deletions patches/lua-nginx-module-v0.10.10-add-usec-times.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
diff --git a/src/ngx_http_lua_time.c b/src/ngx_http_lua_time.c
index 3272a75..d6ff7e4 100644
--- a/src/ngx_http_lua_time.c
+++ b/src/ngx_http_lua_time.c
@@ -17,6 +17,7 @@

static int ngx_http_lua_ngx_today(lua_State *L);
static int ngx_http_lua_ngx_time(lua_State *L);
+static int ngx_http_lua_ngx_usec_time(lua_State *L);
static int ngx_http_lua_ngx_now(lua_State *L);
static int ngx_http_lua_ngx_localtime(lua_State *L);
static int ngx_http_lua_ngx_utctime(lua_State *L);
@@ -75,6 +76,19 @@ ngx_http_lua_ngx_time(lua_State *L)


static int
+ngx_http_lua_ngx_usec_time(lua_State *L)
+{
+ ngx_time_t *tp;
+
+ tp = ngx_timeofday();
+
+ lua_pushnumber(L, (lua_Number) tp->usec);
+
+ return 1;
+}
+
+
+static int
ngx_http_lua_ngx_now(lua_State *L)
{
ngx_time_t *tp;
@@ -217,6 +231,9 @@ ngx_http_lua_inject_time_api(lua_State *L)
lua_pushcfunction(L, ngx_http_lua_ngx_time);
lua_setfield(L, -2, "time");

+ lua_pushcfunction(L, ngx_http_lua_ngx_usec_time);
+ lua_setfield(L, -2, "usec_time");
+
lua_pushcfunction(L, ngx_http_lua_ngx_now);
lua_setfield(L, -2, "now");
46 changes: 46 additions & 0 deletions patches/nginx-1.12.2-add-usec-times.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--- ../../../github/openresty/nginx-releases/src/core/ngx_times.c 2016-07-27 17:25:45.000000000 +0800
+++ nginx-1.9.15/src/core/ngx_times.c 2016-07-08 18:10:29.000000000 +0800
@@ -81,6 +81,7 @@
ngx_tm_t tm, gmt;
time_t sec;
ngx_uint_t msec;
+ ngx_uint_t usec;
ngx_time_t *tp;
struct timeval tv;

@@ -91,7 +92,8 @@
ngx_gettimeofday(&tv);

sec = tv.tv_sec;
- msec = tv.tv_usec / 1000;
+ usec = tv.tv_usec;
+ msec = usec / 1000;

ngx_current_msec = (ngx_msec_t) sec * 1000 + msec;

@@ -99,6 +101,7 @@

if (tp->sec == sec) {
tp->msec = msec;
+ tp->usec = usec;
ngx_unlock(&ngx_time_lock);
return;
}
@@ -113,6 +116,7 @@

tp->sec = sec;
tp->msec = msec;
+ tp->usec = usec;

ngx_gmtime(sec, &gmt);

--- ../../../github/openresty/nginx-releases/src/core/ngx_times.h 2016-07-27 17:25:45.000000000 +0800
+++ nginx-1.9.15/src/core/ngx_times.h 2016-07-08 18:10:50.000000000 +0800
@@ -16,6 +16,7 @@
typedef struct {
time_t sec;
ngx_uint_t msec;
+ ngx_uint_t usec;
ngx_int_t gmtoff;
} ngx_time_t;

0 comments on commit c2b2427

Please sign in to comment.