Skip to content

Commit 49dc8d7

Browse files
authored
[ngxta] refactor log (#23)
* Write log to file with ngx_log_error, which support log level, instead of ngx_write_fd. * Hence, replace ngx_fd_t with ngx_log_t to store log information. * Another advantage of ngx_log_t is it support reopen of log files. * add copyright header
1 parent 196ca7b commit 49dc8d7

File tree

6 files changed

+121
-45
lines changed

6 files changed

+121
-45
lines changed

config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ngx_addon_name=ngx_http_accounting_module
22

33
HTTP_ACCOUNTING_DEPS=" \
4+
$ngx_addon_dir/src/ngxta.h \
45
$ngx_addon_dir/src/ngx_http_accounting_hash.h \
56
$ngx_addon_dir/src/ngx_http_accounting_common.h \
67
$ngx_addon_dir/src/ngx_http_accounting_module.h \
@@ -9,6 +10,7 @@ HTTP_ACCOUNTING_DEPS=" \
910
"
1011

1112
HTTP_ACCOUNTING_SRCS=" \
13+
$ngx_addon_dir/src/ngxta_log.c \
1214
$ngx_addon_dir/src/ngx_http_accounting_hash.c \
1315
$ngx_addon_dir/src/ngx_http_accounting_common.c \
1416
$ngx_addon_dir/src/ngx_http_accounting_module.c \

misc/nginx.conf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ http {
3030
http_accounting_id '_NGXTA_';
3131
http_accounting_log logs/http-accounting.log;
3232

33+
log_not_found off;
34+
3335
server {
3436
listen 1234;
3537
server_name echo;
@@ -60,7 +62,7 @@ http {
6062
location / {
6163
root html;
6264
index index.html index.htm;
63-
http_accounting_id $status;
65+
http_accounting_id $host;
6466
}
6567

6668
#error_page 404 /404.html;

src/ngx_http_accounting_module.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <ngx_core.h>
33
#include <ngx_http.h>
44

5+
#include "ngxta.h"
56
#include "ngx_http_accounting_hash.h"
67
#include "ngx_http_accounting_common.h"
78
#include "ngx_http_accounting_module.h"
@@ -103,6 +104,24 @@ ngx_http_accounting_init(ngx_conf_t *cf)
103104
return NGX_OK;
104105
}
105106

107+
if (amcf->log.len > 0) {
108+
if (NGX_OK != ngxta_log_open(cf->cycle, &ngxta_log, &amcf->log)) {
109+
// ngxta_log = NULL;
110+
return NGX_ERROR;
111+
}
112+
113+
if (ngxta_log.file->name.len > 0
114+
&& ngxta_log.file->fd != NGX_INVALID_FILE ) {
115+
ngx_log_error(NGX_LOG_INFO, ngx_cycle->log, 0,
116+
"pid:%i|accounting log to file: %V",
117+
ngx_getpid(),
118+
ngxta_log.file->name );
119+
}
120+
}
121+
// else {
122+
// // ngxta_log = ngx_cycle->log;
123+
// }
124+
106125
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
107126

108127
h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
@@ -154,15 +173,14 @@ ngx_http_accounting_init_main_conf(ngx_conf_t *cf, void *conf)
154173
ngx_http_accounting_main_conf_t *amcf = conf;
155174

156175
if (amcf->enable == NGX_CONF_UNSET) {
157-
amcf->enable = 0;
176+
amcf->enable = 0;
158177
}
159178
if (amcf->interval == NGX_CONF_UNSET) {
160-
amcf->interval = 60;
179+
amcf->interval = 60;
161180
}
162181
if (amcf->perturb == NGX_CONF_UNSET) {
163-
amcf->perturb = 0;
182+
amcf->perturb = 0;
164183
}
165-
166184

167185
return NGX_CONF_OK;
168186
}

src/ngx_http_accounting_worker_process.c

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <syslog.h>
88
#include <sys/file.h>
99

10+
#include "ngxta.h"
1011
#include "ngx_http_accounting_hash.h"
1112
#include "ngx_http_accounting_module.h"
1213
#include "ngx_http_accounting_common.h"
@@ -21,8 +22,6 @@ static ngx_http_accounting_hash_t stats_hash;
2122

2223
static ngx_int_t ngx_http_accounting_old_time = 0;
2324
static ngx_int_t ngx_http_accounting_new_time = 0;
24-
static ngx_str_t ngx_http_accounting_log;
25-
static ngx_fd_t ngx_http_accounting_log_fd = NGX_INVALID_FILE;
2625

2726
static u_char *ngx_http_accounting_title = (u_char *)"NgxAccounting";
2827

@@ -49,12 +48,15 @@ ngx_http_accounting_worker_process_init(ngx_cycle_t *cycle)
4948

5049
ngx_http_accounting_old_time = time->sec;
5150
ngx_http_accounting_new_time = time->sec;
52-
ngx_http_accounting_log = amcf->log;
5351
worker_process_timer_interval = amcf->interval;
5452
worker_process_timer_perturb = amcf->perturb;
5553

56-
openlog((char *)ngx_http_accounting_title, LOG_NDELAY, LOG_SYSLOG);
57-
syslog(LOG_INFO, "pid:%i|Process:init", ngx_getpid());
54+
if (ngxta_log.file->fd != NGX_INVALID_FILE) {
55+
ngx_log_error(NGX_LOG_NOTICE, &ngxta_log, 0, "pid:%i|worker process start accounting", ngx_getpid());
56+
} else {
57+
openlog((char *)ngx_http_accounting_title, LOG_NDELAY, LOG_SYSLOG);
58+
syslog(LOG_INFO, "pid:%i|worker process start accounting", ngx_getpid());
59+
}
5860

5961
rc = ngx_http_accounting_hash_init(&stats_hash, NGX_HTTP_ACCOUNTING_NR_BUCKETS, cycle->pool);
6062
if (rc != NGX_OK) {
@@ -71,7 +73,7 @@ ngx_http_accounting_worker_process_init(ngx_cycle_t *cycle)
7173
srand(ngx_getpid());
7274
time_t perturb_factor = 1;
7375
if (worker_process_timer_perturb) {
74-
perturb_factor = (1000-rand()%200);
76+
perturb_factor = (1000-rand()%200);
7577
}
7678

7779
ngx_add_timer(&write_out_ev, worker_process_timer_interval * perturb_factor);
@@ -92,7 +94,11 @@ void ngx_http_accounting_worker_process_exit(ngx_cycle_t *cycle)
9294

9395
worker_process_alarm_handler(NULL);
9496

95-
syslog(LOG_INFO, "pid:%i|Process:exit", ngx_getpid());
97+
if (ngxta_log.file->fd != NGX_INVALID_FILE) {
98+
ngx_log_error(NGX_LOG_NOTICE, &ngxta_log, 0, "pid:%i|worker process stop accounting", ngx_getpid());
99+
} else {
100+
syslog(LOG_INFO, "pid:%i|worker process stop accounting", ngx_getpid());
101+
}
96102
}
97103

98104
ngx_int_t
@@ -121,11 +127,11 @@ ngx_http_accounting_handler(ngx_http_request_t *r)
121127
state = r->upstream_states->elts;
122128
if (state[0].status) {
123129
// not even checking the status here...
124-
#if (nginx_version < 1009000)
125-
upstream_req_latency_ms = (state[0].response_sec * 1000 + state[0].response_msec);
126-
#else
127-
upstream_req_latency_ms = state[0].response_time;
128-
#endif
130+
#if (nginx_version < 1009000)
131+
upstream_req_latency_ms = (state[0].response_sec * 1000 + state[0].response_msec);
132+
#else
133+
upstream_req_latency_ms = state[0].response_time;
134+
#endif
129135
}
130136
}
131137
// TODO: key should be cached to save CPU time
@@ -166,11 +172,12 @@ ngx_http_accounting_handler(ngx_http_request_t *r)
166172
static ngx_int_t
167173
worker_process_write_out_stats(u_char *name, size_t len, void *val, void *para1, void *para2)
168174
{
169-
ngx_uint_t i;
175+
ngx_uint_t i;
170176
ngx_http_accounting_stats_t *stats;
171177

172-
char temp_buffer[128];
173-
char output_buffer[1024];
178+
ngx_str_t accounting_msg;
179+
u_char msg_buf[NGX_MAX_ERROR_STR];
180+
u_char *p, *last;
174181

175182
stats = (ngx_http_accounting_stats_t *)val;
176183

@@ -188,7 +195,12 @@ worker_process_write_out_stats(u_char *name, size_t len, void *val, void *para1,
188195
return NGX_OK;
189196
}
190197

191-
sprintf(output_buffer, "pid:%i|from:%ld|to:%ld|accounting_id:%s|requests:%ld|bytes_in:%ld|bytes_out:%ld|latency_ms:%lu|upstream_latency_ms:%lu",
198+
// Output message buffer
199+
p = msg_buf;
200+
last = msg_buf + NGX_MAX_ERROR_STR;
201+
202+
p = ngx_slprintf(p, last,
203+
"pid:%i|from:%i|to:%i|accounting_id:%s|requests:%ui|bytes_in:%ui|bytes_out:%ui|latency_ms:%ui|upstream_latency_ms:%ui",
192204
ngx_getpid(),
193205
ngx_http_accounting_old_time,
194206
ngx_http_accounting_new_time,
@@ -208,22 +220,28 @@ worker_process_write_out_stats(u_char *name, size_t len, void *val, void *para1,
208220

209221
for (i = 0; i < http_status_code_count; i++) {
210222
if(stats->http_status_code[i] > 0) {
211-
sprintf(temp_buffer, "|%ld:%ld",
212-
index_to_http_status_code_map[i],
213-
stats->http_status_code[i]);
214223

215-
strcat(output_buffer, temp_buffer);
224+
p = ngx_slprintf(p, last, "|%i:%i",
225+
index_to_http_status_code_map[i],
226+
stats->http_status_code[i] );
216227

217228
stats->http_status_code[i] = 0;
218229
}
219230
}
220231

221-
if (ngx_http_accounting_log_fd != NGX_INVALID_FILE) {
222-
size_t len = ngx_strlen(output_buffer);
223-
output_buffer[len] = '\n';
224-
ngx_write_fd(ngx_http_accounting_log_fd, output_buffer, len+1);
232+
if (p > last - NGX_LINEFEED_SIZE) {
233+
p = last - NGX_LINEFEED_SIZE;
234+
}
235+
236+
*p++ = '\0';
237+
238+
accounting_msg.len = p - msg_buf;
239+
accounting_msg.data = msg_buf;
240+
241+
if (ngxta_log.file->fd != NGX_INVALID_FILE) {
242+
ngx_log_error(NGX_LOG_NOTICE, &ngxta_log, 0, "%V", &accounting_msg);
225243
} else {
226-
syslog(LOG_INFO, "%s", output_buffer);
244+
syslog(LOG_INFO, "%s", msg_buf);
227245
}
228246

229247
return NGX_OK;
@@ -241,23 +259,8 @@ worker_process_alarm_handler(ngx_event_t *ev)
241259
ngx_http_accounting_old_time = ngx_http_accounting_new_time;
242260
ngx_http_accounting_new_time = time->sec;
243261

244-
if (ngx_http_accounting_log.len) {
245-
ngx_http_accounting_log_fd = ngx_open_file(ngx_http_accounting_log.data, NGX_FILE_APPEND, NGX_FILE_CREATE_OR_OPEN, NGX_FILE_DEFAULT_ACCESS);
246-
if (ngx_http_accounting_log_fd == NGX_INVALID_FILE) {
247-
syslog(LOG_INFO, "Invalid file: %s", ngx_http_accounting_log.data);
248-
}
249-
if (flock(ngx_http_accounting_log_fd, LOCK_EX|LOCK_NB) == -1) {
250-
return; // In the unlikely case that the lock cannot be obtained we will try again on the next alarm
251-
}
252-
}
253-
254262
ngx_http_accounting_hash_iterate(&stats_hash, worker_process_write_out_stats, NULL, NULL);
255263

256-
if (ngx_http_accounting_log_fd != NGX_INVALID_FILE) {
257-
ngx_close_file(ngx_http_accounting_log_fd);
258-
ngx_http_accounting_log_fd = NGX_INVALID_FILE;
259-
}
260-
261264
if (ngx_exiting || ev == NULL)
262265
return;
263266

src/ngxta.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
/*
3+
* Copyright (C) Liu Lantao
4+
*/
5+
6+
7+
#ifndef _NGX_TRAFFIC_ACCOUNTING_H_INCLUDED_
8+
#define _NGX_TRAFFIC_ACCOUNTING_H_INCLUDED_
9+
10+
#include <ngx_core.h>
11+
12+
/*
13+
* Log
14+
*/
15+
16+
ngx_int_t ngxta_log_open(ngx_cycle_t *cycle, ngx_log_t *log, ngx_str_t *log_path);
17+
18+
extern ngx_log_t ngxta_log;
19+
20+
#endif /* _NGX_TRAFFIC_ACCOUNTING_H_INCLUDED_ */

src/ngxta_log.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
/*
3+
* Copyright (C) Liu Lantao
4+
*/
5+
6+
7+
#include "ngxta.h"
8+
9+
ngx_log_t ngxta_log;
10+
11+
ngx_int_t
12+
ngxta_log_open(ngx_cycle_t *cycle, ngx_log_t *log, ngx_str_t *log_path)
13+
{
14+
log->log_level = NGX_LOG_NOTICE;
15+
16+
log->file = ngx_conf_open_file(cycle, log_path);
17+
if (log->file == NULL) {
18+
return NGX_ERROR;
19+
}
20+
log->file->fd = ngx_open_file(&log->file->name, NGX_FILE_APPEND,
21+
NGX_FILE_CREATE_OR_OPEN,
22+
NGX_FILE_DEFAULT_ACCESS);
23+
24+
if (log->file->fd == NGX_INVALID_FILE) {
25+
ngx_log_stderr(ngx_errno,
26+
"[alert] could not open traffic accounting log file: "
27+
ngx_open_file_n " \"%s\" failed", log->file->name.data);
28+
}
29+
30+
return NGX_OK;
31+
}

0 commit comments

Comments
 (0)