Observed since 20 Oct, 18:05 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
This project provides a custom logger for distributed tracing in Node.js applications, along with middleware and an Axios wrapper that automatically includes a tracing ID for propagating requests across services.
This project provides a custom logger for distributed tracing in Node.js applications, along with middleware and an Axios wrapper that automatically includes a tracing ID for propagating requests across services.
npm install 11k-utils
The Logger class is used to create log entries with a tracing ID. This ID helps track requests as they pass through different services.
Logger.setTracingId(tracingId, callback): Sets a tracing ID and runs the provided callback within its context. If no ID is provided, a new one is generated.logger.getLogger(): Returns a logger instance that includes the tracing ID.Example:
import { Logger } from './src/logger/logger';
Logger.setTracingId(undefined, () => {
const logger = new Logger().getLogger();
logger.info('Request started');
});
The tracingMiddleware automatically generates a tracing ID if none is provided and propagates it through subsequent requests.
To use it in an Express application:
import express from 'express';
import { tracingMiddleware } from './src/logger/tracingMiddleware';
import { Logger } from './src/logger/logger';
const app = express();
app.use(tracingMiddleware);
app.get('/example', (req, res) => {
const logger = new Logger().getLogger();
logger.info('Handling /example route');
res.send('Hello, world!');
});
The AxiosWrapper ensures that every outgoing HTTP request contains the tracing ID in the headers (X-Tracing-ID). This helps maintain the tracing context across service calls.
Example:
import { axiosInstance } from './src/axios/axios';
async function exampleServiceCall() {
try {
const response = await axiosInstance.get('http://some-external-service');
console.log(response.data);
} catch (error) {
console.error('Error making request:', error);
}
}
import express from 'express';
import { tracingMiddleware } from './src/logger/tracingMiddleware';
import { Logger } from './src/logger/logger';
import { axiosInstance } from './src/axios/axios';
const app = express();
app.use(tracingMiddleware);
app.get('/service', async (req, res) => {
const logger = new Logger().getLogger();
logger.info('Received request at /service');
await axiosInstance.get('http://another-service');
logger.info('Completed request to another service');
res.send('Service completed');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
AsyncLocalStorage, ensuring that every log within the lifecycle of a request has the same tracing ID.X-Tracing-ID header to all outgoing requests, enabling tracing across distributed services.This project is licensed under the MIT License.
Download counts and popularity will appear here as the collector receives them.
This describes our collected data; it is not a security assessment.
Observed since 20 Oct, 18:05 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:0a67c13628614e3e9a81e99cae24d429ef24d6fdc97c85c6a06e81589d231a4f