Skip to content

Commit

Permalink
Replace isoformat with strftime (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
c24t authored Mar 11, 2019
1 parent e7d4524 commit a6d6834
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from datetime import datetime, timedelta
import unittest

from opencensus.common import utils as common_utils
from opencensus.ext.ocagent.trace_exporter import utils
from opencensus.ext.ocagent.trace_exporter.gen.opencensus.trace.v1 \
import trace_pb2
Expand Down Expand Up @@ -532,7 +533,8 @@ def test_datetime_str_to_proto_ts_conversion(self):
expected_seconds = int(delta.total_seconds())
expected_nanos = delta.microseconds * 1000

proto_ts = utils.proto_ts_from_datetime_str(now.isoformat() + 'Z')
proto_ts = utils.proto_ts_from_datetime_str(
common_utils.to_iso_str(now))
self.assertEqual(proto_ts.seconds, int(expected_seconds))
self.assertEqual(proto_ts.nanos, expected_nanos)

Expand Down
7 changes: 7 additions & 0 deletions opencensus/common/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ def check_str_length(str_to_check, limit=MAX_LENGTH):
return (result, truncated_byte_count)


def to_iso_str(ts=None):
"""Get an ISO 8601 string for a UTC datetime."""
if ts is None:
ts = datetime.datetime.utcnow()
return ts.strftime("%Y-%m-%dT%H:%M:%S.%fZ")


def timestamp_to_microseconds(timestamp):
"""Convert a timestamp string into a microseconds value
:param timestamp
Expand Down
4 changes: 2 additions & 2 deletions opencensus/stats/measurement_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from datetime import datetime
import logging

from opencensus.common import utils
from opencensus.tags import execution_context


Expand Down Expand Up @@ -114,6 +114,6 @@ def record(self, tag_map_tags=None):
self.measure_to_view_map.record(
tags=tag_map_tags,
measurement_map=self.measurement_map,
timestamp=datetime.utcnow().isoformat() + 'Z',
timestamp=utils.to_iso_str(),
attachments=self.attachments
)
7 changes: 4 additions & 3 deletions opencensus/stats/view_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from datetime import datetime
import copy

from opencensus.common import utils


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

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

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

def get_tag_values(self, tags, columns):
"""function to get the tag values from tags and columns"""
Expand Down
4 changes: 2 additions & 2 deletions opencensus/stats/view_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from opencensus.common import utils
from opencensus.stats.measure_to_view_map import MeasureToViewMap
from opencensus.stats import execution_context
from datetime import datetime


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

Expand Down
8 changes: 4 additions & 4 deletions opencensus/trace/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from datetime import datetime
from itertools import chain

from opencensus.common.utils import get_truncatable_str
from opencensus.common import utils
from opencensus.trace import attributes
from opencensus.trace import base_span
from opencensus.trace import link as link_module
Expand Down Expand Up @@ -225,11 +225,11 @@ def add_link(self, link):

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

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

def __iter__(self):
"""Iterate through the span tree."""
Expand Down Expand Up @@ -265,7 +265,7 @@ def format_span_json(span):
:returns: Formatted Span.
"""
span_json = {
'displayName': get_truncatable_str(span.name),
'displayName': utils.get_truncatable_str(span.name),
'spanId': span.span_id,
'startTime': span.start_time,
'endTime': span.end_time,
Expand Down
7 changes: 4 additions & 3 deletions opencensus/trace/time_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from opencensus.common.utils import get_truncatable_str
from opencensus.common import utils


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

def format_annotation_json(self):
annotation_json = {}
annotation_json['description'] = get_truncatable_str(self.description)
annotation_json['description'] = utils.get_truncatable_str(
self.description)

if self.attributes is not None:
annotation_json['attributes'] = self.attributes.\
Expand Down Expand Up @@ -126,7 +127,7 @@ class TimeEvent(object):
spans.
"""
def __init__(self, timestamp, annotation=None, message_event=None):
self.timestamp = timestamp.isoformat() + 'Z'
self.timestamp = utils.to_iso_str(timestamp)

if annotation is not None and message_event is not None:
raise ValueError("A TimeEvent can contain either an Annotation"
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/stats/exporters/test_stackdriver_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from google.cloud import monitoring_v3

from opencensus.common import utils
from opencensus.common.version import __version__
from opencensus.stats import aggregation as aggregation_module
from opencensus.stats import aggregation_data as aggregation_data_module
Expand Down Expand Up @@ -58,7 +59,7 @@
VIDEO_SIZE_VIEW_NAME, "processed video size over time", [FRONTEND_KEY],
VIDEO_SIZE_MEASURE, VIDEO_SIZE_DISTRIBUTION)

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


class _Client(object):
Expand Down
22 changes: 12 additions & 10 deletions tests/unit/stats/test_view_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
import mock
from datetime import datetime
import mock
import unittest

from opencensus.common import utils
from opencensus.stats import aggregation as aggregation_module
from opencensus.stats import measure as measure_module
from opencensus.stats import view_data as view_data_module
Expand Down Expand Up @@ -82,7 +84,7 @@ def test_record(self):

context = mock.Mock()
context.map = {'key1': 'val1', 'key2': 'val2'}
time = datetime.utcnow().isoformat() + 'Z'
time = utils.to_iso_str()
value = 1
self.assertEqual({}, view_data.tag_value_aggregation_data_map)

Expand Down Expand Up @@ -143,7 +145,7 @@ def test_record_with_attachment(self):
view=view, start_time=start_time, end_time=end_time)
context = mock.Mock
context.map = {'key1': 'val1', 'key2': 'val2'}
time = datetime.utcnow().isoformat() + 'Z'
time = utils.to_iso_str()
value = 1

view_data.record(
Expand Down Expand Up @@ -193,7 +195,7 @@ def test_record_with_attachment_no_histogram(self):
view=view, start_time=start_time, end_time=end_time)
context = mock.Mock
context.map = {'key1': 'val1', 'key2': 'val2'}
time = datetime.utcnow().isoformat() + 'Z'
time = utils.to_iso_str()
value = 1
view_data.record(
context=context,
Expand Down Expand Up @@ -223,7 +225,7 @@ def test_record_with_multi_keys(self):
view=view, start_time=start_time, end_time=end_time)
context = mock.Mock()
context.map = {'key1': 'val1', 'key2': 'val2'}
time = datetime.utcnow().isoformat() + 'Z'
time = utils.to_iso_str()
value = 1
self.assertEqual({}, view_data.tag_value_aggregation_data_map)

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

context_2 = mock.Mock()
context_2.map = {'key1': 'val3', 'key2': 'val2'}
time_2 = datetime.utcnow().isoformat() + 'Z'
time_2 = utils.to_iso_str()
value_2 = 2
view_data.record(
context=context_2,
Expand All @@ -258,7 +260,7 @@ def test_record_with_multi_keys(self):
sum_data_2 = view_data.tag_value_aggregation_data_map.get(tuple_vals_2)
self.assertEqual(2, sum_data_2.sum_data)

time_3 = datetime.utcnow().isoformat() + 'Z'
time_3 = utils.to_iso_str()
value_3 = 3
# Use the same context {'key1': 'val1', 'key2': 'val2'}.
# Record to entry [(val1, val2), sum=1].
Expand All @@ -282,7 +284,7 @@ def test_record_with_missing_key_in_context(self):
'key1': 'val1',
'key3': 'val3'
} # key2 is not in the context.
time = datetime.utcnow().isoformat() + 'Z'
time = utils.to_iso_str()
value = 4
view_data.record(
context=context, value=value, timestamp=time, attachments=None)
Expand All @@ -303,7 +305,7 @@ def test_record_with_none_context(self):
end_time = datetime.utcnow()
view_data = view_data_module.ViewData(
view=view, start_time=start_time, end_time=end_time)
time = datetime.utcnow().isoformat() + 'Z'
time = utils.to_iso_str()
value = 4
view_data.record(
context=None, value=value, timestamp=time, attachments=None)
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/trace/test_blank_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import datetime
import mock
import unittest

from opencensus.common import utils
from opencensus.trace.link import Link
from opencensus.trace.span import format_span_json
from opencensus.trace.time_event import TimeEvent
Expand Down Expand Up @@ -85,13 +87,12 @@ def test_do_not_crash(self):
span.finish()

def test_constructor_explicit(self):
from datetime import datetime

span_id = 'test_span_id'
span_name = 'test_span_name'
parent_span = mock.Mock()
start_time = datetime.utcnow().isoformat() + 'Z'
end_time = datetime.utcnow().isoformat() + 'Z'
start_time = utils.to_iso_str()
end_time = utils.to_iso_str()
attributes = {
'http.status_code': '200',
'component': 'HTTP load balancer',
Expand Down
9 changes: 4 additions & 5 deletions tests/unit/trace/test_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime
import unittest

import mock

from google.rpc import code_pb2

from opencensus.common import utils
from opencensus.trace.stack_trace import StackTrace
from opencensus.trace.status import Status
from opencensus.trace.time_event import TimeEvent
Expand Down Expand Up @@ -56,13 +58,12 @@ def test_constructor_defaults(self):
self.assertIsNone(span.context_tracer)

def test_constructor_explicit(self):
from datetime import datetime

span_id = 'test_span_id'
span_name = 'test_span_name'
parent_span = mock.Mock()
start_time = datetime.utcnow().isoformat() + 'Z'
end_time = datetime.utcnow().isoformat() + 'Z'
start_time = utils.to_iso_str()
end_time = utils.to_iso_str()
attributes = {
'http.status_code': '200',
'component': 'HTTP load balancer',
Expand Down Expand Up @@ -136,7 +137,6 @@ def test_add_attribute(self):

def test_add_time_event(self):
from opencensus.trace.time_event import TimeEvent
import datetime

span_name = 'test_span_name'
span = self._make_one(span_name)
Expand Down Expand Up @@ -320,7 +320,6 @@ def test_format_span_json_no_parent_span(self):
@mock.patch.object(TimeEvent, 'format_time_event_json')
def test_format_span_json_with_parent_span(self, time_event_mock,
status_mock, stack_trace_mock):
import datetime

from opencensus.trace.link import Link
from opencensus.trace.span import format_span_json
Expand Down
13 changes: 7 additions & 6 deletions tests/unit/trace/test_span_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import datetime
import unittest

from opencensus.common import utils
from opencensus.trace import link
from opencensus.trace import span_context
from opencensus.trace import span_data as span_data_module
Expand All @@ -31,8 +32,8 @@ def test_create_span_data(self):
span_id='6e0c63257de34c92',
parent_span_id='6e0c63257de34c93',
attributes={'key1': 'value1'},
start_time=datetime.datetime.utcnow().isoformat() + 'Z',
end_time=datetime.datetime.utcnow().isoformat() + 'Z',
start_time=utils.to_iso_str(),
end_time=utils.to_iso_str(),
stack_trace=None,
links=None,
status=None,
Expand All @@ -49,8 +50,8 @@ def test_span_data_immutable(self):
span_id='6e0c63257de34c92',
parent_span_id='6e0c63257de34c93',
attributes={'key1': 'value1'},
start_time=datetime.datetime.utcnow().isoformat() + 'Z',
end_time=datetime.datetime.utcnow().isoformat() + 'Z',
start_time=utils.to_iso_str(),
end_time=utils.to_iso_str(),
stack_trace=None,
links=None,
status=None,
Expand All @@ -76,8 +77,8 @@ def test_format_legacy_trace_json(self):
span_id='6e0c63257de34c92',
parent_span_id='6e0c63257de34c93',
attributes={'key1': 'value1'},
start_time=datetime.datetime.utcnow().isoformat() + 'Z',
end_time=datetime.datetime.utcnow().isoformat() + 'Z',
start_time=utils.to_iso_str(),
end_time=utils.to_iso_str(),
stack_trace=stack_trace.StackTrace(stack_trace_hash_id='111'),
links=[link.Link('1111', span_id='6e0c63257de34c92')],
status=status.Status(code=0, message='pok'),
Expand Down
Loading

0 comments on commit a6d6834

Please sign in to comment.