Skip to content

Commit

Permalink
Span creation in tracer SDK (open-telemetry#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
c24t authored Aug 6, 2019
1 parent 3460432 commit 42415ba
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 46 deletions.
1 change: 0 additions & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[settings]
force_single_line=True
from_first=True
from_first=True
3 changes: 2 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ disable=missing-docstring,
fixme, # Warns about FIXME, TODO, etc. comments.
too-few-public-methods, # Might be good to re-enable this later.
too-many-instance-attributes,
too-many-arguments
too-many-arguments,
ungrouped-imports # Leave this up to isort

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-api/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import os

import setuptools

BASE_DIR = os.path.dirname(__file__)
Expand Down
48 changes: 37 additions & 11 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@

from opentelemetry import loader

# TODO: quarantine
ParentSpan = typing.Optional[typing.Union['Span', 'SpanContext']]


class Span:
"""A span represents a single operation within a trace."""
Expand Down Expand Up @@ -119,7 +122,7 @@ def get_default(cls) -> 'TraceOptions':
return cls(cls.DEFAULT)


DEFAULT_TRACEOPTIONS = TraceOptions.get_default()
DEFAULT_TRACE_OPTIONS = TraceOptions.get_default()


class TraceState(typing.Dict[str, str]):
Expand All @@ -138,7 +141,15 @@ def get_default(cls) -> 'TraceState':
return cls()


DEFAULT_TRACESTATE = TraceState.get_default()
DEFAULT_TRACE_STATE = TraceState.get_default()


def format_trace_id(trace_id: int) -> str:
return '0x{:032x}'.format(trace_id)


def format_span_id(span_id: int) -> str:
return '0x{:016x}'.format(span_id)


class SpanContext:
Expand All @@ -157,12 +168,25 @@ class SpanContext:
def __init__(self,
trace_id: int,
span_id: int,
options: 'TraceOptions',
state: 'TraceState') -> None:
trace_options: 'TraceOptions' = None,
trace_state: 'TraceState' = None
) -> None:
if trace_options is None:
trace_options = DEFAULT_TRACE_OPTIONS
if trace_state is None:
trace_state = DEFAULT_TRACE_STATE
self.trace_id = trace_id
self.span_id = span_id
self.options = options
self.state = state
self.trace_options = trace_options
self.trace_state = trace_state

def __repr__(self) -> str:
return ("{}(trace_id={}, span_id={})"
.format(
type(self).__name__,
format_trace_id(self.trace_id),
format_span_id(self.span_id)
))

def is_valid(self) -> bool:
"""Get whether this `SpanContext` is valid.
Expand All @@ -173,6 +197,8 @@ def is_valid(self) -> bool:
Returns:
True if the `SpanContext` is valid, false otherwise.
"""
return (self.trace_id != INVALID_TRACE_ID and
self.span_id != INVALID_SPAN_ID)


class DefaultSpan(Span):
Expand All @@ -187,10 +213,10 @@ def get_context(self) -> 'SpanContext':
return self._context


INVALID_SPAN_ID = 0
INVALID_TRACE_ID = 0
INVALID_SPAN_ID = 0x0000000000000000
INVALID_TRACE_ID = 0x00000000000000000000000000000000
INVALID_SPAN_CONTEXT = SpanContext(INVALID_TRACE_ID, INVALID_SPAN_ID,
DEFAULT_TRACEOPTIONS, DEFAULT_TRACESTATE)
DEFAULT_TRACE_OPTIONS, DEFAULT_TRACE_STATE)
INVALID_SPAN = DefaultSpan(INVALID_SPAN_CONTEXT)


Expand Down Expand Up @@ -219,7 +245,7 @@ def get_current_span(self) -> 'Span':
@contextmanager # type: ignore
def start_span(self,
name: str,
parent: typing.Union['Span', 'SpanContext'] = CURRENT_SPAN
parent: ParentSpan = CURRENT_SPAN
) -> typing.Iterator['Span']:
"""Context manager for span creation.
Expand Down Expand Up @@ -266,7 +292,7 @@ def start_span(self,

def create_span(self,
name: str,
parent: typing.Union['Span', 'SpanContext'] = CURRENT_SPAN
parent: ParentSpan = CURRENT_SPAN
) -> 'Span':
"""Creates a span.
Expand Down
19 changes: 19 additions & 0 deletions opentelemetry-api/src/opentelemetry/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2019, OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import typing

AttributeValue = typing.Union[str, bool, float]
Attributes = typing.Dict[str, AttributeValue]
4 changes: 2 additions & 2 deletions opentelemetry-api/tests/trace/test_defaultspan.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
class TestDefaultSpan(unittest.TestCase):
def test_ctor(self):
context = trace.SpanContext(1, 1,
trace.DEFAULT_TRACEOPTIONS,
trace.DEFAULT_TRACESTATE)
trace.DEFAULT_TRACE_OPTIONS,
trace.DEFAULT_TRACE_STATE)
span = trace.DefaultSpan(context)
self.assertEqual(context, span.get_context())

Expand Down
1 change: 1 addition & 0 deletions opentelemetry-sdk/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import os

import setuptools

BASE_DIR = os.path.dirname(__file__)
Expand Down
Loading

0 comments on commit 42415ba

Please sign in to comment.