Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some "errors" found by mypy. #204

Merged
merged 3 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions opentelemetry-api/src/opentelemetry/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,14 @@ async def main():
asyncio.run(main())
"""

import typing

from .base_context import BaseRuntimeContext

__all__ = ["Context"]


Context = None # type: typing.Optional[BaseRuntimeContext]

try:
from .async_context import AsyncRuntimeContext

Context = AsyncRuntimeContext()
Context = AsyncRuntimeContext() # type: BaseRuntimeContext
except ImportError:
from .thread_local_context import ThreadLocalRuntimeContext

Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class SpanKind(enum.Enum):
class Span:
"""A span represents a single operation within a trace."""

def start(self, start_time: int = None) -> None:
def start(self, start_time: typing.Optional[int] = None) -> None:
"""Sets the current time as the span's start time.

Each span represents a single operation. The span's start time is the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class B3Format(HTTPTextFormat):
def extract(cls, get_from_carrier, carrier):
trace_id = format_trace_id(trace.INVALID_TRACE_ID)
span_id = format_span_id(trace.INVALID_SPAN_ID)
sampled = 0
sampled = "0"
flags = None

single_header = _extract_first_element(
Expand Down Expand Up @@ -95,8 +95,8 @@ def extract(cls, get_from_carrier, carrier):
# trace an span ids are encoded in hex, so must be converted
trace_id=int(trace_id, 16),
span_id=int(span_id, 16),
trace_options=options,
trace_state={},
trace_options=trace.TraceOptions(options),
trace_state=trace.TraceState(),
)

@classmethod
Expand All @@ -111,17 +111,20 @@ def inject(cls, context, set_in_carrier, carrier):
set_in_carrier(carrier, cls.SAMPLED_KEY, "1" if sampled else "0")


def format_trace_id(trace_id: int):
def format_trace_id(trace_id: int) -> str:
"""Format the trace id according to b3 specification."""
return format(trace_id, "032x")


def format_span_id(span_id: int):
def format_span_id(span_id: int) -> str:
"""Format the span id according to b3 specification."""
return format(span_id, "016x")


def _extract_first_element(list_object: list) -> typing.Optional[object]:
if list_object:
return list_object[0]
return None
_T = typing.TypeVar("_T")


