diff --git a/collector/kong/compose/README.md b/collector/kong/compose/README.md new file mode 100644 index 00000000..7de82b9d --- /dev/null +++ b/collector/kong/compose/README.md @@ -0,0 +1,6 @@ +# Kong Example + +``` +export LS_ACCESS_TOKEN="my-access-token" +docker-compose up +``` diff --git a/collector/kong/compose/app/Dockerfile b/collector/kong/compose/app/Dockerfile new file mode 100644 index 00000000..3ccc34a7 --- /dev/null +++ b/collector/kong/compose/app/Dockerfile @@ -0,0 +1,12 @@ +#COPY ./httpd-info.conf /usr/local/apache2/conf/extra/httpd-info.conf +FROM python:3-alpine3.15 + +RUN apk add build-base + +RUN mkdir /app +WORKDIR /app +ADD requirements.txt . +RUN pip install -r requirements.txt + +ADD *.py ./ +CMD ["python", "-u", "/app/client.py"] diff --git a/collector/kong/compose/app/client.py b/collector/kong/compose/app/client.py new file mode 100644 index 00000000..32990344 --- /dev/null +++ b/collector/kong/compose/app/client.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +# +# perform requests to a local kong service +# +# export DESTINATION_HOST=my-host-address +# export COLLECTOR_HOST=my-collector-host +# python client.py + +import os +import time +import requests +import sys + +def config_kong(host, collector): + while True: + try: + requests.get(f"http://{host}:8001/") + break + except Exception as e: + print("Kong API is not running yet, sleeping") + time.sleep(1) + continue + + try: + # Configure the OpenTelemetry plugin + res = requests.post(f"http://{host}:8001/plugins", json={ + 'name': 'opentelemetry', + 'config': { + 'endpoint': f"http://{collector}:4318/v1/traces", + 'resource_attributes': { 'service.name': 'kong-dev' }, + }, + }) + print(res.text) + # Create the service + res = requests.post(f"http://{host}:8001/services", json={ + 'name': 'httpbin_service', + 'url': 'http://httpbin.org', + }) + print(res.text) + # Create the route + res = requests.post(f"http://{host}:8001/services/httpbin_service/routes", json={ + 'paths': ['/httpbin'], + 'name': 'httpbin_route', + }) + print(res.text) + except Exception as e: + print(f"Initial configuration failed: {e}") + sys.exit(1) + +def send_requests(url): + try: + res = requests.get(url) + print(f"Request to {url}, got {len(res.content)} bytes") + except Exception as e: + print(f"Request to {url} failed {e}") + +if __name__ == "__main__": + host = os.getenv("DESTINATION_HOST", "localhost") + collector = os.getenv("COLLECTOR_HOST", "localhost") + config_kong(host, collector) + + target = f"http://{host}:8000/httpbin/get" + while True: + send_requests(target) + time.sleep(.5) diff --git a/collector/kong/compose/app/requirements.txt b/collector/kong/compose/app/requirements.txt new file mode 100644 index 00000000..f2293605 --- /dev/null +++ b/collector/kong/compose/app/requirements.txt @@ -0,0 +1 @@ +requests diff --git a/collector/kong/compose/collector.yml b/collector/kong/compose/collector.yml new file mode 100644 index 00000000..070cceab --- /dev/null +++ b/collector/kong/compose/collector.yml @@ -0,0 +1,31 @@ +receivers: + otlp: + protocols: + http: + cors: + allowed_origins: + - "*" + grpc: + tls: + +exporters: + logging: + loglevel: debug + # configuring otlp to Lightstep + otlp: + endpoint: ingest.lightstep.com:443 + headers: + "lightstep-access-token": "${LS_ACCESS_TOKEN}" + +processors: + batch: + +service: + telemetry: + logs: + level: debug + pipelines: + traces: + receivers: [otlp] + processors: [batch] + exporters: [logging, otlp] diff --git a/collector/kong/compose/docker-compose.yml b/collector/kong/compose/docker-compose.yml new file mode 100644 index 00000000..89a3e19b --- /dev/null +++ b/collector/kong/compose/docker-compose.yml @@ -0,0 +1,74 @@ +version: '3.9' +services: + kong-database: + image: postgres:10.5 + restart: always + environment: + - POSTGRES_USER=kong + - POSTGRES_PASSWORD=kongpass + logging: + options: + max-size: 10m + max-file: "3" + ports: + - '5432:5432' + kong-db-init: + image: kong/kong-gateway:3.4.1.1 + command: kong migrations bootstrap + environment: + - KONG_DATABASE=postgres + - KONG_PG_HOST=kong-database + - KONG_PG_USER=kong + - KONG_PG_PASSWORD=kongpass + depends_on: + kong-database: + condition: service_started + kong-gateway: + image: kong/kong-gateway:3.4.1.1 + environment: + - KONG_DATABASE=postgres + - KONG_PG_HOST=kong-database + - KONG_PG_USER=kong + - KONG_PG_PASSWORD=kongpass + - KONG_PROXY_ACCESS_LOG=/dev/stdout + - KONG_ADMIN_ACCESS_LOG=/dev/stdout + - KONG_PROXY_ERROR_LOG=/dev/stderr + - KONG_ADMIN_ERROR_LOG=/dev/stderr + - KONG_ADMIN_LISTEN=0.0.0.0:8001 + - KONG_ADMIN_GUI_URL=http://localhost:8002 + ports: + - '8000:8000' + - '8443:8443' + - '8001:8001' + - '8444:8444' + - '8002:8002' + - '8445:8445' + - '8003:8003' + - '8004:8004' + depends_on: + kong-db-init: + condition: service_completed_successfully + volumes: + - ./kong.conf:/etc/kong/kong.conf:rw + client: + build: ./app + container_name: client + environment: + - DESTINATION_HOST=kong-gateway + - COLLECTOR_HOST=otel-collector + depends_on: + kong-gateway: + condition: service_started + otel-collector: + condition: service_started + stop_grace_period: 1s + otel-collector: + container_name: otel-collector + image: otel/opentelemetry-collector-contrib:0.88.0 + command: ["--config=/conf/collector.yml"] + environment: + LS_ACCESS_TOKEN: ${LS_ACCESS_TOKEN} + ports: + - "4318:4318" + volumes: + - ./collector.yml:/conf/collector.yml:rw diff --git a/collector/kong/compose/kong.conf b/collector/kong/compose/kong.conf new file mode 100644 index 00000000..0379780f --- /dev/null +++ b/collector/kong/compose/kong.conf @@ -0,0 +1,2 @@ +tracing_instrumentations = all +tracing_sampling_rate = 1.0 diff --git a/collector/kong/Makefile b/collector/kong/k8s/Makefile similarity index 100% rename from collector/kong/Makefile rename to collector/kong/k8s/Makefile diff --git a/collector/kong/README.md b/collector/kong/k8s/README.md similarity index 100% rename from collector/kong/README.md rename to collector/kong/k8s/README.md diff --git a/collector/kong/install_collector.sh b/collector/kong/k8s/install_collector.sh similarity index 100% rename from collector/kong/install_collector.sh rename to collector/kong/k8s/install_collector.sh diff --git a/collector/kong/kind-config.yaml b/collector/kong/k8s/kind-config.yaml similarity index 100% rename from collector/kong/kind-config.yaml rename to collector/kong/k8s/kind-config.yaml diff --git a/collector/kong/values-collector.yaml b/collector/kong/k8s/values-collector.yaml similarity index 100% rename from collector/kong/values-collector.yaml rename to collector/kong/k8s/values-collector.yaml