Skip to content

Commit

Permalink
add prometheus output
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMeier committed Aug 5, 2017
1 parent 1bf865e commit 943e037
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
.cproject
.vscode
test/test.log
test/test.output
test/test.output*
mod_result_status_counter.so
autom4te.cache
Makefile
Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ __________________________

[![Build Status](https://travis-ci.org/TheMeier/mod_result_status_counter.svg?branch=master)](https://travis-ci.org/TheMeier/mod_result_status_counter)

This apache module counts the result status of each request handled by apache httpd. The counters can be fetched via a module_hablder wicht outputs a simple json array. This is hand to do some monitoruing.
This apache module counts the result status of each request handled by apache httpd. The counters can be fetched via a module_hablder wicht outputs a simple json array or prometheus text output if you use query param prometheus. This is handy to do some monitoring.

To use compile with apxs, add a location handler an LoadModule directive to your config:

Expand All @@ -14,6 +14,25 @@ LoadModule result_status_counter_module modules/mod_result_status_counter.so
</Location>
```

```shell
~ curl http://localhost/mrsc
[
{ "100 Continue": 0 },
{ "101 Switching Protocols": 0 },
{ "102 Processing": 0 },
{ "200 OK": 10 },
...

~ curl http://localhost/mrsc?prometheus
# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{status="100 Continue"} 0
http_requests_total{status="101 Switching Protocols"} 0
http_requests_total{status="102 Processing"} 0
http_requests_total{status="200 OK"} 11
...
```
FIXME:
I could not find a way to translate the apache result-array indices back to status codes and sadly status_lines is not exortet in http_protocol.h so for now in copy&pasted the definition of status_lines[]
Expand Down
9 changes: 8 additions & 1 deletion debian/changelog
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
libapache2-mod-result-status-counter (1.0.1-1) unstable; urgency=medium
libapache2-mod-result-status-counter (1.1.0-1) unstable; urgency=medium

* add simple text only prometheus output
* add test

-- Christoph Maser <[email protected]> Sat, 05 Sep 2017 09:55:23 +0200

libapache2-mod-result-status-counter (1.0.1-1) unstable; urgency=medium

* update response code array

Expand Down
32 changes: 24 additions & 8 deletions mod_result_status_counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,19 +289,35 @@ static int mrsc_handler(request_rec *r)
return DECLINED;
}

r->content_type = "text/json";
base = (mrsc_data *)apr_shm_baseaddr_get(mrsc_shm);

if (!r->header_only) {
ap_rputs("[\n", r);
for (i = 0; i < RESPONSE_CODES; ++i) {
if (status_lines[i] == '\0') {
ap_rprintf(r, "\t{ \"%s apache code %d\": %d },\n", "unknown", i ,base->request_status[i]);
} else {
ap_rprintf(r, "\t{ \"%s\": %d },\n", status_lines[i] ,base->request_status[i]);
if (r->args) {
if (ap_strstr_c(r->args, "prometheus")) {
r->content_type = "text/plain; version=0.0.4";
ap_rputs("# HELP http_requests_total The total number of HTTP requests.\n",r);
ap_rputs("# TYPE http_requests_total counter\n",r);
for (i = 0; i < RESPONSE_CODES; ++i) {
if (status_lines[i] == '\0') {
ap_rprintf(r, "http_requests_total{status=\"%s apache code %d\"} %d\n", "unknown", i ,base->request_status[i]);
} else {
ap_rprintf(r, "http_requests_total{status=\"%s\"} %d\n", status_lines[i] ,base->request_status[i]);
}
}
}
} else {
r->content_type = "text/json";

ap_rputs("[\n", r);
for (i = 0; i < RESPONSE_CODES; ++i) {
if (status_lines[i] == '\0') {
ap_rprintf(r, "\t{ \"%s apache code %d\": %d },\n", "unknown", i ,base->request_status[i]);
} else {
ap_rprintf(r, "\t{ \"%s\": %d },\n", status_lines[i] ,base->request_status[i]);
}
}
ap_rputs("]", r);
}
ap_rputs("]", r);

}
return OK;
Expand Down
4 changes: 4 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ curl -o test.output -q http://localhost:8080/mrsc
grep '{ "200 OK": 10 }' test.output
grep '{ "404 Not Found": 10 }' test.output

curl -o test.output2 http://localhost:8080/mrsc?prometheus

grep 'http_requests_total{status="200 OK"} 11' test.output2
grep 'http_requests_total{status="404 Not Found"} 10' test.output2

0 comments on commit 943e037

Please sign in to comment.