Skip to content

Commit a6324d8

Browse files
committed
Remove _time_ns
1 parent aa0ecdc commit a6324d8

File tree

13 files changed

+46
-81
lines changed

13 files changed

+46
-81
lines changed

opentelemetry-api/src/opentelemetry/util/_time.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import os
2121
import threading
2222
import traceback
23+
from time import time_ns
2324
from typing import Any, Callable, Optional, Tuple, Union, cast
2425

2526
from opentelemetry.sdk._logs.severity import SeverityNumber, std_to_otlp
@@ -37,7 +38,6 @@
3738
)
3839
from opentelemetry.trace.span import TraceFlags
3940
from opentelemetry.util._providers import _load_provider
40-
from opentelemetry.util._time import _time_ns
4141
from opentelemetry.util.types import Attributes
4242

4343
_logger = logging.getLogger(__name__)
@@ -185,9 +185,9 @@ def force_flush(self, timeout_millis: int = 30000) -> bool:
185185
True if all the log processors flushes the logs within timeout,
186186
False otherwise.
187187
"""
188-
deadline_ns = _time_ns() + timeout_millis * 1000000
188+
deadline_ns = time_ns() + timeout_millis * 1000000
189189
for lp in self._log_processors:
190-
current_ts = _time_ns()
190+
current_ts = time_ns()
191191
if current_ts >= deadline_ns:
192192
return False
193193

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
import sys
2121
import threading
2222
from os import linesep
23+
from time import time_ns
2324
from typing import IO, Callable, Deque, List, Optional, Sequence
2425

2526
from opentelemetry.context import attach, detach, set_value
2627
from opentelemetry.sdk._logs import LogData, LogProcessor, LogRecord
2728
from opentelemetry.util._once import Once
28-
from opentelemetry.util._time import _time_ns
2929

3030
_logger = logging.getLogger(__name__)
3131

@@ -205,9 +205,9 @@ def worker(self):
205205
if self._shutdown:
206206
break
207207

208-
start_ns = _time_ns()
208+
start_ns = time_ns()
209209
self._export(flush_request)
210-
end_ns = _time_ns()
210+
end_ns = time_ns()
211211
# subtract the duration of this export call to the next timeout
212212
timeout = self._schedule_delay_millis / 1e3 - (
213213
(end_ns - start_ns) / 1e9

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from atexit import register, unregister
1616
from logging import getLogger
1717
from threading import Lock
18+
from time import time_ns
1819
from typing import Optional, Sequence
1920

2021
# This kind of import is needed to avoid Sphinx errors.
@@ -49,7 +50,6 @@
4950
from opentelemetry.sdk.resources import Resource
5051
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
5152
from opentelemetry.util._once import Once
52-
from opentelemetry.util._time import _time_ns
5353

5454
_logger = getLogger(__name__)
5555

@@ -376,12 +376,12 @@ def __init__(
376376
)
377377

378378
def force_flush(self, timeout_millis: float = 10_000) -> bool:
379-
deadline_ns = _time_ns() + timeout_millis * 10**6
379+
deadline_ns = time_ns() + timeout_millis * 10**6
380380

381381
metric_reader_error = {}
382382

383383
for metric_reader in self._sdk_config.metric_readers:
384-
current_ts = _time_ns()
384+
current_ts = time_ns()
385385
try:
386386
if current_ts >= deadline_ns:
387387
raise MetricsTimeoutError(
@@ -413,7 +413,7 @@ def force_flush(self, timeout_millis: float = 10_000) -> bool:
413413
return True
414414

415415
def shutdown(self, timeout_millis: float = 30_000):
416-
deadline_ns = _time_ns() + timeout_millis * 10**6
416+
deadline_ns = time_ns() + timeout_millis * 10**6
417417

418418
def _shutdown():
419419
self._shutdown = True
@@ -427,7 +427,7 @@ def _shutdown():
427427
metric_reader_error = {}
428428

429429
for metric_reader in self._sdk_config.metric_readers:
430-
current_ts = _time_ns()
430+
current_ts = time_ns()
431431
try:
432432
if current_ts >= deadline_ns:
433433
raise Exception(

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/_view_instrument_match.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from logging import getLogger
1717
from threading import Lock
18+
from time import time_ns
1819
from typing import Dict, Iterable
1920

2021
from opentelemetry.metrics import Instrument
@@ -28,7 +29,6 @@
2829
from opentelemetry.sdk.metrics._internal.measurement import Measurement
2930
from opentelemetry.sdk.metrics._internal.point import DataPointT
3031
from opentelemetry.sdk.metrics._internal.view import View
31-
from opentelemetry.util._time import _time_ns
3232

3333
_logger = getLogger(__name__)
3434

@@ -40,7 +40,7 @@ def __init__(
4040
instrument: Instrument,
4141
instrument_class_aggregation: Dict[type, Aggregation],
4242
):
43-
self._start_time_unix_nano = _time_ns()
43+
self._start_time_unix_nano = time_ns()
4444
self._view = view
4545
self._instrument = instrument
4646
self._attributes_aggregation: Dict[frozenset, _Aggregation] = {}

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from os import environ, linesep
2020
from sys import stdout
2121
from threading import Event, Lock, RLock, Thread
22+
from time import time_ns
2223
from typing import IO, Callable, Dict, Iterable, Optional
2324

2425
from typing_extensions import final
@@ -51,7 +52,6 @@
5152
)
5253
from opentelemetry.sdk.metrics._internal.point import MetricsData
5354
from opentelemetry.util._once import Once
54-
from opentelemetry.util._time import _time_ns
5555

5656
_logger = getLogger(__name__)
5757

@@ -497,7 +497,7 @@ def _receive_metrics(
497497
detach(token)
498498

499499
def shutdown(self, timeout_millis: float = 30_000, **kwargs) -> None:
500-
deadline_ns = _time_ns() + timeout_millis * 10**6
500+
deadline_ns = time_ns() + timeout_millis * 10**6
501501

502502
def _shutdown():
503503
self._shutdown = True
@@ -508,8 +508,8 @@ def _shutdown():
508508
return
509509

510510
self._shutdown_event.set()
511-
self._daemon_thread.join(timeout=(deadline_ns - _time_ns()) / 10**9)
512-
self._exporter.shutdown(timeout=(deadline_ns - _time_ns()) / 10**6)
511+
self._daemon_thread.join(timeout=(deadline_ns - time_ns()) / 10**9)
512+
self._exporter.shutdown(timeout=(deadline_ns - time_ns()) / 10**6)
513513

514514
def force_flush(self, timeout_millis: float = 10_000) -> bool:
515515
super().force_flush(timeout_millis=timeout_millis)

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/measurement_consumer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from abc import ABC, abstractmethod
1818
from threading import Lock
19+
from time import time_ns
1920
from typing import Iterable, List, Mapping
2021

2122
# This kind of import is needed to avoid Sphinx errors.
@@ -29,7 +30,6 @@
2930
MetricReaderStorage,
3031
)
3132
from opentelemetry.sdk.metrics._internal.point import Metric
32-
from opentelemetry.util._time import _time_ns
3333

3434

3535
class MeasurementConsumer(ABC):
@@ -100,13 +100,13 @@ def collect(
100100
metric_reader_storage = self._reader_storages[metric_reader]
101101
# for now, just use the defaults
102102
callback_options = CallbackOptions()
103-
deadline_ns = _time_ns() + timeout_millis * 10**6
103+
deadline_ns = time_ns() + timeout_millis * 10**6
104104

105105
default_timeout_millis = 10000 * 10**6
106106

107107
for async_instrument in self._async_instruments:
108108

109-
remaining_time = deadline_ns - _time_ns()
109+
remaining_time = deadline_ns - time_ns()
110110

111111
if remaining_time < default_timeout_millis:
112112

@@ -115,7 +115,7 @@ def collect(
115115
)
116116

117117
measurements = async_instrument.callback(callback_options)
118-
if _time_ns() >= deadline_ns:
118+
if time_ns() >= deadline_ns:
119119
raise MetricsTimeoutError(
120120
"Timed out while executing callback"
121121
)

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/metric_reader_storage.py

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

1515
from logging import getLogger
1616
from threading import RLock
17+
from time import time_ns
1718
from typing import Dict, List
1819

1920
from opentelemetry.metrics import (
@@ -49,7 +50,6 @@
4950
)
5051
from opentelemetry.sdk.metrics._internal.view import View
5152
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
52-
from opentelemetry.util._time import _time_ns
5353

5454
_logger = getLogger(__name__)
5555

@@ -130,7 +130,7 @@ def collect(self) -> MetricsData:
130130
# streams produced by the SDK, but we still align the output timestamps
131131
# for a single instrument.
132132

133-
collection_start_nanos = _time_ns()
133+
collection_start_nanos = time_ns()
134134

135135
with self._lock:
136136

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from collections import OrderedDict
2525
from contextlib import contextmanager
2626
from os import environ
27+
from time import time_ns
2728
from types import MappingProxyType, TracebackType
2829
from typing import (
2930
Any,
@@ -64,7 +65,6 @@
6465
from opentelemetry.trace import SpanContext
6566
from opentelemetry.trace.status import Status, StatusCode
6667
from opentelemetry.util import types
67-
from opentelemetry.util._time import _time_ns
6868

6969
logger = logging.getLogger(__name__)
7070

@@ -184,9 +184,9 @@ def force_flush(self, timeout_millis: int = 30000) -> bool:
184184
True if all span processors flushed their spans within the
185185
given timeout, False otherwise.
186186
"""
187-
deadline_ns = _time_ns() + timeout_millis * 1000000
187+
deadline_ns = time_ns() + timeout_millis * 1000000
188188
for sp in self._span_processors:
189-
current_time_ns = _time_ns()
189+
current_time_ns = time_ns()
190190
if current_time_ns >= deadline_ns:
191191
return False
192192

