Skip to content

Custom transactions being overwritten by ASGI integration #3663

Closed
@ripperdoc

Description

@ripperdoc

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.16.0

Steps to Reproduce

I'm working on some custom instrumentation. I have some code that creates a manual transaction, like this:

scope = sentry_sdk.get_current_scope()
transaction = scope.start_transaction(
    name="ttr"
    op="function",
    start_timestamp=<timestamp>,
)
scope.span = transaction

Sentry is setup like this and uses FastAPI automatic instrumentation.

    sentry_sdk.init(
        dsn=dsn,
        debug=True,
        sample_rate=or_default(default_config.sentry_error_sample_rate, 1.0),
        enable_tracing=True,
        traces_sample_rate=tracing_rate,
        environment=os.getenv("SENTRY_ENVIRONMENT"),
        release=version,
    )

Expected Result

In Sentry.io I keep seeing a different transaction called /sessions/{session_id} which corresponds to a websocket endpoint we have, instead of seeing ttr.

Actual Result

I debugged this and tracked it down to this code in asgi.py

def event_processor(self, event, hint, asgi_scope):
        # type: (Event, Hint, Any) -> Optional[Event]
        request_data = event.get("request", {})
        request_data.update(_get_request_data(asgi_scope))
        event["request"] = deepcopy(request_data)

        # Only set transaction name if not already set by Starlette or FastAPI (or other frameworks)
        already_set = event["transaction"] != _DEFAULT_TRANSACTION_NAME and event[
            "transaction_info"
        ].get("source") in [
            TRANSACTION_SOURCE_COMPONENT,
            TRANSACTION_SOURCE_ROUTE,
        ]
        if not already_set:
            name, source = self._get_transaction_name_and_source(
                self.transaction_style, asgi_scope
            )
            event["transaction"] = name
            event["transaction_info"] = {"source": source}

            logger.debug(
                "[ASGI] Set transaction name and source in event_processor: '%s' / '%s'",
                event["transaction"],
                event["transaction_info"]["source"],
            )

        return event

The incoming ttr transaction has event["transaction_info"]["source"] = "custom" which means already_set = False as it's not considered a valid source here and thereby the transaction name is overwritten.

I would expect that my custom transaction is not modified.

(on a separate note, the docs don't mention that I need to set my transaction on the current scope manually, I first thought it would happen when I call start_transaction on the scope)

Metadata

Metadata

Assignees

Type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions