Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b096355

Browse files
committed
ngx-json-log: experimental upstream
1 parent 4f4ad79 commit b096355

File tree

4 files changed

+172
-10
lines changed

4 files changed

+172
-10
lines changed

‎src/ngx_http_json_log_module.c

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <ngx_config.h>
2727
#include <ngx_core.h>
2828
#include <ngx_http.h>
29-
#include <ngx_log.h>
3029

3130
#include "ngx_http_json_log_module.h"
3231
#include "ngx_http_json_log_variables.h"
@@ -344,6 +343,81 @@ static ngx_int_t ngx_http_json_log_log_handler(ngx_http_request_t *r)
344343

345344
} // if KAFKA type
346345
#endif
346+
347+
if (location->type == NGX_JSON_LOG_SINK_UPSTREAM) {
348+
349+
if (!location->upstream) {
350+
continue;
351+
}
352+
/*
353+
ngx_http_upstream_t *u;
354+
355+
u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t));
356+
if (u == NULL) {
357+
continue;
358+
}
359+
360+
u->
361+
362+
ngx_int_t rc = ngx_event_connect_peer(location->upstream);
363+
364+
ngx_log_error(NGX_LOG_INFO, r->pool->log, 0,
365+
"http_json_log: u-conn[%lu]\n",
366+
rc
367+
);
368+
*/
369+
370+
//TODO : LOCKED TO FIRST ELEMENT AND FIRST ADDRESS
371+
ngx_http_upstream_server_t *arr = location->upstream->servers->elts;
372+
ngx_http_upstream_server_t *server = &arr[0];
373+
374+
ngx_log_error(NGX_LOG_INFO, r->pool->log, 0,
375+
"http_json_log: name:[%v] down:[%ul] naddrs=[%ul]\n",
376+
&server->name, server->down, server->naddrs
377+
);
378+
379+
ngx_addr_t *addrs = server->addrs;
380+
ngx_socket_t s = ngx_socket(addrs->sockaddr->sa_family,
381+
SOCK_STREAM, 0);
382+
ngx_connection_t *c = ngx_get_connection(s, r->pool->log);
383+
384+
if (c == NULL) {
385+
if (ngx_close_socket(s) == -1) {
386+
ngx_log_error(NGX_LOG_ALERT, r->pool->log, ngx_socket_errno,
387+
ngx_close_socket_n
388+
"failed");
389+
}
390+
continue;
391+
}
392+
393+
c->recv = ngx_recv;
394+
c->send = ngx_send;
395+
c->recv_chain = ngx_recv_chain;
396+
c->send_chain = ngx_send_chain;
397+
398+
int rc = connect(s, addrs->sockaddr, addrs->socklen);
399+
if (rc == -1) {
400+
ngx_close_connection(c);
401+
continue;
402+
}
403+
404+
ngx_http_request_t *req = ngx_http_create_request(c);
405+
406+
if (req == NULL) {
407+
continue;
408+
}
409+
410+
req->keepalive = 1;
411+
412+
//write(s, "HEAD / HTTP/1.0\r\n\r\n", 19);
413+
//printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n");
414+
415+
//ngx_close_connection(c);
416+
continue;
417+
418+
419+
} // for upstream
420+
347421
} // for location
348422

349423
return NGX_OK;
@@ -427,8 +501,9 @@ ngx_http_json_log_create_loc_conf(ngx_conf_t *cf)
427501
*
428502
* Supported output destinations:
429503
*
430-
* file: -> filesystem
431-
* kafka: -> kafka topic
504+
* file: -> filesystem
505+
* kafka: -> kafka topic
506+
* upstream: -> upstream connection
432507
*/
433508
static char *
434509
ngx_http_json_log_loc_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
@@ -439,6 +514,9 @@ ngx_http_json_log_loc_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
439514
ngx_str_t *args = cf->args->elts;
440515
ngx_json_log_output_location_t *new_location;
441516

