Skip to content

Commit 553bdfc

Browse files
authored
Add support for span collection limit via env vars (open-telemetry#1377)
1 parent e6c2f44 commit 553bdfc

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

exporter/opentelemetry-exporter-jaeger/tests/test_jaeger_exporter.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ def setUp(self):
4141
self._test_span = trace._Span("test_span", context=context)
4242
self._test_span.start()
4343
self._test_span.end()
44+
# pylint: disable=protected-access
45+
Configuration._reset()
46+
47+
def tearDown(self):
48+
# pylint: disable=protected-access
49+
Configuration._reset()
4450

4551
def test_constructor_default(self):
4652
"""Test the default values assigned by constructor."""
@@ -142,8 +148,6 @@ def test_constructor_by_environment_variables(self):
142148

143149
environ_patcher.stop()
144150

145-
Configuration._reset()
146-
147151
def test_nsec_to_usec_round(self):
148152
# pylint: disable=protected-access
149153
nsec_to_usec_round = jaeger_exporter._nsec_to_usec_round

opentelemetry-sdk/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
([#1440](https://github.com/open-telemetry/opentelemetry-python/pull/1440))
99
- Add `fields` to propagators
1010
([#1374](https://github.com/open-telemetry/opentelemetry-python/pull/1374))
11+
- Add support for OTEL_SPAN_{ATTRIBUTE_COUNT_LIMIT,EVENT_COUNT_LIMIT,LINK_COUNT_LIMIT}
12+
([#1377](https://github.com/open-telemetry/opentelemetry-python/pull/1377))
1113

1214
## Version 0.16b0
1315

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
from opentelemetry import context as context_api
3939
from opentelemetry import trace as trace_api
40+
from opentelemetry.configuration import Configuration
4041
from opentelemetry.sdk import util
4142
from opentelemetry.sdk.resources import Resource
4243
from opentelemetry.sdk.trace import sampling
@@ -49,9 +50,11 @@
4950

5051
logger = logging.getLogger(__name__)
5152

52-
MAX_NUM_ATTRIBUTES = 1000
53-
MAX_NUM_EVENTS = 1000
54-
MAX_NUM_LINKS = 1000
53+
SPAN_ATTRIBUTE_COUNT_LIMIT = Configuration().get(
54+
"SPAN_ATTRIBUTE_COUNT_LIMIT", 1000
55+
)
56+
SPAN_EVENT_COUNT_LIMIT = Configuration().get("SPAN_EVENT_COUNT_LIMIT", 1000)
57+
SPAN_LINK_COUNT_LIMIT = Configuration().get("SPAN_LINK_COUNT_LIMIT", 1000)
5558
VALID_ATTR_VALUE_TYPES = (bool, str, int, float)
5659

5760

@@ -446,7 +449,7 @@ def __init__(
446449
self.attributes = self._new_attributes()
447450
else:
448451
self.attributes = BoundedDict.from_map(
449-
MAX_NUM_ATTRIBUTES, attributes
452+
SPAN_ATTRIBUTE_COUNT_LIMIT, attributes
450453
)
451454

452455
self.events = self._new_events()
@@ -462,7 +465,7 @@ def __init__(
462465
if links is None:
463466
self.links = self._new_links()
464467
else:
465-
self.links = BoundedList.from_seq(MAX_NUM_LINKS, links)
468+
self.links = BoundedList.from_seq(SPAN_LINK_COUNT_LIMIT, links)
466469

467470
self._end_time = None # type: Optional[int]
468471
self._start_time = None # type: Optional[int]
@@ -483,15 +486,15 @@ def __repr__(self):
483486

484487
@staticmethod
485488
def _new_attributes():
486-
return BoundedDict(MAX_NUM_ATTRIBUTES)
489+
return BoundedDict(SPAN_ATTRIBUTE_COUNT_LIMIT)
487490

488491
@staticmethod
489492
def _new_events():
490-
return BoundedList(MAX_NUM_EVENTS)
493+
return BoundedList(SPAN_EVENT_COUNT_LIMIT)
491494

492495
@staticmethod
493496
def _new_links():
494-
return BoundedList(MAX_NUM_LINKS)
497+
return BoundedList(SPAN_LINK_COUNT_LIMIT)
495498

496499
@staticmethod
497500
def _format_context(context):

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
import shutil
1717
import subprocess
1818
import unittest
19+
from importlib import reload
1920
from logging import ERROR, WARNING
2021
from typing import Optional
2122
from unittest import mock
2223

24+
import pytest
25+
2326
from opentelemetry import trace as trace_api
27+
from opentelemetry.configuration import Configuration
2428
from opentelemetry.context import Context
2529
from opentelemetry.sdk import resources, trace
2630
from opentelemetry.sdk.trace import Resource, sampling
@@ -1182,3 +1186,49 @@ def test_attributes_to_json(self):
11821186
+ date_str
11831187
+ '", "attributes": {"key2": "value2"}}], "links": [], "resource": {}}',
11841188
)
1189+
1190+
1191+
class TestSpanLimits(unittest.TestCase):
1192+
def setUp(self):
1193+
# reset global state of configuration object
1194+
# pylint: disable=protected-access
1195+
Configuration._reset()
1196+
1197+
def tearDown(self):
1198+
# reset global state of configuration object
1199+
# pylint: disable=protected-access
1200+
Configuration._reset()
1201+
1202+
@mock.patch.dict(
1203+
"os.environ",
1204+
{
1205+
"OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT": "10",
1206+
"OTEL_SPAN_EVENT_COUNT_LIMIT": "20",
1207+
"OTEL_SPAN_LINK_COUNT_LIMIT": "30",
1208+
},
1209+
)
1210+
def test_span_environment_limits(self):
1211+
reload(trace)
1212+
tracer = new_tracer()
1213+
ids_generator = trace_api.RandomIdsGenerator()
1214+
some_links = [
1215+
trace_api.Link(
1216+
trace_api.SpanContext(
1217+
trace_id=ids_generator.generate_trace_id(),
1218+
span_id=ids_generator.generate_span_id(),
1219+
is_remote=False,
1220+
)
1221+
)
1222+
for _ in range(100)
1223+
]
1224+
with pytest.raises(ValueError):
1225+
with tracer.start_as_current_span("root", links=some_links):
1226+
pass
1227+
1228+
with tracer.start_as_current_span("root") as root:
1229+
for idx in range(100):
1230+
root.set_attribute("my_attribute_{}".format(idx), 0)
1231+
root.add_event("my_event_{}".format(idx))
1232+
1233+
self.assertEqual(len(root.attributes), 10)
1234+
self.assertEqual(len(root.events), 20)

0 commit comments

Comments
 (0)