opentsdb-datum

OpenTSDB datapoint model.

Usage no npm install needed!

<script type="module">
  import opentsdbDatum from 'https://cdn.skypack.dev/opentsdb-datum';
</script>

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.