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

Make Instances of SpanContext Immutable #1134

Merged
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
2 changes: 2 additions & 0 deletions opentelemetry-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
([#1153](https://github.com/open-telemetry/opentelemetry-python/pull/1153))
- Update baggage propagation header
([#1194](https://github.com/open-telemetry/opentelemetry-python/pull/1194))
- Make instances of SpanContext immutable
([#1134](https://github.com/open-telemetry/opentelemetry-python/pull/1134))

## Version 0.13b0

Expand Down
61 changes: 49 additions & 12 deletions opentelemetry-api/src/opentelemetry/trace/span.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import abc
import logging
import types as python_types
import typing

from opentelemetry.trace.status import Status
from opentelemetry.util import types

_logger = logging.getLogger(__name__)


class Span(abc.ABC):
"""A span represents a single operation within a trace."""
Expand Down Expand Up @@ -143,7 +146,9 @@ def get_default(cls) -> "TraceState":
DEFAULT_TRACE_STATE = TraceState.get_default()


class SpanContext:
class SpanContext(
typing.Tuple[int, int, bool, "TraceFlags", "TraceState", bool]
):
"""The state of a Span to propagate between processes.

This class includes the immutable attributes of a :class:`.Span` that must
Expand All @@ -157,26 +162,58 @@ class SpanContext:
is_remote: True if propagated from a remote parent.
"""

def __init__(
self,
def __new__(
cls,
trace_id: int,
span_id: int,
is_remote: bool,
trace_flags: "TraceFlags" = DEFAULT_TRACE_OPTIONS,
trace_state: "TraceState" = DEFAULT_TRACE_STATE,
) -> None:
) -> "SpanContext":
if trace_flags is None:
trace_flags = DEFAULT_TRACE_OPTIONS
if trace_state is None:
trace_state = DEFAULT_TRACE_STATE
self.trace_id = trace_id
self.span_id = span_id
self.trace_flags = trace_flags
self.trace_state = trace_state
self.is_remote = is_remote
self.is_valid = (
self.trace_id != INVALID_TRACE_ID
and self.span_id != INVALID_SPAN_ID

is_valid = trace_id != INVALID_TRACE_ID and span_id != INVALID_SPAN_ID

return tuple.__new__(

Choose a reason for hiding this comment

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

Since we have to override setattr anyways is there an advantage of using tuple vs normal fields set with super.setattr? Latter might be a bit simpler.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, if I understand correctly, I think the advantage of inheriting from a tuple is that it doesn't allow something like object.__setattr__(context, "trace_id", 2) (while not inheriting does). I'm not sure if that's something we want to avoid or if we should just keep it simple.

Copy link
Member Author

@JasonXZLiu JasonXZLiu Sep 22, 2020

Choose a reason for hiding this comment

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

@anuraaga Hmm, it actually seems a lot cleaner and I dont think the case I mentioned above is something that needs to be avoided. But, because mypy doesn't actually run the code, it's not able to determine that SpanContext actually has the attributes, so I get the following errors:

opentelemetry-api/src/opentelemetry/trace/span.py:200: error: "SpanContext" has no attribute "trace_id"
opentelemetry-api/src/opentelemetry/trace/span.py:200: error: Expression has type "Any"
opentelemetry-api/src/opentelemetry/trace/span.py:201: error: "SpanContext" has no attribute "span_id"
opentelemetry-api/src/opentelemetry/trace/span.py:201: error: Expression has type "Any"
opentelemetry-api/src/opentelemetry/trace/span.py:202: error: "SpanContext" has no attribute "trace_state"
opentelemetry-api/src/opentelemetry/trace/span.py:202: error: Expression has type "Any"
opentelemetry-api/src/opentelemetry/trace/span.py:203: error: "SpanContext" has no attribute "is_remote"
opentelemetry-api/src/opentelemetry/trace/span.py:203: error: Expression has type "Any"

I haven't found a good way around this yet (as only Python 3.6+ supports annotating variables with types).

Choose a reason for hiding this comment

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

I see - in that case no worries :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, if I understand correctly, I think the advantage of inheriting from a tuple is that it doesn't allow something like object.setattr(context, "trace_id", 2) (while not inheriting does). I'm not sure if that's something we want to avoid or if we should just keep it simple.

Do we want to make it impossible to use __setattr__? This should be immutable to prevent users from the mistake of using SpanContext to additional information around. I think making that impossible to do through the public API serves that purpose and self-documents the class. I don't think we need to make it literally impossible to modify the object. Given this is Python, I think anyone included to do it will find a way. We should just make sure we inform the person that they shouldn't do it. If they want to go out of their way and still modify, I think we should probably allow that.

I'm personally fine with using tuple for this but if it is causing too much trouble with tooling and needs a lot of other machinery, perhaps we could use a simpler way that protect modifications through public API but doesn't actually try to make it impossible for users to modify the object.

Copy link
Member Author

Choose a reason for hiding this comment

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

@owais @anuraaga Hmm, I think I solved the tooling issues now (the ignores should be inline). There seems to be a false-positive for pylint in determining if SpanContext was instantiated as a tuple (here: https://github.com/open-telemetry/opentelemetry-python/pull/1134/files#diff-36ec0e77c55d7f4a4caef71eda0b3191R187). The issue for testing if we could change the attributes was also fixed inline (here: https://github.com/open-telemetry/opentelemetry-python/pull/1134/files#diff-0924a59286313e337f50e00112a27df8R46-R51).

I'm not sure if it's worth it to go through with the change as it seems like Python doesn't support immutability very well, but all the tooling changes that I made globally were removed.

cls,
(trace_id, span_id, is_remote, trace_flags, trace_state, is_valid),
)

@property
def trace_id(self) -> int:
return self[0] # pylint: disable=unsubscriptable-object

@property
def span_id(self) -> int:
return self[1] # pylint: disable=unsubscriptable-object

@property
def is_remote(self) -> bool:
return self[2] # pylint: disable=unsubscriptable-object

@property
def trace_flags(self) -> "TraceFlags":
return self[3] # pylint: disable=unsubscriptable-object

@property
def trace_state(self) -> "TraceState":
return self[4] # pylint: disable=unsubscriptable-object

@property
def is_valid(self) -> bool:
return self[5] # pylint: disable=unsubscriptable-object

def __setattr__(self, *args: str) -> None:
_logger.debug(
"Immutable type, ignoring call to set attribute", stack_info=True
)

def __delattr__(self, *args: str) -> None:
_logger.debug(
"Immutable type, ignoring call to set attribute", stack_info=True
)

def __repr__(self) -> str:
Expand Down
58 changes: 58 additions & 0 deletions opentelemetry-api/tests/trace/test_immutablespancontext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright The 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 unittest

from opentelemetry import trace
from opentelemetry.trace import TraceFlags, TraceState


class TestImmutableSpanContext(unittest.TestCase):
def test_ctor(self):
context = trace.SpanContext(
1,
1,
is_remote=False,
trace_flags=trace.DEFAULT_TRACE_OPTIONS,
trace_state=trace.DEFAULT_TRACE_STATE,
)

self.assertEqual(context.trace_id, 1)
self.assertEqual(context.span_id, 1)
self.assertEqual(context.is_remote, False)
self.assertEqual(context.trace_flags, trace.DEFAULT_TRACE_OPTIONS)
self.assertEqual(context.trace_state, trace.DEFAULT_TRACE_STATE)

def test_attempt_change_attributes(self):
context = trace.SpanContext(
1,
2,
is_remote=False,
trace_flags=trace.DEFAULT_TRACE_OPTIONS,
trace_state=trace.DEFAULT_TRACE_STATE,
)

# attempt to change the attribute values
context.trace_id = 2 # type: ignore
context.span_id = 3 # type: ignore
context.is_remote = True # type: ignore
context.trace_flags = TraceFlags(3) # type: ignore
context.trace_state = TraceState([("test", "test")]) # type: ignore

# check if attributes changed
self.assertEqual(context.trace_id, 1)
self.assertEqual(context.span_id, 2)
self.assertEqual(context.is_remote, False)
self.assertEqual(context.trace_flags, trace.DEFAULT_TRACE_OPTIONS)
self.assertEqual(context.trace_state, trace.DEFAULT_TRACE_STATE)