@@ -22,7 +22,7 @@ The backend API for the [Typelets Application](https://github.com/typelets/typel
2222- ⚡ ** Fast & Type-Safe** with TypeScript and Hono
2323- 🐘 ** PostgreSQL** with Drizzle ORM
2424- 🚀 ** Valkey/Redis Caching** for high-performance data access with cluster support
25- - 📊 ** New Relic APM ** integration for monitoring, metrics, and error tracking
25+ - 📊 ** Prometheus Metrics ** with Grafana integration for monitoring and observability
2626- 💻 ** Code Execution** via secure Judge0 API proxy
2727- 🛡️ ** Comprehensive Rate Limiting** for HTTP, WebSocket, file uploads, and code execution
2828- 🏥 ** Health Checks** with detailed system status and readiness probes
@@ -36,8 +36,8 @@ The backend API for the [Typelets Application](https://github.com/typelets/typel
3636- ** Cache** : Valkey/Redis Cluster for high-performance caching
3737- ** Authentication** : [ Clerk] ( https://clerk.com/ )
3838- ** Validation** : [ Zod] ( https://zod.dev/ )
39- - ** Monitoring** : [ New Relic APM ] ( https://newrelic .com/ ) for metrics, logging, and error tracking
40- - ** Logging** : Structured JSON logging with automatic New Relic integration
39+ - ** Monitoring** : [ Prometheus ] ( https://prometheus.io/ ) metrics with [ Grafana ] ( https://grafana .com/ ) for observability
40+ - ** Logging** : Structured JSON logging with Prometheus metrics integration
4141- ** TypeScript** : Strict mode enabled for type safety
4242
4343## Prerequisites
@@ -47,7 +47,7 @@ The backend API for the [Typelets Application](https://github.com/typelets/typel
4747- PostgreSQL database (local installation or Docker)
4848- Clerk account for authentication ([ sign up here] ( https://dashboard.clerk.com ) )
4949- Valkey/Redis cluster for caching (optional - improves performance)
50- - New Relic account for monitoring (optional - [ sign up here ] ( https://newrelic.com/signup ) )
50+ - Grafana workspace for monitoring (optional - AWS Managed Grafana or self-hosted )
5151- Judge0 API key for code execution (optional - [ get from RapidAPI] ( https://rapidapi.com/judge0-official/api/judge0-ce ) )
5252
5353## Local Development Setup
@@ -176,7 +176,7 @@ If you prefer to install PostgreSQL locally instead of Docker:
176176
177177- ` GET / ` - API information and health status
178178- ` GET /health ` - Enhanced health check with system status
179- - ` GET /metrics ` - System metrics for monitoring dashboards
179+ - ` GET /metrics ` - Prometheus metrics endpoint for Grafana (requires Basic Auth)
180180- ` GET /ready ` - Readiness probe for container orchestration
181181- ` GET /live ` - Liveness probe for container orchestration
182182- ` GET /websocket/status ` - WebSocket server status and statistics
@@ -286,9 +286,7 @@ The application uses the following main tables:
286286| ` VALKEY_HOST ` | Valkey/Redis cluster hostname | No | - |
287287| ` VALKEY_PORT ` | Valkey/Redis cluster port | No | 6379 |
288288| ** Monitoring (Optional)** | | | |
289- | ` NEW_RELIC_APP_NAME ` | Application name in New Relic | No | Typelets API |
290- | ` NEW_RELIC_LICENSE_KEY ` | New Relic license key | No | - |
291- | ` NEW_RELIC_LOG_LEVEL ` | New Relic log level (error/warn/info/debug) | No | warn (dev), info (prod) |
289+ | ` METRICS_API_KEY ` | API key for Prometheus metrics endpoint | No | - |
292290| ** Rate Limiting** | | | |
293291| ` HTTP_RATE_LIMIT_WINDOW_MS ` | HTTP rate limit window in milliseconds | No | 900000 (15 min) |
294292| ` HTTP_RATE_LIMIT_MAX_REQUESTS ` | Max HTTP requests per window | No | 1000 |
@@ -311,6 +309,49 @@ The application uses the following main tables:
311309
312310\* Required only for code execution features
313311
312+ ## Monitoring with Prometheus & Grafana
313+
314+ The API exposes Prometheus metrics at the ` /metrics ` endpoint for monitoring with Grafana or other Prometheus-compatible systems.
315+
316+ ### Available Metrics
317+
318+ - ** HTTP Metrics** : Request counts, duration histograms, status codes
319+ - ** WebSocket Metrics** : Active connections, message counts by type and direction
320+ - ** Database Metrics** : Query counts and duration by operation and table
321+ - ** Cache Metrics** : Operations, hit/miss rates, operation duration
322+ - ** Code Execution Metrics** : Execution duration and success rates by language
323+ - ** Business Events** : Custom event tracking
324+ - ** Security Events** : Security-related event tracking
325+ - ** System Metrics** : Memory, CPU, event loop, and other Node.js metrics
326+
327+ ### Grafana Configuration
328+
329+ To connect Grafana to the metrics endpoint:
330+
331+ 1 . ** Generate a secure API key** : ` openssl rand -hex 32 `
332+ 2 . ** Set ` METRICS_API_KEY ` in your environment** (ECS task definition or ` .env ` )
333+ 3 . ** Add Prometheus data source in Grafana** :
334+ - ** Type** : Prometheus
335+ - ** URL** : ` https://api.typelets.com/metrics ` (or your API URL)
336+ - ** Auth** : Basic auth
337+ - ** User** : ` metrics ` (or any username)
338+ - ** Password** : Your ` METRICS_API_KEY ` value
339+
340+ ### Local Testing
341+
342+ Test the metrics endpoint locally:
343+
344+ ``` bash
345+ # Set your API key in .env
346+ METRICS_API_KEY=your-secure-key-here
347+
348+ # Start the server
349+ pnpm run dev
350+
351+ # Test with curl (Basic Auth)
352+ curl -u metrics:your-secure-key-here http://localhost:3000/metrics
353+ ```
354+
314355## Development
315356
316357### Project Structure
323364├── lib/
324365│ ├── cache.ts # Valkey/Redis cluster caching layer
325366│ ├── cache-keys.ts # Centralized cache key patterns and TTL values
326- │ ├── logger.ts # Structured logging with New Relic integration
367+ │ ├── logger.ts # Structured logging with Prometheus metrics integration
368+ │ ├── prometheus.ts # Prometheus metrics definitions and registry
327369│ └── validation.ts # Zod validation schemas
328370├── middleware/
329371│ ├── auth.ts # Authentication middleware
351393│ │ └── rate-limiter.ts # WebSocket rate limiting
352394│ ├── types.ts # WebSocket message types
353395│ └── index.ts # Main WebSocket server manager
354- └── server.ts # Application entry point with New Relic initialization
396+ └── server.ts # Application entry point
355397```
356398
357399### Type Safety
0 commit comments