nicolai’s space.:space:

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 the 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 an 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 can 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 during each request that’s 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 our 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

Levenshtein's Distance Equation 🔍

This post explains, how Levenshtein's distance equation works and why it's related to fuzzy string matching.

April 1st, 2020 · 3 min read

Recommending Products like Amazon 🎁

This post explains, how Amazon uses recommendation to suggest products to us and which techniques are applied in this context.

February 13th, 2020 · 5 min read
© 2017–2022 Nicolai
Link to $mailto:nicolai+blog@disroot.orgLink to $https://github.com/nicolai92Link to $https://www.instagram.com/nicolai92_/Link to $https://www.linkedin.com/in/nicolai92/Link to $https://medium.com/@nicolai92Link to $https://www.xing.com/profile/Nicolai_Ernst/cv
Sometimes, the questions are complicated – and the answers are simple.