|
| 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 | + |
| 16 | +from opentelemetry.attributes import BoundedAttributes |
| 17 | +from opentelemetry.sdk.metrics.export import ( |
| 18 | + AggregationTemporality, |
| 19 | + Gauge, |
| 20 | + Metric, |
| 21 | + NumberDataPoint, |
| 22 | + Sum, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +def _generate_metric( |
| 27 | + name, data, attributes=None, description=None, unit=None |
| 28 | +) -> Metric: |
| 29 | + if description is None: |
| 30 | + description = "foo" |
| 31 | + if unit is None: |
| 32 | + unit = "s" |
| 33 | + return Metric( |
| 34 | + name=name, |
| 35 | + description=description, |
| 36 | + unit=unit, |
| 37 | + data=data, |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def _generate_sum( |
| 42 | + name, |
| 43 | + value, |
| 44 | + attributes=None, |
| 45 | + description=None, |
| 46 | + unit=None, |
| 47 | + is_monotonic=True, |
| 48 | +) -> Metric: |
| 49 | + if attributes is None: |
| 50 | + attributes = BoundedAttributes(attributes={"a": 1, "b": True}) |
| 51 | + return _generate_metric( |
| 52 | + name, |
| 53 | + Sum( |
| 54 | + data_points=[ |
| 55 | + NumberDataPoint( |
| 56 | + attributes=attributes, |
| 57 | + start_time_unix_nano=1641946015139533244, |
| 58 | + time_unix_nano=1641946016139533244, |
| 59 | + value=value, |
| 60 | + ) |
| 61 | + ], |
| 62 | + aggregation_temporality=AggregationTemporality.CUMULATIVE, |
| 63 | + is_monotonic=is_monotonic, |
| 64 | + ), |
| 65 | + description=description, |
| 66 | + unit=unit, |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +def _generate_gauge( |
| 71 | + name, value, attributes=None, description=None, unit=None |
| 72 | +) -> Metric: |
| 73 | + if attributes is None: |
| 74 | + attributes = BoundedAttributes(attributes={"a": 1, "b": True}) |
| 75 | + return _generate_metric( |
| 76 | + name, |
| 77 | + Gauge( |
| 78 | + data_points=[ |
| 79 | + NumberDataPoint( |
| 80 | + attributes=attributes, |
| 81 | + start_time_unix_nano=1641946015139533244, |
| 82 | + time_unix_nano=1641946016139533244, |
| 83 | + value=value, |
| 84 | + ) |
| 85 | + ], |
| 86 | + ), |
| 87 | + description=description, |
| 88 | + unit=unit, |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +def _generate_unsupported_metric( |
| 93 | + name, attributes=None, description=None, unit=None |
| 94 | +) -> Metric: |
| 95 | + return _generate_metric( |
| 96 | + name, |
| 97 | + None, |
| 98 | + description=description, |
| 99 | + unit=unit, |
| 100 | + ) |
0 commit comments