UNPKG

3.44 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 });
18const types_1 = require("../metrics/export/types");
19const types_2 = require("./types");
20/** Utils to convert Stats data models to Metric data models */
21class MetricUtils {
22 /**
23 * Gets the corresponding metric type for the given stats type.
24 * @param measure The measure for which to find a metric type
25 * @param aggregation The aggregation for which to find a metric type
26 * @returns The Type of metric descriptor
27 */
28 static getType(measure, aggregation) {
29 if (aggregation === types_2.AggregationType.SUM) {
30 switch (measure.type) {
31 case types_2.MeasureType.INT64:
32 return types_1.MetricDescriptorType.CUMULATIVE_INT64;
33 case types_2.MeasureType.DOUBLE:
34 return types_1.MetricDescriptorType.CUMULATIVE_DOUBLE;
35 default:
36 throw new Error(`Unknown measure type ${measure.type}`);
37 }
38 }
39 else if (aggregation === types_2.AggregationType.COUNT) {
40 return types_1.MetricDescriptorType.CUMULATIVE_INT64;
41 }
42 else if (aggregation === types_2.AggregationType.DISTRIBUTION) {
43 return types_1.MetricDescriptorType.CUMULATIVE_DISTRIBUTION;
44 }
45 else if (aggregation === types_2.AggregationType.LAST_VALUE) {
46 switch (measure.type) {
47 case types_2.MeasureType.INT64:
48 return types_1.MetricDescriptorType.GAUGE_INT64;
49 case types_2.MeasureType.DOUBLE:
50 return types_1.MetricDescriptorType.GAUGE_DOUBLE;
51 default:
52 throw new Error(`Unknown measure type ${measure.type}`);
53 }
54 }
55 throw new Error(`Unknown aggregation type ${aggregation}`);
56 }
57 /**
58 * Gets a MetricDescriptor for given view.
59 * @param view The view for which to build a metric descriptor
60 * @returns The MetricDescriptor.
61 */
62 static viewToMetricDescriptor(view) {
63 return {
64 name: view.name,
65 description: view.description,
66 unit: view.measure.unit,
67 type: MetricUtils.getType(view.measure, view.aggregation),
68 labelKeys: view.getColumns().map(
69 // TODO(mayurkale): add description
70 tagKey => ({ key: tagKey.name, description: '' })),
71 };
72 }
73 /**
74 * Converts tag values to label values.
75 * @param tagValues the list of tag values
76 * @returns The List of label values
77 */
78 static tagValuesToLabelValues(tagValues) {
79 return tagValues.map(tagValue => ({
80 value: tagValue ? tagValue.value : null,
81 }));
82 }
83}
84exports.MetricUtils = MetricUtils;
85//# sourceMappingURL=metric-utils.js.map
\No newline at end of file