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

express-tracer

v0.1.1

Published

Tracing helpers for ExpressJS

Downloads

1

Readme

express-tracer

Express-trace allows you to follow and inspect the behaviour of your controllers through the response object. It requires that you instrument your application with tracers. Then you can simply call a trace method on your response object each time you want to record something.

Build status: Build Status

Usage

Install it in your project

npm install express-tracer

To add tracing helpers to express, run this module on your express app. Then, configure your tracers at the application level. You're done! Now, each time you want to trace something, call the dedicated function on the response object.

var express = require('express')
var expressTrace = require('express-tracer')
var debug = require('debug')('trace:response')

var app = express()
expressTrace(app)

app.instrument(function (options){
  debug(options.date + ' ' + options.event)
})

app.get('/', function (req, res){
  res.trace('index:hello')
  res.send('hello world')
})

Contributing

To discuss anything on this project, open an issue.

Of course, any code contribution is welcome! To propose your patch make a pull request on the master branch and make sure the Travis tests succeed. If you can't manage to make the tests green, don't worry we'll figure out a way to merge your changes!

### Contributor list

  • Frank Rousseau

## Documentation

### API

app.instrument(tracer)

Add a tracer to the application object. This tracer will be activated each time the trace method of a response is called.

A tracer is a function which takes an options object as argument. It has the following field:

  • res: Response that fired the tracing.
  • req: Request related to the response that fired the tracing.
  • app: Application object.
  • event: String sent by the response to name the event.
  • date: Date when the tracing occured.
  • args: Additional arguments provided by the tracing call.

For example:

app.instrument(function(options){
  debug(options.event + ' ' + options.date + ' ' + options.argv[0]);
});

res.trace(event, [parameters])

Fire all tracers instrumented at the application level.

Optional parameters:

  • event, name of the event to send to tracers.
  • parameters, additional parameters to send to tracers.
app.get('/', function(req, res){
  res.trace('index:visited', 'my-parameter');
  res.render('index');
});

Use cases

Example 1: Time interval

In some cases you, may want to capture time spent on a specific request. Here is a way to do it through tracing.

var express = require('express');
var debug = require('debug')('trace:response');

var app = express();
var responseTime = {};

app.instrument(function (options){
  if (options.event === 'duration:start') {
    responseTime[options.res.id] = options.date;
  } else if (options.event === 'duration:end') {
    var interval = options.date - responseTime[options.res.id];
    debug(options.req.path + ' - ' + interval + 'ms');
    delete responseTime[options.res.id];
  }
})

app.use(function(req, res, next){
  res.id = Math.floor(Math.random() * 1000000);
  res.trace('duration:start');
  next();
});

app.get('/', function (req, res){
  res.trace('index:hello');
  res.send('hello world');
  res.trace('duration:end');
})

Example 2: Send information to dtrace.

Dtrace is a common tool for tracing. It generates probes that will listen and report any event targeted to it. You can simply reach the probe via the trace function.

var express = require('express');
var dtrace = require('dtrace-provider');

var app = express();

var dtp = dtrace.createDTraceProvider("nodeapp");
var p1 = dtp.addProbe("probe1", "char *", "char *");
var p2 = dtp.addProbe("probe2", "char *", "char *");
dtp.enable();

app.instrument(function (options){
  dtp.fire("probe1", function(){
    return [options.event, options.date];
  });
  dtp.fire("probe2", function(){
    return [options.event, options.args[0]];
  });
});

app.get('/', function (req, res){
  res.trace('index:hello');
  res.send('hello world');
})

app.listen(3000);

Example 3: Send information to chrome tracer.

You may want to use the chrome tracer to analyze only some response behaviour.

var express = require('./index');
var app = express();

var events = [];

app.instrument(function (options){
  if (options.event === 'start') {
    events.push({
      "name": options.args[0],
      "cat": "PERF",
      "ph": "B",
      "pid": process.pid,
      "ts": options.date.getTime()
    });
  } else if (options.event === 'end') {
    events.push({
      "name": options.args[0],
      "cat": "PERF",
      "ph": "E",
      "pid": process.pid,
      "ts": options.date.getTime()
    });
  }
})

app.get('/', function (req, res, next){
  res.trace('start', 'index');
  setTimeout(function () {
    res.send('hello world');
    res.trace('end');
  }, Math.random() * 100);
})

process.on('SIGINT', function (){
  require('fs').writeFileSync('./myfile', JSON.stringify(events));
  process.exit(0);
})

app.listen(3000);