private_api.md

November 16, 2018 ยท View on GitHub

Intro

This document contains a description of this library's internal API. Consumers of the artedi library shouldn't need to look at this.

Unless stated otherwise, all of the variables and functions outlined in this document should be considered private, and not used by the library consumers. The internal API is not static, and may change at any time without notice.

To learn about the public, user-facing API provided, see API.md.

Internal Structures

These structures are internal to artedi, and should not be directly instantiated by the user.

Metric

A Metric is the most basic structure that we have implemented. Every collector type uses Metrics, but not directly.

The Metric class represents the value behind an individual metric. For example, a Metric could represent the count of HTTP POST requests made that resulted in a 204 status code. This class has no knowledge of higher-level concepts like counters, gauges, or histograms. It is simply a class that maintains a numeric value, a timestamp, associated labels, and some information related to the optional expiry.

VariableTypeValue
labelsobjectA map of label key/value pairs
valuenumberA number that describes the current value of the metric
timestampnumberISO 8601 timestamp, representing the time this metric was last modified
expiresbooleanIndicates if expiry is enabled for the metric
expiryPeriodnumberExpiry time for the metric in milliseconds
expiryTimerobjectObject representing the expiry timer
defaultValuenumberA number that describes the default value of the metric
FunctionArgumentsResultReturn Value
addnumAdds num to the value field of the metric. No positive/negative check is done on numNone
setnumSets the value field of the metric to num. No positive/negative check is done on numNone
getValueNoneReturns the local value field. Consumed by higher level functionsnumber type
resetValueNoneResets the value field of the metric to defaultValueNone

The labels that belong to each Metric are key/value pairs. There can be two Metrics that have the exact same key/value pairs, but they cannot belong to the same collector. For example, a Counter and a Gauge may both have the labels {method="getObject",code="200"}. The Gauge and Counter will be tracking different things though. In this case, the Counter may be tracking requests completed, while the Gauge is tracking request latency.

All collector functions (add(), observe(), etc.) are all built on top of the Metric's add() function. To accomplish subtraction, add() is called with a negative number.

The user should never directly perform operations on Metrics, but instead use collectors (which build on top of Metrics by way of MetricVectors).

MetricVector

MetricVectors are built on top of Metrics and give them much more utility. Counters and Gauges directly use MetricVectors. Histograms use MetricVectors, but indirectly.

The MetricVector provides a way to organize, create, and retrieve Metric objects. While a Metric represents a single data point, a MetricVector can represent one or more data points. For example, a MetricVector could represent the counts of all HTTP requests separated by method, and response code. Each unique method and response code pair would result in a new Metric object being created and tracked. The MetricVector class has no knowledge of higher-level concepts like counters, gauges, or histograms. Counters, gauges, and histograms are built on top of MetricVectors.

VariableTypeValue
fullNamestringfull name of what is being tracked, resulting from concatenation of namespace, subsystem, and collector name
metricsobjectkey/value mapping. Each key corresponds to a unique Metric object
FunctionArgumentsResultReturn Value
getWithLabelsobjectsearches metrics map for Metrics with the provided labelsa Metric object, or null if not found
createWithLabelsobjectcreates a new Metric with the given labels, adds it to the metric mapthe newly created Metric object
createOrGetWithLabelsobjectcalls getWithLabels() to determine if a Metric with the given labels is already created. If so, returns it. Otherwise, calls createWithLabels() and returns the created MetricMetric object
prometheuscallbackiterates through the metric map, serializing each Metric into a prometheus-parseable stringNone (string and error via callback)
jsoncallbacksame as prometheus(), but in JSON formatNone (string and error via callback)

