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

@uswitch/koa-tracer

v1.7.11

Published

πŸ•΅οΈβ€β™€οΈ A koa.js middleware to add namespaced tracing throughout a requests lifecycle

Downloads

5,491

Readme

Contributors License type language test style

Overview

This package is a Koa middleware to allow tracing the lifecycle of requests.

It tries to implement the following features;

  • πŸ“š Namespaced traces - Group messages by namespace
  • ⏱ Namespaced relative time diffs - Display the time difference in ms between events in a namespace.
  • 🌍 Global relative time diffs - Display time difference between request start and the trace event.
  • πŸ› Decoupled errors - Log errors separately to handling them.
  • πŸ† Per-request trace - Be able to reference all traces back to a single request!

All of this is designed to make it easy to rebuild an exact picture of what's happening in your code.


Usage

npm install @uswitch/koa-tracer
import Koa from 'koa'
import tracer, { eventTrace, eventError } from '@uswitch/koa-tracer'

const app = new Koa()

app.use(tracer())

app.on('koa-tracer:trace', Logger.log)
app.on('koa-tracer:error', Logger.err)

app.on(eventTrace, Logger.log)
app.on(eventError, Logger.err)

This package uses Event Emitters to decouple the handling of runtime tracing and logging. This means that you can decide how you want to handle traces and errors separately with the koa-tracer:trace & koa-tracer:error events.

However, all traces and errors are available in Koa's context at the end of the request to be used in access logs, or whatever else you might want.

Functions

trace - fn (ctx, &optional namespace, message)
  • ctx [Object] - the Koa ctx object
  • namespace [String] - A namespace for the trace message (optional)
  • message [String/Object] - The details of the trace message
traceError - fn (ctx, err)
  • ctx [Object] - the Koa ctx object
  • err [String/Object/Error] - The details of the error

API

Objects

A trace object looks like this;

{
  "time": "Date"       // Date for when that trace was logged
  "timeDiff": 12       // Diff in `ms` between this and the first trace in namspace
  "initDiff": 30       // Diff in `ms` between this and start of request
  "msg": "Foo bar"     // The message passed into the trace
  [other properties]   // Any other properties passed in
}

An error object looks like this;

{
  "time": "Date"       // Date for when that error was logged
  "msg": "Foo bar"     // The message passed into the error
  [other properties]   // Any other properties passed in
}

The state object, after a request, looks like this;

{
  "traceStart": "Date" /* Date when the request started */
  "trace": {
    "__general": { 
      traces: [ /* List of traces without namespace */ ],
      tracesLength: 50,
      timeDiff: 12,
      initDiff: 12
    },
    "namespace": { 
      traces: [ /* List of traces in `namespace` */ ],
      tracesLength: 50,
      timeDiff: 12,
      initDiff: 12
    }
  }

  "errorsCount": 12  /* The number of errors occured in this request */
  "errors": [
    /* List of errors */
  ]
}

Events

The following events are fired on trace and error logging completion.

  • koa-tracer:trace => ({ ctx, key, trace })
  • koa-tracer:error => ({ ctx, error, original })

N.B. These events are called with Objects

They can be imported from the koa-tracer module, as

import { eventTrace, eventError } from 'koa-tracer'

Contributors

Thanks goes to these wonderful people (emoji key):

| Dom CharlesworthπŸ’» πŸ“– πŸ€” πŸ”Œ | | :---: |

This project follows the all-contributors specification. Contributions of any kind welcome!