Giter Site home page Giter Site logo

aneudy1702 / hpropagate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wealthwizardsengineering/hpropagate

1.0 1.0 0.0 361 KB

Node.js module for propagating http headers from incoming requests to the corresponding outgoing requests Magically

License: MIT License

JavaScript 100.00%

hpropagate's Introduction

NPM

hpropagate CircleCI

This package automatically propagates HTTP headers from inbound to outbound HTTP requests.

The why

We use a micro-service architecture with a growing number of HTTP endpoints. We want to propagate certain HTTP headers received from the incoming HTTP requests to all subsequent outbound HTTP requests without the need for our engineers to do it programmatically in each services:

By default, the following headers are automatically propagated:

  1. x-correlation-id. If the header is missing from the inbound request, it will be created with a UUID as value.
  2. x-variant-id to allow us to deploy multiple versions of the same services at the same time.
  3. x-request-id for tracing
  4. x-b3-traceid for tracing
  5. x-b3-spanid for tracing
  6. x-b3-parentspanid for tracing
  7. x-b3-sampled for tracing
  8. x-b3-flags for tracing
  9. x-ot-span-context for tracing

Apart from x-correlation-id, only headers received on the incoming request will be propagated to outbound calls.

The list of headers can be overriden and the initialisation of x-correlation-id disabled, see below

Getting started

To use the default configuration:

// should have this line as early as possible in your code
// it must be before loading express and request
const hpropagate = require('hpropagate');

// then start it
hpropagate();

Or do it in one go:

require('hpropagate')();

Override Defaults

  • to disable the initialisation and generation of the correlation id header:
hpropagate({
    setAndPropagateCorrelationId: false
});
  • to override the list of headers to propagate:
hpropagate({
    headersToPropagate: [
        'x-my-header',
        'x-another-header'
    ]
});

You can also combine those, for example to disable the initialisation of the correlation id and only propagate it:

hpropagate({
    setAndPropagateCorrelationId: false,
    headersToPropagate: [
        'x-correlation-id'
    ]
});
  • to enable the propagation of the headers in the response (to allow more traceability):
hpropagate({
    propagateInResponses: true
});

The How

Inspiration from this talk (Slides and Code) and this module

The first goal is to be able to propagate certain headers (i.e. X-Correlation-ID) to outbound HTTP requests without the need to do it programmatically in the service.

It works by using a global tracer object which keeps a records of traces (a trace object per http request). The header value is saved in the trace object associated with the current request. The http core code is wrapped to record headers on the trace (on the request listener of the http server set with http.createServer) and inject headers to the outbound requests (currently only on http.request).

Node's async_hooks module (new in Node 8) is used to set/reset tracer.currentTrace to the trace relevant to the current execution context. tracer.currentTrace is used in the wrapped functions to record/access the headers data.

Limitations

  • Only tested with Express 4
  • Need Node >= 8
  • Many more....

hpropagate's People

Contributors

daveshepherd avatar dependabot[bot] avatar navbruce avatar pierre-at-wizards avatar ww-scott-timmins avatar yoz2326 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.