Skip to content

feat: Add retrieving Transaction #1775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,24 @@ Sentry.init({
});
```

**Retrieving a Transaction**

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:

```javascript
function myJsFunction() {
const transaction = Sentry.getCurrentHub().getScope().getTransaction();
if (transaction) {
let span = transaction.startChild({
op: "encode",
description: "parseAvatarImages"
});
// Do something
span.finish();
}
}
```

**Adding Query Information and Parameters to Spans**

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.
Expand All @@ -237,7 +255,6 @@ span.setTag("baseUrl", baseUrl);
span.setTag("endpoint", endpoint);
span.setTag("parameters", parameters);
http.get(`${base_url}/${endpoint}/`, data=parameters);
...
```

baseUrl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,23 @@ app.use(function processItems(req, res, next) {
})
});
```

<!-- ENDWIZARD -->

#### Retrieving a Transaction

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:

```javascript
app.get("/success", function successHandler(req, res) {
const transaction = Sentry.getCurrentHub().getScope().getTransaction();
if (transaction) {
let span = transaction.startChild({
op: "encode",
description: "parseAvatarImages"
});
// Do something
span.finish();
}
res.status(200).end();
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ import sentry_sdk
while True:
item = get_from_queue()

with sentry_sdk.start_span(op="task", transaction=item.get_transaction()):
with sentry_sdk.start_transaction(op="task", name=item.get_transaction_name()) as transaction:
# process_item may create more spans internally (see next examples)
process_item(item)
process_item(item, transaction)
```

**Adding More Spans to the Transaction**
Expand All @@ -89,17 +89,31 @@ You can choose the value of `op` and `description`.
```python
import sentry_sdk

def process_item(item):
def process_item(item, transaction):

# omitted code...
with sentry_sdk.start_span(op="http", description="GET /") as span:
with transaction.start_child(op="http", description="GET /") as span:
response = my_custom_http_library.request("GET", "/")
span.set_tag("http.status_code", response.status_code)
span.set_data("http.foobarsessionid", get_foobar_sessionid())
```

<!-- ENDWIZARD -->


#### Retrieving a Transaction

// TODO

In cases where you want to attach Spans to an already ongoing Transaction you can use `Hub.current.scope.transaction`. This function will return a `Transaction` in case there is a running Transaction otherwise it returns `null`.

```python
from sentry_sdk import Hub
transaction = Hub.current.scope.transaction
```

// TODO

**Adding Query Information and Parameters to Spans**

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.
Expand All @@ -116,10 +130,10 @@ Instead, using `span.set_tag` splits the details of this query over several tags
```python
import sentry_sdk
...
with sentry_sdk.start_span(op="request", transaction="setup form") as span:
span.set_tag("base_url", base_url)
span.set_tag("endpoint", endpoint)
span.set_tag("parameters", parameters)
with sentry_sdk.start_transaction(op="request", name="setup form") as transaction:
transaction.set_tag("base_url", base_url)
transaction.set_tag("endpoint", endpoint)
transaction.set_tag("parameters", parameters)
make_request(
"{base_url}/{endpoint}/".format(
base_url=base_url,
Expand Down