From c623d739c3e76f97584230a654cfb37b70745cf4 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Wed, 3 Jun 2026 16:04:03 -0700 Subject: [PATCH 1/2] 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> --- .../benchmarks/trace/test_benchmark_trace.py | 11 +++++++++++ .../src/opentelemetry/sdk/trace/__init__.py | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/opentelemetry-sdk/benchmarks/trace/test_benchmark_trace.py b/opentelemetry-sdk/benchmarks/trace/test_benchmark_trace.py index 3ab23e2c90a..74f1ff90849 100644 --- a/opentelemetry-sdk/benchmarks/trace/test_benchmark_trace.py +++ b/opentelemetry-sdk/benchmarks/trace/test_benchmark_trace.py @@ -45,6 +45,17 @@ def benchmark_start_span(): benchmark(benchmark_start_span) +@pytest.mark.parametrize("num_attrs", [0, 1, 10, 50, 128]) +def test_start_span_with_attributes(benchmark, num_attrs): + attrs = {f"key{i}": f"value{i}" for i in range(num_attrs)} + + def benchmark_start_span(): + span = tracer.start_span("benchmarkedSpan", attributes=attrs) + span.end() + + benchmark(benchmark_start_span) + + # pylint: disable=protected-access,redefined-outer-name def test_simple_start_span_with_tracer_configurator_rules( benchmark, num_tracer_configurator_rules diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 3817408690e..624edef184f 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -1256,7 +1256,7 @@ def start_span( # pylint: disable=too-many-locals parent=parent_span_context, sampler=self.sampler, resource=self.resource, - attributes=sampling_result.attributes.copy(), + attributes=sampling_result.attributes, span_processor=self.span_processor, kind=kind, links=links, From d26f458315e8800348fed3677a2f03488232de0e Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Wed, 3 Jun 2026 16:11:09 -0700 Subject: [PATCH 2/2] add changelog Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- .changelog/5272.changed | 1 + 1 file changed, 1 insertion(+) create mode 100644 .changelog/5272.changed diff --git a/.changelog/5272.changed b/.changelog/5272.changed new file mode 100644 index 00000000000..ab31f57b42a --- /dev/null +++ b/.changelog/5272.changed @@ -0,0 +1 @@ +`opentelemetry-sdk`: remove unnecessary `copy` in Span creation \ No newline at end of file