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

Commit 017628d

Browse files
authored
Refactor code timestampStr to microseconds (#332)
* Refactor Code * Change microsecond abbreviation: ms -> mus
1 parent 9967836 commit 017628d

File tree

3 files changed

+33
-51
lines changed

3 files changed

+33
-51
lines changed

opencensus/trace/exporters/jaeger_exporter.py

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
"""Export the spans data to Jaeger."""
1616

17-
import calendar
18-
import datetime
1917
import logging
2018
import socket
2119

@@ -26,12 +24,12 @@
2624
from opencensus.trace.exporters import base
2725
from opencensus.trace.exporters.gen.jaeger import agent, jaeger
2826
from opencensus.trace.exporters.transports import sync
27+
from opencensus.trace.utils import timestamp_to_microseconds
2928

3029
DEFAULT_HOST_NAME = 'localhost'
3130
DEFAULT_AGENT_PORT = 6831
3231
DEFAULT_ENDPOINT = '/api/traces?format=jaeger.thrift'
3332

34-
ISO_DATETIME_REGEX = '%Y-%m-%dT%H:%M:%S.%fZ'
3533
UDP_PACKET_MAX_LENGTH = 65000
3634

3735
logging = logging.getLogger(__name__)
@@ -176,19 +174,9 @@ def translate_to_jaeger(self, span_datas):
176174
jaeger_spans = []
177175

178176
for span in span_datas:
179-
start_datetime = datetime.datetime.strptime(
180-
span.start_time, ISO_DATETIME_REGEX)
181-
start_microsec = calendar.timegm(start_datetime.timetuple()) \
182-
* 1e6 \
183-
+ start_datetime.microsecond
184-
185-
end_datetime = datetime.datetime.strptime(
186-
span.end_time, ISO_DATETIME_REGEX)
187-
end_microsec = calendar.timegm(end_datetime.timetuple()) \
188-
* 1e6 \
189-
+ end_datetime.microsecond
190-
191-
duration_microsec = end_microsec - start_microsec
177+
start_timestamp_ms = timestamp_to_microseconds(span.start_time)
178+
end_timestamp_ms = timestamp_to_microseconds(span.end_time)
179+
duration_ms = end_timestamp_ms - start_timestamp_ms
192180

193181
tags = _extract_tags(span.attributes)
194182

@@ -220,8 +208,8 @@ def translate_to_jaeger(self, span_datas):
220208
traceIdLow=_convert_hex_str_to_int(trace_id[16:32]),
221209
spanId=_convert_hex_str_to_int(span_id),
222210
operationName=span.name,
223-
startTime=int(round(start_microsec)),
224-
duration=int(round(duration_microsec)),
211+
startTime=int(round(start_timestamp_ms)),
212+
duration=int(round(duration_ms)),
225213
tags=tags,
226214
logs=logs,
227215
references=refs,
@@ -290,12 +278,9 @@ def _extract_logs_from_span(span):
290278
vType=jaeger.TagType.STRING,
291279
vStr=annotation.description))
292280

293-
event_time = datetime.datetime.strptime(
294-
time_event.timestamp, ISO_DATETIME_REGEX)
295-
timestamp = calendar.timegm(event_time.timetuple()) \
296-
* 1e6 + event_time.microsecond
297-
298-
logs.append(jaeger.Log(timestamp=int(round(timestamp)), fields=fields))
281+
event_timestamp = timestamp_to_microseconds(time_event.timestamp)
282+
logs.append(jaeger.Log(timestamp=int(round(event_timestamp)),
283+
fields=fields))
299284
return logs
300285

301286

opencensus/trace/exporters/zipkin_exporter.py

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,21 @@
1414

1515
"""Export the spans data to Zipkin Collector."""
1616

17-
import calendar
18-
import datetime
1917
import json
2018
import logging
2119

2220
import requests
2321

2422
from opencensus.trace.exporters import base
2523
from opencensus.trace.exporters.transports import sync
26-
from opencensus.trace.utils import check_str_length
24+
from opencensus.trace.utils import check_str_length, timestamp_to_microseconds
2725

