Skip to main content

Examples

Real-world examples and patterns for using Crashless. ▶️ Try these live on StackBlitz


🚀 Quick Start Examples

One-Liner Setup

import express from 'express';
import crashless from 'crashless';

const app = express();
app.use(crashless()); // That's it!

app.get('/users/:id', async (req, res) => {
const user = await db.getUser(req.params.id);
res.json(user);
});

app.listen(3000);
// Dashboard: http://localhost:3000/_crashless

▶️ Try this example


🔍 OpenTelemetry Integration

Full OpenTelemetry compatibility for metrics and traces.

Basic OTel Setup

import express from 'express';
import crashless from 'crashless';

const app = express();

app.use(crashless({
telemetry: {
engine: 'otel', // OpenTelemetry format
traces: {
enabled: true,
samplingRate: 0.2 // Sample 20% of requests
}
}
}));

app.listen(3000);

Endpoints:

  • GET /metrics/otel - OpenTelemetry metrics
  • GET /traces.json?format=otlp - OTLP trace export

▶️ Try OTel example

Exporting to OTel Collector

app.use(crashless({
telemetry: {
engine: 'otel',
traces: { enabled: true }
}
}));

// Export traces to OTel collector
app.get('/export-traces', async (req, res) => {
const traces = await fetch('http://localhost:4318/v1/traces', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
resourceSpans: [/* traces from /traces.json?format=otlp */]
})
});
res.json({ exported: true });
});

📊 Prometheus Integration

Prometheus-compatible metrics export for Grafana integration.

Basic Prometheus Setup

import express from 'express';
import crashless from 'crashless';

const app = express();

app.use(crashless({
telemetry: {
engine: 'prometheus' // Prometheus format
}
}));

app.listen(3000);

Endpoint: GET /metrics - Prometheus format

▶️ Try Prometheus example

Grafana Scraping Configuration

# prometheus.yml
scrape_configs:
- job_name: 'crashless'
scrape_interval: 15s
static_configs:
- targets: ['localhost:3000']
metrics_path: '/metrics'

🔌 Custom Exporters

Integrate with Sentry, Datadog, or any monitoring service.

Sentry Integration

import express from 'express';
import crashless from 'crashless';
import * as Sentry from '@sentry/node';

// Register exporter BEFORE creating middleware
crashless.registerExporter('sentry', (err, meta) => {
Sentry.captureException(err, {
tags: {
path: meta.path,
method: meta.method,
status: meta.status
},
extra: meta
});
});

const app = express();
app.use(crashless());

▶️ Try exporters example

Datadog Integration

import crashless from 'crashless';
import { StatsD } from 'hot-shots';

const statsd = new StatsD();

crashless.registerExporter('datadog', (err, meta) => {
statsd.increment('errors.total', 1, {
tags: [
`path:${meta.path}`,
`method:${meta.method}`,
`status:${meta.status}`
]
});

statsd.event('Error occurred', err.message, {
alert_type: 'error',
tags: [`code:${err.code}`]
});
});

app.use(crashless());

Multiple Exporters

import crashless from 'crashless';

// Register multiple exporters
crashless.registerExporter('sentry', (err, meta) => {
Sentry.captureException(err);
});

crashless.registerExporter('logger', (err, meta) => {
logger.error('Error occurred', {
message: err.message,
stack: err.stack,
path: meta.path,
method: meta.method,
timestamp: meta.timestamp
});
});

crashless.registerExporter('metrics', (err, meta) => {
metrics.increment('errors', { path: meta.path });
});

app.use(crashless());

🔍 Distributed Tracing

Automatic and manual tracing capabilities.

Automatic Tracing

app.use(crashless({
telemetry: {
traces: {
enabled: true,
samplingRate: 0.2 // Sample 20% of requests
}
}
}));

// HTTP requests are automatically traced
app.get('/api/users', async (req, res) => {
const users = await db.getUsers(); // Automatically traced
res.json(users);
});

Manual Span Creation

import { trace } from 'crashless';

