Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mhowlett committed Aug 5, 2015
0 parents commit 4860950
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 0 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ngx_stub_status_prometheus

COMPLETELY NOT IMPLEMENTED!

The stub doesn't even compile yet, let alone have any functionaltiy in it.

This is an nginx module that provides access to the same information as the standard stub_status module but in a format recognized by the <a href="http://prometheus.io">prometheus</a> time-series database.

Unfortunately nginx has no dynamic module support - modules must be compiled in.

Fortunately, this isn't that hard and there is plenty of information out there to help you out.

To make things even easier, I've created a docker image (mhowlett/nginx-build-base) that provides a suitable environment and these long-winded instructions:

There is a script included in this repo called docker-up.sh which can be used to bring up the nginx build container. Before you run it, you'll need to edit it to specify the absolute path of this git repository on your system.

You'll also need to get a copy the nginx source and put it in the top level of the git repository. This can be done by running the script download-nginx.sh (it fetches version 1.9.3 and then un-tars it).

Now you're ready to go.

./docker-up.sh
cd /repo
./build.sh

to test:

/usr/sbin/nginx -c /repo/nginx.test.conf

note that --with-stub-status is specified in the included build script, but it is not a pre-requisite.

## Usage

stub_status_prometheus;

Must be placed within a location section in the nginx.config. prometheus friendly status information will be provided at that location.

Does not expose any nginx variables (c.f. stub_status which does). If you need these variables, you can use stub_status in addition to stub_status_prometheus. note that stub_status does not need to be placed in a location section, you can also put it in the server section.
18 changes: 18 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

NGINX_VERSION=1.9.3

pushd "nginx-$NGINX_VERSION"
# General configuration options are as suggested by the book:
# NGINX - A Practical Guide to High Performance
CFLAGS="-O2" ./configure \
--prefix=/usr \
--conf-path=/etc/nginx \
--add-module=../ \
--with-http_ssl_module \
--with-http_spdy_module \
--with-http_realip_module \
--with-http_stub_status_module
make
make install
popd
3 changes: 3 additions & 0 deletions config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ngx_addon_name=ngx_http_stub_status_prometheus_module
HTTP_MODULES="$HTTP_MODULES ngx_http_stub_status_prometheus_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_stub_status_prometheus_module.c"
3 changes: 3 additions & 0 deletions docker-up.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

docker run --rm -it -p 8000:8000 -v /git/ngx_stub_status_prometheus:/repo mhowlett/nginx-build-base /bin/bash
6 changes: 6 additions & 0 deletions download-nginx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

NGINX_VERSION=1.9.3

curl -s -L -O "http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz"
tar xvf nginx-$NGINX_VERSION.tar.gz
15 changes: 15 additions & 0 deletions nginx.test.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
daemon off;

events {
worker_connections 1024;
}

http {
server {
listen 8000;

location ~ ^/api/.*$ {
stub_status_prometheus;
}
}
}
97 changes: 97 additions & 0 deletions ngx_http_stub_status_prometheus_module.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

static char *ngx_http_stub_status_prometheus(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);

static ngx_command_t ngx_http_stub_status_prometheus_commands[] = {

{ ngx_string("stub_status_prometheus"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_stub_status_prometheus,
0,
0,
NULL },

ngx_null_command
};


static ngx_http_module_t ngx_http_stub_status_prometheus_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};


ngx_module_t ngx_http_stub_status_prometheus_module = {
NGX_MODULE_V1,
&ngx_http_stub_status_prometheus_module_ctx, /* module context */
ngx_http_stub_status_prometheus_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};

static ngx_int_t ngx_http_stub_status_prometheus_handler(ngx_http_request_t *r)
{
ngx_buf_t *b;
ngx_chain_t out;
ngx_str_t result_body;

if (1)
{
return NGX_DECLINED;
}

if (result_body.len == 0)
{
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}

r->headers_out.content_type.len = sizeof("application/json; charset=utf-8") - 1;
r->headers_out.content_type.data = (u_char *) "application/json; charset=utf-8";

b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));

out.buf = b;
out.next = NULL;

b->pos = result_body.data;
b->last = result_body.data + result_body.len;
b->memory = 1;
b->last_buf = 1;

r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = result_body.len;
ngx_http_send_header(r);

return ngx_http_output_filter(r, &out);
}


static char *ngx_http_stub_status_prometheus(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf;

clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_stub_status_prometheus_handler;

return NGX_CONF_OK;
}

0 comments on commit 4860950

Please sign in to comment.