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

service-integrity

v0.0.2

Published

Provides an express middleware to setup a health integrity check endpoint according to spec

Downloads

275

Readme

Service Integrity

Description and Rationale

This package provides a drop-in set of functions that provide your production-ready app with some much needed uptime integrity checking of dependent services. This endpoint will be checked by upstream uptime integrity tools managed by the ops team. The data will eventually be available to see our uptime metrics.

What's a dependent service? Any major technology or backend that your app relies on to function correctly. Think databases, caches, hardware limits (e.g. disk or memory), and even potentially other application services (e.g. core auth, s3, etc). Be careful pinging other services though, you may just end up causing a cascading failure across all your products if not done carefully.

The heart of this package is the createHealthCheck function which accepts two arguments:

  • Your express app, ready for some useing.
  • A map of services you wish to report on. The services should be keyed by a string of your choosing with the value being a function taking the request and response, and whose result is a promise which should return a service status object. Checkout src/status.ts for type signatures, or better yet just use the built-in status returning functions: status.ok, status.warn, or status.error. Each can take an optional string message which will be available in any downtime notifications helping us get to the root of the problem quicker.

Documentation

Read the integrity health check specification to learn more.

Installation

yarn add service-integrity

Usage

const express = require('express')
const { createHealthCheck, status } = require('service-integrity')
const Sequelize = require('sequelize')

const verifyMysqlIntegrity = (req, res) =>
  Promise.resolve(new Sequelize(...connectionParams))
    .then(connection => connection.query('SELECT count(id) FROM mytable'))
    .then(result => {
      if (result.count <= 0) {
        // Report a potential issue with MySQL
        return status.warn("Expected some rows but didn't get any")
      }

      // Report that everything with MySQL is OK
      return status.ok()
    })
    .catch(error => status.error(error.message)) // Report MySQL is down

const app = express()

// The following function will register a GET endpoint at /health/integrity which returns a JSON
// response corresponding to the payload Schema described in the linked documentation.
createHealthCheck(app, {
  mysql: verifyMysqlIntegrity
  // ... add any other checks you want
})

Or use async/await if that's your preference:

const express = require('express')
const { createHealthCheck, status } = require('service-integrity')
const Sequelize = require('sequelize')

const verifyMysqlIntegrity = async (req, res) => {
  const connection = new Sequelize(...connectionParams))
  try {
    const result = await connection.query('SELECT count(id) FROM mytable'))
    if (result.count <= 0) {
      // Report a potential issue with MySQL
      return status.warn("Expected some rows but didn't get any")
    }

    // Report that everything with MySQL is OK
    return status.ok()
  } catch (error) {
    return status.error(error.message)) // Report MySQL is down
  }
}

const app = express()

// The following function will register a GET endpoint at /health/integrity which returns a JSON
// response corresponding to the payload Schema described in the linked documentation.
createHealthCheck(app, {
  mysql: verifyMysqlIntegrity
  // ... add any other checks you want
})

You can also pass a set of middleware to invoke before handling the request.

// ...

const getMysqlConnection = (req, res, next) => {
  res.locals.mysqlConnection = buildConnectionForRequest(req)
  next()
}

const services = {
  mysql: (req, res) =>
    res.locals.mysqlConnection
      .query('SELECT count(id) FROM mytable')
      .then(result => status.ok('looking good'))
}

const options = {
  middleware: [getMysqlConnection /* ... */]
}

createHealthCheck(app, services, options)

// ...