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

@w3d3/axios-opentracing

v0.1.5

Published

Axios interceptor which traces your requests

Downloads

28

Readme

axios-opentracing

Build Status Coverage Status NPM version

Axios interceptor which traces your requests 👀

Motivation

Using opentracing in node.js is kinda hard because you need to keep context of current request. This package helps you to abstract tracing logic for your http calls with axios interceptors.

Installation

To use this package, you need to have axios and opentracing: With yarn:

yarn add axios opentracing axios-opentracing

Or with npm:

npm install axios opentracing axios-opentracing

And you also need any APM agent of your choice (Jaeger, Elastic APM, Lightstep, etc.) or your own implementation if it is compatible with Opentracing API.

Usage

This package contains one function-intializer that take your tracer as an argument and produces function-wrapper which will wrap your axios instance with interceptors:

const createAxiosTracing = require('axios-opentracing');

const applyTracingInterceptors = createAxiosTracing(tracer);

applyTracingInterceptors(axiosInstance, { span: rootSpan });

Alternatively, you can use global tracer simply by calling initializer without any arguments:

const opentracing = require('opentracing');
const createAxiosTracing = require('axios-opentracing');

opentracing.initGlobalTracer(new AnyTracer());

const applyTracingInterceptors = createAxiosTracing();

applyTracingInterceptors(AxiosInstance, { span: rootSpan });

Produced function may be called on every request that your server handles. It takes your axios instance as 1st argument and options as 2nd argument, which looks like this:

const axiosTracingOptions = {
  span: rootSpan,
  spanName: 'Your span name'
};

Either span or spanName are required. If you pass span then all the spans for your requests will be inherited from the passed one. If you pass spanName then the new span with passed name will be created and used as a root span. If you pass both then passed span will be used. The wrapper returns span that was used (passed or created).

Example

You can use any tracer, in this examples I will use Jaeger.

Simple

const axios = require('axios');
const { initTracer } = require('jaeger-client');
const createAxiosTracing = require('axios-opentracing');

// Setup tracer
const tracer = initTracer(tracingConfig, tracingOptions);

// Create tracing applyer
const applyTracingInterceptors = createAxiosTracing(tracer);

// Create root span
const rootSpan = tracer.startSpan('api_http_call');

// Setup an axios instance
const API = axios.create({
  baseURL: 'https://example.com'
});

// Setup tracing interceptors
applyTracingInterceptors(API, { span: rootSpan });

// Make some requests
Promise.all([
  API.get('/'),
  API.get('/some/path')
]).then(() => {
  /*
    When root span will be finished, you will see something like this in your tracing dashboard:

      api_http_call
      |
      |__ GET https://example.com/
      |
      |__ GET https://example.com/some/path
  */
  rootSpan.finish();
});

With express

You can use axios-opentracing with express-opentracing middleware:

const express = require('express');
const expressOpentracing = require('express-opentracing');
const { initTracer } = require('jaeger-client');
const createAxiosTracing = require('axios-opentracing');

// Setup tracer
const tracer = initTracer();

// Create tracing applyer
const applyTracingInterceptors = createAxiosTracing(tracer);

//
const app = express();

// Setup express tracer middleware
app.use(expressOpentracing({ tracer }));

app.get('/some/path', (req, res) => {
  // Setup an axios instance
  const API = axios.create({
    baseURL: 'https://example.com'
  });

  applyTracingInterceptors(API, { span: req.span });

  API.get('/some/api/call').then((response) => res.end(response.data));
});

The tricky part is that you need to create an axios instance on every request that your server handles because we need to keep context. This problem can be solved simply by creating middleware which will produce axios instances and pass it to your handlers through request context:

// using global tracer
const applyTracingInterceptors = createAxiosTracing(tracer);

app.use(expressOpentracing({ tracer }));

app.use((req, res, next) => {
  const API = axios.create({
    baseURL: 'https://example.com'
  });

  applyTracingInterceptors(API, { span: req.span });

  req.API = API;

  next();
});

app.get('/some/path', (req, res) => {
  req.API.get('/some/api/call').then((response) => res.end(response.data));
});

Isomorphic applications with SSR (React, Vue, etc.)

axios-opentracing can be used to trace requests that your application makes while using SSR. As in express example, an axios instance can be initialized and passed to an application context and used in an application as a regular axios instance wherever you want. Just setup a common interface for a client and a server so that your logic implementation does not depend on the environment.

Contributing

PRs are welcome! Feel free to ask questions in issues.