Monitor a Kong gateway using Docker and OpenTelemetry

If you’re running a Kong API Gateway, you can monitor its performance by sending logs, Prometheus metrics, and traces to Cloud Observability. This topic shows how to do that by running the gateway in Docker along with an OpenTelemetry Collector that collects, manages, and exports that data to Cloud Observability.

You’ll configure Kong to send telemetry data to the Collector and the Collector to send that data to Cloud Observability.

This topic provides one approach to monitoring your Kong API Gateway using the OpenTelemetry Collector, both running in Docker. You’ll need to customize the approach for your specific environment and requirements. See More information for links to references.

Follow along with this example.

Prerequisites

  • A running instance of Kong API Gateway (this topic and the example use Docker).
  • Prometheus metrics enabled for Kong.
  • A Cloud Observability access token for the project you’ll use to monitor Kong, set as an environment variable.

    If following the example, the variable should be set as export LS_ACCESS_TOKEN=<"your-access-token">

Configure Docker services

If using Docker, in the docker-compose.yml file, declare two services: the Kong API Gateway (kong-gateway) and the OpenTelemetry Collector (otel-collector).

You can consult the docker-compose file in the example for details about how the Collector and Kong are configured as services. What’s important to note is the ports you expose. The Collector listens on 4317 and 4318. Kong listens on the admin port (8001 by default) where the Collector will scrape Prometheus formatted metrics.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# docker-compose.yml  
services:  
    kong-gateway:  
        ports:
          - "8001"
        ...  
        volumes:  
          - ./kong.conf:/etc/kong/kong.conf:rw  
          - ./kong.yml:/etc/kong/kong.yml:rw  
          - ./logs/kong:/var/log/kong  
    ...  
    otel-collector:  
        ...  
        command: ["--config=/conf/collector.yml"]  
        environment:  
          LS_ACCESS_TOKEN: ${LS_ACCESS_TOKEN}  
        ports:  
          - "4317:4317"  
          - "4318:4318"  
        volumes:  
            - ./collector.yml:/conf/collector.yml:rw  
            - ./logs/kong:/var/log/kong  

Details of the configuration files for each telemetry type are explained in the following sections.

Configure logging

The Kong API Gateway uses kong.yml (the declarative configuration file) to configure emitting logs.

In kong.yml, set up the API gateway and configure the file-log plugin. The file-log plugin is configured to write logs to /var/log/kong/kong.log. These logs can be processed by the filelog receiver in the Collector.

1
2
3
4
5
6
7
8
9
# kong.yml  
...  
services:  
  ...  
    plugins:  
      - name: file-log  
        config:  
          path: /var/log/kong/kong.log  
...  

Docker-compose volume mounts are used for illustration in the example.

Configure metrics

In kong.conf (the Kong configuration file), enable metrics collection.

The Prometheus receiver in the OpenTelemetry Collector scrapes metrics from Kong gateway service on port 8001.

1
2
3
4
5
6
7
8
9
10
# collector.yml  
receivers:  
  ...  
  prometheus:  
    config:  
      scrape_configs:  
        - job_name: "otel-collector"  
          static_configs:  
            - targets: ["kong-gateway:8001"]  
...  

Configure traces

Both the kong.conf and kong.yml file are used to configure tracing.

  1. Turn tracing on using settings in the kong.conf file

    1
    2
    3
    4
    
     # kong.conf  
     tracing_instrumentations = all  
     tracing_sampling_rate = 1.0  
     ...  
    
  2. Configure the OpenTelemetry plugin to send traces to the Collector.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
     # kong.yml  
     ...  
     services:  
       ...  
         plugins:  
           ...  
           - name: opentelemetry  
             config:  
               endpoint: http://otel-collector:4318/v1/traces  
               resource_attributes:  
                 service.name: httpbin_service  
     ...  
    

Configure the Collector

Configure the pipelines in the Collector to use the OTLP exporter to send the data to Cloud Observability. These pipeline configurations include recommended baseline processors for collecting all telemetry.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# collector.yml  
...  
  pipelines:  
    logs:  
      receivers: [filelog]  
      processors: [memory_limiter, batch]  
      exporters: [logging, otlp]  
    metrics:  
      receivers: [prometheus]  
      processors: [memory_limiter, batch]  
      exporters: [logging, otlp]  
    traces:  
      receivers: [otlp]  
      processors: [memory_limiter, batch]  
      exporters: [logging, otlp]  

More information

For more details, see the official documentation resources:

Kong documentation

Open Telemetry documentation

See also

Ingest metrics using the OpenTelemetry SDK

Create and manage dashboards

Create alerts

Updated Jan 5, 2024