Skip to content

Commit 3c1b5ac

Browse files
committed
chore: use get_tracer() for all tracer access
Signed-off-by: Cagri Yonca <cagri@ibm.com>
1 parent 6b4f6e4 commit 3c1b5ac

64 files changed

Lines changed: 1229 additions & 884 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/instana/instrumentation/aio_pika.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# (c) Copyright IBM Corp. 2025
22

33
try:
4-
import aio_pika
4+
import aio_pika # noqa: F401
55
import wrapt
66
from typing import (
77
TYPE_CHECKING,
@@ -16,7 +16,7 @@
1616
from instana.log import logger
1717
from instana.propagators.format import Format
1818
from instana.util.traceutils import get_tracer_tuple, tracing_is_off
19-
from instana.singletons import tracer
19+
from instana.singletons import get_tracer
2020

2121
if TYPE_CHECKING:
2222
from instana.span.span import InstanaSpan
@@ -54,10 +54,8 @@ def _bind_args(
5454
**kwargs: object,
5555
) -> Tuple[object, ...]:
5656
return (message, routing_key, args, kwargs)
57-
58-
(message, routing_key, args, kwargs) = _bind_args(
59-
*args, **kwargs
60-
)
57+
58+
(message, routing_key, args, kwargs) = _bind_args(*args, **kwargs)
6159

6260
with tracer.start_as_current_span(
6361
"rabbitmq", span_context=parent_context
@@ -102,6 +100,7 @@ async def callback_wrapper(
102100
kwargs: Dict[str, Any],
103101
) -> Callable[[Type["AbstractMessage"]], Any]:
104102
message = args[0]
103+
tracer = get_tracer()
105104
parent_context = tracer.extract(
106105
Format.HTTP_HEADERS, message.headers, disable_w3c_trace_context=True
107106
)

src/instana/instrumentation/aiohttp/server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from instana.log import logger
1111
from instana.propagators.format import Format
12-
from instana.singletons import agent, tracer
12+
from instana.singletons import agent, get_tracer
1313
from instana.util.secrets import strip_secrets_from_query
1414
from instana.util.traceutils import extract_custom_headers
1515

@@ -29,6 +29,7 @@ async def stan_middleware(
2929
handler: Callable[..., object],
3030
) -> Awaitable["aiohttp.web.Response"]:
3131
try:
32+
tracer = get_tracer()
3233
span_context = tracer.extract(Format.HTTP_HEADERS, request.headers)
3334
span: "InstanaSpan" = tracer.start_span(
3435
"aiohttp-server", span_context=span_context

src/instana/instrumentation/asgi.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict
99

1010
from opentelemetry.semconv.trace import SpanAttributes
11-
from opentelemetry.trace import SpanKind
1211

1312
from instana.log import logger
1413
from instana.propagators.format import Format
15-
from instana.singletons import agent, tracer
14+
from instana.singletons import agent, get_tracer
1615
from instana.util.secrets import strip_secrets_from_query
1716
from instana.util.traceutils import extract_custom_headers
1817

@@ -66,6 +65,7 @@ async def __call__(
6665
send: Callable[[Dict[str, Any]], Awaitable[None]],
6766
) -> None:
6867
request_context = None
68+
tracer = get_tracer()
6969

7070
if scope["type"] not in ("http", "websocket"):
7171
return await self.app(scope, receive, send)
@@ -104,11 +104,14 @@ async def send_wrapper(response: Dict[str, Any]) -> Awaitable[None]:
104104
if status_code:
105105
if 500 <= int(status_code):
106106
current_span.mark_as_errored()
107-
current_span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code)
107+
current_span.set_attribute(
108+
SpanAttributes.HTTP_STATUS_CODE, status_code
109+
)
108110

109111
headers = response.get("headers")
110112
if headers:
111113
extract_custom_headers(current_span, headers)
114+
tracer = get_tracer()
112115
tracer.inject(current_span.context, Format.BINARY, headers)
113116
except Exception:
114117
logger.debug("ASGI send_wrapper error: ", exc_info=True)

src/instana/instrumentation/aws/boto3.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# (c) Copyright IBM Corp. 2025
2+
3+
24
try:
35
from typing import TYPE_CHECKING, Any, Callable, Dict, Sequence, Tuple, Type
46

@@ -19,7 +21,7 @@
1921

2022
from instana.log import logger
2123
from instana.propagators.format import Format
22-
from instana.singletons import tracer
24+
from instana.singletons import get_tracer
2325
from instana.span.span import get_current_span
2426
from instana.util.traceutils import (
2527
extract_custom_headers,
@@ -34,6 +36,7 @@ def lambda_inject_context(payload: Dict[str, Any], span: "InstanaSpan") -> None:
3436
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#Lambda.Client.invoke
3537
"""
3638
try:
39+
tracer = get_tracer()
3740
invoke_payload = payload.get("Payload", {})
3841

3942
if not isinstance(invoke_payload, dict):

src/instana/instrumentation/aws/dynamodb.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# (c) Copyright IBM Corp. 2025
22

3+
34
from typing import TYPE_CHECKING, Any, Callable, Dict, Sequence, Type
45

56
if TYPE_CHECKING:
67
from botocore.client import BaseClient
78

89
from instana.log import logger
9-
from instana.singletons import tracer
10+
from instana.singletons import get_tracer
1011
from instana.span_context import SpanContext
1112

1213

@@ -17,6 +18,7 @@ def create_dynamodb_span(
1718
kwargs: Dict[str, Any],
1819
parent_context: SpanContext,
1920
) -> None:
21+
tracer = get_tracer()
2022
with tracer.start_as_current_span("dynamodb", span_context=parent_context) as span:
2123
try:
2224
span.set_attribute("dynamodb.op", args[0])

src/instana/instrumentation/aws/s3.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# (c) Copyright IBM Corp. 2021
22
# (c) Copyright Instana Inc. 2020
33

4+
45
try:
56
from typing import TYPE_CHECKING, Any, Callable, Dict, Sequence, Type
67

@@ -11,7 +12,7 @@
1112
import wrapt
1213

1314
from instana.log import logger
14-
from instana.singletons import tracer
15+
from instana.singletons import get_tracer
1516
from instana.util.traceutils import (
1617
get_tracer_tuple,
1718
tracing_is_off,
@@ -31,6 +32,7 @@ def create_s3_span(
3132
kwargs: Dict[str, Any],
3233
parent_context: SpanContext,
3334
) -> None:
35+
tracer = get_tracer()
3436
with tracer.start_as_current_span("s3", span_context=parent_context) as span:
3537
try:
3638
span.set_attribute("s3.op", args[0])
@@ -66,15 +68,17 @@ def collect_s3_injected_attributes(
6668
span.set_attribute("s3.bucket", args[1])
6769
except Exception:
6870
logger.debug(
69-
f"collect_s3_injected_attributes collect error: {wrapped.__name__}", exc_info=True
71+
f"collect_s3_injected_attributes collect error: {wrapped.__name__}",
72+
exc_info=True,
7073
)
7174

7275
try:
7376
return wrapped(*args, **kwargs)
7477
except Exception as exc:
7578
span.record_exception(exc)
7679
logger.debug(
77-
f"collect_s3_injected_attributes error: {wrapped.__name__}", exc_info=True
80+
f"collect_s3_injected_attributes error: {wrapped.__name__}",
81+
exc_info=True,
7882
)
7983
raise
8084

src/instana/instrumentation/celery.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
# (c) Copyright Instana Inc. 2020
33

44

5-
import contextvars
6-
from typing import Any, Dict, Tuple
7-
from instana.log import logger
8-
from instana.propagators.format import Format
9-
from instana.singletons import tracer
10-
from instana.span.span import InstanaSpan
11-
from instana.util.traceutils import get_tracer_tuple
12-
from opentelemetry import trace, context
13-
145
try:
15-
import celery
6+
import celery # noqa: F401
7+
import contextvars
8+
from typing import Any, Dict, Tuple
9+
from urllib import parse
10+
1611
from celery import registry, signals
12+
from opentelemetry import context, trace
1713

18-
from urllib import parse
14+
from instana.log import logger
15+
from instana.propagators.format import Format
16+
from instana.singletons import get_tracer
17+
from instana.span.span import InstanaSpan
18+
from instana.util.traceutils import get_tracer_tuple
1919

2020
client_token: Dict[str, Any] = {}
2121
worker_token: Dict[str, Any] = {}
@@ -67,6 +67,7 @@ def task_prerun(
6767
) -> None:
6868
try:
6969
ctx = None
70+
tracer = get_tracer()
7071

7172
task = kwargs.get("sender", None)
7273
task_id = kwargs.get("task_id", None)

src/instana/instrumentation/django/middleware.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# (c) Copyright IBM Corp. 2021
22
# (c) Copyright Instana Inc. 2018
33

4+
45
try:
56
import sys
67

@@ -11,7 +12,7 @@
1112
from typing import TYPE_CHECKING, Dict, Any, Callable, Optional, List, Tuple, Type
1213

1314
from instana.log import logger
14-
from instana.singletons import agent, tracer
15+
from instana.singletons import agent, get_tracer
1516
from instana.util.secrets import strip_secrets_from_query
1617
from instana.util.traceutils import extract_custom_headers
1718
from instana.propagators.format import Format
@@ -55,6 +56,7 @@ def __init__(
5556

5657
def process_request(self, request: Type["HttpRequest"]) -> None:
5758
try:
59+
tracer = get_tracer()
5860
env = request.META
5961

6062
span_context = tracer.extract(Format.HTTP_HEADERS, env)
@@ -81,7 +83,9 @@ def process_request(self, request: Type["HttpRequest"]) -> None:
8183
)
8284
request.span.set_attribute("http.params", scrubbed_params)
8385
if "HTTP_HOST" in env:
84-
request.span.set_attribute(SpanAttributes.HTTP_HOST, env["HTTP_HOST"])
86+
request.span.set_attribute(
87+
SpanAttributes.HTTP_HOST, env["HTTP_HOST"]
88+
)
8589
except Exception:
8690
logger.debug("Django middleware @ process_request", exc_info=True)
8791

@@ -118,6 +122,7 @@ def process_response(
118122
extract_custom_headers(
119123
request.span, response.headers, format=False
120124
)
125+
tracer = get_tracer()
121126
tracer.inject(request.span.context, Format.HTTP_HEADERS, response)
122127
except Exception:
123128
logger.debug("Instana middleware @ process_response", exc_info=True)

src/instana/instrumentation/google/cloud/pubsub.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from instana.log import logger
1010
from instana.propagators.format import Format
11-
from instana.singletons import tracer
11+
from instana.singletons import get_tracer
1212
from instana.util.traceutils import get_tracer_tuple, tracing_is_off
1313

1414
if TYPE_CHECKING:
@@ -98,6 +98,7 @@ def subscribe_with_instana(
9898

9999
def callback_with_instana(message):
100100
if message.attributes:
101+
tracer = get_tracer()
101102
parent_context = tracer.extract(
102103
Format.TEXT_MAP, message.attributes, disable_w3c_trace_context=True
103104
)

src/instana/instrumentation/pika.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# (c) Copyright IBM Corp. 2021
33
# (c) Copyright Instana Inc. 2021
44

5+
56
try:
67
import types
78
from typing import (
@@ -20,7 +21,7 @@
2021

2122
from instana.log import logger
2223
from instana.propagators.format import Format
23-
from instana.singletons import tracer
24+
from instana.singletons import get_tracer
2425
from instana.util.traceutils import get_tracer_tuple, tracing_is_off
2526

2627
if TYPE_CHECKING:
@@ -142,6 +143,7 @@ def _cb_wrapper(
142143
properties: pika.BasicProperties,
143144
body: str,
144145
) -> None:
146+
tracer = get_tracer()
145147
parent_context = tracer.extract(
146148
Format.HTTP_HEADERS, properties.headers, disable_w3c_trace_context=True
147149
)
@@ -189,6 +191,7 @@ def _cb_wrapper(
189191
properties: pika.BasicProperties,
190192
body: str,
191193
) -> None:
194+
tracer = get_tracer()
192195
parent_context = tracer.extract(
193196
Format.HTTP_HEADERS, properties.headers, disable_w3c_trace_context=True
194197
)
@@ -230,6 +233,7 @@ def _bind_args(
230233
(queue, args, kwargs) = _bind_args(*args, **kwargs)
231234

232235
def _consume(gen: Iterator[object]) -> object:
236+
tracer = get_tracer()
233237
for yielded in gen:
234238
# Bypass the delivery created due to inactivity timeout
235239
if not yielded or not any(yielded):

0 commit comments

Comments
 (0)