Skip to content
Merged
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
42 changes: 26 additions & 16 deletions src/instana/instrumentation/django/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,19 @@ def load_middleware_wrapper(
if DJ_INSTANA_MIDDLEWARE in settings.MIDDLEWARE:
return wrapped(*args, **kwargs)

if isinstance(settings.MIDDLEWARE, tuple):
settings.MIDDLEWARE = (DJ_INSTANA_MIDDLEWARE,) + settings.MIDDLEWARE
elif isinstance(settings.MIDDLEWARE, list):
settings.MIDDLEWARE = [DJ_INSTANA_MIDDLEWARE] + settings.MIDDLEWARE
else:
logger.warning("Instana: Couldn't add InstanaMiddleware to Django")
match settings.MIDDLEWARE:
case tuple():
settings.MIDDLEWARE = (
DJ_INSTANA_MIDDLEWARE,
) + settings.MIDDLEWARE
case list():
settings.MIDDLEWARE = [
DJ_INSTANA_MIDDLEWARE
] + settings.MIDDLEWARE
case _:
logger.warning(
"Instana: Couldn't add InstanaMiddleware to Django"
)

elif (
hasattr(settings, "MIDDLEWARE_CLASSES")
Expand All @@ -216,16 +223,19 @@ def load_middleware_wrapper(
if DJ_INSTANA_MIDDLEWARE in settings.MIDDLEWARE_CLASSES:
return wrapped(*args, **kwargs)

if isinstance(settings.MIDDLEWARE_CLASSES, tuple):
settings.MIDDLEWARE_CLASSES = (
DJ_INSTANA_MIDDLEWARE,
) + settings.MIDDLEWARE_CLASSES
elif isinstance(settings.MIDDLEWARE_CLASSES, list):
settings.MIDDLEWARE_CLASSES = [
DJ_INSTANA_MIDDLEWARE
] + settings.MIDDLEWARE_CLASSES
else:
logger.warning("Instana: Couldn't add InstanaMiddleware to Django")
match settings.MIDDLEWARE_CLASSES:
case tuple():
settings.MIDDLEWARE_CLASSES = (
DJ_INSTANA_MIDDLEWARE,
) + settings.MIDDLEWARE_CLASSES
case list():
settings.MIDDLEWARE_CLASSES = [
DJ_INSTANA_MIDDLEWARE
] + settings.MIDDLEWARE_CLASSES
case _:
logger.warning(
"Instana: Couldn't add InstanaMiddleware to Django"
)

else: # pragma: no cover
logger.warning("Instana: Couldn't find middleware settings")
Expand Down
17 changes: 9 additions & 8 deletions src/instana/instrumentation/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ def init_with_instana(
kwargs: Dict[str, Any],
) -> None:
middleware = kwargs.get("middleware")
if middleware is None:
kwargs["middleware"] = [Middleware(InstanaASGIMiddleware)]
elif isinstance(middleware, list):
middleware.append(Middleware(InstanaASGIMiddleware))
elif isinstance(middleware, tuple):
kwargs["middleware"] = (*middleware, Middleware(InstanaASGIMiddleware))
else:
logger.warning("Unsupported FastAPI middleware sequence type.")
match middleware:
case None:
kwargs["middleware"] = [Middleware(InstanaASGIMiddleware)]
case list():
middleware.append(Middleware(InstanaASGIMiddleware))
case tuple():
kwargs["middleware"] = (*middleware, Middleware(InstanaASGIMiddleware))
case _:
logger.warning("Unsupported FastAPI middleware sequence type.")

exception_handlers = kwargs.get("exception_handlers")
if exception_handlers is None:
Expand Down
9 changes: 5 additions & 4 deletions src/instana/instrumentation/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ def init_with_instana(
kwargs: Dict[str, Any],
) -> None:
middleware = kwargs.get("middleware")
if middleware is None:
kwargs["middleware"] = [Middleware(InstanaASGIMiddleware)]
elif isinstance(middleware, list):
middleware.append(Middleware(InstanaASGIMiddleware))
match middleware:
case None:
kwargs["middleware"] = [Middleware(InstanaASGIMiddleware)]
case list():
middleware.append(Middleware(InstanaASGIMiddleware))

return wrapped(*args, **kwargs)

Expand Down
222 changes: 98 additions & 124 deletions src/instana/span/registered_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,25 @@ def __init__(
)

def _populate_entry_span_data(self, span: "InstanaSpan") -> None:
if span.name in HTTP_SPANS:
self._collect_http_attributes(span)

elif span.name == "aioamqp-consumer":
self._collect_aioamqp_attributes(span)

elif span.name == "aws.lambda.entry":
self._collect_lambda_attributes(span)

elif span.name == "celery-worker":
self._collect_celery_attributes(span)

elif span.name == "gcps-consumer":
self._collect_gcps_consumer_attributes(span)

elif span.name == "rabbitmq":
self._collect_rabbitmq_attributes(span)

elif span.name == "rpc-server":
self._collect_rpc_attributes(span)

elif span.name.startswith("kafka"):
self._collect_kafka_attributes(span)

else:
logger.debug(f"SpanRecorder: Unknown entry span: {span.name}")
match span.name:
case name if name in HTTP_SPANS:
self._collect_http_attributes(span)
case "aioamqp-consumer":
self._collect_aioamqp_attributes(span)
case "aws.lambda.entry":
self._collect_lambda_attributes(span)
case "celery-worker":
self._collect_celery_attributes(span)
case "gcps-consumer":
self._collect_gcps_consumer_attributes(span)
case "rabbitmq":
self._collect_rabbitmq_attributes(span)
case "rpc-server":
self._collect_rpc_attributes(span)
case name if name.startswith("kafka"):
self._collect_kafka_attributes(span)
case _:
logger.debug(f"SpanRecorder: Unknown entry span: {span.name}")

def _populate_local_span_data(self, span: "InstanaSpan") -> None:
if span.name == "render":
Expand All @@ -105,71 +98,51 @@ def _populate_local_span_data(self, span: "InstanaSpan") -> None:
logger.debug(f"SpanRecorder: Unknown local span: {span.name}")

def _populate_exit_span_data(self, span: "InstanaSpan") -> None:
if span.name in HTTP_SPANS:
self._collect_http_attributes(span)

elif span.name == "aioamqp-publisher":
self._collect_aioamqp_attributes(span)

elif span.name == "boto3":
self._collect_boto3_attributes(span)

elif span.name == "cassandra":
self._collect_cassandra_attributes(span)

elif span.name == "celery-client":
self._collect_celery_attributes(span)

elif span.name == "couchbase":
self._collect_couchbase_attributes(span)

elif span.name == "dynamodb":
self._collect_dynamodb_attributes(span)

elif span.name == "elasticsearch":
self._collect_elasticsearch_attributes(span)

elif span.name == "rabbitmq":
self._collect_rabbitmq_attributes(span)

elif span.name == "redis":
self._collect_redis_attributes(span)

elif span.name == "rpc-client":
self._collect_rpc_attributes(span)

elif span.name == "s3":
self._collect_s3_attributes(span)

elif span.name == "sqlalchemy":
self._collect_sqlalchemy_attributes(span)

elif span.name == "mysql":
self._collect_mysql_attributes(span)

elif span.name == "mssql":
self._collect_mssql_attributes(span)

elif span.name == "postgres":
self._collect_postgres_attributes(span)

elif span.name == "mongo":
self._collect_mongo_attributes(span)

elif span.name == "gcs":
self._collect_gcs_attributes(span)

elif span.name == "gcps-producer":
self._collect_gcps_producer_attributes(span)

elif span.name == "log":
self._collect_log_attributes(span)

elif span.name.startswith("kafka"):
self._collect_kafka_attributes(span)

else:
logger.debug(f"SpanRecorder: Unknown exit span: {span.name}")
match span.name:
case name if name in HTTP_SPANS:
self._collect_http_attributes(span)
case "aioamqp-publisher":
self._collect_aioamqp_attributes(span)
case "boto3":
self._collect_boto3_attributes(span)
case "cassandra":
self._collect_cassandra_attributes(span)
case "celery-client":
self._collect_celery_attributes(span)
case "couchbase":
self._collect_couchbase_attributes(span)
case "dynamodb":
self._collect_dynamodb_attributes(span)
case "elasticsearch":
self._collect_elasticsearch_attributes(span)
case "rabbitmq":
self._collect_rabbitmq_attributes(span)
case "redis":
self._collect_redis_attributes(span)
case "rpc-client":
self._collect_rpc_attributes(span)
case "s3":
self._collect_s3_attributes(span)
case "sqlalchemy":
self._collect_sqlalchemy_attributes(span)
case "mysql":
self._collect_mysql_attributes(span)
case "mssql":
self._collect_mssql_attributes(span)
case "postgres":
self._collect_postgres_attributes(span)
case "mongo":
self._collect_mongo_attributes(span)
case "gcs":
self._collect_gcs_attributes(span)
case "gcps-producer":
self._collect_gcps_producer_attributes(span)
case "log":
self._collect_log_attributes(span)
case name if name.startswith("kafka"):
self._collect_kafka_attributes(span)
case _:
logger.debug(f"SpanRecorder: Unknown exit span: {span.name}")

def _collect_http_attributes(self, span: "InstanaSpan") -> None:
self.data["http"]["host"] = span.attributes.pop("http.host", None)
Expand Down Expand Up @@ -446,39 +419,40 @@ def _collect_lambda_attributes(self, span: "InstanaSpan") -> None:

trigger_type = self.data["lambda"]["trigger"]

if trigger_type in ["aws:api.gateway", "aws:application.load.balancer"]:
self._collect_http_attributes(span)
elif trigger_type == "aws:cloudwatch.events":
self.data["lambda"]["cw"]["events"]["id"] = span.attributes.pop(
"data.lambda.cw.events.id", None
)
self.data["lambda"]["cw"]["events"]["more"] = span.attributes.pop(
"lambda.cw.events.more", False
)
self.data["lambda"]["cw"]["events"]["resources"] = span.attributes.pop(
"lambda.cw.events.resources", None
)
elif trigger_type == "aws:cloudwatch.logs":
self.data["lambda"]["cw"]["logs"]["group"] = span.attributes.pop(
"lambda.cw.logs.group", None
)
self.data["lambda"]["cw"]["logs"]["stream"] = span.attributes.pop(
"lambda.cw.logs.stream", None
)
self.data["lambda"]["cw"]["logs"]["more"] = span.attributes.pop(
"lambda.cw.logs.more", None
)
self.data["lambda"]["cw"]["logs"]["events"] = span.attributes.pop(
"lambda.cw.logs.events", None
)
elif trigger_type == "aws:s3":
self.data["lambda"]["s3"]["events"] = span.attributes.pop(
"lambda.s3.events", None
)
elif trigger_type == "aws:sqs":
self.data["lambda"]["sqs"]["messages"] = span.attributes.pop(
"lambda.sqs.messages", None
)
match trigger_type:
case "aws:api.gateway" | "aws:application.load.balancer":
self._collect_http_attributes(span)
case "aws:cloudwatch.events":
self.data["lambda"]["cw"]["events"]["id"] = span.attributes.pop(
"data.lambda.cw.events.id", None
)
self.data["lambda"]["cw"]["events"]["more"] = span.attributes.pop(
"lambda.cw.events.more", False
)
self.data["lambda"]["cw"]["events"]["resources"] = span.attributes.pop(
"lambda.cw.events.resources", None
)
case "aws:cloudwatch.logs":
self.data["lambda"]["cw"]["logs"]["group"] = span.attributes.pop(
"lambda.cw.logs.group", None
)
self.data["lambda"]["cw"]["logs"]["stream"] = span.attributes.pop(
"lambda.cw.logs.stream", None
)
self.data["lambda"]["cw"]["logs"]["more"] = span.attributes.pop(
"lambda.cw.logs.more", None
)
self.data["lambda"]["cw"]["logs"]["events"] = span.attributes.pop(
"lambda.cw.logs.events", None
)
case "aws:s3":
self.data["lambda"]["s3"]["events"] = span.attributes.pop(
"lambda.s3.events", None
)
case "aws:sqs":
self.data["lambda"]["sqs"]["messages"] = span.attributes.pop(
"lambda.sqs.messages", None
)

def _collect_log_attributes(self, span: "InstanaSpan") -> None:
# use last special key values
Expand Down
51 changes: 26 additions & 25 deletions src/instana/span/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,31 +154,32 @@ def record_exception(
else:
message = repr(exception)

if self.name in ["rpc-server", "rpc-client"]:
self.set_attribute("rpc.error", message)
elif self.name == "mysql":
self.set_attribute("mysql.error", message)
elif self.name == "postgres":
self.set_attribute("pg.error", message)
elif self.name in HTTP_SPANS:
self.set_attribute("http.error", message)
elif self.name in ["celery-client", "celery-worker"]:
self.set_attribute("error", message)
elif self.name == "sqlalchemy":
self.set_attribute("sqlalchemy.err", message)
elif self.name == "aws.lambda.entry":
self.set_attribute("lambda.error", message)
elif self.name.startswith("kafka"):
self.set_attribute("kafka.error", message)
elif self.name == "mssql":
self.set_attribute("mssql.error", message)
else:
_attributes = {"message": message}
if attributes:
_attributes.update(attributes)
self.add_event(
name="exception", attributes=_attributes, timestamp=timestamp
)
match self.name:
case "rpc-server" | "rpc-client":
self.set_attribute("rpc.error", message)
case "mysql":
self.set_attribute("mysql.error", message)
case "postgres":
self.set_attribute("pg.error", message)
case name if name in HTTP_SPANS:
self.set_attribute("http.error", message)
case "celery-client" | "celery-worker":
self.set_attribute("error", message)
case "sqlalchemy":
self.set_attribute("sqlalchemy.err", message)
case "aws.lambda.entry":
self.set_attribute("lambda.error", message)
case name if name.startswith("kafka"):
self.set_attribute("kafka.error", message)
case "mssql":
self.set_attribute("mssql.error", message)
case _:
_attributes = {"message": message}
if attributes:
_attributes.update(attributes)
self.add_event(
name="exception", attributes=_attributes, timestamp=timestamp
)
except Exception:
logger.debug("span.record_exception", exc_info=True)
raise
Expand Down
Loading
Loading