@logtide/express
v0.5.6
Published
LogTide SDK middleware for Express — request tracing and error capture
Readme
Features
- Automatic request spans for every incoming request
- Error capture with full request context
- W3C Trace Context propagation (
traceparentin/out) - Breadcrumbs for HTTP requests
- Scope access via
req.logtideScope - Express 4 and 5 support
- Full TypeScript support with strict types
Installation
npm install @logtide/express
# or
pnpm add @logtide/express
# or
yarn add @logtide/expressQuick Start
import express from 'express';
import { logtide } from '@logtide/express';
const app = express();
app.use(logtide({
dsn: 'https://[email protected]',
// Or use apiUrl + apiKey instead of dsn:
// apiUrl: 'https://your-instance.com',
// apiKey: 'lp_your_key',
service: 'my-express-api',
environment: 'production',
}));
app.get('/hello', (req, res) => {
res.json({ message: 'Hello World' });
});
app.listen(3000);How It Works
The middleware runs on every request and:
- Extracts incoming
traceparentheader (or generates a new trace ID) - Creates a span named after the request (e.g.
GET /hello) - Stores scope on the request object (
req.logtideScope) - Calls
next()to process the request - Finishes the span with
okorerrorbased on response status - Injects
traceparentinto the response headers - Captures errors for 5xx responses with full context
Accessing the Scope
Use req.logtideScope to access the LogTide scope inside your handlers:
app.get('/users/:id', (req, res) => {
const scope = req.logtideScope;
const traceId = req.logtideTraceId;
// Add custom breadcrumbs
scope?.addBreadcrumb({
type: 'query',
category: 'database',
message: 'SELECT * FROM users WHERE id = ?',
timestamp: Date.now(),
});
res.json({ id: req.params.id });
});Configuration
All ClientOptions from @logtide/core are supported:
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| dsn | string | required | DSN string: https://lp_KEY@host/PROJECT |
| service | string | required | Service name for log attribution |
| environment | string | — | Environment (e.g. production, staging) |
| release | string | — | Release / version identifier |
| debug | boolean | false | Enable debug logging |
| tracesSampleRate | number | 1.0 | Sample rate for traces (0.0 to 1.0) |
See @logtide/core README for the full list of options.
Error Handling
5xx responses are automatically captured with:
- HTTP method and URL
- Request span marked as
error - Error log with status code
For custom error handling, use Express error middleware:
app.use((err, req, res, next) => {
const scope = req.logtideScope;
if (scope) {
const { hub } = require('@logtide/core');
hub.getClient()?.captureError(err, {
'http.method': req.method,
'http.url': req.originalUrl,
}, scope);
}
res.status(500).json({ error: 'Internal Server Error' });
});Exports
import { logtide } from '@logtide/express';
import type { LogtideExpressOptions } from '@logtide/express';License
MIT License - see LICENSE for details.
