Skip to content

Commit a941181

Browse files
HazATrhcarvalho
andauthored
feat: Add retrieving Transaction (#1775)
* fix: SDK specific docs * ref: Update python * Update python docs Co-authored-by: Rodolfo Carvalho <[email protected]>
1 parent ab41434 commit a941181

File tree

3 files changed

+105
-15
lines changed

3 files changed

+105
-15
lines changed

src/collections/_documentation/performance-monitoring/configuration/javascript.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,24 @@ Sentry.init({
214214
});
215215
```
216216

217+
**Retrieving a Transaction**
218+
219+
In cases where you want to attach Spans to an already ongoing Transaction you can use `Sentry.getCurrentHub().getScope().getTransaction()`. This function will return a `Transaction` in case there is a running Transaction otherwise it returns `undefined`. If you are using our Tracing integration by default we attach the Transaction to the Scope. So you could do something like this:
220+
221+
```javascript
222+
function myJsFunction() {
223+
const transaction = Sentry.getCurrentHub().getScope().getTransaction();
224+
if (transaction) {
225+
let span = transaction.startChild({
226+
op: "encode",
227+
description: "parseAvatarImages"
228+
});
229+
// Do something
230+
span.finish();
231+
}
232+
}
233+
```
234+
217235
**Adding Query Information and Parameters to Spans**
218236

219237
Currently, every tag has a maximum character limit of 200 characters. Tags over the 200 character limit will become truncated, losing potentially important information. To retain this data, you can split data over several tags instead.
@@ -237,7 +255,6 @@ span.setTag("baseUrl", baseUrl);
237255
span.setTag("endpoint", endpoint);
238256
span.setTag("parameters", parameters);
239257
http.get(`${base_url}/${endpoint}/`, data=parameters);
240-
...
241258
```
242259

243260
baseUrl

src/collections/_documentation/performance-monitoring/configuration/node.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,23 @@ app.use(function processItems(req, res, next) {
9292
})
9393
});
9494
```
95-
9695
<!-- ENDWIZARD -->
96+
97+
#### Retrieving a Transaction
98+
99+
In cases where you want to attach Spans to an already ongoing Transaction you can use `Sentry.getCurrentHub().getScope().getTransaction()`. This function will return a `Transaction` in case there is a running Transaction otherwise it returns `undefined`. If you are using our Express integration by default we attach the Transaction to the Scope. So you could do something like this:
100+
101+
```javascript
102+
app.get("/success", function successHandler(req, res) {
103+
const transaction = Sentry.getCurrentHub().getScope().getTransaction();
104+
if (transaction) {
105+
let span = transaction.startChild({
106+
op: "encode",
107+
description: "parseAvatarImages"
108+
});
109+
// Do something
110+
span.finish();
111+
}
112+
res.status(200).end();
113+
});
114+
```

src/collections/_documentation/performance-monitoring/configuration/python.md

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,36 +70,91 @@ To manually instrument certain regions of your code, you can create a transactio
7070
The following example creates a transaction for a scope that contains an expensive operation (for example, `process_item`), and sends the result to Sentry:
7171

7272
```python
73-
import sentry_sdk
73+
from sentry_sdk import start_transaction
7474

7575
while True:
7676
item = get_from_queue()
7777

78-
with sentry_sdk.start_transaction(op="task", name=item.get_transaction_name()):
78+
with start_transaction(op="task", name=item.get_transaction_name()):
7979
# process_item may create more spans internally (see next examples)
8080
process_item(item)
8181
```
8282

8383
**Adding More Spans to the Transaction**
8484

85-
The next example contains the implementation of the hypothetical `process_item` function called from the code snippet in the previous section. Our SDK can determine if there is currently an open transaction and add all newly created spans as child operations to that transaction. Keep in mind that each individual span also needs to be manually finished; otherwise, spans will not show up in the transaction.
85+
The next example contains the implementation of the hypothetical `process_item` function called from the code snippet in the previous section. Our SDK can determine if there is currently an open transaction and add all newly created spans as child operations to that transaction. Keep in mind that each individual span also needs to be manually finished; otherwise, spans will not show up in the transaction. When using spans and transactions as context managers, they are automatically finished at the end of the `with` block.
86+
87+
In cases where you want to attach Spans to an already ongoing Transaction you can use `Hub.current.scope.transaction`. This property will return a `Transaction` in case there is a running Transaction otherwise it returns `None`.
88+
89+
Alternatively, instead of adding to the top-level transaction, you can make a child span of the current span, if there is one. Use ``Hub.current.scope.span` in that case.
8690