517+
518+
519+
442520
if (! args) {
443521
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
444522
"Invalid argument for HTTP log JSON output location");
@@ -467,8 +545,8 @@ ngx_http_json_log_loc_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
467545
return NGX_CONF_ERROR;
468546
}
469547

470-
#if (NGX_HAVE_LIBRDKAFKA)
471548
/* If sink type is kafka, then set topic config for this location */
549+
#if (NGX_HAVE_LIBRDKAFKA)
472550
if (new_location->type == NGX_JSON_LOG_SINK_KAFKA) {
473551
/* create topic conf */
474552
new_location->kafka.rktc = ngx_json_log_kafka_topic_conf_new(cf->pool);

‎src/ngx_json_log_output.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,17 @@ ngx_json_log_output_location_conf(ngx_conf_t *cf,
5454
ngx_json_log_output_location_t *new_location = NULL;
5555
size_t prefix_len;
5656

57+
size_t add;
58+
u_short port;
59+
ngx_url_t u;
60+
ngx_str_t *url = NULL;
61+
5762
if (! NGX_JSON_LOG_HAS_FILE_PREFIX(value)
5863
#if (NGX_HAVE_LIBRDKAFKA)
5964
&& ! NGX_JSON_LOG_HAS_KAFKA_PREFIX(value)
6065
#endif
6166
&& ! NGX_JSON_LOG_HAS_SYSLOG_PREFIX(value)
67+
&& ! NGX_JSON_LOG_HAS_UPSTREAM_PREFIX(value)
6268
) {
6369
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
6470
"Invalid prefix [%v] JSON log output location", value);
@@ -85,6 +91,9 @@ ngx_json_log_output_location_conf(ngx_conf_t *cf,
8591
} else if (NGX_JSON_LOG_HAS_SYSLOG_PREFIX(value)) {
8692
new_location->type = NGX_JSON_LOG_SINK_SYSLOG;
8793
prefix_len = NGX_JSON_LOG_SYSLOG_OUT_LEN;
94+
} else if (NGX_JSON_LOG_HAS_UPSTREAM_PREFIX(value)) {
95+
new_location->type = NGX_JSON_LOG_SINK_UPSTREAM;
96+
prefix_len = NGX_JSON_LOG_UPSTREAM_OUT_LEN;
8897
}
8998

9099
/* Saves location without prefix. */
@@ -115,6 +124,52 @@ ngx_json_log_output_location_conf(ngx_conf_t *cf,
115124
}
116125
}
117126

127+
/* If sink type is upstream, then validate upstream */
128+
if (new_location->type == NGX_JSON_LOG_SINK_UPSTREAM) {
129+
130+
url = &new_location->location;
131+
132+
//TODO: to work with a script location
133+
//ngx_uint_t n = ngx_http_script_variables_count(&new_location->location);
134+
135+
if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) {
136+
add = 7;
137+
port = 80;
138+
139+
} else if (ngx_strncasecmp(url->data, (u_char *) "https://", 8) == 0) {
140+
141+
#if (NGX_HTTP_SSL)
142+
plcf->ssl = 1;
143+
144+
add = 8;
145+
port = 443;
146+
#else
147+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
148+
"https protocol requires SSL support");
149+
return NGX_CONF_ERROR;
150+
#endif
151+
152+
} else {
153+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
154+
return NGX_CONF_ERROR;
155+
}
156+
157+
ngx_memzero(&u, sizeof(ngx_url_t));
158+
159+
u.url.len = url->len - add;
160+
u.url.data = url->data + add;
161+
u.default_port = port;
162+
u.uri_part = 1;
163+
u.no_resolve = 1;
164+
165+
new_location->upstream = ngx_http_upstream_add(cf, &u, 0);
166+
167+
if (new_location->upstream) {
168+
printf("UPSTREAM %lu\n", new_location->upstream->servers->nelts);
169+
}
170+
171+
}
172+
118173
return new_location;
119174
}
120175

