Skip to content

Instantly share code, notes, and snippets.

@knabben
Created April 11, 2018 12:14
Show Gist options
  • Save knabben/59129696e331178dadb9e3c18d6ad157 to your computer and use it in GitHub Desktop.
Save knabben/59129696e331178dadb9e3c18d6ad157 to your computer and use it in GitHub Desktop.
NGINX Prometheus
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_event.h>
static char *ngx_http_skill(ngx_conf_t *cf, void *post, void *data);
static ngx_conf_post_handler_pt ngx_http_skill_p = ngx_http_skill;
typedef struct {
ngx_str_t name;
} ngx_http_skill_loc_conf_t;
static void *
ngx_http_skill_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_skill_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_skill_loc_conf_t));
if (conf == NULL) {
return NULL;
}
return conf;
}
static ngx_command_t ngx_http_skill_commands[] = {
{ ngx_string("skill"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_skill_loc_conf_t, name),
&ngx_http_skill_p},
ngx_null_command
};
static ngx_http_module_t ngx_http_skill_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_skill_create_loc_conf, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_skill_module = {
NGX_MODULE_V1,
&ngx_http_skill_module_ctx, /* module context */
ngx_http_skill_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_skill_handler(ngx_http_request_t *r)
{
size_t size;
ngx_int_t rc;
ngx_buf_t *b;
ngx_chain_t out;
ngx_atomic_int_t ac, hn, ap;
if (!(r->method & (NGX_HTTP_GET))) {
return NGX_HTTP_NOT_ALLOWED;
}
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
}
r->headers_out.content_type_len = sizeof("text/plain") - 1;
ngx_str_set(&r->headers_out.content_type, "text/plain");
r->headers_out.content_type_lowcase = NULL;
size = sizeof("# TYPE active gauge\nactive ") + NGX_ATOMIC_T_LEN +
sizeof("# TYPE accepted gauge\naccepted ") + NGX_ATOMIC_T_LEN +
sizeof("# TYPE handled gauge\nhandled ") + NGX_ATOMIC_T_LEN;
;
b = ngx_create_temp_buf(r->pool, size);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
out.buf = b;
out.next = NULL;
ac = *ngx_stat_active;
hn = *ngx_stat_handled;
ap = *ngx_stat_accepted;
b->last = ngx_sprintf(b->last, "# TYPE accepted gauge\naccepted %uA\n", ap);
b->last = ngx_sprintf(b->last, "# TYPE handled gauge\nhandled %uA\n", hn);
b->last = ngx_sprintf(b->last, "# TYPE active gauge\nactive %uA\n", ac);
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = b->last - b->pos;
b->last_buf = (r == r->main) ? 1 : 0;
b->last_in_chain = 1;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
return ngx_http_output_filter(r, &out);
}
static char *
ngx_http_skill(ngx_conf_t *cf, void *post, void *data)
{
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_skill_handler;
return NGX_CONF_OK;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment