diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..cca2563 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,32 @@ +language: c + +compilers: + - clang + - gcc + +env: + # Mainline + - X_NGINX_VERSION=1.13.0 + # Stable + - X_NGINX_VERSION=1.12.0 + # Previous stable + - X_NGINX_VERSION=1.10.3 + +sudo: false +addons: + apt: + packages: + - libpcre3-dev + - libssl-dev +cache: + apt: true + +install: + - wget -O - http://nginx.org/download/nginx-${X_NGINX_VERSION}.tar.gz | tar -xzf - + - git clone https://github.com/trungtm/nginx-statsd + +script: + - cd nginx-${X_NGINX_VERSION} + - ./configure --with-debug --add-module=../nginx-statsd + - make + - objs/nginx -V diff --git a/README.mkd b/README.mkd index 222865a..8b067ee 100644 --- a/README.mkd +++ b/README.mkd @@ -1,5 +1,6 @@ nginx-statsd ============ +[![Build Status](https://travis-ci.org/trungtm/nginx-statsd.svg?branch=master)](https://travis-ci.org/trungtm/nginx-statsd) An nginx module for sending statistics to statsd. diff --git a/config b/config index 529a78e..54a715f 100644 --- a/config +++ b/config @@ -1,4 +1,16 @@ ngx_addon_name=ngx_http_statsd -HTTP_MODULES="$HTTP_MODULES ngx_http_statsd_module" -NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_statsd.c" -CORE_LIBS="$CORE_LIBS -lssl" + +if test -n "$ngx_module_link"; then + ngx_module_type=HTTP + ngx_module_name=ngx_http_statsd_module + ngx_module_srcs="$ngx_addon_dir/ngx_http_statsd.c" + + . auto/module +else + HTTP_MODULES="$HTTP_MODULES ngx_http_statsd_module" + NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_statsd.c" + CORE_LIBS="$CORE_LIBS -lssl" +fi + +USE_OPENSSL=YES + diff --git a/ngx_http_statsd.c b/ngx_http_statsd.c index c8ec856..2e914a3 100644 --- a/ngx_http_statsd.c +++ b/ngx_http_statsd.c @@ -60,7 +60,6 @@ typedef struct { ngx_array_t *stats; } ngx_http_statsd_conf_t; -ngx_int_t ngx_udp_connect(ngx_resolver_connection_t *rec); static void ngx_statsd_updater_cleanup(void *data); static ngx_int_t ngx_http_statsd_udp_send(ngx_udp_endpoint_t *l, u_char *buf, size_t len); @@ -350,6 +349,89 @@ static void ngx_http_statsd_udp_dummy_handler(ngx_event_t *ev) { } +static ngx_int_t +ngx_http_statsd_udp_connect(ngx_resolver_connection_t *rec) +{ + int rc; + ngx_int_t event; + ngx_event_t *rev, *wev; + ngx_socket_t s; + ngx_connection_t *c; + + s = ngx_socket(rec->sockaddr->sa_family, SOCK_DGRAM, 0); + + ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &rec->log, 0, "UDP socket %d", s); + + if (s == (ngx_socket_t) -1) { + ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno, + ngx_socket_n " failed"); + return NGX_ERROR; + } + + c = ngx_get_connection(s, &rec->log); + + if (c == NULL) { + if (ngx_close_socket(s) == -1) { + ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno, + ngx_close_socket_n "failed"); + } + + return NGX_ERROR; + } + + if (ngx_nonblocking(s) == -1) { + ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno, + ngx_nonblocking_n " failed"); + + goto failed; + } + + rev = c->read; + wev = c->write; + + rev->log = &rec->log; + wev->log = &rec->log; + + rec->udp = c; + + c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1); + + ngx_log_debug3(NGX_LOG_DEBUG_EVENT, &rec->log, 0, + "connect to %V, fd:%d #%uA", &rec->server, s, c->number); + + rc = connect(s, rec->sockaddr, rec->socklen); + + /* TODO: iocp */ + + if (rc == -1) { + ngx_log_error(NGX_LOG_CRIT, &rec->log, ngx_socket_errno, + "connect() failed"); + + goto failed; + } + + /* UDP sockets are always ready to write */ + wev->ready = 1; + + event = (ngx_event_flags & NGX_USE_CLEAR_EVENT) ? + /* kqueue, epoll */ NGX_CLEAR_EVENT: + /* select, poll, /dev/poll */ NGX_LEVEL_EVENT; + /* eventport event type has no meaning: oneshot only */ + + if (ngx_add_event(rev, NGX_READ_EVENT, event) != NGX_OK) { + goto failed; + } + + return NGX_OK; + +failed: + + ngx_close_connection(c); + rec->udp = NULL; + + return NGX_ERROR; +} + static ngx_int_t ngx_http_statsd_udp_send(ngx_udp_endpoint_t *l, u_char *buf, size_t len) { @@ -364,7 +446,7 @@ ngx_http_statsd_udp_send(ngx_udp_endpoint_t *l, u_char *buf, size_t len) rec->log.data = NULL; rec->log.action = "logging"; - if(ngx_udp_connect(rec) != NGX_OK) { + if(ngx_http_statsd_udp_connect(rec) != NGX_OK) { if(rec->udp != NULL) { ngx_free_connection(rec->udp); rec->udp = NULL; @@ -747,4 +829,4 @@ ngx_escape_statsd_key(u_char *dst, u_char *src, size_t size) } return (uintptr_t) dst; -} +} \ No newline at end of file