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

monitoring-stream

v1.0.0

Published

Monitor a URI's connectivity

Downloads

11

Readme

:toc: macro :toc-title: :toclevels: 9

Monitoring Stream

image:https://travis-ci.org/jasonkuhrt/monitoring-stream.svg?branch=master["Build Status", link="https://travis-ci.org/jasonkuhrt/monitoring-stream"]

toc::[]

Installation

yarn add monitoring-stream

About

This is a tiny library that makes it trivial to monitor the results of repeatedly executing an asynchronous action. A canonical use-case would be monitoring a URL for healthy (e.g. status code 200) responses but really it could be anything, including actions that don't do any IO but still have something to monitor such random computation. This is a fairly general tool and it interestingly generalizes some things that previously would have been realized with purpose-built code. For example the concept of request-retry can be generally implemented with this library, see the example below.

Examples

Declarative Request-Retry

We can combine streams together to declaratively create request-retry logic. Inspiration for this example was taken from the imperative request-retry logic in https://github.com/elastic/elasticsearch-js/blob/master/test/integration/yaml_suite/client_manager.js#L30-L42[this ElasticSearch test suite] which establishes the connectivity with the database before running its test suite.

import Monitor from "monitoring-stream"

const runTests = () =>
  Promise.resolve("Your integration test suite here...")

const pingElasticSearch = () =>
  Boolean(Math.round(Math.random()))
    ? Promise.reject(new Error("Foobar network error"))
    : Promise.resolve("OK!")

const pingInterval = 1000
const pingAttempts = 3
const monitor = Monitor.create(pingElasticSearch, pingInterval)
const maxRetries = monitor.downs.takeUntil(monitor.ups).take(pingAttempts)

monitor
  .ups
  .take(1)
  .takeUntil(maxRetries)
  .drain()
  .then((result) =>
    result.isResponsive
      ? runTests()
      : Promise.reject(result.data)
  ))

API

Types

MonitorEvent

type MonitorEvent<A> = {
  isResponsive: boolean,
  isResponsiveChanged: boolean,
  data: A | null
  error: Error | null
}

A monitor event represents the result of an action invocation. The type variable A represents the action's resolution type.

isResponsive indicates if the action is resolving while isResponsiveChanged indicates if that state is different than the previous event. For the first check wherein there is no such previous event isResponsiveChanged is considered to be true.

You can think of isResponsive as being your health indicator while isResponsiveChanged as being your alert to when crashing or recovery occurs. isResponsiveChanged is technically a convenience that you could calculate yourself but seeing as its often needed and that its calculation requires state it seems worthwhile.

.create

Monitor.create(action: () => Promise<A>, intervalMs: number?) => Observable<MonitorEvent<A>>
  • action The async action to recurse. This could be anything: HTTP requests, pure calculation, file system checks...

  • intervalMs The milliseconds between each action invocation. Defaults to 1000.

  • returns An observable stream of MonitorEvent. The observable implementation we use is https://github.com/cujojs/most[most].

Event State Helpers

There are several statically exported predicate functions that can tell you what kind of state a MonitorEvent is in.

.isUp
Monitor.isUp(event: MonitorEvent) => boolean

Returns true if event isResponsive is true.

.isDown
Monitor.isDown(event: MonitorEvent) => boolean

Returns true if event isResponsive is false.

.isRise
Monitor.isRise(event: MonitorEvent) => boolean

Returns true if event isResponsive is true and isResponsiveChanged is true.

.isFall
Monitor.isFall(event: MonitorEvent) => boolean

Returns true if event isResponsive is false and isResponsiveChanged is true.

.isChanges
Monitor.isChanges(event: MonitorEvent) => boolean

Returns true if event isResponsiveChanged is true.

Sub-streams

There are several additional streams on monitor instances that are filtered by event state predicates. These sub-streams provide a convenient way to consume just a subset of events without any additional code/work from you.

#ups

Stream filtered by Monitor.isUp

#downs

Stream filtered by Monitor.isDown

#rises

Stream filtered by Monitor.isRise

#falls

Stream filtered by Monitor.isFall

#changes

Stream filtered by Monitor.isChange