Giter Club home page Giter Club logo

kong-plugin-prometheus's Introduction

kong-prometheus-plugin

Widely inspired by nginx-lua-prometheus NGINX module and the Kong StatsD plugin.

Exposes API metrics for Prometheus monitoring system.


Installation

Install the luarocks utility on your system then install it by doing:

$ luarocks install kong-plugin-prometheus

Configuration

Configuring the plugin is straightforward, you can add it on top of an API by executing the following request on your Kong server:

$ curl -X POST http://kong:8001/apis/{api}/plugins \
    --data "name=prometheus"

api: The id or name of the API that this plugin configuration will target

You can also apply it for every API using the http://kong:8001/plugins/ endpoint.

parameter default description
name The name of the plugin to use, in this case: prometheus
config.metrics
optional
All metrics
are logged
List of Metrics to be logged. Available values are described under Metrics.
config.dict_name
optional
kong_cache The name of the nginx shared dictionary which will be used to store all metrics.
config.prefix
optional
kong String to be prefixed to each metric's name.

If you use a custom shared dictionary to store the metrics, you need to create a kong configuration template including the shared dictionary:

lua_shared_dict prometheus_metrics 10M;

Metrics

The plugin exposes is metrics on kong's admin endpoint (port 8001 by default) on the path /prometheus/metrics.

Metrics the plugin can expose in the prometheus format:

Metric description namespace
http_requests_total tracks api request kong_http_requests_total
http_request_size_bytes tracks api request's body size in bytes kong_http_request_size_bytes
http_response_size_bytes tracks api response's body size in bytes kong_http_response_size_bytes
http_request_duration_ms tracks the time interval between the request started and response received from the upstream server kong_http_latency
http_upstream_duration_ms tracks the time it took for the final service to process the request kong_http_upstream_latency
http_kong_duration_ms tracks the internal Kong latency that it took to run all the plugins kong_http_kong_latency

Metric Fields

Plugin can be configured with any combination of Metrics, with each entry containing the following fields.

Field description allowed values
name Prometheus metric's name Metrics
stat_type determines what sort of event the metric represents gauge, counter and histogram

Kong Process Errors

This logging plugin will only log HTTP request and response data. If you are looking for the Kong process error file (which is the nginx error file), then you can find it at the following path: prefix/logs/error.log

Built-in metrics

The module increments the kong_metric_errors_total metric if it encounters an error (for example, when lua_shared_dict becomes full). You might want to configure an alert on that metric.

Caveats

Please keep in mind that all metrics stored by this library are kept in a single shared dictionary (lua_shared_dict). By default, we use the kong_cache shared dict so if you are using it remember to allow extra space to it. While exposing metrics the module has to list all dictionary keys, which has serious performance implications for dictionaries with large number of keys (in this case this means large number of metrics OR metrics with high label cardinality). Listing the keys has to lock the dictionary, which blocks all threads that try to access it (i.e. potentially all nginx worker threads).

There is no elegant solution to this issue (besides keeping metrics in a separate storage system external to nginx), so for latency-critical servers you might want to keep the number of metrics (and distinct metric label values) to a minimum.

Credits

  • Adapted and maintained by Yoann Ciabaud @yciabaud
  • ❤️ kindly improved and tested by @jayhding

Kong StatsD plugin

nginx-lua-prometheus

All prometheus implementation credits goes to nginx-lua-prometheus

  • Created and maintained by Anton Tolchanov (@knyar)
  • Metrix prefix support contributed by david birdsong (@davidbirdsong)
  • Gauge support contributed by Cosmo Petrich (@cosmopetrich)

kong-plugin-prometheus's People

Contributors

jayhding avatar ms2008 avatar yciabaud avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

kong-plugin-prometheus's Issues

Publish to luarocks

When releasing the project we will need to publish it to luarocks in order to make it available easyly for everyone.

Questions on functionality

So this plugin exposes metrics for Prometheus to pull correct? Does it expose the metrics on top of each api at a specific mykongproxy.com/my/proxy/endpoint/prometheus/metrics or something of that sort? As a WIP what is left to work out? My team is interested in Prometheus for Kong and may end up working on something similar.

error when run with kong 0.13

When run this plugin in kong 0.13, report error below

2018/05/28 04:00:07 [debug] 45#0: *5121028 [lua] logger.lua:81: log(): Prometheus: logging metrics...
2018/05/28 04:00:07 [error] 45#0: *5121028 lua entry thread aborted: runtime error: /usr/local/share/lua/5.1/kong/plugins/prometheus/logger.lua:87: bad argument #1 to 'string_gsub' (string expected, got nil)
stack traceback:
coroutine 0:
[C]: in function 'string_gsub'
/usr/local/share/lua/5.1/kong/plugins/prometheus/logger.lua:87: in function 'log'
.../local/share/lua/5.1/kong/plugins/prometheus/handler.lua:21: in function <.../local/share/lua/5.1/kong/plugins/prometheus/handler.lua:16>, context: ngx.timer, client: 192.168.141.0, server: 0.0.0.0:8000
2018/05/28 04:00:07 [info] 45#0: *5121026 client 192.168.141.0 closed keepalive connection

"WIP - not ready yet"

Possible to create a milestone to track what is needed to make it ready for a release?

I can help out as well as this is pretty important for us.

Load test the plugin

Load test the plugin to see if there are database / caching / performance issues

http_upstream_duration_ms_histogram_sum value is -1

After taking plugin version v0.1.1 into use I have seen below errors from Kong container log.

