Skip to content

Commit 6dc96dd

Browse files
authored
merge common code between capture_span and async_capture_span (elastic#1417)
this should avoid issues with changes only being implemented in one of the two context managers
1 parent 1677f7b commit 6dc96dd

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

elasticapm/contrib/asyncio/traces.py

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030

3131
import functools
32+
from types import TracebackType
33+
from typing import Optional, Type
3234

3335
from elasticapm.conf.constants import LABEL_RE
34-
from elasticapm.traces import DroppedSpan, capture_span, error_logger, execution_context
36+
from elasticapm.traces import SpanType, capture_span, execution_context
3537
from elasticapm.utils import get_name_from_func
3638

3739

@@ -46,34 +48,13 @@ async def decorated(*args, **kwds):
4648

4749
return decorated
4850

49-
async def __aenter__(self):
50-
transaction = execution_context.get_transaction()
51-
if transaction and transaction.is_sampled:
52-
return transaction.begin_span(
53-
self.name,
54-
self.type,
55-
context=self.extra,
56-
leaf=self.leaf,
57-
labels=self.labels,
58-
span_subtype=self.subtype,
59-
span_action=self.action,
60-
sync=False,
61-
start=self.start,
62-
)
51+
async def __aenter__(self) -> Optional[SpanType]:
52+
return self.handle_enter(False)
6353

64-
async def __aexit__(self, exc_type, exc_val, exc_tb):
65-
transaction = execution_context.get_transaction()
66-
if transaction and transaction.is_sampled:
67-
try:
68-
span = transaction.end_span(self.skip_frames)
69-
if exc_val and not isinstance(span, DroppedSpan):
70-
try:
71-
exc_val._elastic_apm_span_id = span.id
72-
except AttributeError:
73-
# could happen if the exception has __slots__
74-
pass
75-
except LookupError:
76-
error_logger.debug("ended non-existing span %s of type %s", self.name, self.type)
54+
async def __aexit__(
55+
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
56+
):
57+
self.handle_exit(exc_type, exc_val, exc_tb)
7758

7859

7960
async def set_context(data, key="custom"):

elasticapm/traces.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
import time
3636
import timeit
3737
from collections import defaultdict
38-
from typing import Any, Callable, Dict, Optional, Tuple, Union
38+
from types import TracebackType
39+
from typing import Any, Callable, Dict, Optional, Tuple, Type, Union
3940

4041
from elasticapm.conf import constants
4142
from elasticapm.conf.constants import LABEL_RE, SPAN, TRANSACTION
@@ -867,7 +868,15 @@ def decorated(*args, **kwds):
867868

868869
return decorated
869870

870-
def __enter__(self) -> Union[Span, DroppedSpan, None]:
871+
def __enter__(self) -> Optional[SpanType]:
872+
return self.handle_enter(self.sync)
873+
874+
def __exit__(
875+
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
876+
) -> None:
877+
self.handle_exit(exc_type, exc_val, exc_tb)
878+
879+
def handle_enter(self, sync: bool) -> Optional[SpanType]:
871880
transaction = execution_context.get_transaction()
872881
if transaction and transaction.is_sampled:
873882
return transaction.begin_span(
@@ -879,11 +888,13 @@ def __enter__(self) -> Union[Span, DroppedSpan, None]:
879888
span_subtype=self.subtype,
880889
span_action=self.action,
881890
start=self.start,
882-
sync=self.sync,
891+
sync=sync,
883892
)
884893
return None
885894

886-
def __exit__(self, exc_type, exc_val, exc_tb):
895+
def handle_exit(
896+
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
897+
) -> None:
887898
transaction = execution_context.get_transaction()
888899

889900
if transaction and transaction.is_sampled:

0 commit comments

Comments
 (0)