Skip to content

Commit 1bdfb42

Browse files
committed
Address review: require METRICS_TOKEN self-hosted, fix listener crash, exclude success from upstream errors
- METRICS_TOKEN is now required whenever METRICS_ENABLED=true, on Cloud and self-hosted alike, matching SENTRY_VERIFY_TOKEN's pattern. - Add a listen() error handler so a taken port logs and leaves metrics off instead of crashing the backend. - tool_call_upstream_errors_total now only increments on a 4xx/5xx upstream status; a success (or no status) is never counted. - Add METRICS_* to docker-compose.yml and docker-compose.cloud.yml so Docker users can turn the feature on. - Fix the sample scrape target to app:9464, the real compose service name. - Reset package-lock.json to main and reinstall prom-client so the diff only adds prom-client, tdigest and bintrees.
1 parent 080aa5c commit 1bdfb42

7 files changed

Lines changed: 208 additions & 111 deletions

File tree

‎docker-compose.cloud.yml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ x-backend: &backend
8787
- SENTRY_ENVIRONMENT=${SENTRY_ENVIRONMENT:-production}
8888
- SENTRY_TRACES_SAMPLE_RATE=${SENTRY_TRACES_SAMPLE_RATE:-0.05}
8989
- SENTRY_VERIFY_TOKEN=${SENTRY_VERIFY_TOKEN:-}
90+
# Prometheus /metrics — a separate listener, off by default (docs/operations/observability.md).
91+
# METRICS_TOKEN is required once METRICS_ENABLED=true; the listener refuses to start without it.
92+
# METRICS_TOOL_LABELS is always ignored here regardless of its value — no series may carry a
93+
# connector id or tool name across tenant workspaces.
94+
- METRICS_ENABLED=${METRICS_ENABLED:-false}
95+
- METRICS_PORT=${METRICS_PORT:-9464}
96+
- METRICS_HOST=${METRICS_HOST:-0.0.0.0}
97+
- METRICS_TOKEN=${METRICS_TOKEN:-}
98+
- METRICS_TOOL_LABELS=${METRICS_TOOL_LABELS:-false}
9099
- NEXT_PUBLIC_API_URL=https://${DOMAIN}
91100
- DATABASE_URL=postgresql://amcp:${POSTGRES_PASSWORD}@postgres:5432/anythingmcp
92101
- REDIS_URL=redis://redis:6379

‎docker-compose.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ services:
110110
- OPENAI_API_KEY=${OPENAI_API_KEY:-}
111111
- OPENROUTER_API_KEY=${OPENROUTER_API_KEY:-}
112112
- ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:-}
113+
# Prometheus /metrics — a separate listener, off by default (docs/operations/observability.md).
114+
# METRICS_TOKEN is required once METRICS_ENABLED=true; the listener refuses to start without it.
115+
- METRICS_ENABLED=${METRICS_ENABLED:-false}
116+
- METRICS_PORT=${METRICS_PORT:-9464}
117+
- METRICS_HOST=${METRICS_HOST:-0.0.0.0}
118+
- METRICS_TOKEN=${METRICS_TOKEN:-}
119+
- METRICS_TOOL_LABELS=${METRICS_TOOL_LABELS:-false}
113120

114121
depends_on:
115122
postgres:

‎docs/deployment.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ curl -s http://localhost:4000/api/mcp-api-keys \
362362
| `METRICS_ENABLED` | No | Start a second, separate `/metrics` listener for Prometheus (default: `false`) — see [observability.md](operations/observability.md) |
363363
| `METRICS_PORT` | No | Port for the metrics listener (default: `9464`) |
364364
| `METRICS_HOST` | No | Bind host for the metrics listener (default: `0.0.0.0`) |
365-
| `METRICS_TOKEN` | No on self-hosted, **required on Cloud** | Bearer token required to scrape `/metrics`, compared with `timingSafeEqual` |
365+
| `METRICS_TOKEN` | **Required when `METRICS_ENABLED=true`** | Bearer token required to scrape `/metrics`, compared with `timingSafeEqual` |
366366
| `METRICS_TOOL_LABELS` | No | Add `connector_id`/`tool` labels to tool-call metrics (default: `false`; ignored on Cloud) |
367367

