Skip to content

Commit

Permalink
Add reverse proxy (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
david1542 committed Sep 14, 2024
1 parent 7b9a6ee commit 4fa8dbc
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 9 deletions.
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"typescript.tsdk": "node_modules\\typescript\\lib",
"cSpell.words": ["GCLOUD", "langfuse", "pagerduty"],
"python.defaultInterpreterPath": "/Users/dudulasry/Library/Caches/pypoetry/virtualenvs/playground-E1KBELiO-py3.10/bin/python"
"python.envFile": ""
}
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,24 @@ Additionally, if you want to work on our Python services (currently only the `da
nx dev dashboard
nx dev slackbot
nx dev data-processor
nx dev log-parser
```

That's it. Now you can make changes and work with the code. If you have any problems, please write to us on Slack, in #tech-support. The different ports are listed in the `.env` file.
### Debugging
Sometimes, you might want to debug a service using a debugger. In that case, you can use our VS Code configuration.
Simply go to your Run and Debug tab, select the service you want to debug (API: Debug, Slackbot: Debug, etc.) and hit the green play button.
### Switching between Docker containers & local code
Sometimes you'd want some of the services to run in docker compose, while others will run locally (e.g. Slackbot).

To switch between the two, you can simply stop the docker contaienr and run `nx dev <service>`. Our reverse proxy (envoy) takes care
of routing the requests to the available service (either locally or in docker).

## Conventions

### Code style
Expand Down
5 changes: 5 additions & 0 deletions TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ In this document, you can find common scenarios where we're currently have probl
issues and want to help, feel free to create an [issue](https://github.com/merlinn-co/merlinn/issues).

### `service "X" can't be used with extends as it declare depends_on`

This issue is related to a [new change](https://github.com/docker/compose/issues/11544) the docker compose team has introduced (also mentioned [here](https://github.com/rancher-sandbox/rancher-desktop/issues/6759)).

The solution is to upgrade docker compose to 2.25.0+.
Expand All @@ -24,3 +25,7 @@ More information can be found in the [Slack guide](https://github.com/merlinn-co
This error usually happens when the Slack keys (`SLACK_BOT_TOKEN`, `SLACK_APP_TOKEN` and/or `SLACK_SIGNING_SECRET`) do not match your actual Slack app. Double-verify your keys are correct by going to your Slack app configuration dashboard.

If they are correct, try to restart the `slackbot` service by running `docker compose up slackbot -d`. Sometimes users update `.env` but do not restart the service itself, which causing it to take out-dated variables.

### Environment variabels are out-dated

If you use VSC Code, sometimes it loads environment variables from the `.env` file automatically. In most cases, it happens because of the python extension. In our `settings.json`, we set `"python.envFile": ""` which shoud prevent that. However, if that doesn't work, try to run the project from a separate terminal (not VS Code).
142 changes: 142 additions & 0 deletions config/envoy/envoy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
static_resources:
listeners:
- name: listener_0
address:
socket_address: { address: 0.0.0.0, port_value: 8080 }
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
stat_prefix: ingress_http
access_log:
- name: envoy.access_loggers.stdout
typed_config:
"@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ["*"]
routes:
- match:
prefix: "/api/"
route:
cluster: api_service
prefix_rewrite: "/"
timeout: 90s
- match:
prefix: "/slackbot/"
route:
cluster: slackbot_service
prefix_rewrite: "/"
- match:
prefix: "/data-processor/"
route:
cluster: data_processor_service
prefix_rewrite: "/"
- match:
prefix: "/log-parser/"
route:
cluster: log_parser_service
prefix_rewrite: "/"
http_filters:
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

clusters:
- name: api_service
connect_timeout: 0.25s
type: STRICT_DNS
lb_policy: ROUND_ROBIN
dns_lookup_family: V4_ONLY
load_assignment:
cluster_name: api_service
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: host.docker.internal
port_value: 3000
health_checks:
- timeout: 1s
interval: 5s
unhealthy_threshold: 2
healthy_threshold: 1
http_health_check:
path: "/"

- name: slackbot_service
connect_timeout: 0.25s
type: STRICT_DNS
lb_policy: ROUND_ROBIN
dns_lookup_family: V4_ONLY
load_assignment:
cluster_name: slackbot_service
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: host.docker.internal
port_value: 3003
health_checks:
- timeout: 1s
interval: 5s
unhealthy_threshold: 2
healthy_threshold: 1
http_health_check:
path: "/health"

- name: data_processor_service
connect_timeout: 0.25s
type: STRICT_DNS
lb_policy: ROUND_ROBIN
dns_lookup_family: V4_ONLY
load_assignment:
cluster_name: data_processor_service
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: host.docker.internal
port_value: 3002
health_checks:
- timeout: 1s
interval: 5s
unhealthy_threshold: 2
healthy_threshold: 1
http_health_check:
path: "/"

- name: log_parser_service
connect_timeout: 0.25s
type: STRICT_DNS
lb_policy: ROUND_ROBIN
dns_lookup_family: V4_ONLY
load_assignment:
cluster_name: log_parser_service
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: host.docker.internal
port_value: 3004
health_checks:
- timeout: 1s
interval: 5s
unhealthy_threshold: 2
healthy_threshold: 1
http_health_check:
path: "/"

admin:
address:
socket_address:
address: 0.0.0.0
port_value: 9901
access_log_path: /dev/stdout
10 changes: 9 additions & 1 deletion docker-compose.common.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
services:
# Infra services
envoy-common:
image: envoyproxy/envoy:v1.22-latest
container_name: envoy
profiles: ["app", "infra"]
ports:
- "8080:8080"
volumes:
- ./config/envoy/envoy.yaml:/etc/envoy/envoy.yaml
postgres-common:
image: postgres
container_name: postgres
Expand Down Expand Up @@ -192,7 +200,7 @@ services:
- SLACK_BOT_TOKEN
- SLACK_SIGNING_SECRET
- API_URL
- PORT=3003
- PORT=SLACKBOT_PORT
- NODE_ENV=development
ports:
- "${SLACKBOT_PORT}:${SLACKBOT_PORT}"
Expand Down
4 changes: 4 additions & 0 deletions docker-compose.images.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
services:
# Infra services
envoy:
extends:
file: docker-compose.common.yml
service: envoy-common
postgres:
extends:
file: docker-compose.common.yml
Expand Down
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
services:
# Infra services
envoy:
extends:
file: docker-compose.common.yml
service: envoy-common
postgres:
extends:
file: docker-compose.common.yml
Expand Down
4 changes: 2 additions & 2 deletions services/api/.env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ KRATOS_ADMIN_URL="http://localhost:4434"
ORY_WEBHOOK_SECRET="super-secret-ory-webhook-key"
CHROMA_HOST="http://localhost:8000"
CHROMA_API_KEY="secret-token"
DATA_PROCESSOR_URL=http://localhost:3002
DATA_PROCESSOR_URL=http://localhost:8080/data-processor
LITELLM_URL=http://localhost:4000
LOG_PARSER_URL=http://localhost:3004
LOG_PARSER_URL=http://localhost:8080/log-parser
TELEMETRY_ENABLED=true
6 changes: 3 additions & 3 deletions services/api/src/middlewares/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { uuid } from "uuidv4";
const captureErrorInTelemetry = (error: ErrorPayload, req: Request) => {
const posthog = new PostHogClient();

const userId = req.cookies.ajs_user_id.toString();
const userId = req.cookies.ajs_user_id?.toString();
const distinctId = userId || uuid();

posthog.capture({
Expand All @@ -31,8 +31,8 @@ const productionError = (error: ErrorPayload, req: Request, res: Response) => {

// Send a detailed error message, for debugging purposes
const developmentError = (error: ErrorPayload, req: Request, res: Response) => {
console.error("developmentError error: ", error);

console.error("error: ", error.message);
console.error("stacktrace:", error.stack);
captureErrorInTelemetry(error, req);

res.status(error.statusCode).json({
Expand Down
2 changes: 1 addition & 1 deletion services/slackbot/.env.dev
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PORT=3003
NODE_ENV=development
API_URL=http://localhost:3000
API_URL=http://localhost:8080/api

0 comments on commit 4fa8dbc

Please sign in to comment.