From 0010288003f99d4631b27e53c75c221249d0a579 Mon Sep 17 00:00:00 2001 From: Lorenzo Fontana Date: Fri, 13 Apr 2018 09:32:44 +0200 Subject: [PATCH 1/3] New: request time in the payload Signed-off-by: Lorenzo Fontana --- integration/nginx-tests.sh | 2 +- src/ngx_http_influxdb.c | 2 +- src/ngx_http_influxdb_metric.c | 33 +++++++++++++++++++++++++++------ src/ngx_http_influxdb_metric.h | 4 +++- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/integration/nginx-tests.sh b/integration/nginx-tests.sh index 53f44de..6df335b 100755 --- a/integration/nginx-tests.sh +++ b/integration/nginx-tests.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash - +set -xeuo pipefail module_path=${1:-../../../nginx-influxdb-module} rm -Rf test-build mkdir test-build diff --git a/src/ngx_http_influxdb.c b/src/ngx_http_influxdb.c index 82b8fb5..722a40b 100644 --- a/src/ngx_http_influxdb.c +++ b/src/ngx_http_influxdb.c @@ -97,7 +97,7 @@ static ngx_int_t ngx_http_influxdb_metrics_body_filter(ngx_http_request_t *r, return NGX_HTTP_INTERNAL_SERVER_ERROR; } - ngx_http_influxdb_metric_init(m, r, conf->server_name); + ngx_http_influxdb_metric_init(r->pool, m, r, conf->server_name); ngx_int_t pushret = ngx_http_influxdb_metric_push( r->pool, m, conf->host, conf->port, conf->measurement); diff --git a/src/ngx_http_influxdb_metric.c b/src/ngx_http_influxdb_metric.c index 540f80e..d6133b8 100644 --- a/src/ngx_http_influxdb_metric.c +++ b/src/ngx_http_influxdb_metric.c @@ -32,20 +32,40 @@ static ngx_buf_t *create_temp_char_buf(ngx_pool_t *pool, size_t size) { return b; } -void ngx_http_influxdb_metric_init(ngx_http_influxdb_metric_t *metric, +void ngx_http_influxdb_metric_init(ngx_pool_t *pool, + ngx_http_influxdb_metric_t *metric, ngx_http_request_t *req, ngx_str_t server_name) { + // request data metric->method = req->method_name; - metric->status = req->headers_out.status; metric->server_name = server_name; - metric->body_bytes_sent = req->headers_out.content_length_n; metric->connection_bytes_sent = req->connection->sent; metric->header_bytes_sent = req->header_size; metric->request_length = req->request_length; metric->extension = req->exten; metric->uri = req->uri; + // response data + metric->status = req->headers_out.status; + metric->body_bytes_sent = req->headers_out.content_length_n; metric->content_type = req->headers_out.content_type; + + // request time (how long we are dealing with the request) + ngx_time_t *tp; + ngx_msec_int_t ms; + + tp = ngx_timeofday(); + + ms = (ngx_msec_int_t)((tp->sec - req->start_sec) * 1000 + + (tp->msec - req->start_msec)); + ms = ngx_max(ms, 0); + + size_t len = sizeof(time_t); + ngx_buf_t *buf = create_temp_char_buf(pool, len); + (void)ngx_sprintf(buf->last, "%T.%03M", (time_t)ms / 1000, ms % 1000); + + metric->request_time.data = buf->last; + metric->request_time.len = len; } ngx_int_t ngx_http_influxdb_metric_push(ngx_pool_t *pool, @@ -61,7 +81,8 @@ ngx_int_t ngx_http_influxdb_metric_push(ngx_pool_t *pool, sizeof(",request_length=") - 1 + NGX_INT_T_LEN + sizeof(",uri=") - 1 + sizeof(m->uri) + sizeof(",extension=") - 1 + sizeof(m->extension) + sizeof(",content_type=") - 1 + - sizeof(m->content_type); + sizeof(m->content_type) + sizeof(time_t) - 1 + + sizeof(",request_time="); ngx_buf_t *buf = create_temp_char_buf(pool, len); @@ -70,11 +91,11 @@ ngx_int_t ngx_http_influxdb_metric_push(ngx_pool_t *pool, "method=\"%V\",status=%i,connection_bytes_sent=%O,body_" "bytes_sent=%O,header_" "bytes_sent=%z,request_length=%O,uri=\"%V\",extension=\"%" - "V\",content_type=\"%V\"", + "V\",content_type=\"%V\",request_time=\"%V\"", &measurement, &m->server_name, &m->method, m->status, m->connection_bytes_sent, m->body_bytes_sent, m->header_bytes_sent, m->request_length, &m->uri, - &m->extension, &m->content_type); + &m->extension, &m->content_type, &m->request_time); struct sockaddr_in servaddr; int sockfd = socket(AF_INET, SOCK_DGRAM, 0); diff --git a/src/ngx_http_influxdb_metric.h b/src/ngx_http_influxdb_metric.h index 00503a0..ca5c0e6 100644 --- a/src/ngx_http_influxdb_metric.h +++ b/src/ngx_http_influxdb_metric.h @@ -18,9 +18,11 @@ typedef struct { ngx_str_t uri; ngx_str_t extension; ngx_str_t content_type; + ngx_str_t request_time; } ngx_http_influxdb_metric_t; -void ngx_http_influxdb_metric_init(ngx_http_influxdb_metric_t *metric, +void ngx_http_influxdb_metric_init(ngx_pool_t *pool, + ngx_http_influxdb_metric_t *metric, ngx_http_request_t *req, ngx_str_t server_name); From aec7af9f420d709b524662b7c44f1291928a02a8 Mon Sep 17 00:00:00 2001 From: Lorenzo Fontana Date: Sun, 15 Apr 2018 15:52:48 +0200 Subject: [PATCH 2/3] Local development scripts Signed-off-by: Lorenzo Fontana --- .gitignore | 1 + hack/build-start.sh | 41 +++++++++++++ hack/html/index.html | 11 ++++ hack/influxdb.conf | 135 +++++++++++++++++++++++++++++++++++++++++++ hack/nginx.conf | 53 +++++++++++++++++ 5 files changed, 241 insertions(+) create mode 100755 hack/build-start.sh create mode 100644 hack/html/index.html create mode 100644 hack/influxdb.conf create mode 100644 hack/nginx.conf diff --git a/.gitignore b/.gitignore index 2a2abe0..c71682d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ test-build/ +dev-build/ diff --git a/hack/build-start.sh b/hack/build-start.sh new file mode 100755 index 0000000..96cc3a9 --- /dev/null +++ b/hack/build-start.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +set -xeuo pipefail +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +module_path=$DIR/../ +mkdir -p dev-build +pushd dev-build +if [ -d nginx ]; then + pushd nginx + git clean -f + git reset --hard + git pull +else + git clone --depth 1 https://github.com/nginx/nginx.git + pushd nginx +fi + +cp auto/configure . +./configure --add-module=$module_path +make -j +rm -Rf /tmp/nginx-test-current +mkdir -p /tmp/nginx-test-current/logs +cp -r conf /tmp/nginx-test-current/conf +cp -r $module_path/hack/html /tmp/nginx-test-current/html +cp $module_path/hack/nginx.conf /tmp/nginx-test-current/conf/nginx.conf + +trap "docker rm -f test-nginx-influxdb test-nginx-cats" SIGINT SIGTERM SIGKILL + +# Start influxdb +docker run -d --name test-nginx-influxdb -p 8086:8086 -p 8089:8089/udp -v $DIR/influxdb.conf:/etc/influxdb/influxdb.conf:ro influxdb + +# Start an application to test the proxy pass +docker run -d --name test-nginx-cats -p 8081:8080 docker.io/fntlnz/caturday + +# Start nginx +./objs/nginx -p /tmp/nginx-test-current -c conf/nginx.conf +popd # nginx + +popd # dev-build + + + diff --git a/hack/html/index.html b/hack/html/index.html new file mode 100644 index 0000000..41bb2dc --- /dev/null +++ b/hack/html/index.html @@ -0,0 +1,11 @@ + + + + + + Well, we have an nginx module + + + I mean, this webpage is useful. + + diff --git a/hack/influxdb.conf b/hack/influxdb.conf new file mode 100644 index 0000000..6e9c6de --- /dev/null +++ b/hack/influxdb.conf @@ -0,0 +1,135 @@ +reporting-disabled = false +bind-address = "127.0.0.1:8088" + +[meta] + dir = "/var/lib/influxdb/meta" + retention-autocreate = true + logging-enabled = true + +[data] + dir = "/var/lib/influxdb/data" + index-version = "inmem" + wal-dir = "/var/lib/influxdb/wal" + wal-fsync-delay = "0s" + query-log-enabled = true + cache-max-memory-size = 1073741824 + cache-snapshot-memory-size = 26214400 + cache-snapshot-write-cold-duration = "10m0s" + compact-full-write-cold-duration = "4h0m0s" + max-series-per-database = 1000000 + max-values-per-tag = 100000 + max-concurrent-compactions = 0 + trace-logging-enabled = false + +[coordinator] + write-timeout = "10s" + max-concurrent-queries = 0 + query-timeout = "0s" + log-queries-after = "0s" + max-select-point = 0 + max-select-series = 0 + max-select-buckets = 0 + +[retention] + enabled = true + check-interval = "30m0s" + +[shard-precreation] + enabled = true + check-interval = "10m0s" + advance-period = "30m0s" + +[monitor] + store-enabled = true + store-database = "_internal" + store-interval = "10s" + +[subscriber] + enabled = true + http-timeout = "30s" + insecure-skip-verify = false + ca-certs = "" + write-concurrency = 40 + write-buffer-size = 1000 + +[http] + enabled = true + bind-address = ":8086" + auth-enabled = false + log-enabled = true + write-tracing = false + pprof-enabled = true + https-enabled = false + https-certificate = "/etc/ssl/influxdb.pem" + https-private-key = "" + max-row-limit = 0 + max-connection-limit = 0 + shared-secret = "" + realm = "InfluxDB" + unix-socket-enabled = false + bind-socket = "/var/run/influxdb.sock" + max-body-size = 25000000 + +[ifql] + enabled = false + log-enabled = true + bind-address = ":8082" + +[[graphite]] + enabled = false + bind-address = ":2003" + database = "graphite" + retention-policy = "" + protocol = "tcp" + batch-size = 5000 + batch-pending = 10 + batch-timeout = "1s" + consistency-level = "one" + separator = "." + udp-read-buffer = 0 + +[[collectd]] + enabled = false + bind-address = ":25826" + database = "collectd" + retention-policy = "" + batch-size = 5000 + batch-pending = 10 + batch-timeout = "10s" + read-buffer = 0 + typesdb = "/usr/share/collectd/types.db" + security-level = "none" + auth-file = "/etc/collectd/auth_file" + parse-multivalue-plugin = "split" + +[[opentsdb]] + enabled = false + bind-address = ":4242" + database = "opentsdb" + retention-policy = "" + consistency-level = "one" + tls-enabled = false + certificate = "/etc/ssl/influxdb.pem" + batch-size = 1000 + batch-pending = 5 + batch-timeout = "1s" + log-point-errors = true + +[[udp]] + enabled = true + bind-address = ":8089" + database = "testmod" + retention-policy = "" + batch-size = 5000 + batch-pending = 10 + read-buffer = 0 + batch-timeout = "1s" + precision = "" + +[continuous_queries] + log-enabled = true + enabled = true + query-stats-enabled = false + run-interval = "1s" + + diff --git a/hack/nginx.conf b/hack/nginx.conf new file mode 100644 index 0000000..44490e3 --- /dev/null +++ b/hack/nginx.conf @@ -0,0 +1,53 @@ +worker_processes 1; daemon off; master_process off; + +events { + worker_connections 1024; +} + +http { + include mime.types; + default_type application/octet-stream; + client_body_temp_path /tmp; + proxy_temp_path /tmp; + fastcgi_temp_path /tmp; + uwsgi_temp_path /tmp; + scgi_temp_path /tmp; + error_log /tmp/error.log; + sendfile on; + #tcp_nopush on; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + access_log /tmp/access.log main; + + keepalive_timeout 65; + + gzip on; + + server { + influxdb server_name=myserver host=127.0.0.1 port=8089 measurement=static enabled=true; + listen 8080; + server_name _; + + location / { + root html; + index index.html index.htm; + } + } + + server { + influxdb server_name=myserver host=127.0.0.1 port=8089 measurement=caturday enabled=true; + listen 8082; + server_name _; + location / { + proxy_pass http://127.0.0.1:8081; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-for $remote_addr; + port_in_redirect off; + proxy_connect_timeout 300; + } + } +} From 20a3c3736e7e640fda29763def8364eae8f42a02 Mon Sep 17 00:00:00 2001 From: Lorenzo Fontana Date: Sun, 15 Apr 2018 16:39:03 +0200 Subject: [PATCH 3/3] Fix format unit Signed-off-by: Lorenzo Fontana --- README.md | 24 ++++++++++++++++++++++++ hack/build-start.sh | 8 ++++---- hack/nginx.conf | 2 +- src/ngx_http_influxdb_metric.c | 2 +- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 48d75ce..6e3e093 100644 --- a/README.md +++ b/README.md @@ -103,3 +103,27 @@ http { ``` + +### Local Testing + +To test changes locally, there's a script in `hack/` called `build-start.sh` that can +build the local changes against the current nginx source code taken from github. + +To run, the script also requires docker in order to start influxdb, chronograf and other testing proxy backends + +```bash +./hack/build-start.sh +``` + +- You can reach chronograf (through nginx) at `http://127.0.0.1:8082/` +- You can reach chronograf (directly) at `http://127.0.0.1:8888/` +- A static hello world page can be reached at `http://127.0.0.1:8080` + + +You can access the influxdb instance CLI via: + +```bash + docker exec -it test-nginx-influxdb influx +``` + + diff --git a/hack/build-start.sh b/hack/build-start.sh index 96cc3a9..777b549 100755 --- a/hack/build-start.sh +++ b/hack/build-start.sh @@ -23,13 +23,13 @@ cp -r conf /tmp/nginx-test-current/conf cp -r $module_path/hack/html /tmp/nginx-test-current/html cp $module_path/hack/nginx.conf /tmp/nginx-test-current/conf/nginx.conf -trap "docker rm -f test-nginx-influxdb test-nginx-cats" SIGINT SIGTERM SIGKILL +trap "docker rm -f test-nginx-influxdb test-nginx-chronograf" SIGINT SIGTERM SIGKILL # Start influxdb -docker run -d --name test-nginx-influxdb -p 8086:8086 -p 8089:8089/udp -v $DIR/influxdb.conf:/etc/influxdb/influxdb.conf:ro influxdb +docker run -d --name test-nginx-influxdb -p 8888:8888 -p 8086:8086 -p 8089:8089/udp -v $DIR/influxdb.conf:/etc/influxdb/influxdb.conf:ro influxdb -# Start an application to test the proxy pass -docker run -d --name test-nginx-cats -p 8081:8080 docker.io/fntlnz/caturday +# Start chronograf +docker run --network container:test-nginx-influxdb --name test-nginx-chronograf -d chronograf # Start nginx ./objs/nginx -p /tmp/nginx-test-current -c conf/nginx.conf diff --git a/hack/nginx.conf b/hack/nginx.conf index 44490e3..8d2827b 100644 --- a/hack/nginx.conf +++ b/hack/nginx.conf @@ -42,7 +42,7 @@ http { listen 8082; server_name _; location / { - proxy_pass http://127.0.0.1:8081; + proxy_pass http://127.0.0.1:8888; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-for $remote_addr; diff --git a/src/ngx_http_influxdb_metric.c b/src/ngx_http_influxdb_metric.c index d6133b8..0b1ffcb 100644 --- a/src/ngx_http_influxdb_metric.c +++ b/src/ngx_http_influxdb_metric.c @@ -91,7 +91,7 @@ ngx_int_t ngx_http_influxdb_metric_push(ngx_pool_t *pool, "method=\"%V\",status=%i,connection_bytes_sent=%O,body_" "bytes_sent=%O,header_" "bytes_sent=%z,request_length=%O,uri=\"%V\",extension=\"%" - "V\",content_type=\"%V\",request_time=\"%V\"", + "V\",content_type=\"%V\",request_time=%V", &measurement, &m->server_name, &m->method, m->status, m->connection_bytes_sent, m->body_bytes_sent, m->header_bytes_sent, m->request_length, &m->uri,