Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c94a576

Browse files
Oberon00reyang
authored andcommitted
Move util.time_ns to API. (open-telemetry#205)
1 parent 0d9b7bd commit c94a576

File tree

5 files changed

+22
-20
lines changed

5 files changed

+22
-20
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import time
2+
3+
# Since we want API users to be able to provide timestamps,
4+
# this needs to be in the API.
5+
6+
try:
7+
time_ns = time.time_ns
8+
# Python versions < 3.7
9+
except AttributeError:
10+
11+
def time_ns() -> int:
12+
return int(time.time() * 1e9)

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from opentelemetry.context import Context
2424
from opentelemetry.sdk import util
2525
from opentelemetry.sdk.util import BoundedDict, BoundedList
26-
from opentelemetry.util import types
26+
from opentelemetry.util import time_ns, types
2727

2828
logger = logging.getLogger(__name__)
2929

@@ -205,7 +205,7 @@ def add_event(
205205
) -> None:
206206
if attributes is None:
207207
attributes = Span.empty_attributes
208-
self.add_lazy_event(trace_api.Event(name, util.time_ns(), attributes))
208+
self.add_lazy_event(trace_api.Event(name, time_ns(), attributes))
209209

210210
def add_lazy_event(self, event: trace_api.Event) -> None:
211211
with self._lock:
@@ -249,7 +249,7 @@ def start(self, start_time: int = None):
249249
has_started = self.start_time is not None
250250
if not has_started:
251251
self.start_time = (
252-
start_time if start_time is not None else util.time_ns()
252+
start_time if start_time is not None else time_ns()
253253
)
254254
if has_started:
255255
logger.warning("Calling start() on a started span.")
@@ -264,9 +264,7 @@ def end(self, end_time: int = None):
264264
raise RuntimeError("Calling end() on a not started span.")
265265
has_ended = self.end_time is not None
266266
if not has_ended:
267-
self.end_time = (
268-
end_time if end_time is not None else util.time_ns()
269-
)
267+
self.end_time = end_time if end_time is not None else time_ns()
270268
if has_ended:
271269
logger.warning("Calling end() on an ended span.")
272270
return

opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from enum import Enum
2020

2121
from opentelemetry.context import Context
22-
from opentelemetry.sdk import util
22+
from opentelemetry.util import time_ns
2323

2424
from .. import Span, SpanProcessor
2525

@@ -163,9 +163,9 @@ def worker(self):
163163
break
164164

165165
# substract the duration of this export call to the next timeout
166-
start = util.time_ns()
166+
start = time_ns()
167167
self.export()
168-
end = util.time_ns()
168+
end = time_ns()
169169
duration = (end - start) / 1e9
170170
timeout = self.schedule_delay_millis / 1e3 - duration
171171

opentelemetry-sdk/src/opentelemetry/sdk/util.py

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

1515
import datetime
1616
import threading
17-
import time
1817
from collections import OrderedDict, deque
1918

2019
try:
@@ -26,14 +25,6 @@
2625
from collections import MutableMapping
2726
from collections import Sequence
2827

29-
try:
30-
time_ns = time.time_ns
31-
# Python versions < 3.7
32-
except AttributeError:
33-
34-
def time_ns():
35-
return int(time.time() * 1e9)
36-
3728

3829
def ns_to_iso_str(nanoseconds):
3930
"""Get an ISO 8601 string from time_ns value."""

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
from unittest import mock
1717

1818
from opentelemetry import trace as trace_api
19-
from opentelemetry.sdk import trace, util
19+
from opentelemetry.sdk import trace
20+
from opentelemetry.util import time_ns
2021

2122

2223
class TestTracer(unittest.TestCase):
@@ -174,7 +175,7 @@ def test_span_members(self):
174175
# events
175176
root.add_event("event0")
176177
root.add_event("event1", {"name": "birthday"})
177-
now = util.time_ns()
178+
now = time_ns()
178179
root.add_lazy_event(
179180
trace_api.Event("event2", now, {"name": "hello"})
180181
)

0 commit comments

Comments
 (0)