|
| 1 | +# Copyright The OpenTelemetry 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 | +# pylint: disable=function-redefined |
| 16 | +# pylint: disable=dangerous-default-value |
| 17 | +# Classes in this module use dictionaries as default arguments. This is |
| 18 | +# considered dangerous by pylint because the default dictionary is shared by |
| 19 | +# all instances. Implementations of these classes must not make any change to |
| 20 | +# this default dictionary in __init__. |
| 21 | + |
| 22 | +from opentelemetry._metrics.instrument import ( |
| 23 | + Counter, |
| 24 | + Histogram, |
| 25 | + ObservableCounter, |
| 26 | + ObservableGauge, |
| 27 | + ObservableUpDownCounter, |
| 28 | + UpDownCounter, |
| 29 | +) |
| 30 | +from opentelemetry.sdk._metrics.aggregation import ( |
| 31 | + ExplicitBucketHistogramAggregation, |
| 32 | + LastValueAggregation, |
| 33 | + SumAggregation, |
| 34 | +) |
| 35 | + |
| 36 | + |
| 37 | +class _Instrument: |
| 38 | + def __init__( |
| 39 | + self, |
| 40 | + name, |
| 41 | + unit="", |
| 42 | + description="", |
| 43 | + aggregation=None, |
| 44 | + aggregation_config={}, |
| 45 | + ): |
| 46 | + self._attributes_aggregations = {} |
| 47 | + self._aggregation = aggregation |
| 48 | + self._aggregation_config = aggregation_config |
| 49 | + aggregation(self, **aggregation_config) |
| 50 | + |
| 51 | + |
| 52 | +class Counter(_Instrument, Counter): |
| 53 | + def __init__( |
| 54 | + self, |
| 55 | + name, |
| 56 | + unit="", |
| 57 | + description="", |
| 58 | + aggregation=SumAggregation, |
| 59 | + aggregation_config={}, |
| 60 | + ): |
| 61 | + super().__init__( |
| 62 | + name, |
| 63 | + unit=unit, |
| 64 | + description=description, |
| 65 | + aggregation=aggregation, |
| 66 | + aggregation_config=aggregation_config, |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +class UpDownCounter(_Instrument, UpDownCounter): |
| 71 | + def __init__( |
| 72 | + self, |
| 73 | + name, |
| 74 | + unit="", |
| 75 | + description="", |
| 76 | + aggregation=SumAggregation, |
| 77 | + aggregation_config={}, |
| 78 | + ): |
| 79 | + super().__init__( |
| 80 | + name, |
| 81 | + unit=unit, |
| 82 | + description=description, |
| 83 | + aggregation=aggregation, |
| 84 | + aggregation_config=aggregation_config, |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +class ObservableCounter(_Instrument, ObservableCounter): |
| 89 | + def __init__( |
| 90 | + self, |
| 91 | + name, |
| 92 | + callback, |
| 93 | + unit="", |
| 94 | + description="", |
| 95 | + aggregation=SumAggregation, |
| 96 | + aggregation_config={}, |
| 97 | + ): |
| 98 | + super().__init__( |
| 99 | + name, |
| 100 | + unit=unit, |
| 101 | + description=description, |
| 102 | + aggregation=aggregation, |
| 103 | + aggregation_config=aggregation_config, |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +class ObservableUpDownCounter(_Instrument, ObservableUpDownCounter): |
| 108 | + def __init__( |
| 109 | + self, |
| 110 | + name, |
| 111 | + callback, |
| 112 | + unit="", |
| 113 | + description="", |
| 114 | + aggregation=SumAggregation, |
| 115 | + aggregation_config={}, |
| 116 | + ): |
| 117 | + super().__init__( |
| 118 | + name, |
| 119 | + unit=unit, |
| 120 | + description=description, |
| 121 | + aggregation=aggregation, |
| 122 | + aggregation_config=aggregation_config, |
| 123 | + ) |
| 124 | + |
| 125 | + |
| 126 | +class Histogram(_Instrument, Histogram): |
| 127 | + def __init__( |
| 128 | + self, |
| 129 | + name, |
| 130 | + unit="", |
| 131 | + description="", |
| 132 | + aggregation=ExplicitBucketHistogramAggregation, |
| 133 | + aggregation_config={}, |
| 134 | + ): |
| 135 | + super().__init__( |
| 136 | + name, |
| 137 | + unit=unit, |
| 138 | + description=description, |
| 139 | + aggregation=aggregation, |
| 140 | + aggregation_config=aggregation_config, |
| 141 | + ) |
| 142 | + |
| 143 | + |
| 144 | +class ObservableGauge(_Instrument, ObservableGauge): |
| 145 | + def __init__( |
| 146 | + self, |
| 147 | + name, |
| 148 | + callback, |
| 149 | + unit="", |
| 150 | + description="", |
| 151 | + aggregation=LastValueAggregation, |
| 152 | + aggregation_config={}, |
| 153 | + ): |
| 154 | + super().__init__( |
| 155 | + name, |
| 156 | + unit=unit, |
| 157 | + description=description, |
| 158 | + aggregation=aggregation, |
| 159 | + aggregation_config=aggregation_config, |
| 160 | + ) |
0 commit comments