kong+elasticsearch监控所有请求
Kong文章系列
上面是写了一些Kong基础使用的文章,如果对Kong还不太熟悉可以参考阅读。
本篇文章的内容是使用elasticsearch记录Kong的所有请求,最终目的是通过elasticsearch + kibana对所有请求做进一步分析,不过这篇文章暂时没有kibana部分,因为笔者还没有研究,所以这篇文章主要是将kong的请求信息转存到elasticsearch.
目标
- 通过Kong提供的插件【HTTP Log】,将指定的Service或者Route的请求转发到指定的HTTP URL
- 构建Web服务,接收Kong转发的请求信息,并附加其他信息保存(如果是对Kong比较熟,可以直接定制HTTP Log插件)
- 将构建的请求信息保存到elsticsearch
- 通过kibana对数据进行分析
原理
操作步骤
配置HttpLog插件
给指定的service配置http-log
curl -X POST http://localhost:8001/services/c9beb237-7e85-49b3-8561-0082fc8086ea/plugins \
--data "name=http-log" \
--data "config.http_endpoint=http://172.11.22.10:10150/http_log/post" \ # 配置的代理地址
--data "config.method=POST" \
--data "config.timeout=1000" \
--data "config.keepalive=1000"
请求发送的json数据为
{
"request": {
"method": "GET",
"uri": "/get",
"url": "http://httpbin.org:8000/get",
"size": "75",
"querystring": {
},
"headers": {
"accept": "*/*",
"host": "httpbin.org",
"user-agent": "curl/7.37.1"
},
"tls": {
"version": "TLSv1.2",
"cipher": "ECDHE-RSA-AES256-GCM-SHA384",
"supported_client_ciphers": "ECDHE-RSA-AES256-GCM-SHA384",
"client_verify": "NONE"
}
},
"upstream_uri": "/",
"response": {
"status": 200,
"size": "434",
"headers": {
"Content-Length": "197",
"via": "kong/0.3.0",
"Connection": "close",
"access-control-allow-credentials": "true",
"Content-Type": "application/json",
"server": "nginx",
"access-control-allow-origin": "*"
}
},
"tries": [
{
"state": "next",
"code": 502,
"ip": "127.0.0.1",
"port": 8000
},
{
"ip": "127.0.0.1",
"port": 8000
}
],
"authenticated_entity": {
"consumer_id": "80f74eef-31b8-45d5-c525-ae532297ea8e",
"id": "eaa330c0-4cff-47f5-c79e-b2e4f355207e"
},
"route": {
"created_at": 1521555129,
"hosts": null,
"id": "75818c5f-202d-4b82-a553-6a46e7c9a19e",
"methods": null,
"paths": [
"/example-path"
],
"preserve_host": false,
"protocols": [
"http",
"https"
],
"regex_priority": 0