forked from mailru/graphite-nginx-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua_module_v0_10_6.patch
189 lines (180 loc) · 5.05 KB
/
lua_module_v0_10_6.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
diff --git a/config b/config
index 0f2749d..7b8d1eb 100644
--- a/config
+++ b/config
@@ -360,6 +360,7 @@ HTTP_LUA_SRCS=" \
$ngx_addon_dir/src/ngx_http_lua_ssl_session_storeby.c \
$ngx_addon_dir/src/ngx_http_lua_ssl_session_fetchby.c \
$ngx_addon_dir/src/ngx_http_lua_ssl.c \
+ $ngx_addon_dir/src/ngx_http_lua_graphite.c \
"
HTTP_LUA_DEPS=" \
@@ -420,6 +421,7 @@ HTTP_LUA_DEPS=" \
$ngx_addon_dir/src/ngx_http_lua_ssl_session_storeby.h \
$ngx_addon_dir/src/ngx_http_lua_ssl_session_fetchby.h \
$ngx_addon_dir/src/ngx_http_lua_ssl.h \
+ $ngx_addon_dir/src/ngx_http_lua_graphite.h \
"
CFLAGS="$CFLAGS -DNDK_SET_VAR"
diff --git a/src/ngx_http_lua_graphite.c b/src/ngx_http_lua_graphite.c
new file mode 100644
index 0000000..20812f9
--- /dev/null
+++ b/src/ngx_http_lua_graphite.c
@@ -0,0 +1,117 @@
+#ifndef DDEBUG
+#define DDEBUG 0
+#endif
+#include "ddebug.h"
+
+
+#include "ngx_http_graphite_module.h"
+#include "ngx_http_lua_graphite.h"
+#include "ngx_http_lua_util.h"
+
+
+static int ngx_http_lua_graphite(lua_State *L);
+static int ngx_http_lua_graphite_get(lua_State *L);
+static int ngx_http_lua_graphite_set(lua_State *L);
+
+
+void
+ngx_http_lua_inject_graphite_api(lua_State *L)
+{
+ lua_createtable(L, 0, 2);
+ lua_newtable(L);
+ lua_pushcfunction(L, ngx_http_lua_graphite);
+ lua_setfield(L, -2, "__call");
+ lua_setmetatable(L, -2);
+
+ lua_pushcfunction(L, ngx_http_lua_graphite_get);
+ lua_setfield(L, -2, "get");
+
+ lua_pushcfunction(L, ngx_http_lua_graphite_set);
+ lua_setfield(L, -2, "set");
+
+ lua_setfield(L, -2, "graphite");
+}
+
+
+static int
+ngx_http_lua_graphite(lua_State *L) {
+
+ size_t n = lua_gettop(L) - 1;
+ if (n != 2 && n != 3) {
+ return luaL_error(L, "ngx.graphite expecting 2 or 3 arguments got %d", n);
+ }
+
+ ngx_http_request_t *r;
+ r = ngx_http_lua_get_req(L);
+
+ if (r == NULL) {
+ return luaL_error(L, "no request object found");
+ }
+
+ ngx_str_t name;
+ name.data = (u_char*)lua_tolstring(L, 2, &name.len);
+ if (name.data == NULL)
+ return 0;
+
+ double value = lua_tonumber(L, 3);
+
+ ngx_http_graphite(r, &name, value);
+
+ return 0;
+}
+
+
+static int
+ngx_http_lua_graphite_get(lua_State *L) {
+
+ size_t n = lua_gettop(L);
+ if (n != 1) {
+ return luaL_error(L, "ngx.graphite.get expecting 1 argument got %d", n);
+ }
+
+ ngx_http_request_t *r;
+ r = ngx_http_lua_get_req(L);
+
+ if (r == NULL) {
+ return luaL_error(L, "no request object found");
+ }
+
+ ngx_str_t name;
+ name.data = (u_char*)lua_tolstring(L, 1, &name.len);
+ if (name.data == NULL)
+ return 0;
+
+ double value = ngx_http_graphite_get(r, &name);
+
+ lua_pushnumber(L, value);
+
+ return 1;
+}
+
+
+static int
+ngx_http_lua_graphite_set(lua_State *L) {
+
+ size_t n = lua_gettop(L);
+ if (n != 2) {
+ return luaL_error(L, "ngx.graphite.get expecting 2 arguments got %d", n);
+ }
+
+ ngx_http_request_t *r;
+ r = ngx_http_lua_get_req(L);
+
+ if (r == NULL) {
+ return luaL_error(L, "no request object found");
+ }
+
+ ngx_str_t name;
+ name.data = (u_char*)lua_tolstring(L, 1, &name.len);
+ if (name.data == NULL)
+ return 0;
+
+ double value = lua_tonumber(L, 2);
+
+ ngx_http_graphite_set(r, &name, value);
+
+ return 0;
+}
diff --git a/src/ngx_http_lua_util.c b/src/ngx_http_lua_util.c
index 7f59833..43ca208 100644
--- a/src/ngx_http_lua_util.c
+++ b/src/ngx_http_lua_util.c
@@ -52,6 +52,7 @@
#include "ngx_http_lua_ssl_certby.h"
#include "ngx_http_lua_ssl.h"
+#include "ngx_http_lua_graphite.h"
#if 1
#undef ngx_http_lua_probe_info
@@ -709,7 +710,7 @@ static void
ngx_http_lua_inject_ngx_api(lua_State *L, ngx_http_lua_main_conf_t *lmcf,
ngx_log_t *log)
{
- lua_createtable(L, 0 /* narr */, 116 /* nrec */); /* ngx.* */
+ lua_createtable(L, 0 /* narr */, 117 /* nrec */); /* ngx.* */
lua_pushcfunction(L, ngx_http_lua_get_raw_phase_context);
lua_setfield(L, -2, "_phase_ctx");
@@ -743,6 +744,7 @@ ngx_http_lua_inject_ngx_api(lua_State *L, ngx_http_lua_main_conf_t *lmcf,
ngx_http_lua_inject_timer_api(L);
ngx_http_lua_inject_config_api(L);
ngx_http_lua_inject_worker_api(L);
+ ngx_http_lua_inject_graphite_api(L);
ngx_http_lua_inject_misc_api(L);
@@ -1003,7 +1005,16 @@ ngx_http_lua_run_thread(lua_State *L, ngx_http_request_t *r,
ngx_http_lua_assert(orig_coctx->co_top + nrets
== lua_gettop(orig_coctx->co));
+#ifdef NGX_GRAPHITE_PATCH
+ struct timeval start_tp;
+ ngx_gettimeofday(&start_tp);
+#endif
rv = lua_resume(orig_coctx->co, nrets);
+#ifdef NGX_GRAPHITE_PATCH
+ struct timeval stop_tp;
+ ngx_gettimeofday(&stop_tp);
+ r->lua_time += (stop_tp.tv_sec - start_tp.tv_sec) * 1000 + (stop_tp.tv_usec - start_tp.tv_usec) / 1000.0;
+#endif
#if (NGX_PCRE)
/* XXX: work-around to nginx regex subsystem */