1 | ;
|
2 | /**
|
3 | * Copyright 2019, OpenCensus Authors
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.CumulativePointEntry = exports.Cumulative = void 0;
|
19 | const time_util_1 = require("../../common/time-util");
|
20 | const validations_1 = require("../../common/validations");
|
21 | const utils_1 = require("../utils");
|
22 | /**
|
23 | * Cumulative metric is used to record aggregated metrics that represents a
|
24 | * single numerical value accumulated over a time interval. The value can only
|
25 | * increase or be reset to zero on restart or reset the event.
|
26 | */
|
27 | class Cumulative {
|
28 | /**
|
29 | * Constructs a new Cumulative instance.
|
30 | *
|
31 | * @param name The name of the metric.
|
32 | * @param description The description of the metric.
|
33 | * @param unit The unit of the metric.
|
34 | * @param type The type of metric.
|
35 | * @param labelKeys The list of the label keys.
|
36 | * @param constantLabels The map of constant labels for the Metric.
|
37 | */
|
38 | constructor(name, description, unit, type, labelKeys, constantLabels) {
|
39 | this.labelKeys = labelKeys;
|
40 | this.constantLabels = constantLabels;
|
41 | this.registeredPoints = new Map();
|
42 | this.labelKeysLength = labelKeys.length;
|
43 | const keysAndConstantKeys = [...labelKeys, ...constantLabels.keys()];
|
44 | this.constantLabelValues = [...constantLabels.values()];
|
45 | this.metricDescriptor = {
|
46 | name,
|
47 | description,
|
48 | unit,
|
49 | type,
|
50 | labelKeys: keysAndConstantKeys,
|
51 | };
|
52 | this.defaultLabelValues = utils_1.initializeDefaultLabels(this.labelKeysLength);
|
53 | }
|
54 | /**
|
55 | * Creates a TimeSeries and returns a Point if the specified
|
56 | * labelValues is not already associated with this cumulative, else returns an
|
57 | * existing Point.
|
58 | *
|
59 | * It is recommended to keep a reference to the Point instead of always
|
60 | * calling this method for manual operations.
|
61 | *
|
62 | * @param labelValues The list of the label values.
|
63 | * @returns The value of single cumulative.
|
64 | */
|
65 | getOrCreateTimeSeries(labelValues) {
|
66 | validations_1.validateArrayElementsNotNull(validations_1.validateNotNull(labelValues, 'labelValues'), 'labelValue');
|
67 | return this.registerTimeSeries(labelValues);
|
68 | }
|
69 | /**
|
70 | * Returns a Point for a cumulative with all labels not set, or default
|
71 | * labels.
|
72 | *
|
73 | * @returns The value of single cumulative.
|
74 | */
|
75 | getDefaultTimeSeries() {
|
76 | return this.registerTimeSeries(this.defaultLabelValues);
|
77 | }
|
78 | /**
|
79 | * Removes the TimeSeries from the cumulative metric, if it is present. i.e.
|
80 | * references to previous Point objects are invalid (not part of the
|
81 | * metric).
|
82 | *
|
83 | * @param labelValues The list of label values.
|
84 | */
|
85 | removeTimeSeries(labelValues) {
|
86 | validations_1.validateNotNull(labelValues, 'labelValues');
|
87 | this.registeredPoints.delete(utils_1.hashLabelValues(labelValues));
|
88 | }
|
89 | /**
|
90 | * Removes all TimeSeries from the cumulative metric. i.e. references to all
|
91 | * previous Point objects are invalid (not part of the metric).
|
92 | */
|
93 | clear() {
|
94 | this.registeredPoints.clear();
|
95 | }
|
96 | /**
|
97 | * Registers a TimeSeries and returns a Point if the specified
|
98 | * labelValues is not already associated with this cumulative, else returns an
|
99 | * existing Point.
|
100 | *
|
101 | * @param labelValues The list of the label values.
|
102 | * @returns The value of single cumulative.
|
103 | */
|
104 | registerTimeSeries(labelValues) {
|
105 | const hash = utils_1.hashLabelValues(labelValues);
|
106 | // return if the specified labelValues is already associated with the point.
|
107 | if (this.registeredPoints.has(hash)) {
|
108 | return this.registeredPoints.get(hash);
|
109 | }
|
110 | if (this.labelKeysLength !== labelValues.length) {
|
111 | throw new Error("Label Keys and Label Values don't have same size");
|
112 | }
|
113 | const point = new CumulativePointEntry([
|
114 | ...labelValues,
|
115 | ...this.constantLabelValues,
|
116 | ]);
|
117 | this.registeredPoints.set(hash, point);
|
118 | return point;
|
119 | }
|
120 | /**
|
121 | * Provides a Metric with one or more TimeSeries.
|
122 | *
|
123 | * @returns The Metric, or null if TimeSeries is not present in Metric.
|
124 | */
|
125 | getMetric() {
|
126 | if (this.registeredPoints.size === 0) {
|
127 | return null;
|
128 | }
|
129 | const now = time_util_1.getTimestampWithProcessHRTime();
|
130 | return {
|
131 | descriptor: this.metricDescriptor,
|
132 | timeseries: Array.from(this.registeredPoints, ([_, point]) => point.getTimeSeries(now)),
|
133 | };
|
134 | }
|
135 | }
|
136 | exports.Cumulative = Cumulative;
|
137 | /**
|
138 | * The value of a single point in the Cumulative.TimeSeries.
|
139 | */
|
140 | class CumulativePointEntry {
|
141 | constructor(labelValues) {
|
142 | this.value = 0;
|
143 | this.labelValues = labelValues;
|
144 | this.startTimestamp = time_util_1.getTimestampWithProcessHRTime();
|
145 | }
|
146 | /** Reset cumulative metric. */
|
147 | reset() {
|
148 | this.value = 0;
|
149 | this.startTimestamp = time_util_1.getTimestampWithProcessHRTime();
|
150 | }
|
151 | /**
|
152 | * Increment the cumulative metric.
|
153 | * @param val The new value.
|
154 | */
|
155 | inc(val) {
|
156 | if ((val && !Number.isFinite(val)) || (val !== undefined && isNaN(val))) {
|
157 | throw new TypeError(`Value is not a valid number: ${val}`);
|
158 | }
|
159 | if (val && val < 0) {
|
160 | throw new Error('It is not possible to decrease a cumulative metric');
|
161 | }
|
162 | const incValue = val === null || val === undefined ? 1 : val;
|
163 | this.value += incValue;
|
164 | }
|
165 | /**
|
166 | * Returns the TimeSeries with one or more Point.
|
167 | *
|
168 | * @param now The time at which the cumulative is recorded.
|
169 | * @returns The TimeSeries.
|
170 | */
|
171 | getTimeSeries(now) {
|
172 | return {
|
173 | labelValues: this.labelValues,
|
174 | points: [{ value: this.value, timestamp: now }],
|
175 | startTimestamp: this.startTimestamp,
|
176 | };
|
177 | }
|
178 | }
|
179 | exports.CumulativePointEntry = CumulativePointEntry;
|
180 | //# sourceMappingURL=cumulative.js.map |
\ | No newline at end of file |