Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit a6d6834

Browse files
authored
Replace isoformat with strftime (#552)
1 parent e7d4524 commit a6d6834

File tree

14 files changed

+60
-47
lines changed

14 files changed

+60
-47
lines changed

contrib/opencensus-ext-ocagent/tests/test_trace_exporter_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from datetime import datetime, timedelta
1717
import unittest
1818

19+
from opencensus.common import utils as common_utils
1920
from opencensus.ext.ocagent.trace_exporter import utils
2021
from opencensus.ext.ocagent.trace_exporter.gen.opencensus.trace.v1 \
2122
import trace_pb2
@@ -532,7 +533,8 @@ def test_datetime_str_to_proto_ts_conversion(self):
532533
expected_seconds = int(delta.total_seconds())
533534
expected_nanos = delta.microseconds * 1000
534535

535-
proto_ts = utils.proto_ts_from_datetime_str(now.isoformat() + 'Z')
536+
proto_ts = utils.proto_ts_from_datetime_str(
537+
common_utils.to_iso_str(now))
536538
self.assertEqual(proto_ts.seconds, int(expected_seconds))
537539
self.assertEqual(proto_ts.nanos, expected_nanos)
538540

opencensus/common/utils/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ def check_str_length(str_to_check, limit=MAX_LENGTH):
6969
return (result, truncated_byte_count)
7070

7171

72+
def to_iso_str(ts=None):
73+
"""Get an ISO 8601 string for a UTC datetime."""
74+
if ts is None:
75+
ts = datetime.datetime.utcnow()
76+
return ts.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
77+
78+
7279
def timestamp_to_microseconds(timestamp):
7380
"""Convert a timestamp string into a microseconds value
7481
:param timestamp

opencensus/stats/measurement_map.py

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

15-
from datetime import datetime
1615
import logging
1716

17+
from opencensus.common import utils
1818
from opencensus.tags import execution_context
1919

2020

@@ -114,6 +114,6 @@ def record(self, tag_map_tags=None):
114114
self.measure_to_view_map.record(
115115
tags=tag_map_tags,
116116
measurement_map=self.measurement_map,
117-
timestamp=datetime.utcnow().isoformat() + 'Z',
117+
timestamp=utils.to_iso_str(),
118118
attachments=self.attachments
119119
)

opencensus/stats/view_data.py

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

15-
from datetime import datetime
1615
import copy
1716

17+
from opencensus.common import utils
18+
1819

1920
class ViewData(object):
2021
"""View Data is the aggregated data for a particular view
@@ -62,11 +63,11 @@ def tag_value_aggregation_data_map(self):
6263

6364
def start(self):
6465
"""sets the start time for the view data"""
65-
self._start_time = datetime.utcnow().isoformat() + 'Z'
66+
self._start_time = utils.to_iso_str()
6667

6768
def end(self):
6869
"""sets the end time for the view data"""
69-
self._end_time = datetime.utcnow().isoformat() + 'Z'
70+
self._end_time = utils.to_iso_str()
7071

7172
def get_tag_values(self, tags, columns):
7273
"""function to get the tag values from tags and columns"""

opencensus/stats/view_manager.py

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

15+
from opencensus.common import utils
1516
from opencensus.stats.measure_to_view_map import MeasureToViewMap
1617
from opencensus.stats import execution_context
17-
from datetime import datetime
1818

1919

2020
class ViewManager(object):
2121
"""View Manager allows the registering of Views for collecting stats
2222
and receiving stats data as View Data"""
2323
def __init__(self):
24-
self.time = datetime.utcnow().isoformat() + 'Z'
24+
self.time = utils.to_iso_str()
2525
if execution_context.get_measure_to_view_map() == {}:
2626
execution_context.set_measure_to_view_map(MeasureToViewMap())
2727

opencensus/trace/span.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from datetime import datetime
1616
from itertools import chain
1717

18-
from opencensus.common.utils import get_truncatable_str
18+
from opencensus.common import utils
1919
from opencensus.trace import attributes
2020
from opencensus.trace import base_span
2121
from opencensus.trace import link as link_module
@@ -225,11 +225,11 @@ def add_link(self, link):
225225

226226
def start(self):
227227
"""Set the start time for a span."""
228-
self.start_time = datetime.utcnow().isoformat() + 'Z'
228+
self.start_time = utils.to_iso_str()
229229

230230
def finish(self):
231231
"""Set the end time for a span."""
232-
self.end_time = datetime.utcnow().isoformat() + 'Z'
232+
self.end_time = utils.to_iso_str()
233233

234234
def __iter__(self):
235235
"""Iterate through the span tree."""
@@ -265,7 +265,7 @@ def format_span_json(span):
265265
:returns: Formatted Span.
266266
"""
267267
span_json = {
268-
'displayName': get_truncatable_str(span.name),
268+
'displayName': utils.get_truncatable_str(span.name),
269269
'spanId': span.span_id,
270270
'startTime': span.start_time,
271271
'endTime': span.end_time,

opencensus/trace/time_event.py

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

15-
from opencensus.common.utils import get_truncatable_str
15+
from opencensus.common import utils
1616

1717

1818
class Type(object):
@@ -46,7 +46,8 @@ def __init__(self, description, attributes=None):
4646

4747
def format_annotation_json(self):
4848
annotation_json = {}
49-
annotation_json['description'] = get_truncatable_str(self.description)
49+
annotation_json['description'] = utils.get_truncatable_str(
50+
self.description)
5051

5152
if self.attributes is not None:
5253
annotation_json['attributes'] = self.attributes.\
@@ -126,7 +127,7 @@ class TimeEvent(object):
126127
spans.
127128
"""
128129
def __init__(self, timestamp, annotation=None, message_event=None):
129-
self.timestamp = timestamp.isoformat() + 'Z'
130+
self.timestamp = utils.to_iso_str(timestamp)
130131

131132
if annotation is not None and message_event is not None:
132133
raise ValueError("A TimeEvent can contain either an Annotation"

tests/unit/stats/exporters/test_stackdriver_stats.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from google.cloud import monitoring_v3
2020

21+
from opencensus.common import utils
2122
from opencensus.common.version import __version__
2223
from opencensus.stats import aggregation as aggregation_module
2324
from opencensus.stats import aggregation_data as aggregation_data_module
@@ -58,7 +59,7 @@
5859
VIDEO_SIZE_VIEW_NAME, "processed video size over time", [FRONTEND_KEY],
5960
VIDEO_SIZE_MEASURE, VIDEO_SIZE_DISTRIBUTION)
6061

61-
TEST_TIME = datetime(2018, 12, 25, 1, 2, 3, 4).isoformat() + 'Z'
62+
TEST_TIME = utils.to_iso_str(datetime(2018, 12, 25, 1, 2, 3, 4))
6263

6364

6465
class _Client(object):

tests/unit/stats/test_view_data.py

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

15-
import unittest
16-
import mock
1715
from datetime import datetime
16+
import mock
17+
import unittest
18+
19+
from opencensus.common import utils
1820
from opencensus.stats import aggregation as aggregation_module
1921
from opencensus.stats import measure as measure_module
2022
from opencensus.stats import view_data as view_data_module
@@ -82,7 +84,7 @@ def test_record(self):
8284

8385
context = mock.Mock()
8486
context.map = {'key1': 'val1', 'key2': 'val2'}
85-
time = datetime.utcnow().isoformat() + 'Z'
87+
time = utils.to_iso_str()
8688
value = 1
8789
self.assertEqual({}, view_data.tag_value_aggregation_data_map)
8890

@@ -143,7 +145,7 @@ def test_record_with_attachment(self):
143145
view=view, start_time=start_time, end_time=end_time)
144146
context = mock.Mock
145147
context.map = {'key1': 'val1', 'key2': 'val2'}
146-
time = datetime.utcnow().isoformat() + 'Z'
148+
time = utils.to_iso_str()
147149
value = 1
148150