‎src/ngx_json_log_output.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,33 @@
3030
#include "ngx_json_log_text.h"
3131
#include "ngx_json_log_kafka.h"
3232

33+
#define NGX_JSON_LOG_FILE_OUT_LEN (sizeof("file:") - 1)
34+
#define NGX_JSON_LOG_HAS_FILE_PREFIX(str) \
35+
(ngx_strncmp(str->data, \
36+
"file:", \
37+
NGX_JSON_LOG_FILE_OUT_LEN) == 0 )
38+
39+
#if (NGX_HAVE_LIBRDKAFKA)
40+
#define NGX_JSON_LOG_KAFKA_OUT_LEN (sizeof("kafka:") - 1)
41+
#define NGX_JSON_LOG_HAS_KAFKA_PREFIX(str) \
42+
(ngx_strncmp(str->data, \
43+
"kafka:", \
44+
NGX_JSON_LOG_KAFKA_OUT_LEN) == 0 )
45+
#endif
46+
47+
#define NGX_JSON_LOG_UPSTREAM_OUT_LEN (sizeof("upstream:") - 1)
48+
#define NGX_JSON_LOG_HAS_UPSTREAM_PREFIX(str) \
49+
(ngx_strncmp(str->data, \
50+
"upstream:", \
51+
NGX_JSON_LOG_UPSTREAM_OUT_LEN) == 0 )
52+
3353
typedef enum {
3454
NGX_JSON_LOG_SINK_FILE = 0,
3555
NGX_JSON_LOG_SINK_SYSLOG = 1,
3656
#if (NGX_HAVE_LIBRDKAFKA)
37-
NGX_JSON_LOG_SINK_KAFKA = 2
57+
NGX_JSON_LOG_SINK_KAFKA = 2,
3858
#endif
59+
NGX_JSON_LOG_SINK_UPSTREAM = 3
3960
} ngx_json_log_sink_e;
4061

4162
struct ngx_json_log_output_location_s {
@@ -47,6 +68,7 @@ struct ngx_json_log_output_location_s {
4768
#if (NGX_HAVE_LIBRDKAFKA)
4869
ngx_json_log_kafka_conf_t kafka;
4970
#endif
71+
ngx_http_upstream_srv_conf_t *upstream;
5072
};
5173

5274
typedef struct ngx_json_log_output_location_s ngx_json_log_output_location_t;
@@ -71,5 +93,10 @@ ngx_json_log_write_sink_syslog(ngx_log_t *log,
7193
ngx_syslog_peer_t *syslog,
7294
const char *txt);
7395

96+
ngx_int_t
97+
ngx_json_log_write_sink_upstream(ngx_log_t *log,
98+
ngx_fd_t fd, const char *txt);
99+
100+
74101
#endif // __NGX_JSON_LOG_OUTPUT_H__
75102

‎src/ngx_json_log_text.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ typedef enum {
4040
} ngx_json_log_module_type_e;
4141

4242
struct ngx_json_log_format_s {
43-
ngx_str_t name; /* the format name */
44-
ngx_str_t config; /* value at config files */
45-
ngx_array_t *items; /* format items */
46-
ngx_http_complex_value_t *http_filter; /* filter output */
43+
ngx_str_t name; /* the format name */
44+
ngx_str_t config; /* value at config files */
45+
ngx_array_t *items; /* format items */
46+
ngx_http_complex_value_t *http_filter; /* filter output */
4747
#if nginx_version >= 1011002
48-
ngx_stream_complex_value_t *stream_filter; /* filter output */
48+
ngx_stream_complex_value_t *stream_filter; /* filter output */
4949
#endif
5050
};
5151
typedef struct ngx_json_log_format_s ngx_json_log_format_t;
@@ -84,5 +84,7 @@ ngx_json_log_items_dump_text(ngx_json_log_module_type_e type,
8484
ngx_int_t
8585
ngx_json_log_items_cmp(const void *left, const void *right);
8686

87+
88+
8789
#endif // __NGX_JSON_LOG_TEXT_H__
8890

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /