Repository: getsentry/sentry-python

Issue:
Setting transaction name doesn't work from child span.
### Environment

How do you use Sentry?
Sentry SaaS (sentry.io)

Which SDK and version?
Python/Django: django-sentry-1.13.5, sentry-sdk-1.3.0 (also tried sentry-sdk-1.1.0)

### Steps to Reproduce

We're using Sentry in Django and Graphene (for GraphQL support). We would like to set the transaction name based on GraphQL operation name.

```python

urlpatterns = [
    # ...snip
    path(
        'graphql',
        csrf_exempt(MyGraphQLView.as_view(graphiql=settings.GRAPHIQL_ENABLED)),
    ),
]

class MyGraphQLView(graphene_django.views.GraphQLView):
    def execute_graphql_request(
        self, request, data, query, variables, operation_name, show_graphiql=False
    ):
        with sentry_sdk.configure_scope() as scope:
            transaction_name = f'graphql.{operation_name}' # graphql.OperationName
            scope.transaction = transaction_name
            # This works:
            scope._span._containing_transaction.name = transaction_name
            return super().execute_graphql_request(
                request, data, query, variables, operation_name, show_graphiql
            )
```

From the trace view in Sentry, the GraphQLView span is nested several spans down (in Django middlewares). From my understanding of the [sentry documentation: set transaction name](https://docs.sentry.io/platforms/python/enriching-events/transaction-name/) It seems like the `scope.transaction = 'name'` setter should be enough, but doesn't seem to work from child spans.

### Expected Result

Sentry transaction name updated to `graphql.OperationName`.

### Actual Result

Sentry transaction name stays as `/graphql`




Discussion:
Thanks for reporting, we'll take a look
This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it `Status: Backlog` or `Status: In Progress`, I will leave it alone ... forever!

----

"A weed is but an unloved flower." ― _Ella Wheeler Wilcox_ 🥀
Resetting that clock...
Hi @CoatedMoose, is this still a problem in the latest SDK version?
I'm in the middle of some tasks and I can't check right now, but will hopefully be able to later in the week.
There doesn't seem to be any improvement with version `1.5.7`. The workaround from my initial report is still working.


@CoatedMoose yes you're right that the `transaction` getter and setter on `scope` have different behaviour, I'll try to understand why this is the way this is and get back to you, hopefully with a fix. Thx for reporting!

Resolving patch:
diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py
--- a/sentry_sdk/scope.py
+++ b/sentry_sdk/scope.py
@@ -173,9 +173,8 @@ def transaction(self, value):
         # transaction name or transaction (self._span) depending on the type of
         # the value argument.
         self._transaction = value
-        span = self._span
-        if span and isinstance(span, Transaction):
-            span.name = value
+        if self._span and self._span.containing_transaction:
+            self._span.containing_transaction.name = value
 
     @_attr_setter
     def user(self, value):


Test patch: