项目作者: geekeren

项目描述 :
Library to instrument the axios HTTP-client to Support zipkin, supports all features of axios.
高级语言: JavaScript
项目地址: git://github.com/geekeren/zipkin-js-instrumentation-axios.git
创建时间: 2019-02-14T06:52:38Z
项目社区:https://github.com/geekeren/zipkin-js-instrumentation-axios

开源协议:Apache License 2.0

下载


zipkin-js-instrumentation-axios

NPM Version
NPM Downloads
CircleCI Status
License

[IMPORTANT!] This project was contributed to The Zipkin-js official repository: https://github.com/openzipkin/zipkin-js/tree/master/packages/zipkin-instrumentation-axiosjs.


Adds Zipkin tracing support for the axios JS HTTP client library. It supports all features of axios.

Installation

  1. npm install zipkin-js-instrumentation-axios --save

OR

  1. npm install zipkin-instrumentation-axiosjs --save

Usage

You need to use wrapAxios fucntion to wrap the native axios instance, and the axios instance’s type/functions/attributes are not affected. As a result, you can use zipkinAxios the same as axios

For example:

  • Performing a GET request
  1. const axios = require('axios');
  2. const wrapAxios = require('zipkin-js-instrumentation-axios');
  3. const { Tracer, ExplicitContext, ConsoleRecorder } = require('zipkin');
  4. const ctxImpl = new ExplicitContext();
  5. const recorder = new ConsoleRecorder();
  6. const localServiceName = 'service-a'; // name of this application
  7. const tracer = new Tracer({ ctxImpl, recorder, localServiceName });
  8. const remoteServiceName = 'weather-api';
  9. const zipkinAxios = wrapAxios(axios, { tracer, serviceName: localServiceName, remoteServiceName });
  10. zipkinAxios.get('/user?ID=12345')
  11. .then(function (response) {
  12. console.log(response);
  13. })
  14. .catch(function (error) {
  15. console.log(error);
  16. });
  • Wrap an axios instance
  1. let axiosInstance = axios.create({
  2. baseURL: 'https://some-domain.com/api/',
  3. timeout: 1000,
  4. headers: {'X-Custom-Header': 'foobar'}
  5. });
  6. axiosInstance = wrapAxios(axiosInstance, {
  7. tracer,
  8. serviceName: localServiceName,
  9. remoteServiceName
  10. });

Interceptors of Axios also supported

You can intercept requests or responses before they are handled by then or catch.

  1. // Add a request interceptor
  2. axios.interceptors.request.use(function (config) {
  3. // Do something before request is sent
  4. return config;
  5. }, function (error) {
  6. // Do something with request error
  7. return Promise.reject(error);
  8. });
  9. // Add a response interceptor
  10. axios.interceptors.response.use(function (response) {
  11. // Do something with response data
  12. return response;
  13. }, function (error) {
  14. // Do something with response error
  15. return Promise.reject(error);
  16. });

The test cases all passed:

  1. axios instrumentation - integration test
  2. should add headers to requests
  3. should support request shorthand (defaults to GET)
  4. should support both url and uri options
  5. should support promise callback
  6. should report 404 when path does not exist
  7. should report when service does not exist (41ms)
  8. should report when service returns 400
  9. should report when service returns 500

You can go to /example folder and run npm install && node app.js to see the result of the example.