Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test-build/
dev-build/
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


41 changes: 41 additions & 0 deletions hack/build-start.sh
Original file line number Diff line number Diff line change
@@ -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-chronograf" SIGINT SIGTERM SIGKILL

# Start 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 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
popd # nginx

popd # dev-build



11 changes: 11 additions & 0 deletions hack/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Well, we have an nginx module</title>
</head>
<body>
I mean, this webpage is useful.
</body>
</html>
135 changes: 135 additions & 0 deletions hack/influxdb.conf
Original file line number Diff line number Diff line change
@@ -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"


53 changes: 53 additions & 0 deletions hack/nginx.conf
Original file line number Diff line number Diff line change
@@ -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:8888;
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;
}
}
}
2 changes: 1 addition & 1 deletion integration/nginx-tests.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash

set -xeuo pipefail
module_path=${1:-../../../nginx-influxdb-module}
rm -Rf test-build
mkdir test-build
Expand Down
2 changes: 1 addition & 1 deletion src/ngx_http_influxdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
33 changes: 27 additions & 6 deletions src/ngx_http_influxdb_metric.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);

Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/ngx_http_influxdb_metric.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down