|
| 1 | +/** |
| 2 | + * Copyright 2018, OpenCensus Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +import {LabelKey, LabelValue, MetricDescriptor, MetricDescriptorType} from '../metrics/export/types'; |
| 19 | + |
| 20 | +import {AggregationType, Measure, MeasureType, Tags, View} from './types'; |
| 21 | + |
| 22 | +/** Utils to convert Stats data models to Metric data models */ |
| 23 | +export class MetricUtils { |
| 24 | + /** |
| 25 | + * Gets the corresponding metric type for the given stats type. |
| 26 | + * @param measure The measure for which to find a metric type |
| 27 | + * @param aggregation The aggregation for which to find a metric type |
| 28 | + */ |
| 29 | + private static getType(measure: Measure, aggregation: AggregationType): |
| 30 | + MetricDescriptorType { |
| 31 | + if (aggregation === AggregationType.SUM) { |
| 32 | + switch (measure.type) { |
| 33 | + case MeasureType.INT64: |
| 34 | + return MetricDescriptorType.CUMULATIVE_INT64; |
| 35 | + case MeasureType.DOUBLE: |
| 36 | + return MetricDescriptorType.CUMULATIVE_DOUBLE; |
| 37 | + default: |
| 38 | + throw new Error(`Unknown measure type ${measure.type}`); |
| 39 | + } |
| 40 | + } else if (aggregation === AggregationType.COUNT) { |
| 41 | + return MetricDescriptorType.CUMULATIVE_INT64; |
| 42 | + } else if (aggregation === AggregationType.DISTRIBUTION) { |
| 43 | + return MetricDescriptorType.CUMULATIVE_DISTRIBUTION; |
| 44 | + } else if (aggregation === AggregationType.LAST_VALUE) { |
| 45 | + switch (measure.type) { |
| 46 | + case MeasureType.INT64: |
| 47 | + return MetricDescriptorType.GAUGE_INT64; |
| 48 | + case MeasureType.DOUBLE: |
| 49 | + return MetricDescriptorType.GAUGE_DOUBLE; |
| 50 | + default: |
| 51 | + throw new Error(`Unknown measure type ${measure.type}`); |
| 52 | + } |
| 53 | + } |
| 54 | + throw new Error(`Unknown aggregation type ${aggregation}`); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Gets a MetricDescriptor for given view. |
| 59 | + * @param view The view for which to build a metric descriptor |
| 60 | + */ |
| 61 | + static viewToMetricDescriptor(view: View): MetricDescriptor { |
| 62 | + return { |
| 63 | + name: view.name, |
| 64 | + description: view.description, |
| 65 | + unit: view.measure.unit, |
| 66 | + type: MetricUtils.getType(view.measure, view.aggregation), |
| 67 | + labelKeys: view.getColumns().map(tag => ({key: tag} as LabelKey)) |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Converts tag values to label values. |
| 73 | + * @param tags |
| 74 | + */ |
| 75 | + static tagValuesToLabelValues(tags: Tags): LabelValue[] { |
| 76 | + return Object.keys(tags).map(key => tags[key] as LabelValue); |
| 77 | + } |
| 78 | +} |
0 commit comments