@metrics/metric
Advanced tools
Comparing version 1.0.0 to 2.0.0
'use strict'; | ||
const util = require('util'); | ||
const is = require('./is'); | ||
const metric = Symbol('metric'); | ||
const source = Symbol('source'); | ||
const notEmpty = Symbol('empty'); | ||
const _description = Symbol('metric:key:description'); | ||
const _timestamp = Symbol('metric:key:timestamp'); | ||
const _source = Symbol('metric:key:source'); | ||
const _labels = Symbol('metric:key:labels'); | ||
const _value = Symbol('metric:key:value'); | ||
const _time = Symbol('metric:key:time'); | ||
const _meta = Symbol('metric:key:meta'); | ||
const _type = Symbol('metric:key:type'); | ||
const _name = Symbol('metric:key:name'); | ||
const Metric = class Metric { | ||
constructor(metricObj) { | ||
this[metric] = metricObj; | ||
this[source] = ''; | ||
constructor({ | ||
description, | ||
timestamp = null, | ||
source = null, | ||
labels = [], | ||
value = null, | ||
time = null, | ||
meta = {}, | ||
type = 0, | ||
name, | ||
} = {}) { | ||
// Required arguments | ||
if (!name) throw new Error('The argument "name" must be provided'); | ||
if (!description) throw new Error('The argument "description" must be provided'); | ||
// Validation | ||
if (!is.validDescription(description)) throw new Error('Provided value to argument "description" is not legal'); | ||
if (!is.validTimestamp(timestamp)) throw new Error('Provided value to argument "timestamp" is not legal'); | ||
if (!is.validSource(source)) throw new Error('Provided value to argument "source" is not legal'); | ||
if (!is.validLabels(labels)) throw new Error('Provided value to argument "label" is not legal'); | ||
if (!is.validValue(value)) throw new Error('Provided value to argument "value" is not legal'); | ||
if (!is.validType(type)) throw new Error('Provided value to argument "type" is not legal'); | ||
if (!is.validName(name)) throw new Error('Provided value to argument "name" is not legal'); | ||
// Private properties | ||
this[_description] = description; | ||
this[_timestamp] = timestamp || Date.now() / 1000; | ||
this[_source] = is.notEmpty(source) ? source : null; | ||
this[_labels] = labels; | ||
this[_value] = is.notEmpty(value) ? value : null; | ||
this[_meta] = meta; | ||
this[_type] = type; | ||
this[_name] = name; | ||
// Deprecated | ||
this[_time] = is.notEmpty(time) ? time : null; | ||
} | ||
get source() { | ||
return this[source]; | ||
return this[_source]; | ||
} | ||
set source(value) { | ||
this[source] = value; | ||
this[_source] = value; | ||
} | ||
get type() { | ||
return this[_type]; | ||
} | ||
get name() { | ||
return this[metric].name; | ||
return this[_name]; | ||
} | ||
get description() { | ||
return this[metric].description; | ||
return this[_description]; | ||
} | ||
get timestamp() { | ||
return this[metric].timestamp; | ||
return this[_timestamp]; | ||
} | ||
get value() { | ||
return this[notEmpty](this[metric].value) ? this[metric].value : null; | ||
return this[_value]; | ||
} | ||
get labels() { | ||
return this[_labels]; | ||
} | ||
get time() { | ||
return this[notEmpty](this[metric].time) ? this[metric].time : null; | ||
return this._time; | ||
} | ||
get meta() { | ||
return this[metric].meta || {}; | ||
return this[_meta]; | ||
} | ||
@@ -52,3 +100,5 @@ | ||
timestamp: this.timestamp, | ||
type: this.type, | ||
value: this.value, | ||
labels: this.labels, | ||
time: this.time, | ||
@@ -73,20 +123,4 @@ meta: this.meta, | ||
} | ||
[notEmpty](value) { | ||
if (value === undefined) { | ||
return false; | ||
} | ||
if (value === null) { | ||
return false; | ||
} | ||
if (typeof value === 'string' && value.trim().length === 0) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
}; | ||
module.exports = Metric; |
{ | ||
"name": "@metrics/metric", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "The metric class definition which metric objects in the @metrics library is instansiated from", | ||
@@ -10,2 +10,3 @@ "main": "lib/metric.js", | ||
"scripts": { | ||
"bench": "node benchmark/benchmark.js", | ||
"test": "tap test/*.js", | ||
@@ -37,4 +38,6 @@ "test:coverage": "tap test/*.js --cov", | ||
"eslint-plugin-import": "^2.14.0", | ||
"benchmark": "^2.1.4", | ||
"lolex": "^3.0.0", | ||
"tap": "^12.1.1" | ||
} | ||
} |
100
README.md
@@ -7,2 +7,4 @@ # @metrics/metric | ||
[![Build Status](http://img.shields.io/travis/metrics-js/metric/master.svg?style=flat-square)](https://travis-ci.org/metrics-js/metric) | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/metrics-js/metric.svg?style=flat-square)](https://greenkeeper.io/) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/metrics-js/metric/badge.svg?targetFile=package.json&style=flat-square)](https://snyk.io/test/github/metrics-js/metric?targetFile=package.json) | ||
@@ -40,2 +42,5 @@ ## Installation | ||
The metric object aims to be compatible with the [Open Metrics](https://github.com/OpenObservability/OpenMetrics) | ||
initiative. | ||
## Constructor | ||
@@ -56,4 +61,6 @@ | ||
* **name** - `String` - The name of the metric. | ||
* **value** - `String|Number` - The value of the metric. | ||
* **description** - `String` - The description of the metric. | ||
Each property value is validated and will throw if a property value is found to be invalid. | ||
## Properties | ||
@@ -67,5 +74,14 @@ | ||
* Valid characters: `a-z,A-Z,0-9,_`. | ||
* Value is imutable. | ||
* Valid value: `String` in the range of `[a-zA-Z_][a-zA-Z0-9_]`. | ||
* Value is immutable. | ||
* Is required. | ||
### description | ||
A human readable description of the metric. | ||
* Valid value: `String`. | ||
* Value is immutable. | ||
* Is required. | ||
### value | ||
@@ -75,5 +91,25 @@ | ||
* Valid values: `Number` or `String`. | ||
* Value is imutable. | ||
* Valid value: `Integer` or `null`. | ||
* Value is immutable. | ||
* Defaults to `null`. | ||
### type | ||
A hint of what type of metric this is. | ||
* Valid value: `Integer` in the range of `0-7`. | ||
* Value is immutable. | ||
* Defaults to `0`. | ||
Each numeric value reflect one of the following types: | ||
* `0` represents `unknown`. | ||
* `1` represents `gauge`. | ||
* `2` represents `counter`. | ||
* `3` represents `state_set`. | ||
* `4` represents `info`. | ||
* `5` represents `cumulative histogram`. | ||
* `6` represents `gauge histogram`. | ||
* `7` represents `summary`. | ||
### source | ||
@@ -83,33 +119,51 @@ | ||
* Valid characters: `a-z,A-Z,0-9,_`. | ||
* Valid value: `String` or `null`. | ||
* Value is mutable. | ||
* Defaults to `null`. | ||
### description | ||
### timestamp | ||
A human readable description of the metric. | ||
A timestamp of when the metric was created. | ||
* Valid values: `String`. | ||
* Value is imutable. | ||
* Valid value: `Double` - epoc milliseconds. | ||
* Value is immutable. | ||
* Defaults to `Date.now() / 1000`. | ||
### timestamp | ||
### labels | ||
A timestamp of when the metric was created. | ||
An `Array` of labeled values. Each item in the `Array` must be a | ||
`Label` object. Please see the Labels section for further info. | ||
* Valid values: `Number`. | ||
* Value is imutable. | ||
* Valid value: `Array` of `Label` objects. | ||
* Value is immutable. | ||
* Defaults to an empty `Array`. | ||
### meta | ||
Available to be used to hold any data. The input is not validated. | ||
* Valid value: `any`. | ||
* Value is immutable. | ||
* Default to `null`. | ||
### time | ||
N/A. | ||
Is deprecated. | ||
* Valid values: `Number`. | ||
* Value is imutable. | ||
## Label object | ||
### meta | ||
A `Label` object is a object literal which have the following properties: | ||
Available to be used to hold any misc data. In practice, meta can be used as | ||
a way to label metrics. Use each key of the meta object as the label name and | ||
the value as the label value. | ||
### name | ||
* Valid values: `Object`. | ||
* Value is imutable. | ||
The name of the label. | ||
* Valid value: `String` in the range of `[a-zA-Z0-9_]`. | ||
* Is required. | ||
### value | ||
The value of the label. | ||
* Valid value: `Integer` or `null`. | ||
* Is required. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12147
6
172
164
6