honeycombio/dynsampler-js

Name: dynsampler-js

Owner: Honeycomb

Description: Dynamic samplers for JS

Created: 2018-02-16 21:05:16.0

Updated: 2018-03-19 21:59:17.0

Pushed: 2018-03-19 21:59:16.0

Homepage: null

Size: 71

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Dynamic Sampler

This is a collection of samplers that can be used to provide sample rates when sending data to services like honeycomb

dynsampler is a javascript library for doing dynamic sampling of traffic before sending it on to Honeycomb (or another analytics system). It contains several sampling algorithms to help you select a representative set of events instead of a full stream.

A “sample rate” of 100 means that for every 100 requests, we capture a single event and indicate that it represents 100 similar requests.

For more information about using Honeycomb, see our docs.

Installation

 add dynsampler

Usage

With defaults:
rt { AvgSampleRate } from "dynsampler";
t sampler = new AvgSampleRate();

t rate = sampler.getSampleRate("my key");
With options
rt { AvgSampleRate } from "dynsampler";
t sampler = new AvgSampleRate({
earFrequencySec: 100,
alSampleRate: 50

Choosing a Sampler

This package is intended to help sample a stream of tracking events, where events are typically created in response to a stream of traffic (for the purposes of logging or debugging). In general, sampling is used to reduce the total volume of events necessary to represent the stream of traffic in a meaningful way.

There are a variety of available techniques for reducing a high-volume stream of incoming events to a lower-volume, more manageable stream of events. Depending on the shape of your traffic, one may serve better than another, or you may need to write a new one! Please consider contributing it back to this package if you do.

Implementing New Samplers

The Sampler class includes:

You can extend it to create new samplers. updateMaps is the only function that needs to be defined, but it is often useful to collect additional configuration from the constructor:

rt { Sampler } from "dynsampler";

rt class PerKey extends Sampler {
nstructor(opts = {}) {
super(opts);
this.perKeyThroughputSec = opts.perKeyThroughputSec || 5;

dateMaps() {
if (this.currentCounts.size == 0) {
  //no traffic in the last 30s. clear the result Map
  this.savedSampleRates.clear();
  return;
}
const actualPerKeyRate = this.perKeyThroughputSec * this.clearFrequencySec;

const newRates = new Map();
this.currentCounts.forEach((val, key) => {
  newRates.set(key, Math.floor(Math.max(1, val / actualPerKeyRate)));
});
this.savedSampleRates = newRates;


Modifying getSampleRate

Sometimes it makes sense to check additional state in getSampleRate and return a different result based on that. When overriding the function call super.getSampleRate.

s MySampler extends Sampler {
nstructor(opts = {}) {
super(opts);
this.hasReceivedTraffic = false;

dateMaps() {
// other logic
this.hasReceivedTraffic = true;

tSampleRate(key) {
const superSampleRate = super.getSampleRate(key);
if (!this.hasReceivedTraffic) {
  return this.goalSampleRate;
} else {
  return superSampleRate;
}



This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.