-
Notifications
You must be signed in to change notification settings - Fork 657
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
Allow users to "unset" SDK limits and evaluate default limits lazily instead of on import #1839
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,19 +56,16 @@ | |
from opentelemetry.sdk.util import BoundedDict, BoundedList | ||
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo | ||
from opentelemetry.trace import SpanContext | ||
from opentelemetry.trace.propagation import SPAN_KEY | ||
from opentelemetry.trace.status import Status, StatusCode | ||
from opentelemetry.util import types | ||
from opentelemetry.util._time import _time_ns | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
SPAN_ATTRIBUTE_COUNT_LIMIT = int( | ||
environ.get(OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT, 128) | ||
) | ||
_DEFAULT_SPAN_EVENTS_LIMIT = 128 | ||
_DEFAULT_SPAN_LINKS_LIMIT = 128 | ||
_DEFAULT_SPAN_ATTRIBUTES_LIMIT = 128 | ||
|
||
_SPAN_EVENT_COUNT_LIMIT = int(environ.get(OTEL_SPAN_EVENT_COUNT_LIMIT, 128)) | ||
_SPAN_LINK_COUNT_LIMIT = int(environ.get(OTEL_SPAN_LINK_COUNT_LIMIT, 128)) | ||
# pylint: disable=protected-access | ||
_TRACE_SAMPLER = sampling._get_from_env_or_default() | ||
|
||
|
@@ -502,6 +499,87 @@ def _format_links(links): | |
return f_links | ||
|
||
|
||
class _Limits: | ||
"""The limits that should be enforce on recorded data such as events, links, attributes etc. | ||
|
||
This class does not enforce any limits itself. It only provides an a way read limits from env, | ||
default values and in future from user provided arguments. | ||
|
||
All limit must be either a non-negative integer or ``None``. | ||
Setting a limit to ``None`` will not set any limits for that field/type. | ||
|
||
Args: | ||
max_events: Maximum number of events that can be added to a Span. | ||
max_links: Maximum number of links that can be added to a Span. | ||
max_attributes: Maximum number of attributes that can be added to a Span. | ||
""" | ||
|
||
UNSET = -1 | ||
|
||
max_attributes: int | ||
max_events: int | ||
max_links: int | ||
|
||
def __init__( | ||
self, | ||
max_attributes: Optional[int] = None, | ||
max_events: Optional[int] = None, | ||
max_links: Optional[int] = None, | ||
): | ||
self.max_attributes = self._from_env_if_absent( | ||
max_attributes, | ||
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT, | ||
_DEFAULT_SPAN_ATTRIBUTES_LIMIT, | ||
) | ||
self.max_events = self._from_env_if_absent( | ||
max_events, OTEL_SPAN_EVENT_COUNT_LIMIT, _DEFAULT_SPAN_EVENTS_LIMIT | ||
) | ||
self.max_links = self._from_env_if_absent( | ||
max_links, OTEL_SPAN_LINK_COUNT_LIMIT, _DEFAULT_SPAN_LINKS_LIMIT | ||
) | ||
|
||
def __repr__(self): | ||
return "max_attributes={}, max_events={}, max_links={}".format( | ||
self.max_attributes, self.max_events, self.max_links | ||
) | ||
|
||
@classmethod | ||
def _from_env_if_absent( | ||
cls, value: Optional[int], env_var: str, default: Optional[int] | ||
) -> Optional[int]: | ||
if value is cls.UNSET: | ||
return None | ||
|
||
err_msg = "{0} must be a non-negative integer but got {}" | ||
|
||
if value is None: | ||
str_value = environ.get(env_var, "").strip().lower() | ||
if not str_value: | ||
return default | ||
if str_value == "unset": | ||
return None | ||
|
||
try: | ||
value = int(str_value) | ||
except ValueError: | ||
raise ValueError(err_msg.format(env_var, str_value)) | ||
|
||
if value < 0: | ||
raise ValueError(err_msg.format(env_var, value)) | ||
return value | ||
|
||
|
||
_UnsetLimits = _Limits( | ||
max_attributes=_Limits.UNSET, | ||
max_events=_Limits.UNSET, | ||
max_links=_Limits.UNSET, | ||
) | ||
|
||
SPAN_ATTRIBUTE_COUNT_LIMIT = _Limits._from_env_if_absent( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this only here because it was exposed as an API attribute? Can we put a comment here saying that? |
||
None, OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT, _DEFAULT_SPAN_ATTRIBUTES_LIMIT | ||
) | ||
|
||
|
||
class Span(trace_api.Span, ReadableSpan): | ||
"""See `opentelemetry.trace.Span`. | ||
|
||
|
@@ -566,7 +644,7 @@ def __init__( | |
self._attributes = self._new_attributes() | ||
else: | ||
self._attributes = BoundedDict.from_map( | ||
SPAN_ATTRIBUTE_COUNT_LIMIT, attributes | ||
self._limits.max_attributes, attributes | ||
) | ||
|
||
self._events = self._new_events() | ||
|
@@ -582,24 +660,21 @@ def __init__( | |
if links is None: | ||
self._links = self._new_links() | ||
else: | ||
self._links = BoundedList.from_seq(_SPAN_LINK_COUNT_LIMIT, links) | ||
self._links = BoundedList.from_seq(self._limits.max_links, links) | ||
|
||
def __repr__(self): | ||
return '{}(name="{}", context={})'.format( | ||
type(self).__name__, self._name, self._context | ||
) | ||
|
||
@staticmethod | ||
def _new_attributes(): | ||
return BoundedDict(SPAN_ATTRIBUTE_COUNT_LIMIT) | ||
def _new_attributes(self): | ||
return BoundedDict(self._limits.max_attributes) | ||
|
||
@staticmethod | ||
def _new_events(): | ||
return BoundedList(_SPAN_EVENT_COUNT_LIMIT) | ||
def _new_events(self): | ||
return BoundedList(self._limits.max_events) | ||
|
||
@staticmethod | ||
def _new_links(): | ||
return BoundedList(_SPAN_LINK_COUNT_LIMIT) | ||
def _new_links(self): | ||
return BoundedList(self._limits.max_links) | ||
|
||
def get_span_context(self): | ||
return self._context | ||
|
@@ -772,6 +847,10 @@ class _Span(Span): | |
by other mechanisms than through the `Tracer`. | ||
""" | ||
|
||
def __init__(self, *args, limits=_UnsetLimits, **kwargs): | ||
self._limits = limits | ||
super().__init__(*args, **kwargs) | ||
|
||
|
||
class Tracer(trace_api.Tracer): | ||
"""See `opentelemetry.trace.Tracer`.""" | ||
|
@@ -791,6 +870,7 @@ def __init__( | |
self.span_processor = span_processor | ||
self.id_generator = id_generator | ||
self.instrumentation_info = instrumentation_info | ||
self._limits = None | ||
|
||
@contextmanager | ||
def start_as_current_span( | ||
|
@@ -892,6 +972,7 @@ def start_span( # pylint: disable=too-many-locals | |
instrumentation_info=self.instrumentation_info, | ||
record_exception=record_exception, | ||
set_status_on_exception=set_status_on_exception, | ||
limits=self._limits, | ||
) | ||
span.start(start_time=start_time, parent_context=context) | ||
else: | ||
|
@@ -921,6 +1002,7 @@ def __init__( | |
self.id_generator = id_generator | ||
self._resource = resource | ||
self.sampler = sampler | ||
self._limits = _Limits() | ||
self._atexit_handler = None | ||
if shutdown_on_exit: | ||
self._atexit_handler = atexit.register(self.shutdown) | ||
|
@@ -937,7 +1019,7 @@ def get_tracer( | |
if not instrumenting_module_name: # Reject empty strings too. | ||
instrumenting_module_name = "" | ||
logger.error("get_tracer called with missing module name.") | ||
return Tracer( | ||
tracer = Tracer( | ||
self.sampler, | ||
self.resource, | ||
self._active_span_processor, | ||
|
@@ -946,6 +1028,8 @@ def get_tracer( | |
instrumenting_module_name, instrumenting_library_version | ||
), | ||
) | ||
tracer._limits = self._limits | ||
return tracer | ||
|
||
def add_span_processor(self, span_processor: SpanProcessor) -> None: | ||
"""Registers a new :class:`SpanProcessor` for this `TracerProvider`. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be a constant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just checking is there an issue w/ the spec around using
unset
to disable limits?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. There is precedent for using the value "unset" in other places but the limits section does not mention it in any way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can remove it for now so there will be no way to unset from an env var other than setting the limit to an very large value. Once we add support to set limits via code, it'll allow users to truly "unset" the limits.