368368
---

‎docs/operations/observability.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,14 @@ Off unless `METRICS_ENABLED=true`. When on, the backend starts a **second, separ
185185
| `METRICS_ENABLED` | `false` | Starts the listener. |
186186
| `METRICS_PORT` | `9464` | Metrics listener port. |
187187
| `METRICS_HOST` | `0.0.0.0` | Metrics listener bind host — a Prometheus container needs to reach it over the compose network, so the port is deliberately **not** published in `docker-compose.yml`. |
188-
| `METRICS_TOKEN` | unset | Required `Authorization: Bearer` token, compared with `timingSafeEqual`. **Required on Cloud** — without it the listener refuses to start and logs an error. Optional on self-hosted. |
188+
| `METRICS_TOKEN` | unset | **Required** `Authorization: Bearer` token, compared with `timingSafeEqual`, whenever `METRICS_ENABLED=true` — on Cloud and self-hosted alike. Without it the listener refuses to start and logs an error; the default bind host `0.0.0.0` would otherwise serve `/metrics` to anyone who can reach the host. |
189189
| `METRICS_TOOL_LABELS` | `false` | Adds `connector_id` (cuid, not name) and `tool` labels to the tool-call metrics. Self-hosted only — **always ignored on Cloud**, even if set, so no series can carry a connector or tool identity across tenant workspaces. |
190190

191191
### Metrics (prefix `anythingmcp_`)
192192

193193
- `tool_calls_total{connector_type,status}` — counter, `status` is `success` or `error`.
194194
- `tool_call_duration_seconds{connector_type,status}` — histogram, buckets from 0.05 s to 60 s (tool calls can run 20 s or more).
195-
- `tool_call_upstream_errors_total{connector_type,upstream_status_class}` — `upstream_status_class` is `4xx`, `5xx` or `none`.
195+
- `tool_call_upstream_errors_total{connector_type,upstream_status_class}` — only incremented for an actual upstream error; `upstream_status_class` is `4xx` or `5xx`.
196196
- `tool_calls_in_flight`, `mcp_requests_in_flight`, `registered_tools` — gauges mirrored from the in-process `processGauges` singleton at scrape time (not polled on an interval).
197197
- The standard `prom-client` Node runtime metrics (`collectDefaultMetrics()`: event loop lag, heap, GC, etc.).
198198

@@ -204,12 +204,12 @@ scrape_configs:
204204
scheme: http
205205
bearer_token: '<METRICS_TOKEN>'
206206
static_configs:
207-
- targets: ['anythingmcp-backend:9464']
207+
- targets: ['app:9464']
208208
```
209209
210210
### Cloud vs self-hosted
211211
212-
On Cloud, metrics are instance-wide only: no series may carry an organization id, connector name, tool name or user, which is why `METRICS_TOOL_LABELS` has no effect there and `METRICS_TOKEN` is mandatory. Per-organization usage already exists in the database (`AuditService.getBreakdowns`) and is unaffected by this endpoint. On self-hosted, the operator who sets `METRICS_TOOL_LABELS=true` is the only consumer of their own instance's metrics, so per-connector/per-tool labels are allowed.
212+
On Cloud, metrics are instance-wide only: no series may carry an organization id, connector name, tool name or user, which is why `METRICS_TOOL_LABELS` has no effect there. Per-organization usage already exists in the database (`AuditService.getBreakdowns`) and is unaffected by this endpoint. On self-hosted, the operator who sets `METRICS_TOOL_LABELS=true` is the only consumer of their own instance's metrics, so per-connector/per-tool labels are allowed. `METRICS_TOKEN` is required on both: the listener's default bind host is `0.0.0.0`.
213213

214214
## Fatal crash reports (always on in the Docker image)
215215

0 commit comments

Comments
 (0)