22# (c) Copyright Instana Inc. 2019
33
44
5- import time
6- from contextlib import contextmanager
7- from typing import Any , Callable , Dict , Iterator , Tuple
5+ try :
6+ import time
7+ from contextlib import contextmanager
8+ from typing import TYPE_CHECKING , Any , Callable , Dict , Iterator , Tuple
89
9- import wrapt
10- from opentelemetry .trace import use_span
11- from opentelemetry .trace .status import StatusCode
10+ import wrapt
11+ from opentelemetry .trace import use_span
12+ from opentelemetry .trace .status import StatusCode
1213
13- from instana .configurator import config
14- from instana .log import logger
15- from instana .span .span import InstanaSpan
16- from instana .util .traceutils import get_tracer_tuple , tracing_is_off
14+ from instana .configurator import config
15+ from instana .log import logger
16+ from instana .span .span import InstanaSpan
17+ from instana .util .traceutils import get_tracer_tuple
1718
18- try :
19+ if TYPE_CHECKING :
20+ from instana .tracer import InstanaTracer
1921 import asyncio
2022
2123 @wrapt .patch_function_wrapper ("asyncio" , "ensure_future" )
@@ -25,13 +27,11 @@ def ensure_future_with_instana(
2527 argv : Tuple [object , Tuple [object , ...]],
2628 kwargs : Dict [str , Any ],
2729 ) -> object :
28- if (
29- not config ["asyncio_task_context_propagation" ]["enabled" ]
30- or tracing_is_off ()
31- ):
30+ tracer , parent_span , _ = get_tracer_tuple ()
31+ if not config ["asyncio_task_context_propagation" ]["enabled" ] or not tracer :
3232 return wrapped (* argv , ** kwargs )
3333
34- with _start_as_current_async_span () as span :
34+ with _start_as_current_async_span (tracer , parent_span ) as span :
3535 try :
3636 span .set_status (StatusCode .OK )
3737 return wrapped (* argv , ** kwargs )
@@ -47,26 +47,26 @@ def create_task_with_instana(
4747 argv : Tuple [object , Tuple [object , ...]],
4848 kwargs : Dict [str , Any ],
4949 ) -> object :
50- if (
51- not config ["asyncio_task_context_propagation" ]["enabled" ]
52- or tracing_is_off ()
53- ):
50+ tracer , parent_span , _ = get_tracer_tuple ()
51+ if not config ["asyncio_task_context_propagation" ]["enabled" ] or not tracer :
5452 return wrapped (* argv , ** kwargs )
5553
56- with _start_as_current_async_span () as span :
54+ with _start_as_current_async_span (tracer , parent_span ) as span :
5755 try :
5856 span .set_status (StatusCode .OK )
5957 return wrapped (* argv , ** kwargs )
6058 except Exception as exc :
6159 logger .debug (f"asyncio create_task_with_instana error: { exc } " )
6260
6361 @contextmanager
64- def _start_as_current_async_span () -> Iterator [InstanaSpan ]:
62+ def _start_as_current_async_span (
63+ tracer : "InstanaTracer" ,
64+ parent_span : "InstanaSpan" ,
65+ ) -> Iterator [InstanaSpan ]:
6566 """
6667 Creates and yield a special InstanaSpan to only propagate the Asyncio
6768 context.
6869 """
69- tracer , parent_span , _ = get_tracer_tuple ()
7070 parent_context = parent_span .get_span_context () if parent_span else None
7171
7272 _time = time .time_ns ()
0 commit comments