Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kong example #354

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions kong/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Kong Example

```
export LS_ACCESS_TOKEN="my-access-token"
docker-compose up
```
12 changes: 12 additions & 0 deletions kong/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
65 changes: 65 additions & 0 deletions kong/app/client.py
Original file line number Diff line number Diff line change
@@ -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': 'servicenow_service',
'url': 'http://www.servicenow.com',
})
print(res.text)
# Create the route
res = requests.post(f"http://{host}:8001/services/servicenow_service/routes", json={
'paths': ['/servicenow'],
'name': 'servicenow_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/servicenow/company.html"
while True:
send_requests(target)
time.sleep(.5)
1 change: 1 addition & 0 deletions kong/app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests
31 changes: 31 additions & 0 deletions kong/collector.yml
Original file line number Diff line number Diff line change
@@ -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]
74 changes: 74 additions & 0 deletions kong/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions kong/kong.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tracing_instrumentations = all
tracing_sampling_rate = 1.0
Loading