8791
You can choose the value of `op` and `description`.
8892

8993
```python
90-
import sentry_sdk
94+
from sentry_sdk import Hub
9195

9296
def process_item(item):
97+
transaction = Hub.current.scope.transaction
98+
# omitted code...
99+
with transaction.start_child(op="http", description="GET /") as span:
100+
response = my_custom_http_library.request("GET", "/")
101+
span.set_tag("http.status_code", response.status_code)
102+
span.set_data("http.foobarsessionid", get_foobar_sessionid())
103+
```
104+
105+
The alternative to make a tree of spans:
93106

94-
# omitted code...
95-
with sentry_sdk.start_span(op="http", description="GET /") as span:
96-
response = my_custom_http_library.request("GET", "/")
97-
span.set_tag("http.status_code", response.status_code)
98-
span.set_data("http.foobarsessionid", get_foobar_sessionid())
107+
```python
108+
from sentry_sdk import Hub
109+
110+
def process_item(item):
111+
parent_span = Hub.current.scope.span
112+
# omitted code...
113+
with parent_span.start_child(op="http", description="GET /") as span:
114+
response = my_custom_http_library.request("GET", "/")
115+
span.set_tag("http.status_code", response.status_code)
116+
span.set_data("http.foobarsessionid", get_foobar_sessionid())
99117
```
100118

101119
<!-- ENDWIZARD -->
102120

121+
#### Retrieving a Transaction
122+
123+
In cases where you want to attach Spans to an already ongoing Transaction you can use `Hub.current.scope.transaction`. This property will return a `Transaction` in case there is a running Transaction otherwise it returns `None`.
124+
125+
```python
126+
from sentry_sdk import Hub
127+
128+
transaction = Hub.current.scope.transaction
129+
130+
if transaction is None:
131+
with start_transaction(name="task"):
132+
do_task()
133+
else:
134+
transaction.name = "new name"
135+
with transaction.start_child(op="task"):
136+
do_task()
137+
```
138+
139+
#### Retrieving the current Span
140+
141+
Started spans are stored in the scope, and can be fetched off the scope:
142+
143+
```python
144+
from sentry_sdk import Hub
145+
146+
span = Hub.current.scope.span
147+
148+
if span is None:
149+
# no span in progress, create new transaction
150+
with start_transaction(name="task"):
151+
do_task()
152+
else:
153+
# new task span as child of current span
154+
with span.start_child(op="task"):
155+
do_task()
156+
```
157+
103158
**Adding Query Information and Parameters to Spans**
104159

105160
Currently, every tag has a maximum character limit of 200 characters. Tags over the 200 character limit will become truncated, losing potentially important information. To retain this data, you can split data over several tags instead.
@@ -116,10 +171,10 @@ Instead, using `span.set_tag` splits the details of this query over several tags
116171
```python
117172
import sentry_sdk
118173
...
119-
with sentry_sdk.start_span(op="request", transaction="setup form") as span:
120-
span.set_tag("base_url", base_url)
121-
span.set_tag("endpoint", endpoint)
122-
span.set_tag("parameters", parameters)
174+
with sentry_sdk.start_transaction(op="request", name="setup form") as transaction:
175+
transaction.set_tag("base_url", base_url)
176+
transaction.set_tag("endpoint", endpoint)
177+
transaction.set_tag("parameters", parameters)
123178
make_request(
124179
"{base_url}/{endpoint}/".format(
125180
base_url=base_url,

0 commit comments

Comments
 (0)