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.DerivedCumulative = void 0;
|
19 | const time_util_1 = require("../../common/time-util");
|
20 | const validations_1 = require("../../common/validations");
|
21 | const util = require("../utils");
|
22 | /**
|
23 | * DerivedCumulative metric is used to record aggregated metrics that
|
24 | * represents a single numerical value accumulated over a time interval.
|
25 | */
|
26 | class DerivedCumulative {
|
27 | /**
|
28 | * Constructs a new DerivedCumulative instance.
|
29 | *
|
30 | * @param name The name of the metric.
|
31 | * @param description The description of the metric.
|
32 | * @param unit The unit of the metric.
|
33 | * @param type The type of metric.
|
34 | * @param labelKeys The list of the label keys.
|
35 | * @param constantLabels The map of constant labels for the Metric.
|
36 | * @param startTime The time when the cumulative metric start measuring the
|
37 | * value.
|
38 | */
|
39 | constructor(name, description, unit, type, labelKeys, constantLabels, startTime) {
|
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.startTime = startTime;
|
53 | }
|
54 | /**
|
55 | * Creates a TimeSeries. The value of a single point in the TimeSeries is
|
56 | * observed from an object or function. The ValueExtractor is invoked whenever
|
57 | * metrics are collected, meaning the reported value is up-to-date.
|
58 | *
|
59 | * @param labelValues The list of the label values.
|
60 | * @param objOrFn obj The obj to get the size or length or value from. If
|
61 | * multiple options are available, the value (ToValueInterface) takes
|
62 | * precedence first, followed by length and size. e.g value -> length ->
|
63 | * size.
|
64 | * fn is the function that will be called to get the current value
|
65 | * of the cumulative.
|
66 | */
|
67 | createTimeSeries(labelValues, objOrFn) {
|
68 | validations_1.validateArrayElementsNotNull(validations_1.validateNotNull(labelValues, 'labelValues'), 'labelValue');
|
69 | validations_1.validateNotNull(objOrFn, 'obj');
|
70 | const hash = util.hashLabelValues(labelValues);
|
71 | if (this.registeredPoints.has(hash)) {
|
72 | throw new Error('A different time series with the same labels already exists.');
|
73 | }
|
74 | if (this.labelKeysLength !== labelValues.length) {
|
75 | throw new Error("Label Keys and Label Values don't have same size");
|
76 | }
|
77 | if (objOrFn instanceof Function) {
|
78 | this.extractor = objOrFn;
|
79 | }
|
80 | else if (util.isToValueInterface(objOrFn)) {
|
81 | this.extractor = () => objOrFn.getValue();
|
82 | }
|
83 | else if (util.isLengthAttributeInterface(objOrFn)) {
|
84 | this.extractor = () => objOrFn.length;
|
85 | }
|
86 | else if (util.isLengthMethodInterface(objOrFn)) {
|
87 | this.extractor = () => objOrFn.length();
|
88 | }
|
89 | else if (util.isSizeAttributeInterface(objOrFn)) {
|
90 | this.extractor = () => objOrFn.size;
|
91 | }
|
92 | else if (util.isSizeMethodInterface(objOrFn)) {
|
93 | this.extractor = () => objOrFn.size();
|
94 | }
|
95 | else {
|
96 | throw new Error('Unknown interface/object type');
|
97 | }
|
98 | this.registeredPoints.set(hash, {
|
99 | labelValues,
|
100 | extractor: this.extractor,
|
101 | prevValue: 0,
|
102 | });
|
103 | }
|
104 | /**
|
105 | * Removes the TimeSeries from the cumulative metric, if it is present. i.e.
|
106 | * references to previous Point objects are invalid (not part of the
|
107 | * metric).
|
108 | *
|
109 | * @param labelValues The list of label values.
|
110 | */
|
111 | removeTimeSeries(labelValues) {
|
112 | validations_1.validateNotNull(labelValues, 'labelValues');
|
113 | this.registeredPoints.delete(util.hashLabelValues(labelValues));
|
114 | }
|
115 | /**
|
116 | * Removes all TimeSeries from the cumulative metric. i.e. references to all
|
117 | * previous Point objects are invalid (not part of the metric).
|
118 | */
|
119 | clear() {
|
120 | this.registeredPoints.clear();
|
121 | }
|
122 | /**
|
123 | * Provides a Metric with one or more TimeSeries.
|
124 | *
|
125 | * @returns The Metric, or null if TimeSeries is not present in Metric.
|
126 | */
|
127 | getMetric() {
|
128 | if (this.registeredPoints.size === 0) {
|
129 | return null;
|
130 | }
|
131 | const timestamp = time_util_1.getTimestampWithProcessHRTime();
|
132 | return {
|
133 | descriptor: this.metricDescriptor,
|
134 | timeseries: Array.from(this.registeredPoints, ([_, cumulativeEntry]) => {
|
135 | const newValue = cumulativeEntry.extractor();
|
136 | const value = newValue > cumulativeEntry.prevValue
|
137 | ? newValue
|
138 | : cumulativeEntry.prevValue;
|
139 | cumulativeEntry.prevValue = value;
|
140 | return {
|
141 | labelValues: [
|
142 | ...cumulativeEntry.labelValues,
|
143 | ...this.constantLabelValues,
|
144 | ],
|
145 | points: [{ value, timestamp }],
|
146 | startTimestamp: this.startTime,
|
147 | };
|
148 | }),
|
149 | };
|
150 | }
|
151 | }
|
152 | exports.DerivedCumulative = DerivedCumulative;
|
153 | //# sourceMappingURL=derived-cumulative.js.map |
\ | No newline at end of file |