Skip to content

Java: how to set a transaction to the current Scope. #3025

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 1 commit into from
Feb 19, 2021
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
55 changes: 55 additions & 0 deletions src/includes/performance/set-transaction-current-scope/java.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## Set Transaction to Current Scope

To avoid passing `ITransaction` objects to method calls when creating child spans, you can bind the transaction to the current scope using `Sentry#configureScope` and `Scope#setTransaction` methods:

```java {tabTitle:Java}
import io.sentry.ISpan;
import io.sentry.ITransaction;
import io.sentry.Sentry;

void executeTask() {
ITransaction transaction = Sentry.startTransaction("task");
Sentry.configureScope(scope -> scope.setTransaction(transaction));

executeSubTask();

transaction.finish();
}

private void executeSubTask() {
ISpan span = Sentry.getSpan();
if (span == null) {
span = Sentry.startTransaction("sub-task");
} else {
span = span.startChild();
}
// ...
span.finish();
}
```

```kotlin {tabTitle:Kotlin}
import io.sentry.ISpan
import io.sentry.ITransaction
import io.sentry.Sentry

fun executeTask() {
val transaction = Sentry.startTransaction("task")
Sentry.configureScope(scope -> scope.setTransaction(transaction))

executeSubTask()

transaction.finish()
}

private fun executeSubTask() {
var span = Sentry.getSpan()
span = if (span == null) {
Sentry.startTransaction("sub-task")
} else {
span.startChild()
}
// ...
span.finish()
}
```
2 changes: 2 additions & 0 deletions src/platforms/common/performance/custom-instrumentation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ To instrument certain regions of your code, you can create transactions to captu

<PlatformContent includePath="performance/connect-errors-spans" />

<PlatformContent includePath="performance/set-transaction-current-scope" />

</PlatformSection>