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

agentslug

v2.0.0

Published

AgentSlug.com node SDK

Downloads

39

Readme

AgentSlug.com node SDK

Build Status Coverage Status npm version

What is it?

This package is the client SDK for AgentSlug.com services. Currently it supports Ping which is a client for AgentSlug.com application uptime monitoring.

What is Application Uptime Monitoring?

AgentSlug.com provides multiple website/app uptime monitoring tools. The main one is the Uptime Monitoring. A tool which tests a website by sending a HTTP request and checking it's response code and checking if provided keyword can be found in the response body.

The other one, which works in an opposite manner, is the "Application monitoring", here called Ping. For this test, AgentSlug.com waits for a ping signal, configured in the AgentSlug.com settings. As long as ping signals come in a regular manner, everything is fine. At the moment there is no pings registered beyond a timeout set for a test, an alarm is triggered (with e-mail and/or Slack notification).

How to use this package?

SDK configuration

This software is intended to be used only for registered AgentSlug.com users. In order to use it successfully, you need an API token and configured Application Monitoring test on AgentSlug.com.

Simplest usage for a single function monitoring.

// Importing Ping
const { Ping } = require('agentslug');
// or:
// const Ping = require('agentslug/Ping');

const API_TOKEN = '[YOUR_API_TOKEN]';
const PING_ID = '[PING_ID_TAKEN_FROM_AGENTSLUG.COM]';
const pingInstance = new Ping({token: API_TOKEN});

// Important function that needs to be monitored.
function somethingImportant() {
  // Doing something important.
  
  // When done, sending ping to AgentSlug.com to inform it that this function finished successfully
  // This function is a "fire and forget" function. It schedules a http request to AS API and returns undefined.
  pingInstance.send(PING_ID);
}

// Example interval for the function.
setInterval(() => somethingImportant, 5000);

Singleton use case for bigger applications with multiple ping configurations

For bigger applications you might want to use our singleton helper to register the Ping client once and use it anywhere.

// init.js or bootstrap.js, whatever runs first in your app
const { initSingleton } = require('agentslug/Ping/singleton');
const API_TOKEN = '[YOUR_API_TOKEN]';
// init a singleton Ping, since next line, every `getSingleton` would return same instance of Ping API Client.
initSingleton({ token: API_TOKEN })
// someFunction.js
const { getSingleton } = require('agentslug/Ping/singleton');
const PING_ID = '[PING_ID_TAKEN_FROM_AGENTSLUG.COM_FOR_SOME_FUNCTION]';
// Ping is an instance of previously created Ping.
const ping = getSingleton();
// do something important
ping.send(PING_ID);
// otherFunction.js
const { getSingleton } = require('agentslug/Ping/singleton');
const PING_ID = '[PING_ID_TAKEN_FROM_AGENTSLUG.COM_FOR_OTHER_FUNCTION]';
// Ping is an instance of previously created Ping.
const ping = getSingleton();
// do something important
ping.send(PING_ID);

Success / error handling

Fire and forget

All Ping.send functions schedule a http request to AgentSlug.com API, and immediately return to not block current operation. This way your application monitoring should not have much impact on the appliction performance.

Because of that, errors and successes are handled within events.

Possible events

sent

Emitted on Ping instance when ping was successfully sent. Keep in mind not all Ping.send executions would end up in sent event because of throttling

error

Emitted on Ping instance on error. Usually it catches networking errors, e.x. if our API is down, you should expect many logs from this handler.

Example usage

const { Ping } = require('agentslug');

const pingInstance = new Ping({ token: 'TOKEN'});

pingInstance.on('error', (err) => {
  logger.error('Ping error', err);
});
pingInstance.on('sent', () => {
  logger.info('Ping sent');
});

Throttling

It's expected that Ping client is used in functions which are executed very often. Of course, you should not flood the API with too many requests - that would lead to ban of the client IP.

This SDK is however safe to use since it implements internal throttling.

Whenever Ping.sent is called, internal throttling mechanism checks if it's safe to send an http requests to the API or if this call should be skipped.

To be exact, very first Ping.sent call always results with http request and all consecutive calls executed in next 20 seconds would be ignored.