nicolai’s space.:space:
Navigate back to the homepage

Kong - Log requests to the console

Nicolai
June 19th, 2021 · 2 min read

At Lufthansa, we are currently building a product to classify text into certain categories — based on the individual customer’s needs. In order to cover as many use cases as possible, an API was developed and published along with a documentation on how we expect the data to be sent. While supporting our first customer to send valid requests, we noticed that it can be helpful to log his incoming requests at the staging environment.

Kong API Gateway

During the development of our text classification product, which is targeted at being used by multiple customers, we realized quickly: The usage of an API gateway is mandatory, as it helps in:

  • Authenticating a customer based on his API key
  • Limiting the number of requests a customer can send in a certain time window
  • Eventually whitelisting customer’s IP addresses

Kong is an API gateway, that offers all of our mentioned requirements in its open source version — which we also consider a big plus, because we want to save costs as much as we can. Technically, Kong is built on top of nginx and extends its features with various plugins, that could either be used for free or in combination with an enterprise subscription.

Log incoming requests

It’s up to the customer, how he implements the functionality to consume our offered API. However, in some scenarios the consumer cannot be certain if the incoming request is valid, as e.g. a framework might being used. That was the case with our first customer, who relied on Microsoft’s BizTalk.

The request was mocked using XML and serialized to the expected JSON format within the framework during the network request. Thus, it was impossible for the customer to verify, whether the request matches the documentation.

As part of our product is also supporting the customer, we decided to log the incoming the requests at the staging environment. Although Kong is offering a plugin to log requests along with additional metadata to the console, I decided to implement the functionality in Lua to print out only the raw request body.

Serverless functions

Kong provides a plugin to write custom logic in Lua and execute that code either before or after the request is sent to the upstream target. In our scenario, the request body should be printed to the console, before it hits the API.

First, let’s create a KongClusterPlugin, that will be executed at each request which is being processed by Kong. The custom logic is implemented between line 11 and 15 and prints out the raw request body to stderr if available.

1apiVersion: configuration.konghq.com/v1
2kind: KongClusterPlugin
3metadata:
4 name: global-pre-function
5 annotations:
6 kubernetes.io/ingress.class: kong
7 labels:
8 global: "true"
9config:
10 access:
11 - -- Get request body if available
12 local data = kong.request.get_raw_body()
13 if data then
14 kong.log.err(data)
15 end
16plugin: pre-function

Second, apply the above Kubernetes custom resource definition (CRD) to the cluster by running:

1kubectl -n <namespace-of-kong> apply -f <name-of-file>

As we now had access to the raw incoming messages, we could support the customer to finally send valid requests to our API. 🎉

Join the mailing list

High-quality blog posts are like shiny Pokémon - they don't appear often. But when they do, be the first to receive the latest content with the ability to opt-out at anytime.

More articles from Nicolai

Kubernetes Tips - Copy Resources

This post explains, how a Kubernetes resource, e.g. a Secret or a ConfigMap, can be copied from one namespace to another.

March 9th, 2021 · 1 min read

The Secrets of Speed Dating

This post explains, how we choose our partner and what it needs to get a second date by analyzing a dataset.

May 11th, 2020 · 10 min read
© 2017–2021 Nicolai
Link to $https://github.com/nicolai92Link to $https://www.linkedin.com/in/nicolai92/Link to $https://medium.com/@nicolai92Link to $https://open.spotify.com/user/0d9i7x6e75vd0th0hnok6vpwf?si=wywquy6CQHOXkrtrsWyA4QLink to $https://twitter.com/nicolai4368Link to $https://www.xing.com/profile/Nicolai_Ernst/cv
Sometimes, the questions are complicated – and the answers are simple.