Skip to content

Commit 5c20b12

Browse files
authored
ngx-json-log: have_librdkafka ngx_feature (#26)
1 parent 2d7d781 commit 5c20b12

9 files changed

+60
-65
lines changed

config

+16-27
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,12 @@
11
ngx_addon_name=ngx_json_log_module
22
ngx_module_incs=$ngx_addon_dir/src
33

4-
HTTP_JSON_LOG_KAFKA=NO
5-
6-
cat << END > $NGX_AUTOTEST.c
7-
8-
#include <librdkafka/rdkafka.h>
9-
10-
int main(void) {
11-
rd_kafka_type_t i = 0;
12-
return (int) i;
13-
}
14-
15-
END
16-
17-
$CC $CC_TEST_FLAGS $CC_AUX_FLAGS -o /dev/null \
18-
$NGX_AUTOTEST.c $NGX_LD_OPT $ngx_feature_libs 2> /dev/null > /dev/null
19-
20-
if [ $? -eq 0 ]; then
21-
HTTP_JSON_LOG_KAFKA=YES
22-
fi
23-
24-
254
NGINX_VERSION=`grep version src/core/nginx.h | sed 's/#define nginx_version *//;'`
265

276
if [ ! -z "${NGINX_VERSION}" ]
287
then
298
if [ $NGINX_VERSION -gt 1011002 ]
309
then
31-
HAS_STREAM_JSON_LOG=true
3210
STREAM_MODULES="$STREAM_MODULES ngx_stream_json_log_module"
3311
STREAM_MODULES="$STREAM_MODULES ngx_stream_json_log_preread_module"
3412
NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
@@ -52,13 +30,24 @@ NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
5230
$ngx_addon_dir/src/ngx_http_json_log_module.c \
5331
"
5432

55-
CORE_LIBS="$CORE_LIBS -ljansson"
56-
57-
if [ $HTTP_JSON_LOG_KAFKA = YES ]; then
33+
#
34+
# rdkafka
35+
#
36+
ngx_feature="librdkafka"
37+
ngx_feature_name="NGX_HAVE_LIBRDKAFKA"
38+
ngx_feature_run=no
39+
ngx_feature_incs="#include <librdkafka/rdkafka.h>"
40+
ngx_feature_path=
41+
ngx_feature_libs="-lrdkafka"
42+
ngx_feature_test="rd_kafka_new(RD_KAFKA_PRODUCER, NULL, NULL, 0);"
43+
. auto/feature
44+
45+
if [ $ngx_found = yes ]; then
5846
echo " + ngx_json_log: kafka support"
59-
CFLAGS="$CFLAGS -DHTTP_JSON_LOG_KAFKA_ENABLED"
6047
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_json_log_kafka.c"
61-
CORE_LIBS="$CORE_LIBS -lrdkafka"
48+
CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
6249
fi
6350

6451

52+
53+
CORE_LIBS="$CORE_LIBS -ljansson"

src/ngx_http_json_log_filter_module.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
typedef struct ngx_http_json_log_main_conf_s ngx_http_json_log_main_conf_t;
4343

44-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
44+
#if (NGX_HAVE_LIBRDKAFKA)
4545
/* configuration kafka constants */
4646
static ngx_int_t http_json_log_filter_has_kafka_locations = NGX_CONF_UNSET;
4747
#endif
@@ -59,7 +59,7 @@ typedef struct ngx_http_json_log_resp_headers_s
5959
/* main config */
6060
struct ngx_http_json_log_filter_main_conf_s {
6161
ngx_array_t *formats;
62-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
62+
#if (NGX_HAVE_LIBRDKAFKA)
6363
ngx_json_log_main_kafka_conf_t kafka;
6464
#endif
6565
};
@@ -123,7 +123,7 @@ static ngx_command_t ngx_http_json_log_filter_commands[] = {
123123
0,
124124
NULL
125125
},
126-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
126+
#if (NGX_HAVE_LIBRDKAFKA)
127127
/* KAFKA */
128128
{
129129
ngx_string("json_err_log_kafka_client_id"),
@@ -322,7 +322,7 @@ ngx_http_json_log_header_bad_request(ngx_http_request_t *r) {
322322

323323
ngx_str_t payload;
324324

325-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
325+
#if (NGX_HAVE_LIBRDKAFKA)
326326
ngx_str_t msg_id;
327327
int err;
328328
ngx_http_json_log_filter_main_conf_t *mcf;
@@ -375,7 +375,7 @@ ngx_http_json_log_header_bad_request(ngx_http_request_t *r) {
375375
}
376376
}
377377

378-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
378+
#if (NGX_HAVE_LIBRDKAFKA)
379379
mcf = ngx_http_get_module_main_conf(r, ngx_http_json_log_filter_module);
380380
#endif
381381

@@ -425,7 +425,7 @@ ngx_http_json_log_header_bad_request(ngx_http_request_t *r) {
425425
continue;
426426
}
427427

428-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
428+
#if (NGX_HAVE_LIBRDKAFKA)
429429
/* Write to kafka */
430430
if (location->type == NGX_JSON_LOG_SINK_KAFKA) {
431431

@@ -702,7 +702,7 @@ ngx_http_json_log_filter_create_main_conf(ngx_conf_t *cf) {
702702
return NULL;
703703
}
704704

705-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
705+
#if (NGX_HAVE_LIBRDKAFKA)
706706
if (ngx_json_log_init_kafka(cf->pool, &conf->kafka) != NGX_OK) {
707707
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
708708
"http_json_log: error initialize kafka conf");
@@ -810,7 +810,7 @@ ngx_http_json_log_srv_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
810810
new_location = ngx_array_push(lc->locations);
811811
new_location->type = NGX_JSON_LOG_SINK_FILE;
812812
prefix_len = NGX_JSON_LOG_FILE_OUT_LEN;
813-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
813+
#if (NGX_HAVE_LIBRDKAFKA)
814814
}
815815
else if (NGX_JSON_LOG_HAS_KAFKA_PREFIX(value)) {
816816
new_location = ngx_array_push(lc->locations);
@@ -841,7 +841,7 @@ ngx_http_json_log_srv_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
841841
&new_location->location);
842842
}
843843

844-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
844+
#if (NGX_HAVE_LIBRDKAFKA)
845845
/* If sink type is kafka, then set topic config for this location */
846846
if (new_location->type == NGX_JSON_LOG_SINK_KAFKA) {
847847

@@ -892,7 +892,7 @@ ngx_http_json_log_srv_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
892892
static ngx_int_t
893893
ngx_http_json_log_filter_init_worker(ngx_cycle_t *cycle) {
894894

895-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
895+
#if (NGX_HAVE_LIBRDKAFKA)
896896
ngx_int_t rc = NGX_OK;
897897
ngx_http_json_log_filter_main_conf_t *conf =
898898
ngx_http_cycle_get_module_main_conf(cycle,

src/ngx_http_json_log_module.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#include "ngx_json_log_text.h"
3838
#include "ngx_http_json_log_variables.h"
3939

40-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
40+
#if (NGX_HAVE_LIBRDKAFKA)
4141

4242
#include "ngx_json_log_kafka.h"
4343

@@ -78,7 +78,7 @@ static ngx_command_t ngx_http_json_log_commands[] = {
7878
0,
7979
NULL
8080
},
81-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
81+
#if (NGX_HAVE_LIBRDKAFKA)
8282
/* KAFKA */
8383
{
8484
ngx_string("json_log_kafka_client_id"),
@@ -180,7 +180,7 @@ ngx_module_t ngx_http_json_log_module = {
180180
static ngx_int_t
181181
ngx_http_json_log_init_worker(ngx_cycle_t *cycle) {
182182

183-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
183+
#if (NGX_HAVE_LIBRDKAFKA)
184184
ngx_int_t rc = NGX_OK;
185185
ngx_http_json_log_main_conf_t *conf =
186186
ngx_http_cycle_get_module_main_conf(cycle, ngx_http_json_log_module);
@@ -218,7 +218,7 @@ static ngx_int_t ngx_http_json_log_log_handler(ngx_http_request_t *r) {
218218
ngx_json_log_output_location_t *location;
219219

220220

221-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
221+
#if (NGX_HAVE_LIBRDKAFKA)
222222
int err;
223223
ngx_http_json_log_main_conf_t *mcf;
224224
ngx_str_t msg_id;
@@ -240,7 +240,7 @@ static ngx_int_t ngx_http_json_log_log_handler(ngx_http_request_t *r) {
240240
return NGX_OK;
241241
}
242242

243-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
243+
#if (NGX_HAVE_LIBRDKAFKA)
244244
mcf = ngx_http_get_module_main_conf(r, ngx_http_json_log_module);
245245
#endif
246246

@@ -290,7 +290,7 @@ static ngx_int_t ngx_http_json_log_log_handler(ngx_http_request_t *r) {
290290
continue;
291291
}
292292

293-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
293+
#if (NGX_HAVE_LIBRDKAFKA)
294294
/* Write to kafka */
295295
if (location->type == NGX_JSON_LOG_SINK_KAFKA) {
296296

@@ -406,7 +406,7 @@ ngx_http_json_log_create_main_conf(ngx_conf_t *cf) {
406406
return NULL;
407407
}
408408

409-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
409+
#if (NGX_HAVE_LIBRDKAFKA)
410410
if (ngx_json_log_init_kafka(cf->pool, &conf->kafka) != NGX_OK) {
411411
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
412412
"http_json_log: error initialize kafka conf");
@@ -498,7 +498,7 @@ ngx_http_json_log_loc_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
498498
new_location = ngx_array_push(lc->locations);
499499
new_location->type = NGX_JSON_LOG_SINK_FILE;
500500
prefix_len = NGX_JSON_LOG_FILE_OUT_LEN;
501-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
501+
#if (NGX_HAVE_LIBRDKAFKA)
502502
}
503503
else if (NGX_JSON_LOG_HAS_KAFKA_PREFIX(value)) {
504504
new_location = ngx_array_push(lc->locations);
@@ -529,7 +529,7 @@ ngx_http_json_log_loc_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
529529
&new_location->location);
530530
}
531531

532-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
532+
#if (NGX_HAVE_LIBRDKAFKA)
533533
/* If sink type is kafka, then set topic config for this location */
534534
if (new_location->type == NGX_JSON_LOG_SINK_KAFKA) {
535535

src/ngx_http_json_log_module.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030

3131
#include <ngx_core.h>
3232

33-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
33+
#if (NGX_HAVE_LIBRDKAFKA)
3434
#include "ngx_json_log_kafka.h"
3535
#endif
3636

3737
struct ngx_http_json_log_main_conf_s {
3838
ngx_array_t *formats;
39-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
39+
#if (NGX_HAVE_LIBRDKAFKA)
4040
ngx_json_log_main_kafka_conf_t kafka;
4141
#endif
4242
};

src/ngx_json_log_kafka.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2424
* SUCH DAMAGE.
2525
*/
26-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
26+
27+
#include <ngx_core.h>
28+
29+
#if (NGX_HAVE_LIBRDKAFKA)
30+
2731
#include <ngx_json_log_kafka.h>
2832
#include <ngx_json_log_str.h>
2933

@@ -333,4 +337,4 @@ ngx_json_log_kafka_topic_disable_ack(ngx_pool_t *pool,
333337
ngx_json_log_kafka_topic_conf_set_str(pool, rktc,
334338
conf_req_required_acks_key, &conf_zero_value);
335339
}
336-
#endif// HTTP_JSON_LOG_KAFKA_ENABLED
340+
#endif// (NGX_HAVE_LIBRDKAFKA)

src/ngx_json_log_kafka.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
*/
2626
#ifndef __NGX_JSON_LOG_KAFKA_H__
2727
#define __NGX_JSON_LOG_KAFKA_H__
28-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
2928

3029
#include <ngx_core.h>
3130
#include <ngx_http.h>
3231
#include <ngx_stream.h>
32+
33+
#if (NGX_HAVE_LIBRDKAFKA)
34+
3335
#include <librdkafka/rdkafka.h>
3436

3537
struct ngx_json_log_kafka_conf_s {
@@ -82,5 +84,5 @@ ngx_int_t
8284
ngx_json_log_configure_kafka(ngx_pool_t *pool,
8385
ngx_json_log_main_kafka_conf_t *conf);
8486

85-
#endif// HTTP_JSON_LOG_KAFKA_ENABLED
87+
#endif// (NGX_HAVE_LIBRDKAFKA)
8688
#endif// __NGX_LOG_JSON_KAFKA_H__

src/ngx_json_log_output.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"file:", \
3737
NGX_JSON_LOG_FILE_OUT_LEN) == 0 )
3838

39-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
39+
#if (NGX_HAVE_LIBRDKAFKA)
4040
#define NGX_JSON_LOG_KAFKA_OUT_LEN (sizeof("kafka:") - 1)
4141
#define NGX_JSON_LOG_HAS_KAFKA_PREFIX(str) \
4242
(ngx_strncmp(str->data, \
@@ -46,7 +46,7 @@
4646

4747
typedef enum {
4848
NGX_JSON_LOG_SINK_FILE = 0,
49-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
49+
#if (NGX_HAVE_LIBRDKAFKA)
5050
NGX_JSON_LOG_SINK_KAFKA = 1
5151
#endif
5252
} ngx_json_log_sink_e;
@@ -56,7 +56,7 @@ struct ngx_json_log_output_location_s {
5656
ngx_json_log_sink_e type;
5757
ngx_json_log_format_t format;
5858
ngx_open_file_t *file;
59-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
59+
#if (NGX_HAVE_LIBRDKAFKA)
6060
ngx_json_log_kafka_conf_t kafka;
6161
#endif
6262
};

src/ngx_stream_json_log_module.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "ngx_json_log_text.h"
3232
#include "ngx_json_log_output.h"
3333

34-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
34+
#if (NGX_HAVE_LIBRDKAFKA)
3535
/* global variable to indicate the we have kafka locations*/
3636
static ngx_int_t stream_json_log_has_kafka_locations = NGX_CONF_UNSET;
3737
#endif
@@ -69,7 +69,7 @@ static ngx_command_t ngx_stream_json_log_commands[] = {
6969
0,
7070
NULL
7171
},
72-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
72+
#if (NGX_HAVE_LIBRDKAFKA)
7373
/* KAFKA */
7474
{
7575
ngx_string("json_log_kafka_client_id"),
@@ -176,7 +176,7 @@ ngx_stream_json_log_log_handler(ngx_stream_session_t *s) {
176176
ngx_json_log_output_location_t *arr;
177177
ngx_json_log_output_location_t *location;
178178

179-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
179+
#if (NGX_HAVE_LIBRDKAFKA)
180180
ngx_stream_json_log_main_conf_t *mcf;
181181
int err;
182182

@@ -242,7 +242,7 @@ ngx_stream_json_log_log_handler(ngx_stream_session_t *s) {
242242
continue;
243243
}
244244

245-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
245+
#if (NGX_HAVE_LIBRDKAFKA)
246246
/* Write to kafka */
247247
if (location->type == NGX_JSON_LOG_SINK_KAFKA) {
248248

@@ -320,7 +320,7 @@ ngx_stream_json_log_create_main_conf(ngx_conf_t *cf) {
320320
return NULL;
321321
}
322322

323-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
323+
#if (NGX_HAVE_LIBRDKAFKA)
324324
if (ngx_json_log_init_kafka(cf->pool, &conf->kafka) != NGX_OK) {
325325
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
326326
"http_json_log: error initialize kafka conf");
@@ -424,7 +424,7 @@ ngx_stream_json_log_srv_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
424424
new_location = ngx_array_push(lc->locations);
425425
new_location->type = NGX_JSON_LOG_SINK_FILE;
426426
prefix_len = NGX_JSON_LOG_FILE_OUT_LEN;
427-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
427+
#if (NGX_HAVE_LIBRDKAFKA)
428428
}
429429
else if (NGX_JSON_LOG_HAS_KAFKA_PREFIX(value)) {
430430
new_location = ngx_array_push(lc->locations);
@@ -455,7 +455,7 @@ ngx_stream_json_log_srv_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
455455
&new_location->location);
456456
}
457457

458-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
458+
#if (NGX_HAVE_LIBRDKAFKA)
459459
/* If sink type is kafka, then set topic config for this location */
460460
if (new_location->type == NGX_JSON_LOG_SINK_KAFKA) {
461461

@@ -504,7 +504,7 @@ ngx_stream_json_log_srv_output(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
504504
static ngx_int_t
505505
ngx_stream_json_log_init_worker(ngx_cycle_t *cycle) {
506506

507-
#ifdef HTTP_JSON_LOG_KAFKA_ENABLED
507+
#if (NGX_HAVE_LIBRDKAFKA)
508508
ngx_stream_json_log_main_conf_t *conf =
509509
ngx_stream_cycle_get_module_main_conf(cycle, ngx_stream_json_log_module);
510510

0 commit comments

Comments
 (0)