Skip to content

Commit e12909e

Browse files
authored
Add repr to a few stats and metrics classes (census-instrumentation#479)
1 parent f669aa9 commit e12909e

8 files changed

Lines changed: 91 additions & 0 deletions

File tree

opencensus/metrics/export/metric.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ def __init__(self, descriptor, time_series):
4242
self._descriptor = descriptor
4343
self._check_type()
4444

45+
def __repr__(self):
46+
return ('{}(descriptor.name="{}")'
47+
.format(
48+
type(self).__name__,
49+
self.descriptor.name
50+
))
51+
4552
@property
4653
def time_series(self):
4754
return self._time_series

opencensus/metrics/export/metric_descriptor.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@ def __init__(self, name, description, unit, type_, label_keys):
142142
self._type = type_
143143
self._label_keys = label_keys
144144

145+
def __repr__(self):
146+
type_name = MetricDescriptorType.to_type_class(self.type).__name__
147+
return ('{}(name="{}", description="{}", unit={}, type={})'
148+
.format(
149+
type(self).__name__,
150+
self.name,
151+
self.description,
152+
self.unit,
153+
type_name,
154+
))
155+
145156
@property
146157
def name(self):
147158
return self._name

opencensus/metrics/export/point.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,11 @@ def value(self):
3636
def timestamp(self):
3737
"""Returns the Timestamp when this Point was recorded."""
3838
return self._timestamp
39+
40+
def __repr__(self):
41+
return ("{}(value={}, timestamp={})"
42+
.format(
43+
type(self).__name__,
44+
self.value,
45+
self.timestamp
46+
))

opencensus/metrics/export/time_series.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ def __init__(self, label_values, points, start_timestamp):
4646
self._points = points
4747
self._start_timestamp = start_timestamp
4848

49+
def __repr__(self):
50+
return ('{}(points={}, label_values={}, start_timestamp={})'
51+
.format(
52+
type(self).__name__,
53+
self.points,
54+
self.label_values,
55+
self.start_timestamp
56+
))
57+
4958
@property
5059
def start_timestamp(self):
5160
return self._start_timestamp

opencensus/metrics/export/value.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ class ValueDouble(Value):
7979
def __init__(self, value):
8080
super(ValueDouble, self).__init__(value)
8181

82+
def __repr__(self):
83+
return ("{}({})"
84+
.format(
85+
type(self).__name__,
86+
self.value,
87+
))
88+
8289

8390
class ValueLong(Value):
8491
"""A 64-bit integer.

opencensus/metrics/label_key.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ def __init__(self, key, description):
2626
self._key = key
2727
self._description = description
2828

29+
def __repr__(self):
30+
if self.description:
31+
return ('{}({}, description="{}")'
32+
.format(
33+
type(self).__name__,
34+
self.key,
35+
self.description
36+
))
37+
return ("{}({})"
38+
.format(
39+
type(self).__name__,
40+
self.key,
41+
))
42+
2943
@property
3044
def key(self):
3145
"""the key for the label"""

opencensus/metrics/label_value.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ class LabelValue(object):
2222
def __init__(self, value=None):
2323
self._value = value
2424

25+
def __repr__(self):
26+
return ("{}({})"
27+
.format(
28+
type(self).__name__,
29+
self.value,
30+
))
31+
2532
@property
2633
def value(self):
2734
"""the value for the label"""

opencensus/stats/aggregation_data.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ def __init__(self, sum_data):
6464
super(SumAggregationDataFloat, self).__init__(sum_data)
6565
self._sum_data = sum_data
6666

67+
def __repr__(self):
68+
return ("{}({})"
69+
.format(
70+
type(self).__name__,
71+
self.sum_data,
72+
))
73+
6774
def add_sample(self, value, timestamp=None, attachments=None):
6875
"""Allows the user to add a sample to the Sum Aggregation Data
6976
The value of the sample is then added to the current sum data
@@ -100,6 +107,13 @@ def __init__(self, count_data):
100107
super(CountAggregationData, self).__init__(count_data)
101108
self._count_data = count_data
102109

110+
def __repr__(self):
111+
return ("{}({})"
112+
.format(
113+
type(self).__name__,
114+
self.count_data,
115+
))
116+
103117
def add_sample(self, value, timestamp=None, attachments=None):
104118
"""Adds a sample to the current Count Aggregation Data and adds 1 to
105119
the count data"""
@@ -194,6 +208,13 @@ def __init__(self,
194208
assert len(counts_per_bucket) == len(bounds) + 1
195209
self._counts_per_bucket = counts_per_bucket
196210

211+
def __repr__(self):
212+
return ("{}({})"
213+
.format(
214+
type(self).__name__,
215+
self.count_data,
216+
))
217+
197218
@property
198219
def mean_data(self):
199220
"""The current mean data"""
@@ -340,6 +361,13 @@ def __init__(self, value):
340361
super(LastValueAggregationData, self).__init__(value)
341362
self._value = value
342363

364+
def __repr__(self):
365+
return ("{}({})"
366+
.format(
367+
type(self).__name__,
368+
self.value,
369+
))
370+
343371
def add_sample(self, value, timestamp=None, attachments=None):
344372
"""Adds a sample to the current
345373
LastValue Aggregation Data and overwrite

0 commit comments

Comments
 (0)