Simply put, MetricVectors keep track of multiple Metrics. Counters and Gauges directly wrap MetricVectors (which we'll explain later). Histograms use Counters and Gauges in their implementation, so they also use MetricVectors. MetricVectors do the vast majority of the heavy lifting for collectors.

json() will be implemented when the need arises. No implementation is currently required.

Users should not directly interact with MetricVectors. They should use things like collectors, which use MetricVectors internally.

External Structures

These structures are what the user will interact with. See API.md for more information on publicly available functions. This section extends API.md by describing the private functions, as well as 'class variables'.

Collector

VariableTypeValue
registryobjectkey/value mapping of unique collector names -> child collectors
triggerRegistryarrayArray of trigger functions

registry keeps references to all of the previously-instantiated child collectors. When it is time to serialize metrics, the Collector iterates through this map and calls the serialization method of choice on each child collector. The results are concatenated and returned to the user.

triggerRegistry keeps references to functions that will be invoked before metrics are collected. See collector.processTriggers() for more information.

FunctionArgumentsResultReturn Value
Collectoroptssee createCollector()see createCollector()
registercollector objectif the given collector has already been registered, returns an error. Otherwise, adds the collector to registryerror, or null
processTriggersarray, callbackIterates through triggers in the triggerRegistry, calling each function
processCollectorsarray, callbackIterates through child collectors in the registry, calling a serialization function on each

Collector() is called by the public createCollector function.

Counter

VariableTypeValue
namestringfull name of what is being tracked, resulting from concatenation of namespace, subsystem, and collector name
helpstringuser-provided string explaining this collector
metricVecMetricVectorempty to start, is populated as the user performs metric operations
typestring'counter,' used during serialization
staticLabelsobjectkey/value mapping of labels that will be present in all metrics collected by this collector
FunctionArgumentsResultReturn Value
Counterparent, optscreates a Counter object from traits available in the parent, and options passed ina new Counter object
labelsobjectreturns a metric that has exactly the label key/value pairs provided. If none exists, one is createdA Metric object
getWithLabelsobjectsame as labels(), but returns null if a metric doesn't existA Metric object or null
prometheuscallbackreturns all of the Counter's metrics in prometheus format as a stringNone (string and error via callback)

Counter() is called by the Collector object's counter() function.

Gauge

VariableTypeValue
namestringfull name of what is being tracked, resulting from concatenation of namespace, subsystem, and collector name
helpstringuser-provided string explaining this collector
metricVecMetricVectorempty to start, is populated as the user performs metric operations
typestring'gauge,' used during serialization
staticLabelsobjectkey/value mapping of labels that will be present in all metrics collected by this collector
FunctionArgumentsResultReturn Value
Gaugeparent, optscreates a Gauge object from traits available in the parent, and options passed ina new Gauge object
labelsobjectreturns a metric that has exactly the label key/value pairs provided. If none exists, one is createdA Metric object
getWithLabelsobjectsame as labels(), but returns null if a metric doesn't existA Metric object or null
prometheuscallbackreturns all of the Gauge's metrics in prometheus format as a stringNone (string and error via callback)

Gauge() is called by the Collector object's gauge() function.

Histogram

VariableTypeValue
fullNamestringfull name of what is being tracked, resulting from concatenation of namespace, subsystem, and collector name
namestringname of the collector, used when creating Counters and Gauges
bucketsnumber arrayan array that holds the upper values of each bucket
countersobjectkey/value mapping containing Counters for tracking metrics in each bucket
gaugeGaugea Gauge used to track the _sum field of each metric
helpstringuser-provided string explaining this collector
metricVecMetricVectorempty to start, is populated as the user performs metric operations
typestring'histogram,' used during serialization
staticLabelsobjectkey/value mapping of labels that will be present in all metrics collected by this collector
FunctionArgumentsResultReturn Value
Histogramparent, optscreates a Histogram object from traits available in the parent, and options passed ina new Histogram object
labelsobjectchecks if a Counter with the given labels already exists. If yes, returns it, otherwise creates a new Counter, and initializes another GaugeNone
prometheuscallbackiterates through the Counters, calling prometheus() on their MetricVector object. The results are stitched together and added to the result of calling prometheus() on the Gauge's MetricVectorNone (string and error via callback)

Histogram() is called by the parent object's histogram() function.