Skip to content

Commit c5b315f

Browse files
authored
Merge pull request #6 from typelets/feat/grafana-prometheus-metrics
feat: migrate from New Relic to Prometheus/Grafana metrics
2 parents 1f8e98b + 269dde3 commit c5b315f

File tree

13 files changed

+343
-1315
lines changed

13 files changed

+343
-1315
lines changed

.env.example

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ FREE_TIER_NOTE_LIMIT=100# Note count limit for free users (default: 100)
118118
# LOGGING & MONITORING
119119
# ================================================================
120120

121-
# New Relic APM (OPTIONAL - for production monitoring)
122-
# Get your license key from: https://one.newrelic.com
123-
# NEW_RELIC_APP_NAME=Typelets API
124-
# NEW_RELIC_LICENSE_KEY=your_license_key_here
125-
# NEW_RELIC_LOG_LEVEL=warn # Options: error, warn, info, debug, trace (default: warn in dev, info in prod)
121+
# Prometheus Metrics API Key (REQUIRED for production)
122+
# Protects /metrics endpoint from unauthorized access with Basic Auth
123+
# Generate with: openssl rand -hex 32
124+
# Configure in Grafana data source: Basic Auth with username "metrics" and password <key>
125+
METRICS_API_KEY=your-secure-metrics-api-key-here-min-32-chars
126126

127127
# Debug Logging
128128
# DEBUG=false # Set to true for verbose logging (not recommended in production)

CHANGELOG.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
## [1.8.2](https://github.com/typelets/typelets-api/compare/v1.8.1...v1.8.2) (2025-10-17)
22

3-
4-
### Bug Fixes
5-
6-
* invalidate note counts cache on note and folder mutations ([9c497bb](https://github.com/typelets/typelets-api/commit/9c497bb61338b36f85dc4f1bf66addeabe202fa8))
7-
8-
## [1.8.1](https://github.com/typelets/typelets-api/compare/v1.8.0...v1.8.1) (2025-10-16)
9-
103
### Bug Fixes
114

125
- resolve Redis Cluster CROSSSLOT error in cache deletions ([862d796](https://github.com/typelets/typelets-api/commit/862d796a684a45f7ca76affe8480633d8dc6220a))
136

14-
# [1.8.0](https://github.com/typelets/typelets-api/compare/v1.7.2...v1.8.0) (2025-10-16)
7+
## [1.8.1](https://github.com/typelets/typelets-api/compare/v1.8.0...v1.8.1) (2025-10-16)
158

169
### Features
1710

README.md

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -323,7 +364,8 @@ src/
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
@@ -351,7 +393,7 @@ src/
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

newrelic.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
"drizzle-orm": "^0.44.2",
4949
"hono": "^4.8.3",
5050
"ioredis": "^5.8.1",
51-
"newrelic": "latest",
5251
"postgres": "^3.4.7",
52+
"prom-client": "^15.1.3",
5353
"ws": "^8.18.3",
5454
"zod": "^4.1.12"
5555
},
@@ -61,7 +61,6 @@
6161
"@semantic-release/git": "^10.0.1",
6262
"@semantic-release/github": "^11.0.5",
6363
"@semantic-release/release-notes-generator": "^14.1.0",
64-
"@types/newrelic": "^9.14.8",
6564
"@types/node": "^24.0.10",
6665
"@types/pg": "^8.15.4",
6766
"@typescript-eslint/eslint-plugin": "^8.38.0",

0 commit comments

Comments
 (0)