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

spm-agent

v2.2.5

Published

Node.js agent framework for SPM by Sematext

Downloads

1,514

Readme

Sematext Agent Framework for Node.js

A framework for monitoring agents written in Node.js for sending metrics to Sematext Cloud or InfluxDB. The Sematext Node.js monitoring agent is built on top of this framework.

Functionality

  • Sender interface to Sematext backend receivers
  • Buffering metrics in case of network outages
  • Reconnect after failures
  • Logging functions
  • Configuration handling
  • Pluggable agents

Example to Implement a Monitoring Agent

Sematext Cloud supports the Influx Line Protocol for metric ingestion.

Agent modules must provide a start and stopfunction.

The function agent.addMetrics(metric) collects a metric, which is shipped in bulk requests via Influx Line Protocol to the metrics receiver.

All metric objects, must have the mandatory fields measurement, tags and fields:

  • measurement - the name of the measurement e.g. 'process.memry'
  • tags - key/value pairs, useful for filtering or aggregation of data
  • fields - numeric fields with the measurement values

const SpmAgent = require('spm-agent')
const client = new SpmAgent()
const os = require('os')

// configure client, with non-default values
// or use the file ./.spmagentrc in YAML format
// the default configuration contains values for Sematext Cloud US
// SpmAgent.Config.tokens.spm = process.env.MONITORING_TOKEN
SpmAgent.Config.influx = {
  dbName: 'metrics',
  // change receiver to Sematext Cloud EU
  // default is spm-receiver.sematext.com for US region
  host: 'spm-receiver.eu.sematext.com',
  port: 443,
  protocol: 'https'
}

// a monitoring agent needs a start an stop function
class MemoryMonitor {
  start (agent) {
    this.agent = agent
    // initialize your metrics collector ...
    // Typically agents collect metrics periodically, every N seconds. The time between // two collection activities is the collectionInterval, specified in milliseconds.
    this.tid = setInterval(function () {
      const measurement = {
        // measurment namespace
        measurement: 'process.memory',
        timestap: new Date(),
        tags: {
          role: 'frontend',
          'os.host': os.hostname()
          // The monitoring token can be set as 'tags.token' value
          // Routing a metrics to a different monitoring app
          // requires setting the `token` tag
          // ,token = process.env.OTHER_APP_TOKEN
        },
        // metrics names and values
        fields: {
          rss: process.memoryUsage().rss
        }
      }
      // pass metrics to sender schedule
      agent.addMetrics(measurement)
    }, SpmAgent.Config.collectionInterval)
  }

  stop () {
    if (this.tid) {
      clearInterval(this.tid)
    }
  }
}

client.createAgent(new SpmAgent.Agent(new MemoryMonitor()))

// log collected metrics to console
// observe all metrics for testing/debuggin
client.on('metrics', console.log)
// observe a specific metric using the measurment name
client.on('process.memory', console.log)

Metric Tags

The agent is able to use environment variables as metrics tags. Define a list of environment variables to be used as metric tags.

Let's assume this is the enviroment of the monitored application:

> env
TERM_PROGRAM=Apple_Terminal
TERM=xterm-256color
SHELL=/bin/bash
TMPDIR=/var/folders/v6/bh2q1xsn27g4d2g54z2ylv100000gn/T/
TERM_PROGRAM_VERSION=404.1
TERM_SESSION_ID=F12E0DBD-BF40-466D-85EC-08E89EDC3440
USER=stefan
PWD=/Users/stefan
HOME=/Users/stefan
LOGNAME=stefan
LC_CTYPE=UTF-8
DISPLAY=/private/tmp/com.apple.launchd.J60ybGpban/org.macosforge.xquartz:0
_=/usr/bin/env

You want to see later the performance metrics aggregated or filtered for specific user. In this case the agent should collect the user name as tag. You can find the user name in the process environment variable USER. Let's also assume the workingdirectory of the application PWD is another relevant identifier for your monitored application.

To configure the agent collecting USER and PWD as tags you can specify a commaseparated list of environment variables for the the agent configuration:

# add the values of env. variables USER, PWD as tags to metrics
export MONITORING_TAGS_FROM_ENV="USER, PWD"

The generated metrics include then the environment variable name and its value as tags. The result in Influx Line Protocol (ILP) format, produced by the agent for metric called swap:

swap, USER=stefan,PWD=/Users/stefan out=0i,in=0i 1576765680000000000

Define static tags in key:value format

export MONITORING_TAGS_FROM_ENV="organisation:sematext, service:api"

The result in in ILP format:

swap, organisation=sematext,service=api out=0i,in=0i 1576765680000000000

Both types of tags can be mixed:

export MONITORING_TAGS_FROM_ENV="organisation:sematext, service:api, USER, PWD"

The result in in ILP format:

swap, organisation=sematext,service=api,USER=stefan,PWD=/Users/stefan,os.host=imac.local out=0i,in=0i 1576765680000000000

The config file entry influx.tagsFromEnv in .spmagenrc works as well:

tokens: 
  spm: 'YOUR_MONITORING_TOKEN'
  infra: 'YOUR_INFRA_MONITROING_TOKEN'
influx:
  tagsFromEnv: 'organisation:sematext, USER, PWD' 
  dbName: 'metrics'
  host: spm-receiver.sematext.com
  protocol: https
  port: 443
  

Contribute

Let us know about monitoring agents you need, maybe you like to contribute with your domain expertise!

Related Modules