06/06/2018 07:55:192018/06/06 07:55:19 [error] 51#0: *2715 [lua] prometheus.lua:296: log_error(): Error while setting 'http_upstream_duration_ms_histogram_sum{api="XXXX"}' to '-1': 'Value should not be negative', context: ngx.timer, client: 10.130.100.241, server: 0.0.0.0:8000
06/06/2018 07:55:1910.130.100.241 - - [06/Jun/2018:07:55:19 +1000] "GET /health HTTP/1.1" 200 5 "-" "ELB-HealthChecker/2.0"
06/06/2018 07:55:192018/06/06 07:55:19 [error] 51#0: *2717 [lua] prometheus.lua:296: log_error(): Error while setting 'http_upstream_duration_ms_histogram_sum{api="XXXX"}' to '-1': 'Value should not be negative', context: ngx.timer, client: 10.130.100.241, server: 0.0.0.0:8000
06/06/2018 07:55:1910

Seems it only happens on one specific api, and kong_http_request_duration_ms_gauge for that api is always 0

I have not checked deeply why it happened, just wondering first if you have seen this as well.

The latency metrics is incorrect

By default, nginx-lua-prometheus use a floating-point number for the elapsed time in seconds (including milliseconds as the decimal part). This place:

-- Default set of latency buckets, 5ms to 10s:
local DEFAULT_BUCKETS = {0.005, 0.01, 0.02, 0.03, 0.05, 0.075, 0.1, 0.2, 0.3,
0.4, 0.5, 0.75, 1, 1.5, 2, 3, 4, 5, 10}

But Kong use an interger precision in milliseconds. If we pass it to nginx-lua-prometheus directly, the latency accuracy is lost.

There came up with two solutions:

  1. Define custom buckets
  2. Divided the Kong latency by 1000

attempt to index global 'metrics' (a nil value)

I successfully added the plugin, however I seem to be getting this on /prometheus/metrics:

15/03/2018 10:11:112018/03/15 10:11:11 [error] 51#0: *3482 lua coroutine: runtime error: ...plugins/share/lua/5.1/kong/plugins/prometheus/logger.lua:21: attempt to index global 'metrics' (a nil value)
15/03/2018 10:11:11stack traceback:
15/03/2018 10:11:11coroutine 0:
15/03/2018 10:11:11	...plugins/share/lua/5.1/kong/plugins/prometheus/logger.lua: in function 'update_metric'
15/03/2018 10:11:11	...plugins/share/lua/5.1/kong/plugins/prometheus/logger.lua:144: in function 'logAdmin'
15/03/2018 10:11:11	...om-plugins/share/lua/5.1/kong/plugins/prometheus/api.lua:16: in function <...om-plugins/share/lua/5.1/kong/plugins/prometheus/api.lua:6>
15/03/2018 10:11:11coroutine 1:
15/03/2018 10:11:11	[C]: in function 'resume'
15/03/2018 10:11:11	/usr/local/share/lua/5.1/lapis/application.lua:393: in function 'handler'
15/03/2018 10:11:11	/usr/local/share/lua/5.1/lapis/application.lua:130: in function 'resolve'
15/03/2018 10:11:11	/usr/local/share/lua/5.1/lapis/application.lua:161: in function </usr/local/share/lua/5.1/lapis/application.lua:159>
15/03/2018 10:11:11	[C]: in function 'xpcall'
15/03/2018 10:11:11	/usr/local/share/lua/5.1/lapis/application.lua:159: in function 'dispatch'
15/03/2018 10:11:11	/usr/local/share/lua/5.1/lapis/nginx.lua:215: in function 'serve_admin_api'
15/03/2018 10:11:11	content_by_lua(nginx-kong.conf:150):2: in function <content_by_lua(nginx-kong.conf:150):1>, client: 10.42.150.236, server: kong_admin, request: "GET /prometheus/metrics HTTP/1.1", host: "api-admin.dev.mydomain.com"
15/03/2018 10:11:112018/03/15 10:11:11 [error] 51#0: *3482 [lua] init.lua:83: handle_error(): /usr/local/share/lua/5.1/lapis/application.lua:397: ...plugins/share/lua/5.1/kong/plugins/prometheus/logger.lua:21: attempt to index global 'metrics' (a nil value)
15/03/2018 10:11:11stack traceback:
15/03/2018 10:11:11	...plugins/share/lua/5.1/kong/plugins/prometheus/logger.lua: in function 'update_metric'
15/03/2018 10:11:11	...plugins/share/lua/5.1/kong/plugins/prometheus/logger.lua:144: in function 'logAdmin'
15/03/2018 10:11:11	...om-plugins/share/lua/5.1/kong/plugins/prometheus/api.lua:16: in function <...om-plugins/share/lua/5.1/kong/plugins/prometheus/api.lua:6>
15/03/2018 10:11:11stack traceback:
15/03/2018 10:11:11	[C]: in function 'error'
15/03/2018 10:11:11	/usr/local/share/lua/5.1/lapis/application.lua:397: in function 'handler'
15/03/2018 10:11:11	/usr/local/share/lua/5.1/lapis/application.lua:130: in function 'resolve'
15/03/2018 10:11:11	/usr/local/share/lua/5.1/lapis/application.lua:161: in function </usr/local/share/lua/5.1/lapis/application.lua:159>
15/03/2018 10:11:11	[C]: in function 'xpcall'
15/03/2018 10:11:11	/usr/local/share/lua/5.1/lapis/application.lua:159: in function 'dispatch'
15/03/2018 10:11:11	/usr/local/share/lua/5.1/lapis/nginx.lua:215: in function 'serve_admin_api'
15/03/2018 10:11:11	content_by_lua(nginx-kong.conf:150):2: in function <content_by_lua(nginx-kong.conf:150):1>, client: 10.42.150.236, server: kong_admin, request: "GET /prometheus/metrics HTTP/1.1", host: "api-admin.dev.mydomain.com"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.