Giter Site home page Giter Site logo

context-tracer's Introduction

context-tracer : log context info, for your node apps

Greenkeeper badge

Propagates a unique context ID throughout calls. Similar to Java MDC and the likes.

For node 6+.

Usage

Returns a callback, augmented with a unique context UUID. Within this callback, all operations (and their childs) will inherit the unique ID.

tracer.set(() => {
  // all nested calls have now access to a unique context ID
  console.log(tracer.get()); // you can now log it
});

Example

The following code :

const tracer = require("context-tracer");

function callThings() {
  nested();
}

function nested() {
  console.log("nested call - stack n°", tracer.get());
}

tracer.set(() => {
  console.log("first call - stack n°", tracer.get());

  // called within the tracer, and inherits the ID
  callThings();
});

Will print :

> first call : stack n°AAAA
> nested call : stack n°AAAA

Installation

> npm install context-tracer

API

tracer.set(callback)

  • Creates a unique context ID
  • provides a context-augmented callback :
    • all calls within the callback will have the same context ID
    • you only need to call the first function in the callback to propagate the context to its childs
tracer.set(() => {
  // all operations called within the tracer will inherit a unique context ID
});

You can nest context augments : the new context ID will override the previous one, only for its callback operations.

tracer.get() ⇒ string

  • Retrieves current context ID
  • Returns a "no context" string if set hasn't been called
console.log("stack n°" + tracer.get());

> stack n°AAAA

⚠️ Node 6-7 users

While this package supports node 6-7, the underlying architecture is considered experimental for these versions. Read more here.

Local installation

This project is made using Typescript. To generate the transpiled javascript package, you need to run gulp :

$ gulp

You can run the full test suite with mocha :

$ npm i && npm run test

context-tracer's People

Contributors

greenkeeper[bot] avatar ysnglt 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.