Skip to content

Commit 5cc7ec5

Browse files
authored
Remove time_ns from API (#1602)
1 parent fb1ae06 commit 5cc7ec5

File tree

7 files changed

+50
-35
lines changed

7 files changed

+50
-35
lines changed

Diff for: .github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
# Otherwise, set variable to the commit of your branch on
1111
# opentelemetry-python-contrib which is compatible with these Core repo
1212
# changes.
13-
CONTRIB_REPO_SHA: 8783e0ff97ad123006ff1ff2c2cf3f52161a406f
13+
CONTRIB_REPO_SHA: 5bc0fa1611502be47a1f4eb550fe255e4b707ba1
1414

1515
jobs:
1616
build:

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4646
([#1673](https://github.com/open-telemetry/opentelemetry-python/pull/1673))
4747
- Update OTLP exporter to use OTLP proto `0.7.0`
4848
([#1674](https://github.com/open-telemetry/opentelemetry-python/pull/1674))
49+
- Remove time_ns from API and add a warning for older versions of Python
50+
([#1602](https://github.com/open-telemetry/opentelemetry-python/pull/1602))
4951

5052
### Removed
5153
- Removed unused `get_hexadecimal_trace_id` and `get_hexadecimal_span_id` methods.

Diff for: opentelemetry-api/src/opentelemetry/util/time.py renamed to opentelemetry-api/src/opentelemetry/util/_time.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,24 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from logging import getLogger
1516
from sys import version_info
1617

1718
if version_info.minor < 7:
19+
getLogger(__name__).warning( # pylint: disable=logging-not-lazy
20+
"You are using Python 3.%s. This version does not support timestamps "
21+
"with nanosecond precision and the Opentelemetry SDK will use "
22+
"millisecond precision instead. Please refer to PEP 546 for more "
23+
"information. Please upgrade to Python 3.7 or newer to use nanosecond "
24+
"precision." % version_info.minor
25+
)
1826
from time import time
1927

20-
def time_ns() -> int:
21-
# FIXME this approach can have precision problems as explained here:
22-
# https://github.com/open-telemetry/opentelemetry-python/issues/1594
28+
def _time_ns():
2329
return int(time() * 1e9)
2430

2531

2632
else:
27-
from time import time_ns # pylint: disable=unused-import
33+
from time import time_ns
34+
35+
_time_ns = time_ns

Diff for: opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
from opentelemetry.trace.propagation import SPAN_KEY
5555
from opentelemetry.trace.status import Status, StatusCode
5656
from opentelemetry.util import types
57-
from opentelemetry.util.time import time_ns
57+
from opentelemetry.util._time import _time_ns
5858

5959
logger = logging.getLogger(__name__)
6060

@@ -171,9 +171,9 @@ def force_flush(self, timeout_millis: int = 30000) -> bool:
171171
True if all span processors flushed their spans within the
172172
given timeout, False otherwise.
173173
"""
174-
deadline_ns = time_ns() + timeout_millis * 1000000
174+
deadline_ns = _time_ns() + timeout_millis * 1000000
175175
for sp in self._span_processors:
176-
current_time_ns = time_ns()
176+
current_time_ns = _time_ns()
177177
if current_time_ns >= deadline_ns:
178178
return False
179179

@@ -273,7 +273,7 @@ class EventBase(abc.ABC):
273273
def __init__(self, name: str, timestamp: Optional[int] = None) -> None:
274274
self._name = name
275275
if timestamp is None:
276-
self._timestamp = time_ns()
276+
self._timestamp = _time_ns()
277277
else:
278278
self._timestamp = timestamp
279279

@@ -708,7 +708,7 @@ def add_event(
708708
Event(
709709
name=name,
710710
attributes=attributes,
711-
timestamp=time_ns() if timestamp is None else timestamp,
711+
timestamp=_time_ns() if timestamp is None else timestamp,
712712
)
713713
)
714714

@@ -738,7 +738,7 @@ def start(
738738
logger.warning("Calling start() on a started span.")
739739
return
740740
self._start_time = (
741-
start_time if start_time is not None else time_ns()
741+
start_time if start_time is not None else _time_ns()
742742
)
743743

744744
self._span_processor.on_start(self, parent_context=parent_context)
@@ -751,7 +751,7 @@ def end(self, end_time: Optional[int] = None) -> None:
751751
logger.warning("Calling end() on an ended span.")
752752
return
753753

754-
self._end_time = end_time if end_time is not None else time_ns()
754+
self._end_time = end_time if end_time is not None else _time_ns()
755755

756756
self._span_processor.on_end(self._readable_span())
757757

Diff for: opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
OTEL_BSP_SCHEDULE_DELAY,
3030
)
3131
from opentelemetry.sdk.trace import ReadableSpan, Span, SpanProcessor
32-
from opentelemetry.util.time import time_ns
32+
from opentelemetry.util._time import _time_ns
3333

3434
logger = logging.getLogger(__name__)
3535

@@ -231,9 +231,9 @@ def worker(self):
231231
break
232232

233233
# subtract the duration of this export call to the next timeout
234-
start = time_ns()
234+
start = _time_ns()
235235
self._export(flush_request)
236-
end = time_ns()
236+
end = _time_ns()
237237
duration = (end - start) / 1e9
238238
timeout = self.schedule_delay_millis / 1e3 - duration
239239

Diff for: opentelemetry-sdk/tests/trace/test_trace.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from opentelemetry.sdk.util import ns_to_iso_str
3939
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
4040
from opentelemetry.trace import StatusCode
41-
from opentelemetry.util.time import time_ns
41+
from opentelemetry.util._time import _time_ns
4242

4343

4444
def new_tracer() -> trace_api.Tracer:
@@ -709,7 +709,7 @@ def test_events(self):
709709
)
710710

711711
# event name, attributes and timestamp
712-
now = time_ns()
712+
now = _time_ns()
713713
root.add_event("event2", {"name": ["birthday"]}, now)
714714

715715
mutable_list = ["original_contents"]

Diff for: shim/opentelemetry-opentracing-shim/tests/test_util.py

+23-18
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,46 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import time
16-
import unittest
15+
from time import time
16+
from unittest import TestCase
1717

18-
from opentelemetry.shim.opentracing_shim import util
19-
from opentelemetry.util.time import time_ns
18+
from opentelemetry.shim.opentracing_shim.util import (
19+
DEFAULT_EVENT_NAME,
20+
event_name_from_kv,
21+
time_seconds_from_ns,
22+
time_seconds_to_ns,
23+
)
24+
from opentelemetry.util._time import _time_ns
2025

2126

22-
class TestUtil(unittest.TestCase):
27+
class TestUtil(TestCase):
2328
def test_event_name_from_kv(self):
2429
# Test basic behavior.
2530
event_name = "send HTTP request"
26-
res = util.event_name_from_kv({"event": event_name, "foo": "bar"})
31+
res = event_name_from_kv({"event": event_name, "foo": "bar"})
2732
self.assertEqual(res, event_name)
2833

2934
# Test None.
30-
res = util.event_name_from_kv(None)
31-
self.assertEqual(res, util.DEFAULT_EVENT_NAME)
35+
res = event_name_from_kv(None)
36+
self.assertEqual(res, DEFAULT_EVENT_NAME)
3237

3338
# Test empty dict.
34-
res = util.event_name_from_kv({})
35-
self.assertEqual(res, util.DEFAULT_EVENT_NAME)
39+
res = event_name_from_kv({})
40+
self.assertEqual(res, DEFAULT_EVENT_NAME)
3641

3742
# Test missing `event` field.
38-
res = util.event_name_from_kv({"foo": "bar"})
39-
self.assertEqual(res, util.DEFAULT_EVENT_NAME)
43+
res = event_name_from_kv({"foo": "bar"})
44+
self.assertEqual(res, DEFAULT_EVENT_NAME)
4045

4146
def test_time_seconds_to_ns(self):
42-
time_seconds = time.time()
43-
result = util.time_seconds_to_ns(time_seconds)
47+
time_seconds = time()
48+
result = time_seconds_to_ns(time_seconds)
4449

4550
self.assertEqual(result, int(time_seconds * 1e9))
4651

4752
def test_time_seconds_from_ns(self):
48-
time_nanoseconds = time_ns()
49-
result = util.time_seconds_from_ns(time_nanoseconds)
53+
time_nanoseconds = _time_ns()
54+
result = time_seconds_from_ns(time_nanoseconds)
5055

5156
self.assertEqual(result, time_nanoseconds / 1e9)
5257

@@ -56,8 +61,8 @@ def test_time_conversion_precision(self):
5661
"""
5762

5863
time_seconds = 1570484241.9501917
59-
time_nanoseconds = util.time_seconds_to_ns(time_seconds)
60-
result = util.time_seconds_from_ns(time_nanoseconds)
64+
time_nanoseconds = time_seconds_to_ns(time_seconds)
65+
result = time_seconds_from_ns(time_nanoseconds)
6166

6267
# Tolerate inaccuracies of less than a microsecond.
6368
# TODO: Put a link to an explanation in the docs.

0 commit comments

Comments
 (0)