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

horus-agent

v1.2.0

Published

Autoinstrumentation for metrics and traces, used in the Horus infrastructure.

Downloads

11

Readme

Purpose

This npm package allows you to quickly and seamlessly add instrumentation to the backend of your application. It will automatically generate and export metrics and traces. It is used in the Horus infrastructure.

Set Up

Set Up Generation of Metrics & Traces

In your root service, do the following:

  1. Install the package using npm.
npm install horus-agent
  1. Import agents from horus-agent at the top of code file.
const { MetricsAgent, TracingAgent} = require("horus-agent")
  1. Set up tracing by invoking the TracingAgent function and pass it the name you would like it to be identified by. This should be invoked directly beneath your imported agents and before any other services. Most people give it the name of the service/part of the app they are tracing.
// imported agents...

TracingAgent("checkout-service")

// all other services...
  1. Beneath the initialization of express but above all of your routing, pass startLatency and countRequests from MetricsAgentto the server.
const express = require('express');
const app = express();

app.use(
  MetricsAgent.startLatency,
  MetricsAgent.countRequests
)

// routes...
  1. Beneath all the routes on the same page, pass countErrors and endLatency from MetricsAgentto the server.
// routes...

app.use(
  MetricsAgent.countErrors,
  MetricsAgent.endLatency
)
  1. In every route (or every route that you'd like to monitor), pass in next as a parameter and invoke next() at the very end.
// example route

app.get('/dashboard', async (req, res, next) => {
  const movies = await getUrlContents('http://localhost:4000/movies', nodeFetch);

  res.type('json');
  res.send(JSON.stringify({ dashboard: movies }));

  next(); // next() is the last line of the route
})
  1. In order for errors to be detected by MetricsAgent, you must explicitly throw an error within a middleware or in a route handler. This is because when there is an error in Express, by default it will give a status code that matches the error, but it will not throw an error unless you explictly tell it to.

You can throw an error in a route:

app.get('/dashboard', async (req, res, next) => {
  const movies = await axios.get('http://localhost/information');
  
  if (movies.body.length === 0) {
    return next (new Error('500')); // custom error to be thrown if information is empty
  }

  res.type('json');
  res.send(JSON.stringify({ dashboard: movies }));

  next(); // next() is the last line of the route
})

Or you can use a custom catch all middleware, placed beneath all other route handlers in the file:

// all other route handlers

app.use(function(req, res, next) {
  if (!req.route) {                     // if the route does not exist (can add to this if/else conditional)
      return next (new Error('404'));   // throw a 404 error
    }  

  next();                              // send the req to the next middleware
})

// app.listen....

You're done!

Traces in Other Services

If you would like to see detailed spans/traces throughout every service of your application, add a TracingAgent to the top of the backend file for each service. Having the MetricsAgent in your root service alone is sufficient for metrics generation and capturing. Do not add a MetricsAgent to other services in the same application.

At the top of the backend file for another service:

const { TracingAgent } = require("horus-agent")
TracingAgent("inventory-service")

Set Up Exporting of Metrics & Traces

Change the endpoint in config.json to point to the host of your choice.

If you are hosting Horus via Docker on your local machine, you can keep the endpoint at localhost (default). Otherwise, if you are hosting Horus via Docker on a VPS (e.g. DigitalOcean or AWS), replace it with the IP address or domain name.