OpenTSDB
JavaScript library for OpenTSDB.
Table of Contents
Installation
For use in Node.js,
$ npm install opentsdb
For use in the browser, use browserify.
Usage
This library is comprised of the following core components...
Client
To interface with OpenTSDB, one must first create a client. To do so,
var opentsdb = ;var client = opentsdb;
OpenTSDB clients are configurable and have the following methods...
client.host( [host] )
This method is a setter/getter. If no host
is provided, the method returns the configured host
. By default, the client host
is 127.0.0.1
. To point to a remote host
,
clienthost '192.168.92.11' ;
client.port( [port] )
This method is a setter/getter. If no port
is provided, the method returns the configured port
. By default, the client port is 4242
. To set a different port
,
clientport 8080 ;
client.ms( [bool] )
This method is a setter/getter. If no boolean flag is provided, the method returns the flag indicating whether time should be output at millisecond resolution. By default, millisecond resolution is on, so as to ensure timestamps are 13 digit UNIX timestamps. To turn off millisecond resolution,
client;
client.arrays( [bool] )
This method is a setter/getter. If no boolean flag is provided, the method returns the flag indicating whether data is output as an array
. By default, array
output is on. To turn off array
output,
client;
client.tsuids( [bool] )
This method is a setter/getter. If no boolean flag is provided, the method returns the flag indicating whether TSUIDs accompany data output. By default, TSUIDs are not returned. To turn on TSUID output,
client;
client.annotations( [option] )
This method is a setter/getter. If no option is provided, the method returns the option indicating whether annotations should accompany data output. OpenTSDB supports two types of annotations: local and global. By default, annotations are not returned.
Three options are possible: none
, local
, and all
. none
indicates to return no annotations. local
indicates to return only local annotations; i.e., annotations specific to a timeseries. all
indicates to return both local and global annotations. OpenTSDB does not support returning only global annotations.
To set annotation output,
client;
client.start( [time] )
This method is a setter/getter. If no time
is provided, the method returns the configured query start time
. Before making an OpenTSDB query, a start time is required. To do so,
// UNIX timestamp:clientstart Date-1000 ;// Absolute time:clientstart '2014/10/18 09:45' ;// Relative time:clientstart '72m-ago' ;
client.end( [time | null] )
This method is a setter/getter. If no time
is provided, the method returns the configured query end time
. An end time is optional when making an OpenTSDB query. If no end time is set upon making a query, OpenTSDB defaults to the time at which the request is made.
// UNIX timestamp:client;// Absolute time:client;// Relative time:client;
If at time t1
you specify an end time and later decide at t2
to make a request which does not specify an end time, you can null
the configuration value.
client;
client.queries( [query1, query2, query3,...] )
This method is a setter/getter. If no queries are provided, the method returns any previously set queries. Queries must be set before making an OpenTSDB data request. To create queries, see below.
client;
client.url()
Generate an OpenTSDB request URL based on a client's configuration. Both queries and a start time are required before running this method.
var url = client;
An example returned url
:
http://127.0.0.1:4242/api/query?ms=true&arrays=true&show_tsuids=true&no_annotations=true&global_annotations=false&start=72000ms-ago&end=60s-ago&m=avg:5s-avg:cpu.utilization{nid=*}&m=avg:5s-avg:mem.utilization{nid=*}
client.get( clbk )
Make a data request from OpenTSDB. Results are passed along to the provided callback.
client;
client.aggregators( clbk )
Requests the current list of supported aggregation operators from OpenTSDB. Results are passed along to the provided callback.
client;
client.metrics( clbk )
Requests the current list of stored metrics from OpenTSDB. Results are passed along to the provided callback.
client;
client.config( clbk )
Requests the current OpenTSDB configuration. Results are passed along to the provided callback.
client;
client.version( clbk )
Requests the current OpenTSDB version. Results are passed along to the provided callback.
clientversion {if errorconsole;return;console;};
client.dropcaches( clbk )
Instructs an OpenTSDB to purge its in-memory caches. The response is passed along to the provided callback.
client;
Queries
OpenTSDB permits two query types: metric and tsuid.
Metric queries are general queries which return an indeterministic number of timeseries. OpenTSDB implements metric queries by searching for timeseries matching the metric criteria, e.g., metric name
and tag
.
TSUID queries request a specific timeseries having a unique id. Every timeseries has an assigned unique identifier, which is based on metric name
and any tags
.
The opentsdb
module supports both query types. To create queries,
var opentsdb = ;// Metric query generator:var mQuery = opentsdb;// TSUID query generator:var tQuery = opentsdb;
The distinctions between the two types are 1) metric queries require a metric name and tsuid queries require a string list of tsuids and 2) tsuid queries do not accept tags. Otherwise, both types have the same methods, as outlined below.
query.aggregator( [aggregator] )
This method is a setter/getter. If no aggregator
is provided, returns the query aggregator. The default aggregator is avg
. To set a different aggregator
,
query;
query.downsample( [downsample] )
This method is a setter/getter. If no downsample
function is provided, returns the configured downsample
function. By default, downsampling is turned off (i.e., set to null
). To specify a downsample
function,
query;
query.rate( [bool] )
This method is a setter/getter. If no boolean flag is provided, returns the flag indicating whether to return the difference between consecutive data values. By default, the flag is false
. To turn on difference calculation,
query;
Note that rate calculation requires a set of three options.
query.rateOptions( [object] )
This method is a setter/getter. If no configuration object is provided, returns the rate options: counter
, counterMax
, resetValue
. counter
must be a boolean; counterMax
must be numeric or null
; and resetValue
must be numeric.
By default,
var rateOptions ="counter": false"counterMax": null"resetValue": 0;
mQuery.tags( [tag, [value]] )
This method is a setter/getter. If no arguments are provided, returns all tag names and their values. If a tag
name is specified, returns the value for that tag. Otherwise, sets a tag
to the specified value
.
mQuerytags 'nid' '*' ;
The *
(wildcard) indicates all values for a tag
.
mQuery.dtag( tag )
This method deletes a query tag
.
// Add a tag:mQuerytags 'nid' '*' ;// Delete the tag:mQuery;
mQuery.metric( [name] )
This method is a setter/getter. If no metric name
is provided, returns the query metric name
. A metric name
is required to encode a metric query. To set a metric name
,
mQuery;
tQuery.tsuids( [tsuids] )
This method is a setter/getter. If no tsuids
are provided, return the query tsuids
. tsuids
are required to encode a tsuid query. To set tsuids
,
// Comma-delimited string:tQuery;// Array:tQuery;
Data Model
OpenTSDB specifies a data model for every timeseries datapoint. To create an OpenTSDB datum,
var opentsdb = ;var datum = opentsdb;
A datum is configurable and has the following methods...
datum.metric( [name] )
This method is a setter/getter. If no metric name
is provided, returns the metric name
assigned to a datum. A metric name
is required to properly describe a datum. To set a metric name
,
datum;
datum.timestamp( [timestamp] )
This method is a setter/getter. If no timestamp
is provided, returns the timestamp
assigned to a datum. A timestamp
is required to properly describe a datum. To set a timestamp
,
datum;
A timestamp
may either be a date string or a UNIX timestamp.
datum.value( [value] )
This method is a setter/getter. If no value
is provided, returns the datum value
. A value
is required to properly describe a datum. To set a datum value
,
datumvalue Math ;
datum.tags( [tag, [value]] )
This method is a setter/getter. If no arguments are provided, returns all tag names and their values. If a tag
name is specified, returns the value for that tag. Otherwise, sets a tag
to the specified value
.
datumtags '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,
datumtags '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 )
This method deletes a datum tag
.
// Add a tag:datumtags 'beep' 'boop' ;// Delete the tag:datum;
datum.toString()
This method serializes the datum. The datum must have a metric name
, timestamp
, and value
to be serializable. To serialize a datum,
datum;
Because a datum
is configurable, a datum
serves as a factory for serializing similar data.
var data = 100 ;// Configure a datum:datumtags 'beep' 'boop'tags 'foo' 'bar' ;for var i = 0; i < datalength; i++// Assign values to the datum:datumvalue Math ;// Serialize and store:data i = datum;// Convert to a newline delimited string:data = data;console;
Socket
To interface with OpenTSDB, one can create a socket client. To do so,
var createSocket = ;var socket = ;
OpenTSDB socket clients are configurable and have the following methods...
socket.host( [host] )
This method is a setter/getter. If no host
is provided, the method returns the configured host
. By default, the client host
is 127.0.0.1
. To point to a remote host
,
sockethost '192.168.92.11' ;
socket.port( [port] )
This method is a setter/getter. If no port
is provided, the method returns the configured port
. By default, the client port is 4242
. To set a different port
,
socketport 8080 ;
socket.connect()
Creates a TCP socket connection.
socket;
socket.status()
Returns the current connection status. If a socket connection exists, returns true
. If no socket connection exists, returns false
.
socketstatus;
socket.strict( [bool] )
This method is a setter/getter. If no boolean flag
is provided, the method returns the strict setting. By default, the socket enforces strict type checking on socket writes. To turn off strict mode,
socket;
Turn off strict mode when you are certain that arguments provided to the socket.write()
method are of the proper type.
socket.write( string[, clbk] )
Writes to the socket connection. If strict mode is off
, no type checking of input arguments occurs. An optional callback is invoked after writing all data to the socket. To write to the socket,
var value = '';value += 'put ';value += 'cpu.utilization ';value += Date + ' ';value += Math + ' ';value += 'beep=boop ';value += 'foo=bar\n';socket;
socket.end()
Closes a socket connection. To close a socket,
socket;
Events
The socket is an event-emitter and emits the following events...
'connect'
The socket emits a connect
event upon successfully establishing a socket connection. To register a listener,
socket;
'error'
The socket emits an error
event upon encountering an error. To register a listener,
socket;
'close'
The socket emits a close
event when the other end of the connection closes the socket. To register a listener,
socket;
'warn'
The socket emits a warn
event when attempting to create a new socket connection when a connection already exists. To register a listener,
socket;
Notes
When used as setters, all setter/getter methods are chainable. For example,
var opentsdb =client = opentsdbmQuery = opentsdb;mQuerytags 'nid' '1234,5678'tags 'name' 'beep,boop' ;clienthost '192.168.92.111'port 8080start Date-1000;
Tests
Unit
Unit tests use the Mocha test framework with Chai assertions. 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
License
Copyright
Copyright © 2014. Athan Reines.