Skip to content

Commit a2a68c8

Browse files
pvitalGSVarsha
andcommitted
refactor(instrumentation): Use context parameter instead of span_context
This commit updates all instrumentation modules to use the `context` parameter instead of the deprecated `span_context` parameter when calling `start_span()` and `start_as_current_span()` methods. This change aligns with OpenTelemetry's API conventions and improves consistency across the codebase. This commit fixes #847. Signed-off-by: Paulo Vital <paulo.vital@ibm.com> Co-authored-by: Varsha GS <varsha.gs@ibm.com>
1 parent 4861510 commit a2a68c8

33 files changed

Lines changed: 191 additions & 183 deletions

src/instana/instrumentation/aio_pika.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
# (c) Copyright IBM Corp. 2025
22

33
try:
4+
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Tuple, Type
5+
46
import aio_pika # noqa: F401
57
import wrapt
6-
from typing import (
7-
TYPE_CHECKING,
8-
Dict,
9-
Any,
10-
Callable,
11-
Tuple,
12-
Type,
13-
Optional,
14-
)
8+
from opentelemetry.context import get_current
159

1610
from instana.log import logger
1711
from instana.propagators.format import Format
18-
from instana.util.traceutils import get_tracer_tuple
1912
from instana.singletons import get_tracer
13+
from instana.util.traceutils import get_tracer_tuple
2014

2115
if TYPE_CHECKING:
22-
from instana.span.span import InstanaSpan
16+
from aio_pika.abc import AbstractMessage, ConsumerTag
2317
from aio_pika.exchange import Exchange
24-
from aiormq.abc import ConfirmationFrameType
25-
from aio_pika.abc import ConsumerTag, AbstractMessage
2618
from aio_pika.queue import Queue, QueueIterator
19+
from aiormq.abc import ConfirmationFrameType
20+
21+
from instana.span.span import InstanaSpan
2722

