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

feat: kong plugin #1073

Merged
merged 12 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 7 additions & 0 deletions packages/kong-plugin/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
return {
default = {
verbose = true,
coverage = false,
output = "gtest",
},
}
22 changes: 22 additions & 0 deletions packages/kong-plugin/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

[*.lua]
indent_style = space
indent_size = 2

[kong/templates/nginx*]
indent_style = space
indent_size = 4

[*.template]
indent_style = space
indent_size = 4

[Makefile]
indent_style = tab
12 changes: 12 additions & 0 deletions packages/kong-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# servroot typically is the Kong working directory for tests
servroot
# exclude generated packed rocks
*.rock
# exclude Pongo shell history
.pongo/.bash_history
# exclude LuaCov statistics file
luacov.stats.out
# exclude LuaCov report
luacov.report.out
# exclude Pongo containerid file
.containerid
35 changes: 35 additions & 0 deletions packages/kong-plugin/.luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
std = "ngx_lua"
unused_args = false
redefined = false
max_line_length = false


include_files = {
"**/*.lua",
"*.rockspec",
".busted",
".luacheckrc",
}


globals = {
"_KONG",
"kong",
"ngx.IS_CLI",
}


not_globals = {
"string.len",
"table.getn",
}


ignore = {
"6.", -- ignore whitespace warnings
}


files["spec/**/*.lua"] = {
std = "ngx_lua+busted",
}
7 changes: 7 additions & 0 deletions packages/kong-plugin/.luacov
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include = {
"%/kong%-plugin%/kong%/.+$",
}

statsfile = "/kong-plugin/luacov.stats.out"
reportfile = "/kong-plugin/luacov.report.out"
runreport = true
1 change: 1 addition & 0 deletions packages/kong-plugin/.pongo/pongorc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--postgres
17 changes: 17 additions & 0 deletions packages/kong-plugin/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM kong/kong-gateway:3.7
# Ensure any patching steps are executed as root user
USER root

# Add custom plugin to the image
COPY ./kong/plugins/readme-plugin /usr/local/share/lua/5.1/kong/plugins/readme-plugin
ENV KONG_PLUGINS=bundled,readme-plugin

# Ensure kong user is selected for image execution
USER kong

# Run kong
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 8000 8443 8001 8444
STOPSIGNAL SIGQUIT
HEALTHCHECK --interval=10s --timeout=10s --retries=10 CMD kong health
CMD ["kong", "docker-start"]
85 changes: 85 additions & 0 deletions packages/kong-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# ReadMe Metrics + Kong

<p align="center">
<img src="https://user-images.githubusercontent.com/33762/182927634-2aebeb46-c215-4ac3-9e98-61f931e33583.png" />
</p>

<p align="center">
Track usage of your API and troubleshoot issues faster.
</p>

With [ReadMe's Metrics API](https://readme.com/metrics) your team can get deep insights into your API's usage. If you're a developer, it takes a few small steps to send your API logs to [ReadMe](http://readme.com). Here's an overview of how the integration works:

- Install this plugin in Kong. You can the provided docker image or copy the plugin into your own Kong image.
- The plugin sends ReadMe the details of your API's incoming requests and outgoing responses, with the option for you to redact any private headers using the configuration options.
- ReadMe uses these request and response details to create an API Metrics Dashboard which can be used to analyze specific API calls or monitor aggregate usage data.

### 📦 Deploying locally
anandkumarpatel marked this conversation as resolved.
Show resolved Hide resolved

```bash
docker build -t kong-readme-plugin:1 .
curl -Ls https://get.konghq.com/quickstart | bash -s -- -r "" -i kong-readme-plugin -t 1
curl -i -s -X POST http://localhost:8001/plugins --data name=readme-plugin --data 'config.api_key=<Your API Key>'

# Setup endpoints or test
curl -i http://localhost:8000/mock/anything
```

### 🧑‍🔬 Testing

```bash
pongo up
pongo shell
kms

# Check if the plugin is available
curl -s localhost:8001 | jq '.plugins.available_on_server."readme-plugin"'
```

#### Enable for all services
anandkumarpatel marked this conversation as resolved.
Show resolved Hide resolved

```bash
curl -i -s -X POST http://localhost:8001/plugins --data name=readme-plugin --data 'config.api_key=<Your API Key>'
```

#### Enable for a specific service route

```bash
# Add a new service
curl -i -s -X POST http://localhost:8001/services --data name=example_service --data url='http://httpbin.org'

# Associate the custom plugin with the `example_service` service
curl -is -X POST http://localhost:8001/services/example_service/plugins --data 'name=readme-plugin' -d "config.queue.max_retry_time=1"

# Add a new route for sending requests through the `example_service` service
curl -i -X POST http://localhost:8001/services/example_service/routes --data 'paths[]=/mock' --data name=example_route

# Test
curl -i http://localhost:8000/mock/anything
```

### Development tricks
anandkumarpatel marked this conversation as resolved.
Show resolved Hide resolved

Get plugin config for a route
anandkumarpatel marked this conversation as resolved.
Show resolved Hide resolved

```bash
curl -s http://localhost:8001/plugins | jq '.data | map(select(.name == "readme-plugin")) | first'
```

```bash
# Retrieve the plugin ID
export PLUGIN_ID=$(curl -s http://localhost:8001/plugins | jq '.data | map(select(.name == "readme-plugin")) | first | .id' | tr -d '"')

# Configure the plugin with your API key
curl -sX PATCH http://localhost:8001/plugins/$PLUGIN_ID --data "config.api_key=<Your API Key>" | jq '.config.api_key'

# Configure `hide_headers`
curl -sX PATCH -H'Content-Type: application/json' http://localhost:8001/plugins/$PLUGIN_ID --data '{"config": {"hide_headers": {"foo": "", "bar": "default"}}}' | jq '.config.hide_headers'

# Configure `id_header`
curl -sX PATCH -H'Content-Type: application/json' http://localhost:8001/plugins/$PLUGIN_ID --data '{"config": {"id_header": "email"}}' | jq '.config.id_header'
```

> 🚧 Any Issues?
>
> Integrations can be tricky! [Contact support](https://docs.readme.com/guides/docs/contact-support) if you have any questions/issues.
Loading
Loading