Monitoring TypeScript Applications: Metrics and Alerts
In modern web development, monitoring TypeScript applications is not a luxury, but a necessity. Without it, you risk finding out about a failure only from users. In this article, we will look at how to set up effective monitoring for TypeScript projects, which metrics to track, and how to configure alerts to receive notifications about problems before they become critical.
Why is monitoring TypeScript applications so important?
TypeScript, being a superset of JavaScript, is often used to create complex enterprise systems and server-side solutions on Node.js. However, the dynamic nature of JavaScript under the hood can lead to unexpected runtime errors. Monitoring allows you to:
- Detect problems at an early stage — before they affect users.
- Analyze performance — identify bottlenecks in the code.
- Predict failures — based on metric trends.
- Improve code quality — understand which modules are the most "problematic".
Key metrics for TypeScript applications
For monitoring to be effective, you need to collect the right metrics. Let's divide them into three main groups.
1. Technical metrics (RED method)
The RED approach (Rate, Errors, Duration) is ideal for microservices and APIs:
- Rate (request rate) — number of requests per second (RPS).
- Errors — percentage of errors (4xx, 5xx).
- Duration (response time) — request processing time (p50, p95, p99).
Example of collecting metrics using the prom-client library:
import client from 'prom-client';import express from 'express';
const app = express();
// Create metricsconst httpRequestDuration = new client.Histogram({ name: 'http_request_duration_seconds', help: 'Duration of HTTP requests in seconds', labelNames: ['method', 'route', 'status_code'], buckets: [0.1, 0.5, 1, 2, 5]});
const httpRequestsTotal = new client.Counter({ name: 'http_requests_total', help: 'Total number of HTTP requests', labelNames: ['method', 'route', 'status_code']});
// Middleware for collecting metricsapp.use((req, res, next) => { const end = httpRequestDuration.startTimer(); res.on('finish', () => { const labels = { method: req.method, route: req.path, status_code: res.statusCode }; httpRequestsTotal.inc(labels); end(labels); }); next();});
// Endpoint for Prometheusapp.get('/metrics', async (req, res) => { res.set('Content-Type', client.register.contentType); res.end(await client.register.metrics());});
app.listen(3000, () => console.log('Server running on port 3000'));2. Business metrics
These metrics reflect the health of the business:
- Number of successful transactions.
- Average cart size (for e-commerce).
- Database response time.
- Number of active users.
Example of a custom metric for tracking orders:
const ordersCreated = new client.Counter({ name: 'orders_created_total', help: 'Total number of orders created', labelNames: ['status']});
// Somewhere in the codeexport function createOrder(data: OrderData): Order { // order creation logic ordersCreated.inc({ status: 'success' }); return order;}3. Infrastructure metrics
For Node.js/TypeScript applications, the following are critical:
- Memory usage (heap, RSS).
- CPU load.
- Number of open file descriptors.
- Number of active connections.
- Garbage collection (GC).
Example of collecting memory metrics:
Blogs
Book Recommendations
Read also
Mutation Testing in C#: Breaking Code to Make It Better
WebSocket in C#: Advanced Level Real-Time Applications
Pointers and References in C++: Differences and Best Practices 2025
Working with WebAssembly and C#: Advanced Level in .NET
How to Add Constraints Programmatically in Swift: Optimization and Performance