Gathering detailed insights and metrics for express-rtracer
Gathering detailed insights and metrics for express-rtracer
Gathering detailed insights and metrics for express-rtracer
Gathering detailed insights and metrics for express-rtracer
Express Request Tracer - a middleware for CLS-based request id generation, batteries included
npm install express-rtracer
Typescript
Module System
Min. Node Version
Node Version
NPM Version
62.5
Supply Chain
98.7
Quality
73.8
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
2,391
Last Day
1
Last Week
2
Last Month
14
Last Year
119
MIT License
8 Stars
11 Commits
1 Branches
1 Contributors
Updated on Oct 30, 2022
Minified
Minified + Gzipped
Latest Version
1.0.1
Package Id
express-rtracer@1.0.1
Unpacked Size
7.79 kB
Size
3.53 kB
File Count
4
NPM Version
6.4.1
Node Version
10.13.0
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
0%
2
Compared to previous week
Last Month
250%
14
Compared to previous month
Last Year
-1.7%
119
Compared to previous year
Express Request Tracer - a middleware for CLS-based request id generation, batteries included. An out-of-the-box solution for adding request id into your logs.
Automatically generates a UUID value as the id for each request and stores it in Continuation-Local Storage (CLS, see cls-hooked). If the request contains X-Request-Id
header, uses its value instead. Allows to obtain the generated id anywhere in your routes later and use it for logging or any other purposes.
Install:
1npm install --save express-rtracer
Use the middleware provided by the library before the first middleware that needs to have access to request ids. Note that some middlewares, e.g. body-parser or express-jwt, may cause CLS context to get lost. To avoid such issues, you should use any third party middleware that does not need access to request ids before you use this middleware.
1const express = require('express') 2const rTracer = require('express-rtracer') 3 4const app = express() 5// any third party middleware that does not need access to request ids goes here 6// ... 7 8app.use(rTracer.middleware()) 9// you can override default middleware config: 10// app.use(rTracer.middleware({ 11// headerName: 'X-Your-Request-Header' 12// })) 13 14// all code starting from here has access to request ids
Obtain request id in middlewares on the incoming request:
1// an example middleware for a generic find entity endpoint 2app.get('/api/v1/entity/{id}', (req, res, next) => { 3 entityService.find(req.params.id) 4 .then((entity) => { 5 // you can obtain the request id here 6 const requestId = rTracer.id() 7 logger.log(`requestId: ${requestId}`) 8 9 res.json(entity) 10 }) 11 .catch(next) 12})
You can access the same request id from code that does not have access to the Express req
object.
1// an imaginary entity-service.js 2async function find (entityId) { 3 // you can obtain the request id here 4 const requestId = rTracer.id() 5 // ... 6}
The main use case for this library is request id generation and logging automation. You can integrate with any logger library in a single place and get request ids in logs across your Express application.
Without having a request id, as a correlation value, in your logs, you will not be able to determine which log entries belong to the process of handling the same request. You could generate a request id manually and store it in the Express req
object, but then you will have to explicitly pass req
into all other modules on the route. And express-rtracer comes to the rescue!
Let's consider integration with winston, one of most popular logging libraries.
1const { createLogger, format, transports } = require('winston') 2const { combine, timestamp, printf } = format 3 4// a custom format that outputs request id 5const rTracerFormat = printf((info) => { 6 const rid = rTracer.id() 7 return rid 8 ? `${info.timestamp} [request-id:${rid}]: ${info.message}` 9 : `${info.timestamp}: ${info.message}` 10}) 11 12const logger = createLogger({ 13 format: combine( 14 timestamp(), 15 rTracerFormat 16 ), 17 transports: [new transports.Console()] 18})
A complete example is available in /examples/winston.js
file.
These are the available config options for the middleware(options)
function. Options are optional.
1{ 2 // Respect request header flag (default: true). 3 // If set to true, the middleware will be using a value from the specified header (if the value is present). 4 useHeader: true, 5 // Request header name, case insensitive (default: X-Request-Id). 6 // Used if useHeader is set to true. 7 headerName: 'X-Request-Id' 8}
To avoid weird behavior with Express:
express-rtracer
as the first dependency in your app. Some popular packages may use async which breaks CLS.For Node 10 users:
Licensed under MIT.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/11 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
74 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More