Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions src/instana/instrumentation/aio_pika.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
# (c) Copyright IBM Corp. 2025

try:
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Tuple, Type

import aio_pika # noqa: F401
import wrapt
from typing import (
TYPE_CHECKING,
Dict,
Any,
Callable,
Tuple,
Type,
Optional,
)
from opentelemetry.context import get_current

from instana.log import logger
from instana.propagators.format import Format
from instana.util.traceutils import get_tracer_tuple
from instana.singletons import get_tracer
from instana.util.traceutils import get_tracer_tuple

if TYPE_CHECKING:
from instana.span.span import InstanaSpan
from aio_pika.abc import AbstractMessage, ConsumerTag
from aio_pika.exchange import Exchange
from aiormq.abc import ConfirmationFrameType
from aio_pika.abc import ConsumerTag, AbstractMessage
from aio_pika.queue import Queue, QueueIterator
from aiormq.abc import ConfirmationFrameType

from instana.span.span import InstanaSpan

def _extract_span_attributes(
span: "InstanaSpan", connection, sort: str, routing_key: str, exchange: str
Expand All @@ -41,11 +36,11 @@ async def publish_with_instana(
args: Tuple[object],
kwargs: Dict[str, Any],
) -> Optional["ConfirmationFrameType"]:
tracer, parent_span, _ = get_tracer_tuple()
tracer, _, _ = get_tracer_tuple()
if not tracer:
return await wrapped(*args, **kwargs)

parent_context = parent_span.get_span_context() if parent_span else None
parent_context = get_current()

def _bind_args(
message: Type["AbstractMessage"],
Expand All @@ -57,9 +52,7 @@ def _bind_args(

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

with tracer.start_as_current_span(
"rabbitmq", span_context=parent_context
) as span:
with tracer.start_as_current_span("rabbitmq", context=parent_context) as span:
connection = instance.channel._connection

_extract_span_attributes(
Expand Down Expand Up @@ -105,7 +98,7 @@ async def callback_wrapper(
Format.HTTP_HEADERS, message.headers, disable_w3c_trace_context=True
)
with tracer.start_as_current_span(
"rabbitmq", span_context=parent_context
"rabbitmq", context=parent_context
) as span:
_extract_span_attributes(
span, connection, "consume", message.routing_key, message.exchange
Expand Down
11 changes: 6 additions & 5 deletions src/instana/instrumentation/aioamqp.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# (c) Copyright IBM Corp. 2025

try:
import aioamqp
from typing import Any, Callable, Dict, Tuple

import aioamqp
import wrapt
from opentelemetry.context import get_current
from opentelemetry.trace.status import StatusCode

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

parent_context = parent_span.get_span_context() if parent_span else None
parent_context = get_current()
with tracer.start_as_current_span(
"aioamqp-publisher", span_context=parent_context
"aioamqp-publisher", context=parent_context
) as span:
try:
span.set_attribute("amqp.command", "publish")
Expand Down Expand Up @@ -62,7 +63,7 @@ async def basic_consume_with_instana(
return await wrapped(*argv, **kwargs)

callback = argv[0]
parent_context = parent_span.get_span_context() if parent_span else None
parent_context = get_current()

@wrapt.decorator
async def callback_wrapper(
Expand All @@ -72,7 +73,7 @@ async def callback_wrapper(
kwargs: Dict,
) -> object:
with tracer.start_as_current_span(
"aioamqp-consumer", span_context=parent_context
"aioamqp-consumer", context=parent_context
) as span:
try:
span.set_status(StatusCode.OK)
Expand Down
10 changes: 6 additions & 4 deletions src/instana/instrumentation/aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@

from types import SimpleNamespace
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict, Tuple
import wrapt

import wrapt
from opentelemetry.semconv.trace import SpanAttributes

from instana.log import logger
from instana.propagators.format import Format
from instana.singletons import agent
from instana.util.secrets import strip_secrets_from_query
from instana.util.traceutils import get_tracer_tuple, extract_custom_headers
from instana.util.traceutils import extract_custom_headers, get_tracer_tuple

try:
import aiohttp
from opentelemetry.context import get_current

if TYPE_CHECKING:
from aiohttp.client import ClientSession

from instana.span.span import InstanaSpan

async def stan_request_start(
Expand All @@ -31,9 +33,9 @@ async def stan_request_start(
trace_config_ctx.span_context = None
return

parent_context = parent_span.get_span_context() if parent_span else None
parent_context = get_current()

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

extract_custom_headers(span, params.headers)

Expand Down
2 changes: 1 addition & 1 deletion src/instana/instrumentation/aiohttp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def stan_middleware(
tracer = get_tracer()
span_context = tracer.extract(Format.HTTP_HEADERS, request.headers)
span: "InstanaSpan" = tracer.start_span(
"aiohttp-server", span_context=span_context
"aiohttp-server", context=span_context
)
request["span"] = span

Expand Down
2 changes: 1 addition & 1 deletion src/instana/instrumentation/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async def __call__(
if isinstance(request_headers, list):
request_context = tracer.extract(Format.BINARY, request_headers)

with tracer.start_as_current_span("asgi", span_context=request_context) as span:
with tracer.start_as_current_span("asgi", context=request_context) as span:
self._collect_kvs(scope, span)
if "headers" in scope:
extract_custom_headers(span, scope["headers"])
Expand Down
12 changes: 4 additions & 8 deletions src/instana/instrumentation/aws/boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
try:
from typing import TYPE_CHECKING, Any, Callable, Dict, Sequence, Tuple, Type

from opentelemetry.context import get_current
from opentelemetry.semconv.trace import SpanAttributes

from instana.instrumentation.aws.dynamodb import create_dynamodb_span
Expand All @@ -23,10 +24,7 @@
from instana.log import logger
from instana.propagators.format import Format
from instana.singletons import get_tracer
from instana.util.traceutils import (
extract_custom_headers,
get_tracer_tuple,
)
from instana.util.traceutils import extract_custom_headers, get_tracer_tuple

def lambda_inject_context(
tracer: "InstanaTracer",
Expand Down Expand Up @@ -74,16 +72,14 @@ def make_api_call_with_instana(
if not tracer:
return wrapped(*args, **kwargs)

parent_context = parent_span.get_span_context() if parent_span else None
parent_context = get_current()

if instance.meta.service_model.service_name == "dynamodb":
create_dynamodb_span(wrapped, instance, args, kwargs, parent_context)
elif instance.meta.service_model.service_name == "s3":
create_s3_span(wrapped, instance, args, kwargs, parent_context)
else:
with tracer.start_as_current_span(
"boto3", span_context=parent_context
) as span:
with tracer.start_as_current_span("boto3", context=parent_context) as span:
operation = args[0]
payload = args[1]

Expand Down
2 changes: 1 addition & 1 deletion src/instana/instrumentation/aws/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def create_dynamodb_span(
parent_context: SpanContext,
) -> None:
tracer = get_tracer()
with tracer.start_as_current_span("dynamodb", span_context=parent_context) as span:
with tracer.start_as_current_span("dynamodb", context=parent_context) as span:
try:
span.set_attribute("dynamodb.op", args[0])
span.set_attribute("dynamodb.region", instance._client_config.region_name)
Expand Down
2 changes: 1 addition & 1 deletion src/instana/instrumentation/aws/lambda_inst.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def lambda_handler_with_instana(

result = None
with tracer.start_as_current_span(
"aws.lambda.entry", span_context=incoming_ctx
"aws.lambda.entry", context=incoming_ctx
) as span:
enrich_lambda_span(agent, span, *args)
try:
Expand Down
12 changes: 6 additions & 6 deletions src/instana/instrumentation/aws/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
try:
from typing import TYPE_CHECKING, Any, Callable, Dict, Sequence, Type

from opentelemetry.context import get_current

from instana.span_context import SpanContext

if TYPE_CHECKING:
Expand All @@ -13,9 +15,7 @@

from instana.log import logger
from instana.singletons import get_tracer
from instana.util.traceutils import (
get_tracer_tuple,
)
from instana.util.traceutils import get_tracer_tuple

operations = {
"upload_file": "UploadFile",
Expand All @@ -32,7 +32,7 @@ def create_s3_span(
parent_context: SpanContext,
) -> None:
tracer = get_tracer()
with tracer.start_as_current_span("s3", span_context=parent_context) as span:
with tracer.start_as_current_span("s3", context=parent_context) as span:
try:
span.set_attribute("s3.op", args[0])
if "Bucket" in args[1].keys():
Expand All @@ -52,9 +52,9 @@ def collect_s3_injected_attributes(
if not tracer:
return wrapped(*args, **kwargs)

parent_context = parent_span.get_span_context() if parent_span else None
parent_context = get_current()

with tracer.start_as_current_span("s3", span_context=parent_context) as span:
with tracer.start_as_current_span("s3", context=parent_context) as span:
try:
span.set_attribute("s3.op", operations[wrapped.__name__])
if "Bucket" in kwargs:
Expand Down
5 changes: 3 additions & 2 deletions src/instana/instrumentation/cassandra.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import cassandra
import wrapt
from opentelemetry.context import get_current

from instana.log import logger
from instana.util.traceutils import get_tracer_tuple
Expand Down Expand Up @@ -76,7 +77,7 @@ def request_init_with_instana(
if not tracer:
return

parent_context = parent_span.get_span_context() if parent_span else None
parent_context = get_current()

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

with tracer.start_as_current_span(
"cassandra",
span_context=parent_context,
context=parent_context,
attributes=attributes,
end_on_exit=False,
) as span:
Expand Down
9 changes: 5 additions & 4 deletions src/instana/instrumentation/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@


try:
import celery # noqa: F401
import contextvars
from typing import Any, Dict, Tuple
from urllib import parse

import celery # noqa: F401
from celery import registry, signals
from opentelemetry import context, trace
from opentelemetry.context import get_current

from instana.log import logger
from instana.propagators.format import Format
Expand Down Expand Up @@ -79,7 +80,7 @@ def task_prerun(
Format.HTTP_HEADERS, headers, disable_w3c_trace_context=True
)

span = tracer.start_span("celery-worker", span_context=ctx)
span = tracer.start_span("celery-worker", context=ctx)
span.set_attribute("task", task.name)
span.set_attribute("task_id", task_id)
add_broker_attributes(span, task.app.conf["broker_url"])
Expand Down Expand Up @@ -148,15 +149,15 @@ def before_task_publish(
if not tracer:
return

parent_context = parent_span.get_span_context() if parent_span else None
parent_context = get_current()

body = kwargs["body"]
headers = kwargs["headers"]
task_name = kwargs["sender"]
task = registry.tasks.get(task_name)
task_id = _get_task_id(headers, body)

span = tracer.start_span("celery-client", span_context=parent_context)
span = tracer.start_span("celery-client", context=parent_context)
span.set_attribute("task", task_name)
span.set_attribute("task_id", task_id)
add_broker_attributes(span, task.app.conf["broker_url"])
Expand Down
17 changes: 8 additions & 9 deletions src/instana/instrumentation/couchbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

try:
import couchbase

from instana.log import logger

if not (
Expand All @@ -17,12 +18,12 @@
logger.debug("Instana supports 2.3.4 <= couchbase_versions < 3.0.0. Skipping.")
raise ImportError

from couchbase.bucket import Bucket
from couchbase.n1ql import N1QLQuery

from typing import Any, Callable, Dict, Tuple, Union

import wrapt
from couchbase.bucket import Bucket
from couchbase.n1ql import N1QLQuery
from opentelemetry.context import get_current

from instana.span.span import InstanaSpan
from instana.util.traceutils import get_tracer_tuple
Expand Down Expand Up @@ -98,10 +99,10 @@ def wrapper(
if not tracer:
return wrapped(*args, **kwargs)

parent_context = parent_span.get_span_context() if parent_span else None
parent_context = get_current()

with tracer.start_as_current_span(
"couchbase", span_context=parent_context
"couchbase", context=parent_context
) as span:
collect_attributes(span, instance, None, op)
try:
Expand All @@ -124,11 +125,9 @@ def query_with_instana(
if not tracer:
return wrapped(*args, **kwargs)

parent_context = parent_span.get_span_context() if parent_span else None
parent_context = get_current()

with tracer.start_as_current_span(
"couchbase", span_context=parent_context
) as span:
with tracer.start_as_current_span("couchbase", context=parent_context) as span:
try:
collect_attributes(span, instance, args[0], "n1ql_query")
return wrapped(*args, **kwargs)
Expand Down
Loading
Loading