@@ -286,7 +286,7 @@ class EventBase(abc.ABC):
286286
def __init__(self, name: str, timestamp: Optional[int] = None) -> None:
287287
self._name = name
288288
if timestamp is None:
289-
self._timestamp = _time_ns()
289+
self._timestamp = time_ns()
290290
else:
291291
self._timestamp = timestamp
292292

@@ -864,7 +864,7 @@ def start(
864864
logger.warning("Calling start() on a started span.")
865865
return
866866
self._start_time = (
867-
start_time if start_time is not None else _time_ns()
867+
start_time if start_time is not None else time_ns()
868868
)
869869

870870
self._span_processor.on_start(self, parent_context=parent_context)
@@ -877,7 +877,7 @@ def end(self, end_time: Optional[int] = None) -> None:
877877
logger.warning("Calling end() on an ended span.")
878878
return
879879

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

882882
self._span_processor.on_end(self._readable_span())
883883

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import typing
2121
from enum import Enum
2222
from os import environ, linesep
23+
from time import time_ns
2324
from typing import Optional
2425

2526
from opentelemetry.context import (
@@ -37,7 +38,6 @@
3738
)
3839
from opentelemetry.sdk.trace import ReadableSpan, Span, SpanProcessor
3940
from opentelemetry.util._once import Once
40-
from opentelemetry.util._time import _time_ns
4141

4242
logger = logging.getLogger(__name__)
4343

@@ -273,9 +273,9 @@ def worker(self):
273273
break
274274

275275
# subtract the duration of this export call to the next timeout
276-
start = _time_ns()
276+
start = time_ns()
277277
self._export(flush_request)
278-
end = _time_ns()
278+
end = time_ns()
279279
duration = (end - start) / 1e9
280280
timeout = self.schedule_delay_millis / 1e3 - duration
281281

0 commit comments

Comments
 (0)