Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 9c07ae4

Browse files
authored
Add MetricDescriptor class, tests (#342)
1 parent 7e671ef commit 9c07ae4

3 files changed

Lines changed: 200 additions & 0 deletions

File tree

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Copyright 2018, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import six
17+
18+
19+
class _MetricDescriptorTypeMeta(type):
20+
"""Helper for `x in MetricDescriptorType`."""
21+
22+
def __contains__(cls, item):
23+
return item in {MetricDescriptorType.GAUGE_INT64,
24+
MetricDescriptorType.GAUGE_DOUBLE,
25+
MetricDescriptorType.GAUGE_DISTRIBUTION,
26+
MetricDescriptorType.CUMULATIVE_INT64,
27+
MetricDescriptorType.CUMULATIVE_DOUBLE,
28+
MetricDescriptorType.CUMULATIVE_DISTRIBUTION}
29+
30+
31+
@six.add_metaclass(_MetricDescriptorTypeMeta)
32+
class MetricDescriptorType(object):
33+
"""The kind of metric. It describes how the data is reported.
34+
35+
MetricDescriptorType is an enum of valid MetricDescriptor type values. See
36+
opencensus-proto for details:
37+
38+
https://github.com/census-instrumentation/opencensus-proto/blob/24333298e36590ea0716598caacc8959fc393c48/src/opencensus/proto/metrics/v1/metrics.proto#L73 # noqa
39+
40+
A gauge is an instantaneous measurement of a value.
41+
42+
A cumulative measurement is a value accumulated over a time interval. In a
43+
time series, cumulative measurements should have the same start time and
44+
increasing end times, until an event resets the cumulative value to zero
45+
and sets a new start time for the following points.
46+
47+
"""
48+
# Integer gauge. The value can go both up and down.
49+
GAUGE_INT64 = 1
50+
51+
# Floating point gauge. The value can go both up and down.
52+
GAUGE_DOUBLE = 2
53+
54+
# Distribution gauge measurement. The count and sum can go both up and
55+
# down. Recorded values are always >= 0.
56+
# Used in scenarios like a snapshot of time the current items in a queue
57+
# have spent there.
58+
GAUGE_DISTRIBUTION = 3
59+
60+
# Integer cumulative measurement. The value cannot decrease, if resets then
61+
# the start_time should also be reset.
62+
CUMULATIVE_INT64 = 4
63+
64+
# Floating point cumulative measurement. The value cannot decrease, if
65+
# resets then the start_time should also be reset. Recorded values are
66+
# always >= 0.
67+
CUMULATIVE_DOUBLE = 5
68+
69+
# Distribution cumulative measurement. The count and sum cannot decrease,
70+
# if resets then the start_time should also be reset.
71+
CUMULATIVE_DISTRIBUTION = 6
72+
73+
# Some frameworks implemented Histograms as a summary of observations
74+
# (usually things like request durations and response sizes). While it also
75+
# provides a total count of observations and a sum of all observed values,
76+
# it calculates configurable percentiles over a sliding time window. This
77+
# is not recommended, since it cannot be aggregated.
78+
SUMMARY = 7
79+
80+
81+
class MetricDescriptor(object):
82+
"""Defines a metric type and its schema.
83+
84+
This class implements the spec for v1 MetricDescriptors, as of
85+
opencensus-proto release v0.0.2. See opencensus-proto for details:
86+
87+
https://github.com/census-instrumentation/opencensus-proto/blob/24333298e36590ea0716598caacc8959fc393c48/src/opencensus/proto/metrics/v1/metrics.proto#L53 # noqa
88+
89+
:type name: str
90+
:param name: The metric type, including its DNS name prefix. It must be
91+
unique.
92+
93+
:type description: str
94+
:param description: A detailed description of the metric, which can be used
95+
in documentation.
96+
97+
:type unit: str
98+
:param unit: The unit in which the metric value is reported. Follows the
99+
format described by http://unitsofmeasure.org/ucum.html.
100+
101+
:type type_: int
102+
:param unit: The unit in which the metric value is reported. The
103+
MetricDescriptorType class enumerates valid options.
104+
105+
:type label_keys: list(:class: '~opencensus.metrics.label_key.LabelKey')
106+
:param label_keys: The label keys associated with the metric descriptor.
107+
"""
108+
def __init__(self, name, description, unit, type_, label_keys):
109+
if type_ not in MetricDescriptorType:
110+
raise ValueError("Invalid type")
111+
112+
if not label_keys:
113+
raise ValueError("label_keys must not be empty or null")
114+
115+
if any(key is None for key in label_keys):
116+
raise ValueError("label_keys must not contain null keys")
117+
118+
self._name = name
119+
self._description = description
120+
self._unit = unit
121+
self._type = type_
122+
self._label_keys = label_keys
123+
124+
@property
125+
def name(self):
126+
return self._name
127+
128+
@property
129+
def description(self):
130+
return self._description
131+
132+
@property
133+
def unit(self):
134+
return self._unit
135+
136+
@property
137+
def type(self):
138+
return self._type
139+
140+
@property
141+
def label_keys(self):
142+
return self._label_keys

tests/unit/metrics/export/__init__.py

Whitespace-only changes.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# -*- coding: utf-8 -*-
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+
17+
import unittest
18+
19+
from opencensus.metrics.export.metric_descriptor import MetricDescriptor
20+
from opencensus.metrics.export.metric_descriptor import MetricDescriptorType
21+
from opencensus.metrics.label_key import LabelKey
22+
23+
24+
NAME = 'metric'
25+
DESCRIPTION = 'Metric description'
26+
UNIT = '0.738.[ft_i].[lbf_av]/s'
27+
LABEL_KEY1 = LabelKey('key1', 'key description one')
28+
LABEL_KEY2 = LabelKey('值', '测试用键')
29+
LABEL_KEYS = (LABEL_KEY1, LABEL_KEY2)
30+
31+
32+
class TestMetricDescriptor(unittest.TestCase):
33+
34+
def test_init(self):
35+
metric_descriptor = MetricDescriptor(NAME, DESCRIPTION, UNIT,
36+
MetricDescriptorType.GAUGE_DOUBLE,
37+
(LABEL_KEY1, LABEL_KEY2))
38+
39+
self.assertEqual(metric_descriptor.name, NAME)
40+
self.assertEqual(metric_descriptor.description, DESCRIPTION)
41+
self.assertEqual(metric_descriptor.unit, UNIT)
42+
self.assertEqual(metric_descriptor.type,
43+
MetricDescriptorType.GAUGE_DOUBLE)
44+
self.assertEqual(metric_descriptor.label_keys, LABEL_KEYS)
45+
46+
def test_bogus_type(self):
47+
with self.assertRaises(ValueError):
48+
MetricDescriptor(NAME, DESCRIPTION, UNIT, 0, (LABEL_KEY1,))
49+
50+
def test_null_label_keys(self):
51+
with self.assertRaises(ValueError):
52+
MetricDescriptor(NAME, DESCRIPTION, UNIT,
53+
MetricDescriptorType.GAUGE_DOUBLE, None)
54+
55+
def test_null_label_key_values(self):
56+
with self.assertRaises(ValueError):
57+
MetricDescriptor(NAME, DESCRIPTION, UNIT,
58+
MetricDescriptorType.GAUGE_DOUBLE, (None,))

0 commit comments

Comments
 (0)