Skip to content

Commit

Permalink
Move util.time_ns to API. (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oberon00 authored and reyang committed Oct 7, 2019
1 parent 0d9b7bd commit c94a576
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
12 changes: 12 additions & 0 deletions opentelemetry-api/src/opentelemetry/util/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import time

# Since we want API users to be able to provide timestamps,
# this needs to be in the API.

try:
time_ns = time.time_ns
# Python versions < 3.7
except AttributeError:

def time_ns() -> int:
return int(time.time() * 1e9)
10 changes: 4 additions & 6 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from opentelemetry.context import Context
from opentelemetry.sdk import util
from opentelemetry.sdk.util import BoundedDict, BoundedList
from opentelemetry.util import types
from opentelemetry.util import time_ns, types

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -205,7 +205,7 @@ def add_event(
) -> None:
if attributes is None:
attributes = Span.empty_attributes
self.add_lazy_event(trace_api.Event(name, util.time_ns(), attributes))
self.add_lazy_event(trace_api.Event(name, time_ns(), attributes))

def add_lazy_event(self, event: trace_api.Event) -> None:
with self._lock:
Expand Down Expand Up @@ -249,7 +249,7 @@ def start(self, start_time: int = None):
has_started = self.start_time is not None
if not has_started:
self.start_time = (
start_time if start_time is not None else util.time_ns()
start_time if start_time is not None else time_ns()
)
if has_started:
logger.warning("Calling start() on a started span.")
Expand All @@ -264,9 +264,7 @@ def end(self, end_time: int = None):
raise RuntimeError("Calling end() on a not started span.")
has_ended = self.end_time is not None
if not has_ended:
self.end_time = (
end_time if end_time is not None else util.time_ns()
)
self.end_time = end_time if end_time is not None else time_ns()
if has_ended:
logger.warning("Calling end() on an ended span.")
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from enum import Enum

from opentelemetry.context import Context
from opentelemetry.sdk import util
from opentelemetry.util import time_ns

from .. import Span, SpanProcessor

Expand Down Expand Up @@ -163,9 +163,9 @@ def worker(self):
break

# substract the duration of this export call to the next timeout
start = util.time_ns()
start = time_ns()
self.export()
end = util.time_ns()
end = time_ns()
duration = (end - start) / 1e9
timeout = self.schedule_delay_millis / 1e3 - duration

Expand Down
9 changes: 0 additions & 9 deletions opentelemetry-sdk/src/opentelemetry/sdk/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import datetime
import threading
import time
from collections import OrderedDict, deque

try:
Expand All @@ -26,14 +25,6 @@
from collections import MutableMapping
from collections import Sequence

try:
time_ns = time.time_ns
# Python versions < 3.7
except AttributeError:

def time_ns():
return int(time.time() * 1e9)


def ns_to_iso_str(nanoseconds):
"""Get an ISO 8601 string from time_ns value."""
Expand Down
5 changes: 3 additions & 2 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
from unittest import mock

from opentelemetry import trace as trace_api
from opentelemetry.sdk import trace, util
from opentelemetry.sdk import trace
from opentelemetry.util import time_ns


class TestTracer(unittest.TestCase):
Expand Down Expand Up @@ -174,7 +175,7 @@ def test_span_members(self):
# events
root.add_event("event0")
root.add_event("event1", {"name": "birthday"})
now = util.time_ns()
now = time_ns()
root.add_lazy_event(
trace_api.Event("event2", now, {"name": "hello"})
)
Expand Down

0 comments on commit c94a576

Please sign in to comment.