Skip to content
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

Update Local Scope Documentation #5136

Merged
merged 5 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions src/includes/enriching-events/scopes/scope-callback-param/java.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
```java {tabTitle: Java}
import io.sentry.Sentry;
import io.sentry.SentryLevel;

// will be tagged with my-tag="my value"
Sentry.captureException(new Exception("my error"), scope -> {
scope.setTag("my-tag", "my value");
scope.setLevel(SentryLevel.WARNING);
});

// will not be tagged with my-tag
Sentry.captureException(new Exception("my error"));
```

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

// will be tagged with my-tag="my value"
Sentry.captureException(Exception("my error")) { scope ->
scope.setTag("my-tag", "my value")
scope.level = SentryLevel.WARNING
}

// will not be tagged with my-tag
Sentry.captureException(Exception("my error"))
```
69 changes: 7 additions & 62 deletions src/includes/enriching-events/scopes/with-scope/java.mdx
Original file line number Diff line number Diff line change
@@ -1,57 +1,9 @@
<PlatformSection supported={["android"]}>

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

// will be tagged with my-tag="my value"
Sentry.captureException(new Exception("my error"), scope -> {
scope.setTag("my-tag", "my value");
scope.setLevel(SentryLevel.WARNING);
});

// will not be tagged with my-tag
Sentry.captureException(new Exception("my error"));
```

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

// will be tagged with my-tag="my value"
Sentry.captureException(Exception("my error")) { scope ->
scope.setTag("my-tag", "my value")
scope.level = SentryLevel.WARNING
}

// will not be tagged with my-tag
Sentry.captureException(Exception("my error"))
```

</PlatformSection>

<PlatformSection notSupported={["android"]}>

The JavaSDK provides two alternative ways of configuring a local scope.

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

// will be tagged with my-tag="my value"
Sentry.captureException(new Exception("my error"), scope -> {
scope.setTag("my-tag", "my value");
scope.setLevel(SentryLevel.WARNING);
});

// will not be tagged with my-tag
Sentry.captureException(new Exception("my error"));
```

```java {tabTitle: Java-Alternative}
import io.sentry.Sentry;
import io.sentry.SentryLevel;

Sentry.withScope(scope -> {
scope.setTag("my-tag", "my value");
scope.setLevel(SentryLevel.WARNING);
Expand All @@ -68,20 +20,6 @@ Sentry.captureException(new Exception("my error"));
import io.sentry.Sentry
import io.sentry.SentryLevel

// will be tagged with my-tag="my value"
Sentry.captureException(Exception("my error")) { scope ->
scope.setTag("my-tag", "my value")
scope.level = SentryLevel.WARNING
}

// will not be tagged with my-tag
Sentry.captureException(Exception("my error"))
```

```kotlin {tabTitle: Kotlin-Alternative}
import io.sentry.Sentry
import io.sentry.SentryLevel

Sentry.withScope { scope ->
scope.setTag("my-tag", "my value")
scope.level = SentryLevel.WARNING
Expand All @@ -93,4 +31,11 @@ Sentry.withScope { scope ->
// will not be tagged with my-tag
Sentry.captureException(Exception("my error"))
```

<Alert level="info" title="Important">

In the Java SDK `with-scope` does **not** work reliably in `globalHubMode` as the `scope` gets pushed on the stack global to the hub. In `globalHubMode` use the callback parameter of the capture methods detailed below.

</Alert>

</PlatformSection>
34 changes: 30 additions & 4 deletions src/platforms/common/enriching-events/scopes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,17 @@ You can also apply this configuration when unsetting a user at logout:
To learn what useful information can be associated with scopes see
[the context documentation](../context/).

<PlatformSection>

## Local Scopes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Further down is an <Alert> explaining that exceptions in withScope will be ignored. If I'm not mistaken, the new local scope overloads don't behave the same way. I think they should. Can we please update the implementation to catch Exceptions as withScope does?

https://github.com/getsentry/sentry-docs/pull/5136/files#diff-1b74f2288f1f938f1b7595fe4986f66d21eb90be88c3670334e42ca2520746d1R117-R122

Copy link
Collaborator Author

@lbloder lbloder Jun 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we do have a bit of a discrepancy between the SDKs that use the callback parameter in this case: cocoa and dart don't capture exceptions that are thrown within the callback, while dotnet and java do because they invoke the callback within the try/catch of the respective capture methods

WDYT the correct behavior should be?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to clarify

Copy link
Member

@adinauer adinauer Jun 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created this getsentry/sentry-java#2123 to keep track of it. It's not blocking this PR just noticed this here and didn't wanna drop it.

We also support pushing and configuring a scope within a single call. This is typically
called `with-scope` or `push-scope` depending on the SDK, and is very helpful if
you only want to send data for one specific event. In the following example we use
called `with-scope`, `push-scope` or implemented as a function parameter on the capture methods, depending on the SDK. It's very helpful if
you only want to send data for one specific event.

<PlatformSection supported={["go", "java", "javascript", "native", "php", "python", "ruby", "rust"]}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did we arrive at this list of supported platforms? Is it all that have a file in src/includes/enriching-events/scopes/with-scope?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Native just has _This is not supported by the Native SDK._ shouldn't that simply be removed from the list?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, went through the platforms in the with-scope folder.

Removing native sounds good to me. Will have a look if that has any implications.


### Using `with-scope`

In the following example we use
`with-scope` to attach a `level` and a `tag` to only one specific error:

<PlatformContent includePath="enriching-events/scopes/with-scope" />
Expand All @@ -123,6 +127,28 @@ caught, and all errors that occur will be silently ignored and **not** reported.

</PlatformSection>

<PlatformSection supported={["apple", "dart", "dotnet", "java", "unreal"]}>

### Using Scope Callback Parameter

In the following example we use the scope callback parameter that is available for all `capture` methods to attach a `level` and a `tag` to only one specific error:

<PlatformContent includePath="enriching-events/scopes/scope-callback-param" />

Before the callback is invoked the SDK creates a clone of the current scope, and the changes
made will stay isolated within the callback function. This allows you to
more easily isolate pieces of context information to specific locations in your code or
even call `clear` to briefly remove all context information.

<Alert level="info" title="Important">

Any exceptions that occur within the callback function for configuring a local scope will not be
caught, and all errors that occur will be silently ignored and **not** reported.

</Alert>

</PlatformSection>

<PlatformSection supported={["java"]} notSupported={["android"]}>

## Kotlin Coroutines
Expand Down