API logging in Kong

Many plugins are available in Kong to log requests to multiple targets. A target is a system that collects the log and persists it somewhere. These are the important plugins available for logging:

  • File log
  • Syslog
  • HTTP log

The first one is file logging. If we need the Kong server to store the request and response logs in the form of JSON to a file, use this plugin. We should call on Kong's admin REST API (http://localhost:8001/apis/myapi/plugins) to do that:

Hit the Send button and the gateway returns the response, like this:

{
"created_at": 1509202704000,
"config": {
"path": "/tmp/file.log",
"reopen": false
},
"id": "57954bdd-ee11-4f00-a7aa-1a48f672d36d",
"name": "file-log",
"api_id": "795409ae-89ae-4810-8520-15418b96161f",
"enabled": true
}

It basically tells Kong that, for the given API called myapi, log every request to a file called /tmp/file.log. Now, make one more request for the health check (http://localhost:8000/api/v1/healthcheck) to the API gateway. The log for this request will be saved in the given file path.

How do we watch these logs? Those logs will be saved in the /tmp folder of the container. Open a new tab of a terminal and enter the Kong container using this command:

docker exec -i -t kong /bin/bash

This takes you into the container's bash shell. Now, inspect the log file:

cat /tmp/file.log

And you will see a lengthy JSON written to the file:

{"api":{"created_at":1509195475000,"strip_uri":true,"id":"795409ae-89ae-4810-8520-15418b96161f","hosts":["server1"],"name":"myapi","headers":{"host":["server1"]},"http_if_terminated":false,"https_only":false,"retries":5,"uris":["/api/v1"],"preserve_host":false,"upstream_connect_timeout":60000,"upstream_read_timeout":60000,"upstream_send_timeout":60000,"upstream_url":"http://go-server:3000"},"request":{"querystring":{},"size":"423","uri":"/api/v1/healthcheck","request_uri":"http://server1:8000/api/v1/healthcheck","method":"GET","headers":{"cache-control":"no-cache","cookie":"session.id=MTUwODY2NTE3MnxOd3dBTkZaUVNqVTBURmRTUlRSRVRsUlpRMHhGU2xkQlZVNDFVMFJNVmxjMlRFNDJUVXhDTWpaWE1rOUNORXBFVkRJMlExSXlSMEU9fNFxTxKgoEsN2IWvrF-sJgH4tSLxTw8o52lfgj2DwnHI","postman-token":"b70b1881-d7bd-4d8e-b893-494952e44033","user-agent":"PostmanRuntime/3.0.11-hotfix.2","accept":"*/*","connection":"keep-alive","accept-encoding":"gzip, deflate","host":"server1"}},"client_ip":"172.17.0.1","latencies":{"request":33,"kong":33,"proxy":0},"response":{"headers":{"content-type":"text/plain; charset=utf-8","date":"Sat, 28 Oct 2017 15:02:05 GMT","via":"kong/0.11.0","connection":"close","x-kong-proxy-latency":"33","x-kong-upstream-latency":"0","content-length":"58"},"status":200,"size":"271"},"tries":[{"balancer_latency":0,"port":3000,"ip":"172.17.0.3"}],"started_at":1509202924971}

IP addresses logged here are the internal IP assigned by the Docker to the containers. This log also has a breakdown of latency information about the Kong proxy, Go server, and so on. You can learn more about the format of logged fields at https://getkong.org/plugins/file-log/. Kong admin API for enabling the other logging types is similar to the file-log.

The POST requests we are making from Postman to the admin API has the header of Content-Type: "application/json".
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.28.179