def _extract_first_element(items: typing.Iterable[_T]) -> typing.Optional[_T]:
if items is None:
return None
return next(iter(items), None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From #201 (comment).

There's a small runtime behavior change here, it throws if items is null.

Oberon00 marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 4 additions & 3 deletions opentelemetry-sdk/src/opentelemetry/sdk/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ def __init__(
enabled: bool,
monotonic: bool,
):
self.data = 0
self.data = value_type()
self.value_type = value_type
self.enabled = enabled
self.monotonic = monotonic

def _validate_update(self, value: metrics_api.ValueT):
def _validate_update(self, value: metrics_api.ValueT) -> bool:
if not self.enabled:
return False
if not isinstance(value, self.value_type):
Expand Down Expand Up @@ -232,7 +232,8 @@ def create_metric(
monotonic: bool = False,
) -> metrics_api.MetricT:
"""See `opentelemetry.metrics.Meter.create_metric`."""
return metric_type(
# Ignore type b/c of mypy bug in addition to missing annotations
return metric_type( # type: ignore
name,
description,
unit,
Expand Down
47 changes: 25 additions & 22 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ class MultiSpanProcessor(SpanProcessor):
def __init__(self):
# use a tuple to avoid race conditions when adding a new span and
# iterating through it on "on_start" and "on_end".
self._span_processors = ()
self._span_processors = () # type: typing.Tuple[SpanProcessor, ...]
self._lock = threading.Lock()

def add_span_processor(self, span_processor: SpanProcessor):
def add_span_processor(self, span_processor: SpanProcessor) -> None:
"""Adds a SpanProcessor to the list handled by this instance."""
with self._lock:
self._span_processors = self._span_processors + (span_processor,)
Expand Down Expand Up @@ -122,11 +122,11 @@ class Span(trace_api.Span):
def __init__(
self,
name: str,
context: "trace_api.SpanContext",
context: trace_api.SpanContext,
parent: trace_api.ParentSpan = None,
sampler=None, # TODO
trace_config=None, # TODO
resource=None, # TODO
sampler: None = None, # TODO
trace_config: None = None, # TODO
resource: None = None, # TODO
attributes: types.Attributes = None, # TODO
events: typing.Sequence[trace_api.Event] = None, # TODO
links: typing.Sequence[trace_api.Link] = None, # TODO
Expand All @@ -140,9 +140,6 @@ def __init__(
self.sampler = sampler
self.trace_config = trace_config
self.resource = resource
self.attributes = attributes
self.events = events
self.links = links
self.kind = kind

self.span_processor = span_processor
Expand All @@ -165,8 +162,8 @@ def __init__(
else:
self.links = BoundedList.from_seq(MAX_NUM_LINKS, links)

self.end_time = None
self.start_time = None
self.end_time = None # type: typing.Optional[int]
self.start_time = None # type: typing.Optional[int]

def __repr__(self):
return '{}(name="{}", context={})'.format(
Expand Down Expand Up @@ -203,9 +200,13 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
def add_event(
self, name: str, attributes: types.Attributes = None
) -> None:
if attributes is None:
attributes = Span.empty_attributes
self.add_lazy_event(trace_api.Event(name, time_ns(), attributes))
self.add_lazy_event(
trace_api.Event(
name,
time_ns(),
Span.empty_attributes if attributes is None else attributes,
)
)

def add_lazy_event(self, event: trace_api.Event) -> None:
with self._lock:
Expand All @@ -226,7 +227,9 @@ def add_link(
attributes: types.Attributes = None,
) -> None:
if attributes is None:
attributes = Span.empty_attributes
attributes = (
Span.empty_attributes
) # TODO: empty_attributes is not a Dict. Use Mapping?
self.add_lazy_link(trace_api.Link(link_target_context, attributes))

def add_lazy_link(self, link: "trace_api.Link") -> None:
Expand All @@ -242,7 +245,7 @@ def add_lazy_link(self, link: "trace_api.Link") -> None:
return
self.links.append(link)

def start(self, start_time: int = None):
def start(self, start_time: typing.Optional[int] = None) -> None:
with self._lock:
if not self.is_recording_events():
return
Expand All @@ -256,7 +259,7 @@ def start(self, start_time: int = None):
return
self.span_processor.on_start(self)

def end(self, end_time: int = None):
def end(self, end_time: int = None) -> None:
with self._lock:
if not self.is_recording_events():
return
Expand All @@ -283,7 +286,7 @@ def is_recording_events(self) -> bool:
return True


def generate_span_id():
def generate_span_id() -> int:
"""Get a new random span ID.

Returns:
Expand All @@ -292,7 +295,7 @@ def generate_span_id():
return random.getrandbits(64)


def generate_trace_id():
def generate_trace_id() -> int:
"""Get a new random trace ID.

Returns:
Expand Down Expand Up @@ -325,7 +328,7 @@ def start_span(
name: str,
parent: trace_api.ParentSpan = trace_api.Tracer.CURRENT_SPAN,
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
) -> typing.Iterator["Span"]:
) -> typing.Iterator[trace_api.Span]:
"""See `opentelemetry.trace.Tracer.start_span`."""

span = self.create_span(name, parent, kind)
Expand Down Expand Up @@ -368,8 +371,8 @@ def create_span(

@contextmanager
def use_span(
self, span: Span, end_on_exit: bool = False
) -> typing.Iterator[Span]:
self, span: trace_api.Span, end_on_exit: bool = False
) -> typing.Iterator[trace_api.Span]:
"""See `opentelemetry.trace.Tracer.use_span`."""
try:
span_snapshot = self._current_span_slot.get()
Expand Down
13 changes: 9 additions & 4 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ def __init__(
)

self.span_exporter = span_exporter
self.queue = collections.deque([], max_queue_size)
self.queue = collections.deque(
[], max_queue_size
) # type: typing.Deque[Span]
self.worker_thread = threading.Thread(target=self.worker, daemon=True)
self.condition = threading.Condition(threading.Lock())
self.schedule_delay_millis = schedule_delay_millis
Expand All @@ -128,7 +130,9 @@ def __init__(
# flag that indicates that spans are being dropped
self._spans_dropped = False
# precallocated list to send spans to exporter
self.spans_list = [None] * self.max_export_batch_size
self.spans_list = [
None
] * self.max_export_batch_size # type: typing.List[typing.Optional[Span]]
self.worker_thread.start()

def on_start(self, span: Span) -> None:
Expand Down Expand Up @@ -172,7 +176,7 @@ def worker(self):
# be sure that all spans are sent
self._flush()

def export(self) -> bool:
def export(self) -> None:
"""Exports at most max_export_batch_size spans."""
idx = 0

Expand All @@ -184,7 +188,8 @@ def export(self) -> bool:
suppress_instrumentation = Context.suppress_instrumentation
try:
Context.suppress_instrumentation = True
self.span_exporter.export(self.spans_list[:idx])
# Ignore type b/c the Optional[None]+slicing is too "clever" for mypy
self.span_exporter.export(self.spans_list[:idx]) # type: ignore
# pylint: disable=broad-except
except Exception:
logger.exception("Exception while exporting data.")
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-sdk/src/opentelemetry/sdk/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class BoundedList(Sequence):

def __init__(self, maxlen):
self.dropped = 0
self._dq = deque(maxlen=maxlen)
self._dq = deque(maxlen=maxlen) # type: deque
self._lock = threading.Lock()

def __repr__(self):
Expand Down Expand Up @@ -97,8 +97,8 @@ def __init__(self, maxlen):
raise ValueError
self.maxlen = maxlen
self.dropped = 0
self._dict = OrderedDict()
self._lock = threading.Lock()
self._dict = OrderedDict() # type: OrderedDict
self._lock = threading.Lock() # type: threading.Lock

def __repr__(self):
return "{}({}, maxlen={})".format(
Expand Down