Skip to content

Commit b22cb50

Browse files
committed
check if span is a dropped span before closing it or setting tags on it (#687)
fixes #683
1 parent ae14201 commit b22cb50

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

elasticapm/contrib/opentracing/span.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def __init__(self, tracer, context, elastic_apm_ref):
5454
super(OTSpan, self).__init__(tracer, context)
5555
self.elastic_apm_ref = elastic_apm_ref
5656
self.is_transaction = isinstance(elastic_apm_ref, traces.Transaction)
57+
self.is_dropped = isinstance(elastic_apm_ref, traces.DroppedSpan)
5758
if not context.span:
5859
context.span = self
5960

@@ -101,7 +102,7 @@ def set_tag(self, key, value):
101102
traces.set_context({"framework": {"name": value}}, "service")
102103
else:
103104
self.elastic_apm_ref.label(**{key: value})
104-
else:
105+
elif not self.is_dropped:
105106
if key.startswith("db."):
106107
span_context = self.elastic_apm_ref.context or {}
107108
if "db" not in span_context:
@@ -125,7 +126,7 @@ def set_tag(self, key, value):
125126
def finish(self, finish_time=None):
126127
if self.is_transaction:
127128
self.tracer._agent.end_transaction()
128-
else:
129+
elif not self.is_dropped:
129130
self.elastic_apm_ref.transaction.end_span()
130131

131132

tests/contrib/opentracing/tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ def test_span_tags(tracer):
166166
assert span2["context"]["tags"] == {"something_else": "bar"}
167167

168168

169+
@pytest.mark.parametrize("elasticapm_client", [{"transaction_max_spans": 1}], indirect=True)
170+
def test_dropped_spans(tracer):
171+
assert tracer._agent.config.transaction_max_spans == 1
172+
with tracer.start_active_span("transaction") as ot_scope_t:
173+
with tracer.start_active_span("span") as ot_scope_s:
174+
s = ot_scope_s.span
175+
s.set_tag("db.type", "sql")
176+
with tracer.start_active_span("span") as ot_scope_s:
177+
s = ot_scope_s.span
178+
s.set_tag("db.type", "sql")
179+
client = tracer._agent
180+
spans = client.events[constants.SPAN]
181+
assert len(spans) == 1
182+
183+
169184
def test_error_log(tracer):
170185
with tracer.start_active_span("transaction") as tx_scope:
171186
try:

0 commit comments

Comments
 (0)