Skip to content

Commit 1702ae2

Browse files
committed
fix: Adapted instrumentations to the new get_tracer_tuple function
Signed-off-by: Cagri Yonca <cagri@ibm.com>
1 parent 9227ef5 commit 1702ae2

27 files changed

+203
-162
lines changed

src/instana/instrumentation/aio_pika.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from instana.log import logger
1717
from instana.propagators.format import Format
18-
from instana.util.traceutils import get_tracer_tuple, tracing_is_off
18+
from instana.util.traceutils import get_tracer_tuple
1919
from instana.singletons import get_tracer
2020

2121
if TYPE_CHECKING:
@@ -41,10 +41,10 @@ async def publish_with_instana(
4141
args: Tuple[object],
4242
kwargs: Dict[str, Any],
4343
) -> Optional["ConfirmationFrameType"]:
44-
if tracing_is_off():
44+
tracer, parent_span, _ = get_tracer_tuple()
45+
if not tracer:
4546
return await wrapped(*args, **kwargs)
4647

47-
tracer, parent_span, _ = get_tracer_tuple()
4848
parent_context = parent_span.get_span_context() if parent_span else None
4949

5050
def _bind_args(

src/instana/instrumentation/aioamqp.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from opentelemetry.trace.status import StatusCode
99

1010
from instana.log import logger
11-
from instana.util.traceutils import get_tracer_tuple, tracing_is_off
11+
from instana.util.traceutils import get_tracer_tuple
1212

1313
@wrapt.patch_function_wrapper("aioamqp.channel", "Channel.basic_publish")
1414
async def basic_publish_with_instana(
@@ -17,10 +17,10 @@ async def basic_publish_with_instana(
1717
argv: Tuple[object, Tuple[object, ...]],
1818
kwargs: Dict[str, Any],
1919
) -> object:
20-
if tracing_is_off():
20+
tracer, parent_span, _ = get_tracer_tuple()
21+
if not tracer:
2122
return await wrapped(*argv, **kwargs)
2223

23-
tracer, parent_span, _ = get_tracer_tuple()
2424
parent_context = parent_span.get_span_context() if parent_span else None
2525
with tracer.start_as_current_span(
2626
"aioamqp-publisher", span_context=parent_context
@@ -57,11 +57,11 @@ async def basic_consume_with_instana(
5757
argv: Tuple[object, Tuple[object, ...]],
5858
kwargs: Dict[str, Any],
5959
) -> object:
60-
if tracing_is_off():
60+
tracer, parent_span, _ = get_tracer_tuple()
61+
if not tracer:
6162
return await wrapped(*argv, **kwargs)
6263

6364
callback = argv[0]
64-
tracer, parent_span, _ = get_tracer_tuple()
6565
parent_context = parent_span.get_span_context() if parent_span else None
6666

6767
@wrapt.decorator

src/instana/instrumentation/aiohttp/client.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from instana.propagators.format import Format
1313
from instana.singletons import agent
1414
from instana.util.secrets import strip_secrets_from_query
15-
from instana.util.traceutils import get_tracer_tuple, tracing_is_off, extract_custom_headers
15+
from instana.util.traceutils import get_tracer_tuple, extract_custom_headers
1616

1717
try:
1818
import aiohttp
@@ -21,17 +21,16 @@
2121
from aiohttp.client import ClientSession
2222
from instana.span.span import InstanaSpan
2323

24-
2524
async def stan_request_start(
2625
session: "ClientSession", trace_config_ctx: SimpleNamespace, params
2726
) -> Awaitable[None]:
2827
try:
28+
tracer, parent_span, _ = get_tracer_tuple()
2929
# If we're not tracing, just return
30-
if tracing_is_off():
30+
if not tracer:
3131
trace_config_ctx.span_context = None
3232
return
3333

34-
tracer, parent_span, _ = get_tracer_tuple()
3534
parent_context = parent_span.get_span_context() if parent_span else None
3635

3736
span = tracer.start_span("aiohttp-client", span_context=parent_context)

src/instana/instrumentation/asyncio.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import time
66
from contextlib import contextmanager
7-
from typing import Any, Callable, Dict, Iterator, Tuple
7+
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterator, Tuple
88

99
import wrapt
1010
from opentelemetry.trace import use_span
@@ -13,25 +13,26 @@
1313
from instana.configurator import config
1414
from instana.log import logger
1515
from instana.span.span import InstanaSpan
16-
from instana.util.traceutils import get_tracer_tuple, tracing_is_off
16+
from instana.util.traceutils import get_tracer_tuple
1717

1818
try:
1919
import asyncio
2020

21+
if TYPE_CHECKING:
22+
from instana.tracer import InstanaTracer
23+
2124
@wrapt.patch_function_wrapper("asyncio", "ensure_future")
2225
def ensure_future_with_instana(
2326
wrapped: Callable[..., asyncio.ensure_future],
2427
instance: object,
2528
argv: Tuple[object, Tuple[object, ...]],
2629
kwargs: Dict[str, Any],
2730
) -> object:
28-
if (
29-
not config["asyncio_task_context_propagation"]["enabled"]
30-
or tracing_is_off()
31-
):
31+
tracer, parent_span, _ = get_tracer_tuple()
32+
if not config["asyncio_task_context_propagation"]["enabled"] or not tracer:
3233
return wrapped(*argv, **kwargs)
3334

34-
with _start_as_current_async_span() as span:
35+
with _start_as_current_async_span(tracer, parent_span) as span:
3536
try:
3637
span.set_status(StatusCode.OK)
3738
return wrapped(*argv, **kwargs)
@@ -47,26 +48,26 @@ def create_task_with_instana(
4748
argv: Tuple[object, Tuple[object, ...]],
4849
kwargs: Dict[str, Any],
4950
) -> object:
50-
if (
51-
not config["asyncio_task_context_propagation"]["enabled"]
52-
or tracing_is_off()
53-
):
51+
tracer, parent_span, _ = get_tracer_tuple()
52+
if not config["asyncio_task_context_propagation"]["enabled"] or not tracer:
5453
return wrapped(*argv, **kwargs)
5554

56-
with _start_as_current_async_span() as span:
55+
with _start_as_current_async_span(tracer, parent_span) as span:
5756
try:
5857
span.set_status(StatusCode.OK)
5958
return wrapped(*argv, **kwargs)
6059
except Exception as exc:
6160
logger.debug(f"asyncio create_task_with_instana error: {exc}")
6261

6362
@contextmanager
64-
def _start_as_current_async_span() -> Iterator[InstanaSpan]:
63+
def _start_as_current_async_span(
64+
tracer: "InstanaTracer",
65+
parent_span: "InstanaSpan",
66+
) -> Iterator[InstanaSpan]:
6567
"""
6668
Creates and yield a special InstanaSpan to only propagate the Asyncio
6769
context.
6870
"""
69-
tracer, parent_span, _ = get_tracer_tuple()
7071
parent_context = parent_span.get_span_context() if parent_span else None
7172

7273
_time = time.time_ns()

src/instana/instrumentation/aws/boto3.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from botocore.client import BaseClient
1515

1616
from instana.span.span import InstanaSpan
17+
from instana.tracer import InstanaTracer
1718

1819
import json
1920

@@ -22,14 +23,16 @@
2223
from instana.log import logger
2324
from instana.propagators.format import Format
2425
from instana.singletons import get_tracer
25-
from instana.span.span import get_current_span
2626
from instana.util.traceutils import (
2727
extract_custom_headers,
2828
get_tracer_tuple,
29-
tracing_is_off,
3029
)
3130

32-
def lambda_inject_context(payload: Dict[str, Any], span: "InstanaSpan") -> None:
31+
def lambda_inject_context(
32+
tracer: "InstanaTracer",
33+
payload: Dict[str, Any],
34+
span: "InstanaSpan",
35+
) -> None:
3336
"""
3437
When boto3 lambda client 'Invoke' is called, we want to inject the tracing context.
3538
boto3/botocore has specific requirements:
@@ -54,9 +57,9 @@ def emit_add_auth_with_instana(
5457
args: Tuple[object],
5558
kwargs: Dict[str, Any],
5659
) -> Callable[..., None]:
57-
current_span = get_current_span()
58-
if not tracing_is_off() and current_span and current_span.is_recording():
59-
extract_custom_headers(current_span, args[0].headers)
60+
_, parent_span, _ = get_tracer_tuple()
61+
if parent_span:
62+
extract_custom_headers(parent_span, args[0].headers)
6063
return wrapped(*args, **kwargs)
6164

6265
@wrapt.patch_function_wrapper("botocore.client", "BaseClient._make_api_call")
@@ -66,12 +69,11 @@ def make_api_call_with_instana(
6669
args: Sequence[Dict[str, Any]],
6770
kwargs: Dict[str, Any],
6871
) -> Dict[str, Any]:
72+
tracer, parent_span, _ = get_tracer_tuple()
6973
# If we're not tracing, just return
70-
if tracing_is_off():
74+
if not tracer:
7175
return wrapped(*args, **kwargs)
7276

73-
tracer, parent_span, _ = get_tracer_tuple()
74-
7577
parent_context = parent_span.get_span_context() if parent_span else None
7678

7779
if instance.meta.service_model.service_name == "dynamodb":
@@ -101,7 +103,7 @@ def make_api_call_with_instana(
101103

102104
# Inject context when invoking lambdas
103105
if "lambda" in instance._endpoint.host and operation == "Invoke":
104-
lambda_inject_context(payload, span)
106+
lambda_inject_context(tracer, payload, span)
105107

106108
try:
107109
result = wrapped(*args, **kwargs)

src/instana/instrumentation/aws/s3.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from instana.singletons import get_tracer
1616
from instana.util.traceutils import (
1717
get_tracer_tuple,
18-
tracing_is_off,
1918
)
2019

2120
operations = {
@@ -48,12 +47,11 @@ def collect_s3_injected_attributes(
4847
args: Sequence[object],
4948
kwargs: Dict[str, Any],
5049
) -> Callable[..., object]:
50+
tracer, parent_span, _ = get_tracer_tuple()
5151
# If we're not tracing, just return
52-
if tracing_is_off():
52+
if not tracer:
5353
return wrapped(*args, **kwargs)
5454

55-
tracer, parent_span, _ = get_tracer_tuple()
56-
5755
parent_context = parent_span.get_span_context() if parent_span else None
5856

5957
with tracer.start_as_current_span("s3", span_context=parent_context) as span:

src/instana/instrumentation/cassandra.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import wrapt
1515

1616
from instana.log import logger
17-
from instana.util.traceutils import get_tracer_tuple, tracing_is_off
17+
from instana.util.traceutils import get_tracer_tuple
1818

1919
if TYPE_CHECKING:
2020
from cassandra.cluster import ResponseFuture, Session
@@ -73,11 +73,11 @@ def request_init_with_instana(
7373
fn: "ResponseFuture",
7474
) -> None:
7575
tracer, parent_span, _ = get_tracer_tuple()
76-
parent_context = parent_span.get_span_context() if parent_span else None
77-
78-
if tracing_is_off():
76+
if not tracer:
7977
return
8078

79+
parent_context = parent_span.get_span_context() if parent_span else None
80+
8181
attributes = {}
8282
if isinstance(fn.query, cassandra.query.SimpleStatement):
8383
attributes["cassandra.query"] = fn.query.query_string

src/instana/instrumentation/celery.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -145,40 +145,42 @@ def before_task_publish(
145145
) -> None:
146146
try:
147147
tracer, parent_span, _ = get_tracer_tuple()
148+
if not tracer:
149+
return
150+
148151
parent_context = parent_span.get_span_context() if parent_span else None
149152

150-
if tracer:
151-
body = kwargs["body"]
152-
headers = kwargs["headers"]
153-
task_name = kwargs["sender"]
154-
task = registry.tasks.get(task_name)
155-
task_id = _get_task_id(headers, body)
156-
157-
span = tracer.start_span("celery-client", span_context=parent_context)
158-
span.set_attribute("task", task_name)
159-
span.set_attribute("task_id", task_id)
160-
add_broker_attributes(span, task.app.conf["broker_url"])
161-
162-
# Context propagation
163-
context_headers = {}
164-
tracer.inject(
165-
span.context,
166-
Format.HTTP_HEADERS,
167-
context_headers,
168-
disable_w3c_trace_context=True,
169-
)
153+
body = kwargs["body"]
154+
headers = kwargs["headers"]
155+
task_name = kwargs["sender"]
156+
task = registry.tasks.get(task_name)
157+
task_id = _get_task_id(headers, body)
158+
159+
span = tracer.start_span("celery-client", span_context=parent_context)
160+
span.set_attribute("task", task_name)
161+
span.set_attribute("task_id", task_id)
162+
add_broker_attributes(span, task.app.conf["broker_url"])
163+
164+
# Context propagation
165+
context_headers = {}
166+
tracer.inject(
167+
span.context,
168+
Format.HTTP_HEADERS,
169+
context_headers,
170+
disable_w3c_trace_context=True,
171+
)
172+
173+
# Fix for broken header propagation
174+
# https://github.com/celery/celery/issues/4875
175+
task_headers = kwargs.get("headers") or {}
176+
task_headers.setdefault("headers", {})
177+
task_headers["headers"].update(context_headers)
178+
kwargs["headers"] = task_headers
170179

171-
# Fix for broken header propagation
172-
# https://github.com/celery/celery/issues/4875
173-
task_headers = kwargs.get("headers") or {}
174-
task_headers.setdefault("headers", {})
175-
task_headers["headers"].update(context_headers)
176-
kwargs["headers"] = task_headers
177-
178-
ctx = trace.set_span_in_context(span)
179-
token = context.attach(ctx)
180-
client_token["token"] = token
181-
client_span.set(span)
180+
ctx = trace.set_span_in_context(span)
181+
token = context.attach(ctx)
182+
client_token["token"] = token
183+
client_span.set(span)
182184
except Exception:
183185
logger.debug("celery-client before_task_publish: ", exc_info=True)
184186

src/instana/instrumentation/couchbase.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import wrapt
2626

2727
from instana.span.span import InstanaSpan
28-
from instana.util.traceutils import get_tracer_tuple, tracing_is_off
28+
from instana.util.traceutils import get_tracer_tuple
2929

3030
# List of operations to instrument
3131
# incr, incr_multi, decr, decr_multi, retrieve_in are wrappers around operations above
@@ -94,12 +94,12 @@ def wrapper(
9494
kwargs: Dict[str, Any],
9595
) -> object:
9696
tracer, parent_span, _ = get_tracer_tuple()
97-
parent_context = parent_span.get_span_context() if parent_span else None
98-
9997
# If we're not tracing, just return
100-
if tracing_is_off():
98+
if not tracer:
10199
return wrapped(*args, **kwargs)
102100

101+
parent_context = parent_span.get_span_context() if parent_span else None
102+
103103
with tracer.start_as_current_span(
104104
"couchbase", span_context=parent_context
105105
) as span:
@@ -120,12 +120,12 @@ def query_with_instana(
120120
kwargs: Dict[str, Any],
121121
) -> object:
122122
tracer, parent_span, _ = get_tracer_tuple()
123-
parent_context = parent_span.get_span_context() if parent_span else None
124-
125123
# If we're not tracing, just return
126-
if tracing_is_off():
124+
if not tracer:
127125
return wrapped(*args, **kwargs)
128126

127+
parent_context = parent_span.get_span_context() if parent_span else None
128+
129129
with tracer.start_as_current_span(
130130
"couchbase", span_context=parent_context
131131
) as span:

0 commit comments

Comments
 (0)