2823
def _extract_span_attributes(
2924
span: "InstanaSpan", connection, sort: str, routing_key: str, exchange: str
@@ -41,11 +36,11 @@ async def publish_with_instana(
4136
args: Tuple[object],
4237
kwargs: Dict[str, Any],
4338
) -> Optional["ConfirmationFrameType"]:
44-
tracer, parent_span, _ = get_tracer_tuple()
39+
tracer, _, _ = get_tracer_tuple()
4540
if not tracer:
4641
return await wrapped(*args, **kwargs)
4742

48-
parent_context = parent_span.get_span_context() if parent_span else None
43+
parent_context = get_current()
4944

5045
def _bind_args(
5146
message: Type["AbstractMessage"],
@@ -57,9 +52,7 @@ def _bind_args(
5752

5853
(message, routing_key, args, kwargs) = _bind_args(*args, **kwargs)
5954

60-
with tracer.start_as_current_span(
61-
"rabbitmq", span_context=parent_context
62-
) as span:
55+
with tracer.start_as_current_span("rabbitmq", context=parent_context) as span:
6356
connection = instance.channel._connection
6457

6558
_extract_span_attributes(
@@ -105,7 +98,7 @@ async def callback_wrapper(
10598
Format.HTTP_HEADERS, message.headers, disable_w3c_trace_context=True
10699
)
107100
with tracer.start_as_current_span(
108-
"rabbitmq", span_context=parent_context
101+
"rabbitmq", context=parent_context
109102
) as span:
110103
_extract_span_attributes(
111104
span, connection, "consume", message.routing_key, message.exchange

src/instana/instrumentation/aioamqp.py

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

33
try:
4-
import aioamqp
54
from typing import Any, Callable, Dict, Tuple
65

6+
import aioamqp
77
import wrapt
8+
from opentelemetry.context import get_current
89
from opentelemetry.trace.status import StatusCode
910

1011
from instana.log import logger
@@ -21,9 +22,9 @@ async def basic_publish_with_instana(
2122
if not tracer:
2223
return await wrapped(*argv, **kwargs)
2324

24-
parent_context = parent_span.get_span_context() if parent_span else None
25+
parent_context = get_current()
2526
with tracer.start_as_current_span(
26-
"aioamqp-publisher", span_context=parent_context
27+
"aioamqp-publisher", context=parent_context
2728
) as span:
2829
try:
2930
span.set_attribute("amqp.command", "publish")
@@ -62,7 +63,7 @@ async def basic_consume_with_instana(
6263
return await wrapped(*argv, **kwargs)
6364

6465
callback = argv[0]
65-
parent_context = parent_span.get_span_context() if parent_span else None
66+
parent_context = get_current()
6667

6768
@wrapt.decorator
6869
async def callback_wrapper(
@@ -72,7 +73,7 @@ async def callback_wrapper(
7273
kwargs: Dict,
7374
) -> object:
7475
with tracer.start_as_current_span(
75-
"aioamqp-consumer", span_context=parent_context
76+
"aioamqp-consumer", context=parent_context
7677
) as span:
7778
try:
7879
span.set_status(StatusCode.OK)

src/instana/instrumentation/aiohttp/client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44

55
from types import SimpleNamespace
66
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict, Tuple
7-
import wrapt
87

8+
import wrapt
99
from opentelemetry.semconv.trace import SpanAttributes
1010

1111
from instana.log import logger
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, extract_custom_headers
15+
from instana.util.traceutils import extract_custom_headers, get_tracer_tuple
1616

1717
try:
1818
import aiohttp
19+
from opentelemetry.context import get_current
1920

2021
if TYPE_CHECKING:
2122
from aiohttp.client import ClientSession
23+
2224
from instana.span.span import InstanaSpan
2325

2426
async def stan_request_start(
@@ -31,9 +33,9 @@ async def stan_request_start(
3133
trace_config_ctx.span_context = None
3234
return
3335

34-
parent_context = parent_span.get_span_context() if parent_span else None
36+
parent_context = get_current()
3537

36-
span = tracer.start_span("aiohttp-client", span_context=parent_context)
38+
span = tracer.start_span("aiohttp-client", context=parent_context)
3739

3840
extract_custom_headers(span, params.headers)
3941

src/instana/instrumentation/aiohttp/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ async def stan_middleware(
3030
) -> Awaitable["aiohttp.web.Response"]:
3131
try:
3232
tracer = get_tracer()
33-
span_context = tracer.extract(Format.HTTP_HEADERS, request.headers)
33+
parent_context = tracer.extract(Format.HTTP_HEADERS, request.headers)
3434
span: "InstanaSpan" = tracer.start_span(
35-
"aiohttp-server", span_context=span_context
35+
"aiohttp-server", context=parent_context
3636
)
3737
request["span"] = span
3838

src/instana/instrumentation/asgi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async def __call__(
7474
if isinstance(request_headers, list):
7575
request_context = tracer.extract(Format.BINARY, request_headers)
7676

77-
with tracer.start_as_current_span("asgi", span_context=request_context) as span:
77+
with tracer.start_as_current_span("asgi", context=request_context) as span:
7878
self._collect_kvs(scope, span)
7979
if "headers" in scope:
8080
extract_custom_headers(span, scope["headers"])

src/instana/instrumentation/aws/boto3.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
try:
55
from typing import TYPE_CHECKING, Any, Callable, Dict, Sequence, Tuple, Type
66

7+
from opentelemetry.context import get_current
78
from opentelemetry.semconv.trace import SpanAttributes
89

910
from instana.instrumentation.aws.dynamodb import create_dynamodb_span
@@ -23,10 +24,7 @@
2324
from instana.log import logger
2425
from instana.propagators.format import Format
2526
from instana.singletons import get_tracer
26-
from instana.util.traceutils import (
27-
extract_custom_headers,
28-
get_tracer_tuple,
29-
)
27+
from instana.util.traceutils import extract_custom_headers, get_tracer_tuple
3028

3129
def lambda_inject_context(
3230
tracer: "InstanaTracer",
@@ -74,16 +72,14 @@ def make_api_call_with_instana(
7472
if not tracer:
7573
return wrapped(*args, **kwargs)
7674

77-
parent_context = parent_span.get_span_context() if parent_span else None
75+
parent_context = get_current()
7876

7977
if instance.meta.service_model.service_name == "dynamodb":
8078
create_dynamodb_span(wrapped, instance, args, kwargs, parent_context)
8179
elif instance.meta.service_model.service_name == "s3":
8280
create_s3_span(wrapped, instance, args, kwargs, parent_context)
8381
else:
84-
with tracer.start_as_current_span(
85-
"boto3", span_context=parent_context
86-
) as span:
82+
with tracer.start_as_current_span("boto3", context=parent_context) as span:
8783
operation = args[0]
8884
payload = args[1]
8985

src/instana/instrumentation/aws/dynamodb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def create_dynamodb_span(
1919
parent_context: SpanContext,
2020
) -> None:
2121
tracer = get_tracer()
22-
with tracer.start_as_current_span("dynamodb", span_context=parent_context) as span:
22+
with tracer.start_as_current_span("dynamodb", context=parent_context) as span:
2323
try:
2424
span.set_attribute("dynamodb.op", args[0])
2525
span.set_attribute("dynamodb.region", instance._client_config.region_name)

src/instana/instrumentation/aws/lambda_inst.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def lambda_handler_with_instana(
3838

3939
result = None
4040
with tracer.start_as_current_span(
41-
"aws.lambda.entry", span_context=incoming_ctx
41+
"aws.lambda.entry", context=incoming_ctx
4242
) as span:
4343
enrich_lambda_span(agent, span, *args)
4444
try:

src/instana/instrumentation/aws/s3.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
try:
66
from typing import TYPE_CHECKING, Any, Callable, Dict, Sequence, Type
77

8+
from opentelemetry.context import get_current
9+
810
from instana.span_context import SpanContext
911

1012
if TYPE_CHECKING:
@@ -13,9 +15,7 @@
1315

1416
from instana.log import logger
1517
from instana.singletons import get_tracer
16-
from instana.util.traceutils import (
17-
get_tracer_tuple,
18-
)
18+
from instana.util.traceutils import get_tracer_tuple
1919

2020
operations = {
2121
"upload_file": "UploadFile",
@@ -32,7 +32,7 @@ def create_s3_span(
3232
parent_context: SpanContext,
3333
) -> None:
3434
tracer = get_tracer()
35-
with tracer.start_as_current_span("s3", span_context=parent_context) as span:
35+
with tracer.start_as_current_span("s3", context=parent_context) as span:
3636
try:
3737
span.set_attribute("s3.op", args[0])
3838
if "Bucket" in args[1].keys():
@@ -52,9 +52,9 @@ def collect_s3_injected_attributes(
5252
if not tracer:
5353
return wrapped(*args, **kwargs)
5454

55-
parent_context = parent_span.get_span_context() if parent_span else None
55+
parent_context = get_current()
5656

57-
with tracer.start_as_current_span("s3", span_context=parent_context) as span:
57+
with tracer.start_as_current_span("s3", context=parent_context) as span:
5858
try:
5959
span.set_attribute("s3.op", operations[wrapped.__name__])
6060
if "Bucket" in kwargs:

src/instana/instrumentation/cassandra.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import cassandra
1414
import wrapt
15+
from opentelemetry.context import get_current
1516

1617
from instana.log import logger
1718
from instana.util.traceutils import get_tracer_tuple
@@ -76,7 +77,7 @@ def request_init_with_instana(
7677
if not tracer:
7778
return
7879

79-
parent_context = parent_span.get_span_context() if parent_span else None
80+
parent_context = get_current()
8081

8182
attributes = {}
8283
if isinstance(fn.query, cassandra.query.SimpleStatement):
@@ -89,7 +90,7 @@ def request_init_with_instana(
8990

9091
with tracer.start_as_current_span(
9192
"cassandra",
92-
span_context=parent_context,
93+
context=parent_context,
9394
attributes=attributes,
9495
end_on_exit=False,
9596
) as span:

0 commit comments

Comments
 (0)