All Projects → fapspirit → axios-opentracing

fapspirit / axios-opentracing

Licence: MIT license
Axios interceptor which traces your requests 👀

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to axios-opentracing

mobxSpa
企业级SPA项目,完整开发脚手架
Stars: ✭ 96 (+540%)
Mutual labels:  spa, axios
Todolist Frontend Vuejs
Front-end application for Todolist Web application built with Laravel and Vue.js
Stars: ✭ 120 (+700%)
Mutual labels:  spa, axios
vuetibook
Integrating Vue.js, Vuetify and Storybook
Stars: ✭ 16 (+6.67%)
Mutual labels:  spa, axios
Csharp Netcore
OpenTracing instrumentation for .NET Core & .NET 5 apps
Stars: ✭ 225 (+1400%)
Mutual labels:  tracing, opentracing
microservices-demo
A Monorepo Demoing Microservices Architecture
Stars: ✭ 36 (+140%)
Mutual labels:  tracing, opentracing
Instrumentedsql
A sql driver that will wrap any other driver and log/trace all its calls
Stars: ✭ 244 (+1526.67%)
Mutual labels:  tracing, opentracing
Vue axios spa
基于vue2+axios+vux+vue-router+vuex构建的单页微信端项目
Stars: ✭ 54 (+260%)
Mutual labels:  spa, axios
Kanali
A Kubernetes Native API Management Solution
Stars: ✭ 192 (+1180%)
Mutual labels:  tracing, opentracing
nodejs
Node.js in-process collectors for Instana
Stars: ✭ 66 (+340%)
Mutual labels:  tracing, opentracing
java-concurrent
OpenTracing-aware helpers related to java.util.concurrent
Stars: ✭ 36 (+140%)
Mutual labels:  tracing, opentracing
Natchez
functional tracing for cats
Stars: ✭ 214 (+1326.67%)
Mutual labels:  tracing, opentracing
jaeger-node
Out of the box distributed tracing for Node.js applications.
Stars: ✭ 66 (+340%)
Mutual labels:  tracing, opentracing
Dd Trace Js
JavaScript APM Tracer
Stars: ✭ 212 (+1313.33%)
Mutual labels:  tracing, opentracing
Dd Trace Go
A Go tracing package for Datadog APM
Stars: ✭ 244 (+1526.67%)
Mutual labels:  tracing, opentracing
Dd Trace Php
Datadog Tracing PHP Client
Stars: ✭ 203 (+1253.33%)
Mutual labels:  tracing, opentracing
Vue Spa
vue-spa : vue + vue-router + axios + vuex + vux 快速成型移动端项目,直接使用。欢迎star
Stars: ✭ 46 (+206.67%)
Mutual labels:  spa, axios
Stagemonitor
an open source solution to application performance monitoring for java server applications
Stars: ✭ 1,664 (+10993.33%)
Mutual labels:  tracing, opentracing
Java Specialagent
Automatic instrumentation for 3rd-party libraries in Java applications with OpenTracing.
Stars: ✭ 156 (+940%)
Mutual labels:  tracing, opentracing
Symfony Vuejs
Source code of the tutorial "Building a single-page application with Symfony 4 and Vue.js"
Stars: ✭ 170 (+1033.33%)
Mutual labels:  spa, axios
smallrye-opentracing
An MicroProfile-OpenTracing implementation
Stars: ✭ 17 (+13.33%)
Mutual labels:  tracing, opentracing

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].