149151
view_data.record(
@@ -193,7 +195,7 @@ def test_record_with_attachment_no_histogram(self):
193195
view=view, start_time=start_time, end_time=end_time)
194196
context = mock.Mock
195197
context.map = {'key1': 'val1', 'key2': 'val2'}
196-
time = datetime.utcnow().isoformat() + 'Z'
198+
time = utils.to_iso_str()
197199
value = 1
198200
view_data.record(
199201
context=context,
@@ -223,7 +225,7 @@ def test_record_with_multi_keys(self):
223225
view=view, start_time=start_time, end_time=end_time)
224226
context = mock.Mock()
225227
context.map = {'key1': 'val1', 'key2': 'val2'}
226-
time = datetime.utcnow().isoformat() + 'Z'
228+
time = utils.to_iso_str()
227229
value = 1
228230
self.assertEqual({}, view_data.tag_value_aggregation_data_map)
229231

@@ -242,7 +244,7 @@ def test_record_with_multi_keys(self):
242244

243245
context_2 = mock.Mock()
244246
context_2.map = {'key1': 'val3', 'key2': 'val2'}
245-
time_2 = datetime.utcnow().isoformat() + 'Z'
247+
time_2 = utils.to_iso_str()
246248
value_2 = 2
247249
view_data.record(
248250
context=context_2,
@@ -258,7 +260,7 @@ def test_record_with_multi_keys(self):
258260
sum_data_2 = view_data.tag_value_aggregation_data_map.get(tuple_vals_2)
259261
self.assertEqual(2, sum_data_2.sum_data)
260262

261-
time_3 = datetime.utcnow().isoformat() + 'Z'
263+
time_3 = utils.to_iso_str()
262264
value_3 = 3
263265
# Use the same context {'key1': 'val1', 'key2': 'val2'}.
264266
# Record to entry [(val1, val2), sum=1].
@@ -282,7 +284,7 @@ def test_record_with_missing_key_in_context(self):
282284
'key1': 'val1',
283285
'key3': 'val3'
284286
} # key2 is not in the context.
285-
time = datetime.utcnow().isoformat() + 'Z'
287+
time = utils.to_iso_str()
286288
value = 4
287289
view_data.record(
288290
context=context, value=value, timestamp=time, attachments=None)
@@ -303,7 +305,7 @@ def test_record_with_none_context(self):
303305
end_time = datetime.utcnow()
304306
view_data = view_data_module.ViewData(
305307
view=view, start_time=start_time, end_time=end_time)
306-
time = datetime.utcnow().isoformat() + 'Z'
308+
time = utils.to_iso_str()
307309
value = 4
308310
view_data.record(
309311
context=None, value=value, timestamp=time, attachments=None)

tests/unit/trace/test_blank_span.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import datetime
1616
import mock
1717
import unittest
18+
19+
from opencensus.common import utils
1820
from opencensus.trace.link import Link
1921
from opencensus.trace.span import format_span_json
2022
from opencensus.trace.time_event import TimeEvent
@@ -85,13 +87,12 @@ def test_do_not_crash(self):
8587
span.finish()
8688

8789
def test_constructor_explicit(self):
88-
from datetime import datetime
8990

9091
span_id = 'test_span_id'
9192
span_name = 'test_span_name'
9293
parent_span = mock.Mock()
93-
start_time = datetime.utcnow().isoformat() + 'Z'
94-
end_time = datetime.utcnow().isoformat() + 'Z'
94+
start_time = utils.to_iso_str()
95+
end_time = utils.to_iso_str()
9596
attributes = {
9697
'http.status_code': '200',
9798
'component': 'HTTP load balancer',

0 commit comments

Comments
 (0)