DEV Community

AnushaPalle
AnushaPalle

Posted on

Kong Transformations and Observability with Zipkin, Prometheus and ELK

Meta description: Change requests and responses on the fly, then trace, measure and log every call that passes through Kong.

What you'll learn

  • Request and response transformer plugins
  • Correlation IDs
  • Tracing with Zipkin, metrics with Prometheus, logs with Elasticsearch and Kibana

Part 1: Transformations

Sometimes your backend and your clients disagree about headers or fields. Instead of changing and redeploying code, adjust traffic at the gateway.

Request transformer

Modifies the request before it reaches your service.

curl -X POST http://localhost:8001/services/alpha-service/plugins \
  --data name=request-transformer \
  --data config.add.headers=X-Source:kong \
  --data config.remove.headers=X-Debug \
  --data config.rename.headers=X-Old:X-New \
  --data config.add.querystring=version:2
Enter fullscreen mode Exit fullscreen mode

Common uses:

  • Add an internal header your backend needs
  • Strip headers clients shouldn't send
  • Add a query parameter

Response transformer

Modifies the response before it reaches the client.

curl -X POST http://localhost:8001/services/alpha-service/plugins \
  --data name=response-transformer \
  --data config.add.headers=X-Served-By:kong \
  --data config.remove.headers=Server \
  --data config.remove.json=internalNotes
Enter fullscreen mode Exit fullscreen mode

Handy for hiding internal fields or adding cache and security headers.

Correlation ID

A tiny plugin with a big payoff. It gives every request a unique ID you can follow through logs and services.

curl -X POST http://localhost:8001/plugins \
  --data name=correlation-id \
  --data config.header_name=X-Request-Id \
  --data config.generator=uuid \
  --data config.echo_downstream=true
Enter fullscreen mode Exit fullscreen mode

echo_downstream=true returns the ID to the client too, so a user reporting a problem can quote it.

Part 2: Observability

You can't fix what you can't see. Three complementary views:

Question Pillar Tool
Is something wrong right now? Metrics Prometheus
Where is the time going? Traces Zipkin
What exactly happened in that request? Logs Elasticsearch + Kibana

Traces with Zipkin

curl -X POST http://localhost:8001/plugins \
  --data name=zipkin \
  --data config.http_endpoint=http://zipkin:9411/api/v2/spans \
  --data config.sample_ratio=1
Enter fullscreen mode Exit fullscreen mode

sample_ratio=1 traces every request, fine for learning. In production use something like 0.1.

Run Zipkin with Docker (openzipkin/zipkin), open http://localhost:9411, find a trace and see how long Kong took versus your backend.

Metrics with Prometheus

curl -X POST http://localhost:8001/plugins --data name=prometheus
Enter fullscreen mode Exit fullscreen mode

Kong then exposes request counts by status code, latency, bandwidth and upstream health at http://localhost:8001/metrics. Point Prometheus at it:

scrape_configs:
  - job_name: kong
    metrics_path: /metrics
    static_configs:
      - targets: ["kong:8001"]
Enter fullscreen mode Exit fullscreen mode

Graph request rate and error rate, or alert when 5xx responses climb.

Logs with Elasticsearch and Kibana

Kong's logging plugins (for example http-log) send one JSON document per request to an endpoint you choose:

curl -X POST http://localhost:8001/plugins \
  --data name=http-log \
  --data config.http_endpoint=http://logstash:8080
Enter fullscreen mode Exit fullscreen mode

A minimal Logstash pipeline stores them in Elasticsearch:

input  { http { port => 8080 codec => json } }
output { elasticsearch { hosts => ["http://elasticsearch:9200"] index => "kong-logs-%{+YYYY.MM.dd}" } }
Enter fullscreen mode Exit fullscreen mode

In Kibana, create a data view for kong-logs-* and search things like "all 500 responses on /alpha in the last hour". Each entry includes route, consumer, status and latencies.

How the pieces work together

  1. A metric alert says the error rate rose.
  2. Open Kibana and filter logs for status 500.
  3. Copy the correlation ID from a failing entry.
  4. Find the same request in Zipkin and see which hop was slow.

The correlation ID ties all three together, so always enable it.

Common mistakes

  • Tracing 100% of traffic in production.
  • Forgetting that transformers run in a defined order; test the final result.
  • Logging sensitive headers or bodies into Elasticsearch.
  • Leaving metrics and admin endpoints open to the internet.

Key takeaways

  • Transformer plugins adapt traffic without redeploying code.
  • A correlation ID is the glue between metrics, traces and logs.
  • Use metrics to notice, traces to locate, logs to understand.
  • The gateway is the ideal place to start observability, because every request passes through it.

Series recap

Post Topic
1 What a gateway is, Kong setup with Docker
2 Services, Routes, Consumers, Kong Manager
3 Upstreams, Targets, load balancing, health checks
4 Authentication and traffic-control plugins
5 Transformations and observability

Top comments (0)