Skip to content

Commit effa3dd

Browse files
authored
opentelemetry-sdk: remove copy on span instantiation (#5272)
* opentelemetry-sdk: remove copy on span instantiation This copy isn't necessary as the init method of the BoundedAttributes (inside the span's init method) already iterates through and copies all the attributes in a new Dict. Ran some benchmarks before and after the change with: ``` pytest opentelemetry-sdk/benchmarks/trace/test_benchmark_trace.py::test_start_span_with_attributes \ --memray --trace-python-allocators --benchmark-disable -v ``` The following table shows the before and after change savings: | attrs | Before (KiB) | After (KiB) | Saved (KiB) | |------:|-------------:|------------:|------------:| | 0 | 7.5 | 7.3 | 0.2 | | 1 | 8.3 | 8.1 | 0.2 | | 10 | 11.3 | 11.3 | 0.0 | | 50 | 15.8 | 14.3 | 1.5 | | 128 | 28.0 | 24.8 | 3.2 | Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> * add changelog Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --------- Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
1 parent ed622c3 commit effa3dd

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

.changelog/5272.changed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
`opentelemetry-sdk`: remove unnecessary `copy` in Span creation

opentelemetry-sdk/benchmarks/trace/test_benchmark_trace.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ def benchmark_start_span():
4545
benchmark(benchmark_start_span)
4646

4747

48+
@pytest.mark.parametrize("num_attrs", [0, 1, 10, 50, 128])
49+
def test_start_span_with_attributes(benchmark, num_attrs):
50+
attrs = {f"key{i}": f"value{i}" for i in range(num_attrs)}
51+
52+
def benchmark_start_span():
53+
span = tracer.start_span("benchmarkedSpan", attributes=attrs)
54+
span.end()
55+
56+
benchmark(benchmark_start_span)
57+
58+
4859
# pylint: disable=protected-access,redefined-outer-name
4960
def test_simple_start_span_with_tracer_configurator_rules(
5061
benchmark, num_tracer_configurator_rules

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ def start_span( # pylint: disable=too-many-locals
12561256
parent=parent_span_context,
12571257
sampler=self.sampler,
12581258
resource=self.resource,
1259-
attributes=sampling_result.attributes.copy(),
1259+
attributes=sampling_result.attributes,
12601260
span_processor=self.span_processor,
12611261
kind=kind,
12621262
links=links,

0 commit comments

Comments
 (0)