Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 55e0dbe

Browse files
authored
Adds metric utils to convert Stats data models to Metric data models (#191)
1 parent ba04ff7 commit 55e0dbe

4 files changed

Lines changed: 135 additions & 11 deletions

File tree

packages/opencensus-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export * from './stats/stats';
4747
export * from './stats/view';
4848
export * from './stats/recorder';
4949
export * from './stats/bucket-boundaries';
50+
export * from './stats/metric-utils';
5051

5152
// interfaces
5253
export * from './stats/types';

packages/opencensus-core/src/metrics/export/types.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,8 @@ export interface TimeSeries {
135135
readonly points: Point[];
136136
}
137137

138-
/** Properties of a LabelValue. */
139-
export interface LabelValue {
140-
/** The value for the label */
141-
readonly value: string;
142-
/**
143-
* If false the value field is ignored and considered not set.
144-
* This is used to differentiate a missing label from an empty string.
145-
*/
146-
readonly hasValue: boolean;
147-
}
138+
/** The LabelValue type. null value indicates an unset. */
139+
export type LabelValue = string|null;
148140

149141
/** A timestamped measurement. */
150142
export interface Point {
@@ -260,7 +252,7 @@ export interface Exemplar {
260252
/** The observation (sampling) time of the above value. */
261253
readonly timestamp: Timestamp;
262254
/** Contextual information about the example value. */
263-
readonly attachments: ({[k: string]: string}|null);
255+
readonly attachments: {[key: string]: string};
264256
}
265257

266258
/**
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
import * as assert from 'assert';
18+
19+
import {BaseView, MetricUtils} from '../src';
20+
import {MetricDescriptorType} from '../src/metrics/export/types';
21+
import {AggregationType, Measure, MeasureType, MeasureUnit, Tags} from '../src/stats/types';
22+
23+
24+
describe('MetricUtil', () => {
25+
it('should convert view to MetricDescriptor', () => {
26+
const VIEW_DESCRIPTION = 'view description';
27+
const measure: Measure = {
28+
name: 'Test Measure',
29+
type: MeasureType.DOUBLE,
30+
unit: MeasureUnit.UNIT
31+
};
32+
const tagKeys = ['testKey1', 'testKey2'];
33+
const view = new BaseView(
34+
'test/view/name', measure, AggregationType.LAST_VALUE, tagKeys,
35+
VIEW_DESCRIPTION);
36+
const metricDescriptor = MetricUtils.viewToMetricDescriptor(view);
37+
38+
assert.ok(metricDescriptor);
39+
assert.strictEqual(metricDescriptor.name, view.name);
40+
assert.strictEqual(metricDescriptor.unit, MeasureUnit.UNIT);
41+
assert.strictEqual(
42+
metricDescriptor.type, MetricDescriptorType.GAUGE_DOUBLE);
43+
assert.strictEqual(metricDescriptor.description, VIEW_DESCRIPTION);
44+
assert.deepStrictEqual(
45+
metricDescriptor.labelKeys, [{key: 'testKey1'}, {key: 'testKey2'}]);
46+
});
47+
48+
it('should convert tag values to label values', () => {
49+
const tags: Tags = {test: 'test1', tag: 'test2', empty: '', fake: null};
50+
assert.deepStrictEqual(
51+
MetricUtils.tagValuesToLabelValues(tags), ['test1', 'test2', '', null]);
52+
});
53+
});

0 commit comments

Comments
 (0)