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

opentsdb-datum

v1.0.0

Published

OpenTSDB datapoint model.

Downloads

68

Readme

Datum

NPM version Build Status Coverage Status Dependencies

OpenTSDB datapoint model.

This library implements the OpenTSDB data model, where the model represents a single timeseries datapoint (datum).

Installation

$ npm install opentsdb-datum

Usage

var createDatum = require( 'opentsdb-datum' );

Data Model

To create an OpenTSDB datum,

var datum = createDatum();

A datum is configurable and has the following methods...

datum.metric( [name] )

This method is a setter/getter. If no metric name is provided, the method returns the metric name assigned to a datum. A metric name is required to properly describe a datum. To set a metric name,

// Set a metric name:
datum.metric( 'cpu.utilization' );

// Get a metric name:
var metric = datum.metric();
// returns 'cpu.utilization'

datum.timestamp( [timestamp] )

This method is a setter/getter. If no timestamp is provided, the method returns the timestamp assigned to a datum. A timestamp is required to properly describe a datum. To set a timestamp,

var ts = Date.now();

// Set a timestamp:
datum.timestamp( ts );

// Get a timestamp:
var v = datum.timestamp();
// returns <ts>

A timestamp may either be a date string or a UNIX timestamp. For further details, see the time validation utility.

datum.value( [value] )

This method is a setter/getter. If no value is provided, the method returns the datum value. A value is required to properly describe a datum. To set a datum value,

var value = Math.random();

// Set a value:
datum.value( value );

// Get a value:
var v = datum.value();
// returns <value>

datum.tags( [tag, [value]] )

This method is a setter/getter. If no arguments are provided, the method returns all tag names and their values. If a tag name is specified, the method returns the value for that tag. Otherwise, the method sets a tag to the specified value. At least one tag is required to properly describe a datum. To set a tag,

// Set a tag:
datum.tags( 'beep', 'boop' );

// Get a tag:
var tag = datum.tags( 'beep' );
// returns 'boop';

// Get all tags:
var tags = datum.tags();
// returns {'beep':'boop'}

A tag is an additional piece of information which describes a datum. For example, a cpu.user timeseries datum may originate from a particular host; e.g., host=webserver1. To later be able to query OpenTSDB for only those cpu.user timeseries originating from webserver1 while optimizing for aggregations across multiple web servers, OpenTSDB allows data tagging. In this case,

datum.tags( 'host', 'webserver1' );

The decision to tag a datum or include additional information in the metric name depends on your naming schema. Be careful to consider your query needs before deciding one way or the other.

datum.dtag( tag )

Deletes a datum tag.

// Add a tag:
datum.tags( 'beep', 'boop' );

// Delete the tag:
datum.dtag( 'beep' );

datum.toString()

Serializes the datum. A datum must have a metric name, timestamp, value, and at least one tag to be serializable. To serialize a datum,

datum = createDatum();

datum.metric( 'cpu.utilization' )
	.timestamp( 1456814180309 )
	.value( 0.94 )
	.tags( 'beep', 'boop' )
	.tags( 'foo', 'bar' );

var str = datum.toString();
// returns 'cpu.utilization 1456814180309 0.94 beep=boop foo=bar'

Notes

  • When used as setters, all setter/getter methods are chainable. For example,

    var datum = createDatum();
    
    var str = datum
    	.metric( 'cpu.utilization' )
    	.timestamp( Date.now() )
    	.value( Math.random() )
    	.tags( 'beep', 'boop' )
    	.tags( 'foo', 'bar' )
    	.toString();
    // returns <string>
  • Because a datum is configurable, a datum serves as a factory for serializing similar data.

    var datum = createDatum();
    
    var data = new Array( 100 );
    
    // Configure a datum:
    datum
    	.metric( 'cpu.utilization' )
    	.tags( 'beep', 'boop' )
    	.tags( 'foo', 'bar' );
    
    for ( var i = 0; i < data.length; i++ ) {
    	// Assign values to the datum:
    	datum
    		.timestamp( Date.now() )
    		.value( Math.random() );
    
    	// Serialize and store:
    	data[ i ] = datum.toString();
    }
    
    // Convert to a newline delimited string:
    data = data.join( '\n' );
    
    console.log( data );
    // returns <string>

Examples

var createDatum = require( 'opentsdb-datum' );

// Create a new datum instance:
var datum = createDatum();

// Configure the datum:
datum
	.metric( 'cpu.utilization' )
	.tags( 'beep', 'boop' )
	.tags( 'foo', 'bar' );

// Give the datum a timestamp and value:
datum
	.timestamp( Date.now() )
	.value( Math.random() );

// Serialize the datum:
console.log( datum.toString() );
/* returns
	"cpu.utilization <timestamp> <value> beep=boop foo=bar"
*/

// One can use a datum as a serialized datum factory...
var data = new Array( 100 );
for ( var i = 0; i < data.length; i++ ) {
	datum
		.timestamp( Date.now() )
		.value( Math.random() );

	data[ i ] = datum.toString();
}

// Convert the data to a newline delimited string:
data = data.join( '\n' );

console.log( data );
/* returns
	"cpu.utilization <timestamp> <value> beep=boop foo=bar"
	"cpu.utilization <timestamp> <value> beep=boop foo=bar"
	"cpu.utilization <timestamp> <value> beep=boop foo=bar"
	...
	"cpu.utilization <timestamp> <value> beep=boop foo=bar"
*/

To run the example code from the top-level application directory,

$ node ./examples/index.js

Tests

Unit

This repository uses tape for unit tests. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

Browser Support

This repository uses Testling for browser testing. To run the tests in a (headless) local web browser, execute the following command in the top-level application directory:

$ make test-browsers

To view the tests in a local web browser,

$ make view-browser-tests

License

MIT license.

Copyright

Copyright © 2014-2016. The OpenTSDB.js Authors.