Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 672c449

Browse files
author
Alex Boten
authored
implement to_json for Metric (open-telemetry#2475)
1 parent 4ed4fd0 commit 672c449

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_metrics/point.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from dataclasses import dataclass
15+
import json
16+
from dataclasses import asdict, dataclass
1617
from enum import IntEnum
1718
from typing import Sequence, Union
1819

@@ -74,4 +75,18 @@ class Metric:
7475
"""Contains non-common fields for the given metric"""
7576

7677
def to_json(self) -> str:
77-
raise NotImplementedError()
78+
return json.dumps(
79+
{
80+
"attributes": self.attributes if self.attributes else "",
81+
"description": self.description if self.description else "",
82+
"instrumentation_info": repr(self.instrumentation_info)
83+
if self.instrumentation_info
84+
else "",
85+
"name": self.name,
86+
"resource": repr(self.resource.attributes)
87+
if self.resource
88+
else "",
89+
"unit": self.unit if self.unit else "",
90+
"point": asdict(self.point) if self.point else "",
91+
}
92+
)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)