2826
DEFAULT_ENDPOINT = '/api/v2/spans'
2927
DEFAULT_HOST_NAME = 'localhost'
3028
DEFAULT_PORT = 9411
3129
DEFAULT_PROTOCOL = 'http'
3230
ZIPKIN_HEADERS = {'Content-Type': 'application/json'}
3331

34-
ISO_DATETIME_REGEX = '%Y-%m-%dT%H:%M:%S.%fZ'
35-
3632
SPAN_KIND_MAP = {
3733
0: None, # span kind unspecified
3834
1: "SERVER",
@@ -152,28 +148,16 @@ def translate_to_zipkin(self, span_datas):
152148

153149
for span in span_datas:
154150
# Timestamp in zipkin spans is int of microseconds.
155-
start_datetime = datetime.datetime.strptime(
156-
span.start_time,
157-
ISO_DATETIME_REGEX)
158-
start_timestamp_ms = calendar.timegm(
159-
start_datetime.timetuple()) * 1000 * 1000 \
160-
+ start_datetime.microsecond
161-
162-
end_datetime = datetime.datetime.strptime(
163-
span.end_time,
164-
ISO_DATETIME_REGEX)
165-
end_timestamp_ms = calendar.timegm(
166-
end_datetime.timetuple()) * 1000 * 1000 \
167-
+ end_datetime.microsecond
168-
169-
duration_ms = end_timestamp_ms - start_timestamp_ms
151+
start_timestamp_mus = timestamp_to_microseconds(span.start_time)
152+
end_timestamp_mus = timestamp_to_microseconds(span.end_time)
153+
duration_mus = end_timestamp_mus - start_timestamp_mus
170154

171155
zipkin_span = {
172156
'traceId': span.context.trace_id,
173157
'id': str(span.span_id),
174158
'name': span.name,
175-
'timestamp': int(round(start_timestamp_ms)),
176-
'duration': int(round(duration_ms)),
159+
'timestamp': int(round(start_timestamp_mus)),
160+
'duration': int(round(duration_mus)),
177161
'localEndpoint': local_endpoint,
178162
'tags': _extract_tags_from_span(span.attributes),
179163
'annotations': _extract_annotations_from_span(span),
@@ -225,11 +209,8 @@ def _extract_annotations_from_span(span):
225209
if not annotation:
226210
continue
227211

228-
event_time = datetime.datetime.strptime(time_event.timestamp,
229-
ISO_DATETIME_REGEX)
230-
epoch_time = calendar.timegm(
231-
event_time.timetuple()) * 1e6 + event_time.microsecond
232-
annotations.append({'timestamp': int(round(epoch_time)),
212+
event_timestamp_mus = timestamp_to_microseconds(time_event.timestamp)
213+
annotations.append({'timestamp': int(round(event_timestamp_mus)),
233214
'value': annotation.description})
234215

235216
return annotations

opencensus/trace/utils.py

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

15+
import calendar
16+
import datetime
17+
1518
UTF8 = 'utf-8'
1619

1720
# Max length is 128 bytes for a truncatable string.
1821
MAX_LENGTH = 128
1922

23+
ISO_DATETIME_REGEX = '%Y-%m-%dT%H:%M:%S.%fZ'
24+
2025

2126
def _get_truncatable_str(str_to_convert):
2227
"""Truncate a string if exceed limit and record the truncated bytes
@@ -56,3 +61,14 @@ def check_str_length(str_to_check, limit=MAX_LENGTH):
5661
result = str(str_bytes.decode(UTF8, errors='ignore'))
5762

5863
return (result, truncated_byte_count)
64+
65+
66+
def timestamp_to_microseconds(timestamp):
67+
"""Convert a timestamp string into a microseconds value
68+
:param timestamp
69+
:return time in microseconds
70+
"""
71+
timestamp_str = datetime.datetime.strptime(timestamp, ISO_DATETIME_REGEX)
72+
epoch_time_secs = calendar.timegm(timestamp_str.timetuple())
73+
epoch_time_mus = epoch_time_secs * 1e6 + timestamp_str.microsecond
74+
return epoch_time_mus

0 commit comments

Comments
 (0)