{"_id":"11k-utils","_rev":"1-0745b5579980c341001cb736e298de4e","name":"11k-utils","dist-tags":{"latest":"0.1.1"},"versions":{"0.1.0":{"name":"11k-utils","version":"0.1.0","keywords":[],"author":"","license":"ISC","_id":"11k-utils@0.1.0","maintainers":[{"name":"thisisamank","email":"thisisamank@gmail.com"}],"dist":{"shasum":"32081dc596481c929174eb94ec1990e248a4c8e1","tarball":"https://registry.npmjs.org/11k-utils/-/11k-utils-0.1.0.tgz","fileCount":25,"integrity":"sha512-I2C1lkNoohMYqBKsMiElpT8D+NYd/gEoXjK3N5CNBU0NthC4D0fePUdsnzeCu7go3FbVzSTmwyXZpaawEzo9eg==","signatures":[{"sig":"MEQCIFljwz9q27flkrxdedjOwNueewYmAIT1qsw4daOg+3KiAiAaq58kTdOUT9w5xowRI9ryI7SAeredbZH45yMKLvnC8A==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":15665},"main":"dist/app.js","type":"module","gitHead":"9fd6664ada5774eda72fa2589d48fba5af7c058d","scripts":{"dev":"nodemon --exec 'npm run start' --ext ts","lint":"eslint . --ext .ts","test":"echo \"Error: no test specified\" && exit 1","build":"npx tsc","start":"tsc && node dist/index.js"},"_npmUser":{"name":"thisisamank","email":"thisisamank@gmail.com"},"_npmVersion":"8.19.4","description":"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.","directories":{},"_nodeVersion":"16.20.2","dependencies":{"pino":"^9.5.0","uuid":"^10.0.0","axios":"^1.7.7","express":"^4.21.1","typescript":"^5.6.3","async_hooks":"^1.0.0","@types/axios":"^0.14.4"},"_hasShrinkwrap":false,"devDependencies":{"@types/node":"^22.8.1","@types/express":"^5.0.0"},"_npmOperationalInternal":{"tmp":"tmp/11k-utils_0.1.0_1729973841873_0.2538939857407201","host":"s3://npm-registry-packages"}},"0.1.1":{"name":"11k-utils","version":"0.1.1","description":"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.","type":"module","main":"dist/app.js","scripts":{"build":"npx tsc","start":"tsc && node dist/index.js","dev":"nodemon --exec 'npm run start' --ext ts","lint":"eslint . --ext .ts","test":"echo \"Error: no test specified\" && exit 1"},"keywords":[],"author":"","license":"ISC","dependencies":{"@types/axios":"^0.14.4","async_hooks":"^1.0.0","axios":"^1.7.7","express":"^4.21.1","pino":"^9.5.0","typescript":"^5.6.3","uuid":"^10.0.0"},"devDependencies":{"@types/express":"^5.0.0","@types/node":"^22.8.1"},"gitHead":"51b97236bb3e96c79b154b69ad8440e97c4df31c","_id":"11k-utils@0.1.1","_nodeVersion":"16.20.2","_npmVersion":"8.19.4","dist":{"integrity":"sha512-UoC6FDBihKZYS3ZcszMhiPSupdyDIEk/wAiezxBP2yHNUPzdiFIgRLyjzCjL7pFc3gSHNtmDmONLsQetj0uAXw==","shasum":"0bf8a984b6a83a24837c99ff60fb168ecf14359f","tarball":"https://registry.npmjs.org/11k-utils/-/11k-utils-0.1.1.tgz","fileCount":27,"unpackedSize":17087,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDBXhrtLo4ek/b+tN6mDIyy+v9rMjSMXZs1Op83DE+TqAIhAIgu1tXamFZjkIw02TudYEJJc3tTh4D0CNP5OLtCf6Td"}]},"_npmUser":{"name":"thisisamank","email":"thisisamank@gmail.com"},"directories":{},"maintainers":[{"name":"thisisamank","email":"thisisamank@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/11k-utils_0.1.1_1729974956852_0.25931527742418403"},"_hasShrinkwrap":false}},"time":{"created":"2024-10-26T20:17:21.777Z","modified":"2024-10-26T20:35:57.232Z","0.1.0":"2024-10-26T20:17:22.077Z","0.1.1":"2024-10-26T20:35:57.042Z"},"license":"ISC","keywords":[],"description":"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.","maintainers":[{"name":"thisisamank","email":"thisisamank@gmail.com"}],"readme":"# Distributed Tracing Logger and Axios Wrapper\n\nThis 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.\n\n## Features\n\n- **Distributed Tracing Logger**: Logs messages with a tracing ID that propagates across services.\n- **Express Middleware**: Middleware for generating and propagating tracing IDs in HTTP requests.\n- **Axios Wrapper**: Automatically adds tracing IDs to outgoing HTTP requests.\n\n## Installation\n\n1. Install the package:\n\n```bash\nnpm install 11k-utils\n```\n\n## Usage\n\n### 1. Logger Class\n\nThe `Logger` class is used to create log entries with a tracing ID. This ID helps track requests as they pass through different services.\n\n- **`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.\n- **`logger.getLogger()`**: Returns a logger instance that includes the tracing ID.\n\nExample:\n\n```typescript\nimport { Logger } from './src/logger/logger';\n\nLogger.setTracingId(undefined, () => {\n  const logger = new Logger().getLogger();\n  logger.info('Request started');\n});\n```\n\n### 2. Tracing Middleware\n\nThe `tracingMiddleware` automatically generates a tracing ID if none is provided and propagates it through subsequent requests.\n\nTo use it in an Express application:\n\n```typescript\nimport express from 'express';\nimport { tracingMiddleware } from './src/logger/tracingMiddleware';\nimport { Logger } from './src/logger/logger';\n\nconst app = express();\napp.use(tracingMiddleware);\n\napp.get('/example', (req, res) => {\n  const logger = new Logger().getLogger();\n  logger.info('Handling /example route');\n  res.send('Hello, world!');\n});\n```\n\n### 3. Axios Wrapper\n\nThe `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.\n\nExample:\n\n```typescript\nimport { axiosInstance } from './src/axios/axios';\n\nasync function exampleServiceCall() {\n  try {\n    const response = await axiosInstance.get('http://some-external-service');\n    console.log(response.data);\n  } catch (error) {\n    console.error('Error making request:', error);\n  }\n}\n```\n\n### 4. Full Example\n\n```typescript\nimport express from 'express';\nimport { tracingMiddleware } from './src/logger/tracingMiddleware';\nimport { Logger } from './src/logger/logger';\nimport { axiosInstance } from './src/axios/axios';\n\nconst app = express();\napp.use(tracingMiddleware);\n\napp.get('/service', async (req, res) => {\n  const logger = new Logger().getLogger();\n  logger.info('Received request at /service');\n  await axiosInstance.get('http://another-service');\n  logger.info('Completed request to another service');\n  res.send('Service completed');\n});\n\napp.listen(3000, () => {\n  console.log('Server running on port 3000');\n});\n```\n\n## Notes\n\n- **Tracing Context**: The tracing ID is propagated using `AsyncLocalStorage`, ensuring that every log within the lifecycle of a request has the same tracing ID.\n- **Axios Interceptor**: The Axios interceptor adds the `X-Tracing-ID` header to all outgoing requests, enabling tracing across distributed services.\n\n## License\n\nThis project is licensed under the MIT License.\n","readmeFilename":"README.md"}