1 | ;
|
2 | /**
|
3 | * Copyright 2018, 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.PointEntry = exports.Gauge = 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 | * Gauge metric
|
24 | */
|
25 | class Gauge {
|
26 | /**
|
27 | * Constructs a new Gauge instance.
|
28 | *
|
29 | * @param name The name of the metric.
|
30 | * @param description The description of the metric.
|
31 | * @param unit The unit of the metric.
|
32 | * @param type The type of metric.
|
33 | * @param labelKeys The list of the label keys.
|
34 | * @param constantLabels The map of constant labels for the Metric.
|
35 | */
|
36 | constructor(name, description, unit, type, labelKeys, constantLabels) {
|
37 | this.labelKeys = labelKeys;
|
38 | this.constantLabels = constantLabels;
|
39 | this.registeredPoints = new Map();
|
40 | this.labelKeysLength = labelKeys.length;
|
41 | const keysAndConstantKeys = [...labelKeys, ...constantLabels.keys()];
|
42 | this.constantLabelValues = [...constantLabels.values()];
|
43 | this.metricDescriptor = {
|
44 | name,
|
45 | description,
|
46 | unit,
|
47 | type,
|
48 | labelKeys: keysAndConstantKeys,
|
49 | };
|
50 | this.defaultLabelValues = utils_1.initializeDefaultLabels(this.labelKeysLength);
|
51 | }
|
52 | /**
|
53 | * Creates a TimeSeries and returns a Point if the specified
|
54 | * labelValues is not already associated with this gauge, else returns an
|
55 | * existing Point.
|
56 | *
|
57 | * It is recommended to keep a reference to the Point instead of always
|
58 | * calling this method for manual operations.
|
59 | *
|
60 | * @param labelValues The list of the label values.
|
61 | * @returns The value of single gauge.
|
62 | */
|
63 | getOrCreateTimeSeries(labelValues) {
|
64 | validations_1.validateArrayElementsNotNull(validations_1.validateNotNull(labelValues, Gauge.LABEL_VALUES), Gauge.LABEL_VALUE);
|
65 | return this.registerTimeSeries(labelValues);
|
66 | }
|
67 | /**
|
68 | * Returns a Point for a gauge with all labels not set, or default
|
69 | * labels.
|
70 | *
|
71 | * @returns The value of single gauge.
|
72 | */
|
73 | getDefaultTimeSeries() {
|
74 | return this.registerTimeSeries(this.defaultLabelValues);
|
75 | }
|
76 | /**
|
77 | * Removes the TimeSeries from the gauge metric, if it is present. i.e.
|
78 | * references to previous Point objects are invalid (not part of the
|
79 | * metric).
|
80 | *
|
81 | * @param labelValues The list of label values.
|
82 | */
|
83 | removeTimeSeries(labelValues) {
|
84 | validations_1.validateNotNull(labelValues, Gauge.LABEL_VALUES);
|
85 | this.registeredPoints.delete(utils_1.hashLabelValues(labelValues));
|
86 | }
|
87 | /**
|
88 | * Removes all TimeSeries from the gauge metric. i.e. references to all
|
89 | * previous Point objects are invalid (not part of the metric).
|
90 | */
|
91 | clear() {
|
92 | this.registeredPoints.clear();
|
93 | }
|
94 | /**
|
95 | * Registers a TimeSeries and returns a Point if the specified
|
96 | * labelValues is not already associated with this gauge, else returns an
|
97 | * existing Point.
|
98 | *
|
99 | * @param labelValues The list of the label values.
|
100 | * @returns The value of single gauge.
|
101 | */
|
102 | registerTimeSeries(labelValues) {
|
103 | const hash = utils_1.hashLabelValues(labelValues);
|
104 | // return if the specified labelValues is already associated with the point.
|
105 | if (this.registeredPoints.has(hash)) {
|
106 | return this.registeredPoints.get(hash);
|
107 | }
|
108 | if (this.labelKeysLength !== labelValues.length) {
|
109 | throw new Error(Gauge.ERROR_MESSAGE_INVALID_SIZE);
|
110 | }
|
111 | const point = new PointEntry([...labelValues, ...this.constantLabelValues]);
|
112 | this.registeredPoints.set(hash, point);
|
113 | return point;
|
114 | }
|
115 | /**
|
116 | * Provides a Metric with one or more TimeSeries.
|
117 | *
|
118 | * @returns The Metric, or null if TimeSeries is not present in Metric.
|
119 | */
|
120 | getMetric() {
|
121 | if (this.registeredPoints.size === 0) {
|
122 | return null;
|
123 | }
|
124 | const timestamp = time_util_1.getTimestampWithProcessHRTime();
|
125 | return {
|
126 | descriptor: this.metricDescriptor,
|
127 | timeseries: Array.from(this.registeredPoints, ([_, point]) => point.getTimeSeries(timestamp)),
|
128 | };
|
129 | }
|
130 | }
|
131 | exports.Gauge = Gauge;
|
132 | Gauge.LABEL_VALUE = 'labelValue';
|
133 | Gauge.LABEL_VALUES = 'labelValues';
|
134 | Gauge.ERROR_MESSAGE_INVALID_SIZE = "Label Keys and Label Values don't have same size";
|
135 | /**
|
136 | * The value of a single point in the Gauge.TimeSeries.
|
137 | */
|
138 | class PointEntry {
|
139 | constructor(labelValues) {
|
140 | this.value = 0;
|
141 | this.labelValues = labelValues;
|
142 | }
|
143 | /**
|
144 | * Adds the given value to the current value. The values can be negative.
|
145 | *
|
146 | * @param amt The value to add.
|
147 | */
|
148 | add(amt) {
|
149 | this.value = this.value + amt;
|
150 | }
|
151 | /**
|
152 | * Sets the given value.
|
153 | *
|
154 | * @param val The new value.
|
155 | */
|
156 | set(val) {
|
157 | this.value = val;
|
158 | }
|
159 | /**
|
160 | * Returns the TimeSeries with one or more Point.
|
161 | *
|
162 | * @param timestamp The time at which the gauge is recorded.
|
163 | * @returns The TimeSeries.
|
164 | */
|
165 | getTimeSeries(timestamp) {
|
166 | return {
|
167 | labelValues: this.labelValues,
|
168 | points: [{ value: this.value, timestamp }],
|
169 | };
|
170 | }
|
171 | }
|
172 | exports.PointEntry = PointEntry;
|
173 | //# sourceMappingURL=gauge.js.map |
\ | No newline at end of file |