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

probe-trace

v0.1.1

Published

Instrument code with probes.

Downloads

6

Readme

probe-trace

Stability: 1 - Experimental

Instrument code with probes.

Installation

npm install probe-trace

Tests

npm test

Benchmarks

npm run-script benchmark

Overview

probe-trace enables instrumenting live Node.js code using probes using only JavaScript.

It is sometimes useful to instrument live code in order to find out what's going on. In such cases, for code designed to support it, one could open a REPL to a running Node.js process, add instrumentation, and turn on various probes.

Example

We will use the Node.js REPL to illustrate instrumenting live code. For this purpose we will use examples/hello.js module as an example. The module is an HTTP server that returns "hello world" via calling a hello() and a world() function to construct the response.

node
> var Hello = require('./examples/hello.js');
undefined
> var hello = new Hello();
undefined
> hello.listen(8080, function () { console.log('listening on 8080...'); });
undefined
> listening on 8080...

At this point, you can curl localhost:8080 to see that the server is up.

We will now attach a probe to the

> var Probe = require('./index.js');
undefined
> var probe = new Probe();
undefined
> Hello.prototype.hello = probe.instrument('hello', Hello.prototype.hello);
{ [Function] _original: [Function: hello] }

You can now curl localhost:8080 and see that it still works.

We will dump the entry event to the console.

> probe.on('~probe:hello:enter', function (event) { console.dir(event); });
{ activeProbes: {},
  _events: { '~probe:hello:enter': [Function] } }
> probe.addProbe('hello');
undefined

Now, when you curl localhost:8080 you'll see output in the console:

> { name: 'hello',
  context: undefined,
  timestamp: 1380487865677,
  args: [] }

To turn off the probe again

> probe.removeProbe('hello');
undefined

Now, curl localhost:8080 will not log anymore.

To return the server to original state:

> Hello.prototype.hello = probe.uninstrument(Hello.prototype.hello);

curl localhost:8080 continues to work.

Performance

Instrumenting functions has its performance penalty, hence it is useful to uninstrument() when no longer needed.

The current performance benchmark gives intuition as to the cost of probe-trace instrumentation:

Starting benchmark...
Running uninstrumented...
Running instrumented but off...
Running instrumented and on...
Running instrumented, on, and with listeners registered...
Benchmark results:
  uninstrumented time                    :     7655464ns  130625655 calls/sec
  instrumented but off time              :   154998699ns    6451667 calls/sec
  instrumented and on time               :  4746867607ns     210665 calls/sec
  instrumented and on with listeners time:  5128034667ns     195006 calls/sec

Documentation

Probe

Public API

new Probe()

Creates a new Probe instance.

probe.addProbe(name)

  • name: String Name of a probe to turn on.

Turns on a probe for previously instrumented function.

probe.instrument(name, func)

  • name: String Name of the probe.
  • func: Function Function to instrument.
  • Return: Function Instrumented function.

Instruments a function for probing and relates it to the name.

probe.removeProbe(name)

  • name: String Name of a probe to turn off.

Turns off a previously turned on probe for a function.

probe.uninstrument(func)

  • func: Function Previously instrumented function.
  • Return: Function Uninstrumented function.

If the func has not been previously instrumented, it is simply returned.

Event ~probe:<name>:enter

  • event: Object
    • name: String Name of the probe.
    • context: Object Reserved for future use.
    • timestamp: Integer Result of new Date().getTime().
    • args: Array Arguments passed to the instrumented function.

Emitted upon entry into an instrumented function for which the probe is on.

Event ~probe:<name>:error

  • event: Object
    • name: String Name of the probe.
    • context: Object Reserved for future use.
    • timestamp: Integer Result of new Date().getTime().
    • duration: Integer Instrumented function duration in nanoseconds.
    • args: Array Arguments passed to the instrumented function.
    • error: Any Any exception thrown by the instrumented function.

Emitted if an instrumented function, for which the probe is on, throws. After the event is emitted, the throw is propagated.

Event ~probe:<name>:return

  • event: Object
    • name: String Name of the probe.
    • context: Object Reserved for future use.
    • timestamp: Integer Result of new Date().getTime().
    • duration: Integer Instrumented function duration in nanoseconds.
    • args: Array Arguments passed to the instrumented function.
    • result: Any Any result returned by the instrumented function.

Emitted upon return from an instrumented function for which the probe is on.