Skip to content

Commit d1e078d

Browse files
committed
ngx-http-log-json: #10 - kafka msg-id
``` 2017/04/04 22:07:09 [debug] 1462#0: *1 http_log_json: kafka msg-id:[00fec89b2a2ff15b11573f231cc135dc] msg:[{"_ts":"1491343629.532","req":{"body":"","headers":{"Host":"localhost","User-Agent":"curl/7.52.1","Accept":"*/*"}},"resp":{"headers":{"Last-Modified":"Fri, 31 Mar 2017 16:52:11 GMT","ETag":"\"58de893b-264\"","X-Foo":"bar","Accept-Ranges":"bytes"}}} ] ```
1 parent f93b5cc commit d1e078d

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

src/ngx_http_log_json_module.c

+37-6
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,17 @@ static ngx_str_t conf_zero_value = ngx_string("0");
102102
/* data structures */
103103

104104
struct ngx_http_log_json_format_s {
105-
ngx_str_t name; /* the format name */
106-
ngx_str_t config; /* value at config files */
107-
ngx_array_t *items; /* format items */
105+
ngx_str_t name; /* the format name */
106+
ngx_str_t config; /* value at config files */
107+
ngx_array_t *items; /* format items */
108108
ngx_http_complex_value_t *filter; /* filter output */
109109
};
110110
typedef struct ngx_http_log_json_format_s ngx_http_log_json_format_t;
111111

112112
struct ngx_http_log_json_loc_kafka_conf_s {
113-
rd_kafka_topic_t *rkt; /* kafka topic */
114-
rd_kafka_topic_conf_t *rktc; /* kafka topic configuration */
113+
rd_kafka_topic_t *rkt; /* kafka topic */
114+
rd_kafka_topic_conf_t *rktc; /* kafka topic configuration*/
115+
ngx_http_complex_value_t *msg_id_var; /* variable for message id */
115116
};
116117

117118
/* configuration data structures */
@@ -447,6 +448,8 @@ static ngx_int_t ngx_http_log_json_log_handler(ngx_http_request_t *r) {
447448
ngx_http_log_json_output_location_t *arr;
448449
ngx_http_log_json_output_location_t *location;
449450

451+
ngx_str_t msg_id;
452+
450453
lc = ngx_http_get_module_loc_conf(r, ngx_http_log_json_module);
451454

452455
/*FIXME: Try to discard local upstream requests */
@@ -526,6 +529,13 @@ static ngx_int_t ngx_http_log_json_log_handler(ngx_http_request_t *r) {
526529
/* Write to kafka */
527530
if (location->type == NGX_HTTP_LOG_JSON_SINK_KAFKA) {
528531

532+
ngx_http_complex_value(r, location->kafka.msg_id_var, &msg_id);
533+
#if (NGX_DEBUG)
534+
ngx_log_error(NGX_LOG_DEBUG, r->pool->log, 0,
535+
"http_log_json: kafka msg-id:[%v] msg:[%s]",
536+
&msg_id, txt);
537+
#endif
538+
529539
/* don't do anything if no kafka brokers to send */
530540
if (! mcf->kafka.valid_brokers) {
531541
continue;
@@ -556,7 +566,8 @@ static ngx_int_t ngx_http_log_json_log_handler(ngx_http_request_t *r) {
556566
/* Payload and length */
557567
txt, strlen(txt),
558568
/* Optional key and its length */
559-
NULL, 0,
569+
msg_id.data ? (const char *) msg_id.data: NULL,
570+
msg_id.len,
560571
/* Message opaque, provided in
561572
* delivery report callback as
562573
* msg_opaque. */
@@ -573,6 +584,7 @@ static ngx_int_t ngx_http_log_json_log_handler(ngx_http_request_t *r) {
573584
} else {
574585

575586
#if (NGX_DEBUG)
587+
576588
if (mcf) {
577589
ngx_log_error(NGX_LOG_DEBUG, r->pool->log, 0,
578590
"http_log_json: kafka msg:[%s] ERR:[%d] QUEUE:[%d]",
@@ -957,6 +969,10 @@ ngx_http_log_json_loc_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
957969
size_t i;
958970
ngx_uint_t found = 0;
959971
ngx_http_log_json_main_conf_t *mcf;
972+
ngx_http_compile_complex_value_t ccv;
973+
974+
/*FIXME: Change this to an user's configured variable */
975+
ngx_str_t msg_id_variable = ngx_string("$request_id");
960976

961977
if (! args) {
962978
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
@@ -1043,6 +1059,21 @@ ngx_http_log_json_loc_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
10431059

10441060
/* Set global variable */
10451061
http_log_json_has_kafka_locations = NGX_OK;
1062+
1063+
/* Set variable for message id */
1064+
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
1065+
1066+
ccv.cf = cf;
1067+
ccv.value = &msg_id_variable;
1068+
ccv.complex_value = ngx_pcalloc(cf->pool,
1069+
sizeof(ngx_http_complex_value_t));
1070+
if (ccv.complex_value == NULL) {
1071+
return NGX_CONF_ERROR;
1072+
}
1073+
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
1074+
return NGX_CONF_ERROR;
1075+
}
1076+
new_location->kafka.msg_id_var = ccv.complex_value;
10461077
}
10471078

10481079
return NGX_CONF_OK;

0 commit comments

Comments
 (0)