app.get('/orders/:id', async (req, res) => {
const span = trace.startSpan('getOrder', {
attributes: { 'order.id': req.params.id }
});

try {
const order = await db.getOrder(req.params.id);
span.setAttributes({ 'order.status': order.status });
span.end();
res.json(order);
} catch (err) {
span.setStatus('error', err);
throw err;
}
});

Database Operation Tracing

import { trace } from 'crashless';

async function findUser(id) {
const span = trace.startSpan('db.findUser', {
kind: 'client',
attributes: { 'db.operation': 'find', 'db.collection': 'users' }
});

try {
const user = await db.users.findOne({ id });
span.setAttributes({ 'db.result': user ? 'found' : 'not_found' });
span.end();
return user;
} catch (err) {
span.setStatus('error', err);
throw err;
}
}

🛡️ Error Handling Examples

Using createError

import crashless, { createError } from 'crashless';

app.use(crashless());

app.get('/users/:id', async (req, res) => {
const user = await db.getUser(req.params.id);

if (!user) {
throw createError('User not found', 404, 'USER_NOT_FOUND');
}

res.json(user);
});

Custom Error Codes

app.post('/users', async (req, res) => {
const { email, password } = req.body;

if (!email || !password) {
throw createError('Email and password required', 400, 'VALIDATION_ERROR');
}

if (await db.userExists(email)) {
throw createError('User already exists', 409, 'USER_EXISTS');
}

const user = await db.createUser({ email, password });
res.json(user);
});

⚙️ Configuration Examples

Minimal (Error Handling Only)

app.use(crashless({
telemetry: { engine: 'none' }
}));

Overhead: ~3% | Use case: High-traffic apps needing only error handling

Standard Production

app.use(crashless({
log: true,
maskMessages: true,
enableDashboard: false,
telemetry: {
engine: 'builtin'
}
}));

Overhead: ~20% | Use case: Standard production with metrics

Production with Dashboard

app.use(crashless({
enableDashboard: true,
dashboardAuth: (req) => {
const ip = req.ip;
const token = req.headers['x-dashboard-token'];
return ip === '127.0.0.1' || token === process.env.DASHBOARD_SECRET;
},
telemetry: {
engine: 'builtin',
traces: {
enabled: true,
samplingRate: 0.2
}
}
}));

▶️ Try production dashboard example

Development/Staging

app.use(crashless({
log: true,
maskMessages: false,
enableDashboard: true,
telemetry: {
engine: 'builtin',
traces: {
enabled: true,
samplingRate: 1.0,
maxStored: 1000
}
}
}));

🔐 Production Security Patterns

IP Whitelist Dashboard

app.use(crashless({
enableDashboard: true,
dashboardAuth: (req) => {
const ip = req.ip || req.connection?.remoteAddress;
const allowedIPs = process.env.DASHBOARD_ALLOWED_IPS?.split(',') || [];
return allowedIPs.includes(ip);
}
}));

Token-Based Dashboard Auth

app.use(crashless({
enableDashboard: true,
dashboardAuth: (req) => {
const token = req.headers['x-dashboard-token'];
return token === process.env.DASHBOARD_SECRET;
}
}));

Combined Authentication

app.use(crashless({
enableDashboard: true,
dashboardAuth: (req) => {
const ip = req.ip;
const token = req.headers['x-dashboard-token'];

// Allow localhost
if (ip === '127.0.0.1' || ip === '::1') return true;

// Check token
if (token === process.env.DASHBOARD_SECRET) return true;

// Deny by default
return false;
}
}));

📈 Metrics Export

Prometheus Integration

app.use(crashless({
telemetry: { engine: 'prometheus' }
}));

// Metrics available at /metrics

OpenTelemetry Integration

app.use(crashless({
telemetry: { engine: 'otel' }
}));

// Metrics available at /metrics/otel

Custom Metrics Endpoint

app.get('/custom-metrics', (req, res) => {
const metrics = getMetrics(); // Get Crashless metrics
// Combine with your own metrics
res.json({
crashless: metrics,
custom: getCustomMetrics()
});
});

🎮 Try All Examples

▶️ Full Featured Demo - All features enabled

▶️ Browse All Examples


Next Steps