|
| 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 | +from unittest import TestCase |
| 16 | + |
| 17 | +from opentelemetry.sdk._metrics.point import Gauge, Histogram, Metric, Sum |
| 18 | +from opentelemetry.sdk.resources import Resource |
| 19 | +from opentelemetry.sdk.util.instrumentation import InstrumentationInfo |
| 20 | + |
| 21 | + |
| 22 | +def _create_metric(value): |
| 23 | + return Metric( |
| 24 | + attributes={"attr-key": "test-val"}, |
| 25 | + description="test-description", |
| 26 | + instrumentation_info=InstrumentationInfo( |
| 27 | + name="name", version="version" |
| 28 | + ), |
| 29 | + name="test-name", |
| 30 | + resource=Resource({"resource-key": "resource-val"}), |
| 31 | + unit="test-unit", |
| 32 | + point=value, |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +class TestDatapointToJSON(TestCase): |
| 37 | + def test_sum(self): |
| 38 | + point = _create_metric( |
| 39 | + Sum( |
| 40 | + start_time_unix_nano=10, |
| 41 | + time_unix_nano=20, |
| 42 | + value=9, |
| 43 | + aggregation_temporality=2, |
| 44 | + is_monotonic=True, |
| 45 | + ) |
| 46 | + ) |
| 47 | + self.assertEqual( |
| 48 | + '{"attributes": {"attr-key": "test-val"}, "description": "test-description", "instrumentation_info": "InstrumentationInfo(name, version, None)", "name": "test-name", "resource": "BoundedAttributes({\'resource-key\': \'resource-val\'}, maxlen=None)", "unit": "test-unit", "point": {"start_time_unix_nano": 10, "time_unix_nano": 20, "value": 9, "aggregation_temporality": 2, "is_monotonic": true}}', |
| 49 | + point.to_json(), |
| 50 | + ) |
| 51 | + |
| 52 | + def test_gauge(self): |
| 53 | + point = _create_metric(Gauge(time_unix_nano=40, value=20)) |
| 54 | + self.assertEqual( |
| 55 | + '{"attributes": {"attr-key": "test-val"}, "description": "test-description", "instrumentation_info": "InstrumentationInfo(name, version, None)", "name": "test-name", "resource": "BoundedAttributes({\'resource-key\': \'resource-val\'}, maxlen=None)", "unit": "test-unit", "point": {"time_unix_nano": 40, "value": 20}}', |
| 56 | + point.to_json(), |
| 57 | + ) |
| 58 | + |
| 59 | + def test_histogram(self): |
| 60 | + point = _create_metric( |
| 61 | + Histogram( |
| 62 | + start_time_unix_nano=50, |
| 63 | + time_unix_nano=60, |
| 64 | + bucket_counts=[0, 0, 1, 0], |
| 65 | + explicit_bounds=[0.1, 0.5, 0.9, 1], |
| 66 | + aggregation_temporality=1, |
| 67 | + sum=0.8, |
| 68 | + ) |
| 69 | + ) |
| 70 | + self.maxDiff = None |
| 71 | + self.assertEqual( |
| 72 | + '{"attributes": {"attr-key": "test-val"}, "description": "test-description", "instrumentation_info": "InstrumentationInfo(name, version, None)", "name": "test-name", "resource": "BoundedAttributes({\'resource-key\': \'resource-val\'}, maxlen=None)", "unit": "test-unit", "point": {"start_time_unix_nano": 50, "time_unix_nano": 60, "bucket_counts": [0, 0, 1, 0], "explicit_bounds": [0.1, 0.5, 0.9, 1], "sum": 0.8, "aggregation_temporality": 1}}', |
| 73 | + point.to_json(), |
| 74 | + ) |
0 commit comments