Skip to content

Commit 371061a

Browse files
authored
decrement span_counter when span is compressed (#1377)
* decrement span_counter when span is compressed this ensures that compressed spans are not counted against transaction_max_spans alternative for #1376 * update changelog
1 parent 9f524bf commit 371061a

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ endif::[]
3939
===== Bug fixes
4040
4141
* Fix some context fields and metadata handling in AWS Lambda support {pull}1368[#1368]
42+
* Fix an issue where compressed spans would count against `transaction_max_spans` {pull}1377[#1377]
4243
4344
[[release-notes-6.6.0]]
4445
==== 6.6.0 - 2021-10-18

elasticapm/traces.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ def _begin_span(
274274
elif tracer.config.transaction_max_spans and self._span_counter > tracer.config.transaction_max_spans - 1:
275275
self.dropped_spans += 1
276276
span = DroppedSpan(parent_span, context=context)
277-
self._span_counter += 1
278277
else:
279278
span = Span(
280279
transaction=self,
@@ -375,7 +374,7 @@ def to_dict(self) -> dict:
375374
"timestamp": int(self.timestamp * 1000000), # microseconds
376375
"outcome": self.outcome,
377376
"sampled": self.is_sampled,
378-
"span_count": {"started": self._span_counter - self.dropped_spans, "dropped": self.dropped_spans},
377+
"span_count": {"started": self._span_counter, "dropped": self.dropped_spans},
379378
}
380379
if self._dropped_span_statistics:
381380
result["dropped_spans_stats"] = [
@@ -639,6 +638,7 @@ def try_to_compress(self, sibling: SpanType) -> bool:
639638
self.composite["count"] += 1
640639
self.composite["sum"] += sibling.duration
641640
self.duration = sibling.ended_time - self.start_time
641+
self.transaction._span_counter -= 1
642642
return True
643643

644644
def _try_to_compress_composite(self, sibling: SpanType) -> Optional[str]:

tests/client/span_compression_tests.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import pytest
3131

3232
import elasticapm
33-
from elasticapm.conf.constants import SPAN
33+
from elasticapm.conf.constants import SPAN, TRANSACTION
3434

3535

3636
@pytest.mark.parametrize(
@@ -220,3 +220,37 @@ def test_buffer_is_reported_if_next_child_ineligible(elasticapm_client):
220220
elasticapm_client.end_transaction("test")
221221
spans = elasticapm_client.events[SPAN]
222222
assert len(spans) == 3
223+
224+
225+
@pytest.mark.parametrize(
226+
"elasticapm_client",
227+
[{"span_compression_same_kind_max_duration": "5ms", "span_compression_exact_match_max_duration": "5ms"}],
228+
indirect=True,
229+
)
230+
def test_compressed_spans_not_counted(elasticapm_client):
231+
elasticapm_client.begin_transaction("test")
232+
with elasticapm.capture_span(
233+
"test1",
234+
span_type="a",
235+
span_subtype="b",
236+
span_action="c",
237+
leaf=True,
238+
duration=2,
239+
extra={"destination": {"service": {"resource": "x"}}},
240+
) as span1:
241+
pass
242+
with elasticapm.capture_span(
243+
"test2",
244+
span_type="a",
245+
span_subtype="b",
246+
span_action="c",
247+
leaf=True,
248+
duration=3,
249+
extra={"destination": {"service": {"resource": "x"}}},
250+
) as span2:
251+
pass
252+
elasticapm_client.end_transaction("test")
253+
transaction = elasticapm_client.events[TRANSACTION][0]
254+
spans = elasticapm_client.events[SPAN]
255+
assert len(spans) == transaction["span_count"]["started"] == 1
256+
assert transaction["span_count"]["dropped"] == 0

0 commit comments

Comments
 (0)