forked from mailru/graphite-nginx-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua_module_v0_9_20.patch
141 lines (133 loc) · 3.77 KB
/
lua_module_v0_9_20.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 4b770d7..22a9827 100644
--- a/config
+++ b/config
@@ -352,6 +352,7 @@ NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
$ngx_addon_dir/src/ngx_http_lua_worker.c \
$ngx_addon_dir/src/ngx_http_lua_lex.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 \
@@ -406,6 +407,7 @@ NGX_ADDON_DEPS="$NGX_ADDON_DEPS \
$ngx_addon_dir/src/ngx_http_lua_worker.h \
$ngx_addon_dir/src/ngx_http_lua_lex.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 e9d40fc..a0aff09 100644
--- a/src/ngx_http_lua_util.c
+++ b/src/ngx_http_lua_util.c
@@ -50,6 +50,7 @@
#include "ngx_http_lua_worker.h"
#include "ngx_http_lua_socket_tcp.h"
+#include "ngx_http_lua_graphite.h"
#if 1
#undef ngx_http_lua_probe_info
@@ -719,7 +720,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");
@@ -753,6 +754,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);