npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

11k-utils

v0.1.1

Published

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.

Downloads

145

Readme

Distributed Tracing Logger and Axios Wrapper

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.

Features

  • Distributed Tracing Logger: Logs messages with a tracing ID that propagates across services.
  • Express Middleware: Middleware for generating and propagating tracing IDs in HTTP requests.
  • Axios Wrapper: Automatically adds tracing IDs to outgoing HTTP requests.

Installation

  1. Install the package:
npm install 11k-utils

Usage

1. Logger Class

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');
});

2. Tracing Middleware

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!');
});

3. Axios Wrapper

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);
  }
}

4. Full Example

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');
});

Notes

  • Tracing Context: The tracing ID is propagated using AsyncLocalStorage, ensuring that every log within the lifecycle of a request has the same tracing ID.
  • Axios Interceptor: The Axios interceptor adds the X-Tracing-ID header to all outgoing requests, enabling tracing across distributed services.

License

This project is licensed under the MIT License.