forked from mailru/graphite-nginx-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua_module_v0_8_6.patch
141 lines (130 loc) · 3.74 KB
/
lua_module_v0_8_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
diff --git a/config b/config
index a7e13bc..b5246d7 100644
--- a/config
+++ b/config
@@ -223,6 +223,7 @@ NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
$ngx_addon_dir/src/ngx_http_lua_uthread.c \
$ngx_addon_dir/src/ngx_http_lua_timer.c \
"
+NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_lua_graphite.c"
NGX_ADDON_DEPS="$NGX_ADDON_DEPS \
$ngx_addon_dir/src/ddebug.h \
@@ -273,6 +274,7 @@ NGX_ADDON_DEPS="$NGX_ADDON_DEPS \
$ngx_addon_dir/src/ngx_http_lua_uthread.h \
$ngx_addon_dir/src/ngx_http_lua_timer.h \
"
+NGX_ADDON_DEPS="$NGX_ADDON_DEPS $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..e959199
--- /dev/null
+++ b/src/ngx_http_lua_graphite.c
@@ -0,0 +1,69 @@
+#ifndef DDEBUG
+#define DDEBUG 0
+#endif
+#include "ddebug.h"
+
+
+#include "ngx_http_lua_graphite.h"
+#include "ngx_http_lua_util.h"
+
+
+typedef ngx_int_t (*ngx_http_graphite_custom_pt)(ngx_http_request_t*, ngx_str_t*, double);
+
+
+static ngx_http_graphite_custom_pt custom_pt = NULL;
+static int ngx_http_lua_graphite_custom(lua_State *L);
+
+
+void
+ngx_http_lua_inject_graphite_api(lua_State *L)
+{
+ ngx_str_t name = ngx_string("graphite_custom");
+
+ int i;
+ for (i = 0; ngx_modules[i]; i++) {
+
+ ngx_module_t *module = ngx_modules[i];
+ if (module->type != NGX_HTTP_MODULE)
+ continue;
+
+ ngx_command_t *cmd = module->commands;
+ if (cmd == NULL)
+ continue;
+
+ for (; cmd->name.len; cmd++) {
+ if ((cmd->name.len == name.len) && (ngx_strncmp(cmd->name.data, name.data, name.len) == 0)) {
+ custom_pt = cmd->post;
+ }
+ }
+ }
+
+ lua_pushcfunction(L, ngx_http_lua_graphite_custom);
+ lua_setfield(L, -2, "graphite");
+}
+
+
+static int
+ngx_http_lua_graphite_custom(lua_State *L) {
+
+ ngx_http_request_t *r;
+ r = ngx_http_lua_get_req(L);
+
+ if (r == NULL) {
+ return luaL_error(L, "no request object found");
+ }
+
+ double value = lua_tonumber(L, -1);
+ lua_pop(L, 1);
+
+ ngx_str_t name;
+ name.data = (u_char*)lua_tolstring(L, -1, &name.len);
+ if (name.data == NULL)
+ return 0;
+ lua_pop(L, 1);
+
+ if (custom_pt)
+ custom_pt(r, &name, value);
+
+ return 0;
+}
diff --git a/src/ngx_http_lua_graphite.h b/src/ngx_http_lua_graphite.h
new file mode 100644
index 0000000..cf76efa
--- /dev/null
+++ b/src/ngx_http_lua_graphite.h
@@ -0,0 +1,11 @@
+#ifndef NGX_HTTP_LUA_GRAPHITE_H
+#define NGX_HTTP_LUA_GRAPHITE_H
+
+#include "ngx_http_lua_common.h"
+
+
+void ngx_http_lua_inject_graphite_api(lua_State *L);
+
+
+#endif /* NGX_HTTP_LUA_GRAPHITE_H */
+
diff --git a/src/ngx_http_lua_util.c b/src/ngx_http_lua_util.c
index 1b9d24c..9791fec 100644
--- a/src/ngx_http_lua_util.c
+++ b/src/ngx_http_lua_util.c
@@ -47,6 +47,7 @@
#include "ngx_http_lua_contentby.h"
#include "ngx_http_lua_timer.h"
+#include "ngx_http_lua_graphite.h"
#if 1
#undef ngx_http_lua_probe_info
@@ -742,7 +743,7 @@ ngx_http_lua_inject_ngx_api(ngx_conf_t *cf, lua_State *L)
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
- lua_createtable(L, 0 /* narr */, 95 /* nrec */); /* ngx.* */
+ lua_createtable(L, 0 /* narr */, 96 /* nrec */); /* ngx.* */
ngx_http_lua_inject_arg_api(L);
@@ -770,6 +771,7 @@ ngx_http_lua_inject_ngx_api(ngx_conf_t *cf, lua_State *L)
ngx_http_lua_inject_socket_udp_api(cf->log, L);
ngx_http_lua_inject_uthread_api(cf->log, L);
ngx_http_lua_inject_timer_api(L);
+ ngx_http_lua_inject_graphite_api(L);
ngx_http_lua_inject_misc_api(L);