Skip to content

Commit 5c46281

Browse files
committed
chore: use get_tracer() for all tracer access
Signed-off-by: Cagri Yonca <[email protected]>
1 parent 9369fdd commit 5c46281

Some content is hidden

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

64 files changed

+1279
-986
lines changed

src/instana/instrumentation/aio_pika.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# (c) Copyright IBM Corp. 2025
1+
# (c) Copyright IBM Corp. 2021, 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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# (c) Copyright IBM Corp. 2021
2-
# (c) Copyright Instana Inc. 2019
1+
# (c) Copyright IBM Corp. 2021, 2025
32

43

54
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict, Tuple
@@ -9,7 +8,7 @@
98

109
from instana.log import logger
1110
from instana.propagators.format import Format
12-
from instana.singletons import agent, tracer
11+
from instana.singletons import agent, get_tracer
1312
from instana.util.secrets import strip_secrets_from_query
1413
from instana.util.traceutils import extract_custom_headers
1514

@@ -29,6 +28,7 @@ async def stan_middleware(
2928
handler: Callable[..., object],
3029
) -> Awaitable["aiohttp.web.Response"]:
3130
try:
31+
tracer = get_tracer()
3232
span_context = tracer.extract(Format.HTTP_HEADERS, request.headers)
3333
span: "InstanaSpan" = tracer.start_span(
3434
"aiohttp-server", span_context=span_context

src/instana/instrumentation/asgi.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# (c) Copyright IBM Corp. 2021
2-
# (c) Copyright Instana Inc. 2020
1+
# (c) Copyright IBM Corp. 2021, 2025
32

43
"""
54
Instana ASGI Middleware
@@ -8,11 +7,10 @@
87
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict
98

109
from opentelemetry.semconv.trace import SpanAttributes
11-
from opentelemetry.trace import SpanKind
1210

1311
from instana.log import logger
1412
from instana.propagators.format import Format
15-
from instana.singletons import agent, tracer
13+
from instana.singletons import agent, get_tracer
1614
from instana.util.secrets import strip_secrets_from_query
1715
from instana.util.traceutils import extract_custom_headers
1816

@@ -66,6 +64,7 @@ async def __call__(
6664
send: Callable[[Dict[str, Any]], Awaitable[None]],
6765
) -> None:
6866
request_context = None
67+
tracer = get_tracer()
6968

7069
if scope["type"] not in ("http", "websocket"):
7170
return await self.app(scope, receive, send)
@@ -104,11 +103,14 @@ async def send_wrapper(response: Dict[str, Any]) -> Awaitable[None]:
104103
if status_code:
105104
if 500 <= int(status_code):
106105
current_span.mark_as_errored()
107-
current_span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code)
106+
current_span.set_attribute(
107+
SpanAttributes.HTTP_STATUS_CODE, status_code
108+
)
108109

109110
headers = response.get("headers")
110111
if headers:
111112
extract_custom_headers(current_span, headers)
113+
tracer = get_tracer()
112114
tracer.inject(current_span.context, Format.BINARY, headers)
113115
except Exception:
114116
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: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# (c) Copyright IBM Corp. 2021
2-
# (c) Copyright Instana Inc. 2020
1+
# (c) Copyright IBM Corp. 2021, 2025
2+
33

44
try:
55
from typing import TYPE_CHECKING, Any, Callable, Dict, Sequence, Type
@@ -11,7 +11,7 @@
1111
import wrapt
1212

1313
from instana.log import logger
14-
from instana.singletons import tracer
14+
from instana.singletons import get_tracer
1515
from instana.util.traceutils import (
1616
get_tracer_tuple,
1717
tracing_is_off,
@@ -31,6 +31,7 @@ def create_s3_span(
3131
kwargs: Dict[str, Any],
3232
parent_context: SpanContext,
3333
) -> None:
34+
tracer = get_tracer()
3435
with tracer.start_as_current_span("s3", span_context=parent_context) as span:
3536
try:
3637
span.set_attribute("s3.op", args[0])
@@ -66,15 +67,17 @@ def collect_s3_injected_attributes(
6667
span.set_attribute("s3.bucket", args[1])
6768
except Exception:
6869
logger.debug(
69-
f"collect_s3_injected_attributes collect error: {wrapped.__name__}", exc_info=True
70+
f"collect_s3_injected_attributes collect error: {wrapped.__name__}",
71+
exc_info=True,
7072
)
7173

7274
try:
7375
return wrapped(*args, **kwargs)
7476
except Exception as exc:
7577
span.record_exception(exc)
7678
logger.debug(
77-
f"collect_s3_injected_attributes error: {wrapped.__name__}", exc_info=True
79+
f"collect_s3_injected_attributes error: {wrapped.__name__}",
80+
exc_info=True,
7881
)
7982
raise
8083

src/instana/instrumentation/celery.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
# (c) Copyright IBM Corp. 2021
2-
# (c) Copyright Instana Inc. 2020
1+
# (c) Copyright IBM Corp. 2021, 2025
32

43

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-
144
try:
15-
import celery
5+
import celery # noqa: F401
6+
import contextvars
7+
from typing import Any, Dict, Tuple
8+
from urllib import parse
9+
1610
from celery import registry, signals
11+
from opentelemetry import context, trace
1712

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

2019
client_token: Dict[str, Any] = {}
2120
worker_token: Dict[str, Any] = {}
@@ -67,6 +66,7 @@ def task_prerun(
6766
) -> None:
6867
try:
6968
ctx = None
69+
tracer = get_tracer()
7070

7171
task = kwargs.get("sender", None)
7272
task_id = kwargs.get("task_id", None)

src/instana/instrumentation/django/middleware.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# (c) Copyright IBM Corp. 2021
2-
# (c) Copyright Instana Inc. 2018
1+
# (c) Copyright IBM Corp. 2021, 2025
2+
33

44
try:
55
import sys
@@ -11,7 +11,7 @@
1111
from typing import TYPE_CHECKING, Dict, Any, Callable, Optional, List, Tuple, Type
1212

1313
from instana.log import logger
14-
from instana.singletons import agent, tracer
14+
from instana.singletons import agent, get_tracer
1515
from instana.util.secrets import strip_secrets_from_query
1616
from instana.util.traceutils import extract_custom_headers
1717
from instana.propagators.format import Format
@@ -55,6 +55,7 @@ def __init__(
5555

5656
def process_request(self, request: Type["HttpRequest"]) -> None:
5757
try:
58+
tracer = get_tracer()
5859
env = request.META
5960

6061
span_context = tracer.extract(Format.HTTP_HEADERS, env)
@@ -81,7 +82,9 @@ def process_request(self, request: Type["HttpRequest"]) -> None:
8182
)
8283
request.span.set_attribute("http.params", scrubbed_params)
8384
if "HTTP_HOST" in env:
84-
request.span.set_attribute(SpanAttributes.HTTP_HOST, env["HTTP_HOST"])
85+
request.span.set_attribute(
86+
SpanAttributes.HTTP_HOST, env["HTTP_HOST"]
87+
)
8588
except Exception:
8689
logger.debug("Django middleware @ process_request", exc_info=True)
8790

@@ -118,6 +121,7 @@ def process_response(
118121
extract_custom_headers(
119122
request.span, response.headers, format=False
120123
)
124+
tracer = get_tracer()
121125
tracer.inject(request.span.context, Format.HTTP_HEADERS, response)
122126
except Exception:
123127
logger.debug("Instana middleware @ process_response", exc_info=True)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# (c) Copyright IBM Corp. 2021
2-
# (c) Copyright Instana Inc. 2021
1+
# (c) Copyright IBM Corp. 2021, 2025
32

43

54
from typing import TYPE_CHECKING, Any, Callable, Dict, Tuple
@@ -8,7 +7,7 @@
87

98
from instana.log import logger
109
from instana.propagators.format import Format
11-
from instana.singletons import tracer
10+
from instana.singletons import get_tracer
1211
from instana.util.traceutils import get_tracer_tuple, tracing_is_off
1312

1413
if TYPE_CHECKING:
@@ -98,6 +97,7 @@ def subscribe_with_instana(
9897

9998
def callback_with_instana(message):
10099
if message.attributes:
100+
tracer = get_tracer()
101101
parent_context = tracer.extract(
102102
Format.TEXT_MAP, message.attributes, disable_w3c_trace_context=True
103103
)

src/instana/instrumentation/pika.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# coding: utf-8
2-
# (c) Copyright IBM Corp. 2021
3-
# (c) Copyright Instana Inc. 2021
2+
# (c) Copyright IBM Corp. 2021, 2025
43

54
try:
65
import types
@@ -20,7 +19,7 @@
2019

2120
from instana.log import logger
2221
from instana.propagators.format import Format
23-
from instana.singletons import tracer
22+
from instana.singletons import get_tracer
2423
from instana.util.traceutils import get_tracer_tuple, tracing_is_off
2524

2625
if TYPE_CHECKING:
@@ -142,6 +141,7 @@ def _cb_wrapper(
142141
properties: pika.BasicProperties,
143142
body: str,
144143
) -> None:
144+
tracer = get_tracer()
145145
parent_context = tracer.extract(
146146
Format.HTTP_HEADERS, properties.headers, disable_w3c_trace_context=True
147147
)
@@ -189,6 +189,7 @@ def _cb_wrapper(
189189
properties: pika.BasicProperties,
190190
body: str,
191191
) -> None:
192+
tracer = get_tracer()
192193
parent_context = tracer.extract(
193194
Format.HTTP_HEADERS, properties.headers, disable_w3c_trace_context=True
194195
)
@@ -230,6 +231,7 @@ def _bind_args(
230231
(queue, args, kwargs) = _bind_args(*args, **kwargs)
231232

232233
def _consume(gen: Iterator[object]) -> object:
234+
tracer = get_tracer()
233235
for yielded in gen:
234236
# Bypass the delivery created due to inactivity timeout
235237
if not yielded or not any(yielded):

0 commit comments

Comments
 (0)