|
14 | 14 |
|
15 | 15 | """Export the spans data to Jaeger.""" |
16 | 16 |
|
17 | | -import calendar |
18 | | -import datetime |
19 | 17 | import logging |
20 | 18 | import socket |
21 | 19 |
|
|
26 | 24 | from opencensus.trace.exporters import base |
27 | 25 | from opencensus.trace.exporters.gen.jaeger import agent, jaeger |
28 | 26 | from opencensus.trace.exporters.transports import sync |
| 27 | +from opencensus.trace.utils import timestamp_to_microseconds |
29 | 28 |
|
30 | 29 | DEFAULT_HOST_NAME = 'localhost' |
31 | 30 | DEFAULT_AGENT_PORT = 6831 |
32 | 31 | DEFAULT_ENDPOINT = '/api/traces?format=jaeger.thrift' |
33 | 32 |
|
34 | | -ISO_DATETIME_REGEX = '%Y-%m-%dT%H:%M:%S.%fZ' |
35 | 33 | UDP_PACKET_MAX_LENGTH = 65000 |
36 | 34 |
|
37 | 35 | logging = logging.getLogger(__name__) |
@@ -176,19 +174,9 @@ def translate_to_jaeger(self, span_datas): |
176 | 174 | jaeger_spans = [] |
177 | 175 |
|
178 | 176 | 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 |
192 | 180 |
|
193 | 181 | tags = _extract_tags(span.attributes) |
194 | 182 |
|
@@ -220,8 +208,8 @@ def translate_to_jaeger(self, span_datas): |
220 | 208 | traceIdLow=_convert_hex_str_to_int(trace_id[16:32]), |
221 | 209 | spanId=_convert_hex_str_to_int(span_id), |
222 | 210 | 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)), |
225 | 213 | tags=tags, |
226 | 214 | logs=logs, |
227 | 215 | references=refs, |
@@ -290,12 +278,9 @@ def _extract_logs_from_span(span): |
290 | 278 | vType=jaeger.TagType.STRING, |
291 | 279 | vStr=annotation.description)) |
292 | 280 |
|
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)) |
299 | 284 | return logs |
300 | 285 |
|
301 | 286 |
|
|
0 commit comments