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.
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/v12kind: KongClusterPlugin3metadata:4 name: global-pre-function5 annotations:6 kubernetes.io/ingress.class: kong7 labels:8 global: "true"9config:10 access:11 - -- Get request body if available12 local data = kong.request.get_raw_body()13 if data then14 kong.log.err(data)15 end16plugin: 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. 🎉