UNPKG

13.5 kBJavaScriptView Raw
1"use strict";
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 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.MetricRegistry = void 0;
19const time_util_1 = require("../common/time-util");
20const validations_1 = require("../common/validations");
21const types_1 = require("../stats/types");
22const cumulative_1 = require("./cumulative/cumulative");
23const derived_cumulative_1 = require("./cumulative/derived-cumulative");
24const base_metric_producer_1 = require("./export/base-metric-producer");
25const types_2 = require("./export/types");
26const derived_gauge_1 = require("./gauges/derived-gauge");
27const gauge_1 = require("./gauges/gauge");
28/**
29 * Creates and manages application's set of metrics.
30 */
31class MetricRegistry {
32 constructor() {
33 this.registeredMetrics = new Map();
34 this.metricProducer = new MetricProducerForRegistry(this.registeredMetrics);
35 }
36 /**
37 * Builds a new Int64 gauge to be added to the registry. This is more
38 * convenient form when you want to manually increase and decrease values as
39 * per your service requirements.
40 *
41 * @param name The name of the metric.
42 * @param options The options for the metric.
43 * @returns A Int64 Gauge metric.
44 */
45 addInt64Gauge(name, options) {
46 const description = (options && options.description) || MetricRegistry.DEFAULT_DESCRIPTION;
47 const unit = (options && options.unit) || MetricRegistry.DEFAULT_UNIT;
48 const labelKeys = (options && options.labelKeys) || MetricRegistry.DEFAULT_LABEL_KEYS;
49 const constantLabels = (options && options.constantLabels) ||
50 MetricRegistry.DEFAULT_CONSTANT_LABEL;
51 // TODO(mayurkale): Add support for resource
52 this.validateLables(labelKeys, constantLabels);
53 const labelKeysCopy = Object.assign([], labelKeys);
54 const int64Gauge = new gauge_1.Gauge(validations_1.validateNotNull(name, MetricRegistry.NAME), description, unit, types_2.MetricDescriptorType.GAUGE_INT64, labelKeysCopy, constantLabels);
55 this.registerMetric(name, int64Gauge);
56 return int64Gauge;
57 }
58 /**
59 * Builds a new double gauge to be added to the registry. This is more
60 * convenient form when you want to manually increase and decrease values as
61 * per your service requirements.
62 *
63 * @param name The name of the metric.
64 * @param options The options for the metric.
65 * @returns A Double Gauge metric.
66 */
67 addDoubleGauge(name, options) {
68 const description = (options && options.description) || MetricRegistry.DEFAULT_DESCRIPTION;
69 const unit = (options && options.unit) || MetricRegistry.DEFAULT_UNIT;
70 const labelKeys = (options && options.labelKeys) || MetricRegistry.DEFAULT_LABEL_KEYS;
71 const constantLabels = (options && options.constantLabels) ||
72 MetricRegistry.DEFAULT_CONSTANT_LABEL;
73 // TODO(mayurkale): Add support for resource
74 this.validateLables(labelKeys, constantLabels);
75 const labelKeysCopy = Object.assign([], labelKeys);
76 const doubleGauge = new gauge_1.Gauge(validations_1.validateNotNull(name, MetricRegistry.NAME), description, unit, types_2.MetricDescriptorType.GAUGE_DOUBLE, labelKeysCopy, constantLabels);
77 this.registerMetric(name, doubleGauge);
78 return doubleGauge;
79 }
80 /**
81 * Builds a new derived Int64 gauge to be added to the registry. This is more
82 * convenient form when you want to manually increase and decrease values as
83 * per your service requirements.
84 *
85 * @param name The name of the metric.
86 * @param options The options for the metric.
87 * @returns A Int64 DerivedGauge metric.
88 */
89 addDerivedInt64Gauge(name, options) {
90 const description = (options && options.description) || MetricRegistry.DEFAULT_DESCRIPTION;
91 const unit = (options && options.unit) || MetricRegistry.DEFAULT_UNIT;
92 const labelKeys = (options && options.labelKeys) || MetricRegistry.DEFAULT_LABEL_KEYS;
93 const constantLabels = (options && options.constantLabels) ||
94 MetricRegistry.DEFAULT_CONSTANT_LABEL;
95 // TODO(mayurkale): Add support for resource
96 this.validateLables(labelKeys, constantLabels);
97 const labelKeysCopy = Object.assign([], labelKeys);
98 const derivedInt64Gauge = new derived_gauge_1.DerivedGauge(validations_1.validateNotNull(name, MetricRegistry.NAME), description, unit, types_2.MetricDescriptorType.GAUGE_INT64, labelKeysCopy, constantLabels);
99 this.registerMetric(name, derivedInt64Gauge);
100 return derivedInt64Gauge;
101 }
102 /**
103 * Builds a new derived double gauge to be added to the registry. This is more
104 * convenient form when you want to manually increase and decrease values as
105 * per your service requirements.
106 *
107 * @param name The name of the metric.
108 * @param options The options for the metric.
109 * @returns A Double DerivedGauge metric.
110 */
111 addDerivedDoubleGauge(name, options) {
112 const description = (options && options.description) || MetricRegistry.DEFAULT_DESCRIPTION;
113 const unit = (options && options.unit) || MetricRegistry.DEFAULT_UNIT;
114 const labelKeys = (options && options.labelKeys) || MetricRegistry.DEFAULT_LABEL_KEYS;
115 const constantLabels = (options && options.constantLabels) ||
116 MetricRegistry.DEFAULT_CONSTANT_LABEL;
117 // TODO(mayurkale): Add support for resource
118 this.validateLables(labelKeys, constantLabels);
119 const labelKeysCopy = Object.assign([], labelKeys);
120 const derivedDoubleGauge = new derived_gauge_1.DerivedGauge(validations_1.validateNotNull(name, MetricRegistry.NAME), description, unit, types_2.MetricDescriptorType.GAUGE_DOUBLE, labelKeysCopy, constantLabels);
121 this.registerMetric(name, derivedDoubleGauge);
122 return derivedDoubleGauge;
123 }
124 /**
125 * Builds a new Int64 cumulative to be added to the registry. This API is
126 * useful when you want to manually increase and reset values as per service
127 * requirements.
128 *
129 * @param name The name of the metric.
130 * @param options The options for the metric.
131 * @returns A Int64 Cumulative metric.
132 */
133 addInt64Cumulative(name, options) {
134 const description = (options && options.description) || MetricRegistry.DEFAULT_DESCRIPTION;
135 const unit = (options && options.unit) || MetricRegistry.DEFAULT_UNIT;
136 const labelKeys = (options && options.labelKeys) || MetricRegistry.DEFAULT_LABEL_KEYS;
137 const constantLabels = (options && options.constantLabels) ||
138 MetricRegistry.DEFAULT_CONSTANT_LABEL;
139 // TODO(mayurkale): Add support for resource
140 this.validateLables(labelKeys, constantLabels);
141 const labelKeysCopy = Object.assign([], labelKeys);
142 const int64Cumulative = new cumulative_1.Cumulative(validations_1.validateNotNull(name, MetricRegistry.NAME), description, unit, types_2.MetricDescriptorType.CUMULATIVE_INT64, labelKeysCopy, constantLabels);
143 this.registerMetric(name, int64Cumulative);
144 return int64Cumulative;
145 }
146 /**
147 * Builds a new double cumulative to be added to the registry. This API is
148 * useful when you want to manually increase and reset values as per service
149 * requirements.
150 *
151 * @param name The name of the metric.
152 * @param options The options for the metric.
153 * @returns A Double Cumulative metric.
154 */
155 addDoubleCumulative(name, options) {
156 const description = (options && options.description) || MetricRegistry.DEFAULT_DESCRIPTION;
157 const unit = (options && options.unit) || MetricRegistry.DEFAULT_UNIT;
158 const labelKeys = (options && options.labelKeys) || MetricRegistry.DEFAULT_LABEL_KEYS;
159 const constantLabels = (options && options.constantLabels) ||
160 MetricRegistry.DEFAULT_CONSTANT_LABEL;
161 // TODO(mayurkale): Add support for resource
162 this.validateLables(labelKeys, constantLabels);
163 const labelKeysCopy = Object.assign([], labelKeys);
164 const doubleCumulative = new cumulative_1.Cumulative(validations_1.validateNotNull(name, MetricRegistry.NAME), description, unit, types_2.MetricDescriptorType.CUMULATIVE_DOUBLE, labelKeysCopy, constantLabels);
165 this.registerMetric(name, doubleCumulative);
166 return doubleCumulative;
167 }
168 /**
169 * Builds a new derived Int64 Cumulative to be added to the registry.
170 *
171 * @param name The name of the metric.
172 * @param options The options for the metric.
173 * @returns A Int64 DerivedCumulative metric.
174 */
175 addDerivedInt64Cumulative(name, options) {
176 const description = (options && options.description) || MetricRegistry.DEFAULT_DESCRIPTION;
177 const unit = (options && options.unit) || MetricRegistry.DEFAULT_UNIT;
178 const labelKeys = (options && options.labelKeys) || MetricRegistry.DEFAULT_LABEL_KEYS;
179 const constantLabels = (options && options.constantLabels) ||
180 MetricRegistry.DEFAULT_CONSTANT_LABEL;
181 // TODO(mayurkale): Add support for resource
182 this.validateLables(labelKeys, constantLabels);
183 const labelKeysCopy = Object.assign([], labelKeys);
184 const startTime = time_util_1.getTimestampWithProcessHRTime();
185 const derivedInt64Cumulative = new derived_cumulative_1.DerivedCumulative(validations_1.validateNotNull(name, MetricRegistry.NAME), description, unit, types_2.MetricDescriptorType.CUMULATIVE_INT64, labelKeysCopy, constantLabels, startTime);
186 this.registerMetric(name, derivedInt64Cumulative);
187 return derivedInt64Cumulative;
188 }
189 /**
190 * Builds a new derived Double Cumulative to be added to the registry.
191 *
192 * @param name The name of the metric.
193 * @param options The options for the metric.
194 * @returns A Double DerivedCumulative metric.
195 */
196 addDerivedDoubleCumulative(name, options) {
197 const description = (options && options.description) || MetricRegistry.DEFAULT_DESCRIPTION;
198 const unit = (options && options.unit) || MetricRegistry.DEFAULT_UNIT;
199 const labelKeys = (options && options.labelKeys) || MetricRegistry.DEFAULT_LABEL_KEYS;
200 const constantLabels = (options && options.constantLabels) ||
201 MetricRegistry.DEFAULT_CONSTANT_LABEL;
202 // TODO(mayurkale): Add support for resource
203 this.validateLables(labelKeys, constantLabels);
204 const labelKeysCopy = Object.assign([], labelKeys);
205 const startTime = time_util_1.getTimestampWithProcessHRTime();
206 const derivedDoubleCumulative = new derived_cumulative_1.DerivedCumulative(validations_1.validateNotNull(name, MetricRegistry.NAME), description, unit, types_2.MetricDescriptorType.CUMULATIVE_DOUBLE, labelKeysCopy, constantLabels, startTime);
207 this.registerMetric(name, derivedDoubleCumulative);
208 return derivedDoubleCumulative;
209 }
210 /**
211 * Registers metric to register.
212 *
213 * @param name The name of the metric.
214 * @param meter The metric to register.
215 */
216 registerMetric(name, meter) {
217 if (this.registeredMetrics.has(name)) {
218 throw new Error(`A metric with the name ${name} has already been registered.`);
219 }
220 this.registeredMetrics.set(name, meter);
221 }
222 /**
223 * Gets a metric producer for registry.
224 *
225 * @returns The metric producer.
226 */
227 getMetricProducer() {
228 return this.metricProducer;
229 }
230 /** Validates labelKeys and constantLabels. */
231 validateLables(labelKeys, constantLabels) {
232 validations_1.validateArrayElementsNotNull(labelKeys, MetricRegistry.LABEL_KEY);
233 validations_1.validateMapElementNotNull(constantLabels, MetricRegistry.CONSTANT_LABELS);
234 validations_1.validateDuplicateKeys(labelKeys, constantLabels);
235 }
236}
237exports.MetricRegistry = MetricRegistry;
238MetricRegistry.NAME = 'name';
239MetricRegistry.LABEL_KEY = 'labelKey';
240MetricRegistry.CONSTANT_LABELS = 'constantLabels';
241MetricRegistry.DEFAULT_DESCRIPTION = '';
242MetricRegistry.DEFAULT_UNIT = types_1.MeasureUnit.UNIT;
243MetricRegistry.DEFAULT_LABEL_KEYS = [];
244MetricRegistry.DEFAULT_CONSTANT_LABEL = new Map();
245/**
246 * A MetricProducerForRegistry is a producer that can be registered for
247 * exporting using MetricProducerManager.
248 */
249class MetricProducerForRegistry extends base_metric_producer_1.BaseMetricProducer {
250 constructor(registeredMetrics) {
251 super();
252 this.registeredMetrics = registeredMetrics;
253 }
254 /**
255 * Gets a collection of produced Metric`s to be exported.
256 *
257 * @returns The list of metrics.
258 */
259 getMetrics() {
260 return Array.from(this.registeredMetrics.values())
261 .map(meter => meter.getMetric())
262 .filter(meter => !!meter);
263 }
264}
265//# sourceMappingURL=metric-registry.js.map
\No newline at end of file