|
| 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 |
0 commit comments