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 scopes documentation for Java and Android #4747

Merged
merged 19 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
1bb105b
Improve clarity for Local Scopes documentation
designedbyz Feb 19, 2022
2621110
Mention Kotlin Extensions in Android setup
designedbyz Feb 19, 2022
512c9e3
Cleanup sentry for android local scopes doc.
designedbyz Feb 19, 2022
4cd11c7
Hide local scopes from Android, show Kotlin
designedbyz Feb 19, 2022
85df56d
Extract scope synchronization from configure scope include
designedbyz Feb 19, 2022
52945f7
trigger build
imatwawana Feb 22, 2022
1d1fbe0
Fix accidentally excluded platform section
designedbyz Mar 1, 2022
014eaa2
Remove Local Scope information from Android docs
designedbyz Mar 8, 2022
70231d3
Only show Kotlin info for Java and Android
designedbyz Mar 8, 2022
59072db
Fix removing local scope for Android breaking a link in attachments.
designedbyz Mar 29, 2022
fb07118
Update src/platforms/common/enriching-events/scopes.mdx
marandaneto Apr 5, 2022
74f8aa8
Update src/platforms/common/enriching-events/scopes.mdx
marandaneto Apr 5, 2022
ba85cae
Update src/platforms/common/enriching-events/scopes.mdx
marandaneto Apr 5, 2022
e146473
Update src/platforms/common/enriching-events/scopes.mdx
marandaneto Apr 5, 2022
fb551f7
Update src/platforms/common/enriching-events/scopes.mdx
marandaneto Apr 5, 2022
a1d24cf
Update src/platforms/common/enriching-events/scopes.mdx
marandaneto Apr 5, 2022
e2de2e3
Update src/includes/getting-started-install/android.mdx
marandaneto Apr 5, 2022
10b558e
Remove mention of kotlin extensions dependency from Android scopes info
designedbyz Apr 26, 2022
bc7165a
Merge branch 'master' into update-scopes
marandaneto Apr 26, 2022
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
33 changes: 33 additions & 0 deletions src/includes/enriching-events/attachment-upload/android.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
```java
import io.sentry.Sentry;
import io.sentry.Attachment;

Attachment fileAttachment = new Attachment("your/path/file.log");

// Add an attachment
Sentry.configureScope(scope -> {
scope.addAttachment(fileAttachment);
});

// Clear all attachments
Sentry.configureScope(scope -> {
scope.clearAttachments();
});
```

```kotlin
import io.sentry.Sentry
import io.sentry.Attachment

val fileAttachment = Attachment("your/path/file.log")

// Add an attachment
Sentry.configureScope { scope ->
scope.addAttachment(fileAttachment)
}

// Clear all attachments
Sentry.configureScope { scope ->
scope.clearAttachments()
}
```
10 changes: 0 additions & 10 deletions src/includes/enriching-events/scopes/configure-scope/android.mdx
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
<PlatformContent includePath="enriching-events/scopes/configure-scope/java.mdx" />

### Scope Synchronization

If you want to set context data in the Java layer, then receive that context data when a native (C/C++) crash happens, you need to synchronize the Java `Scope` with the native `Scope`. This feature was introduced in version `3.0.0` of the Android SDK and requires an opt-in:

```xml {filename:AndroidManifest.xml}
<application>
<meta-data android:name="io.sentry.ndk.scope-sync.enable" android:value="true" />
</application>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Scope Synchronization

If you want to set context data in the Java layer, then receive that context data when a native (C/C++) crash happens, you need to synchronize the Java `Scope` with the native `Scope`. This feature was introduced in version `3.0.0` of the Android SDK and requires an opt-in:
marandaneto marked this conversation as resolved.
Show resolved Hide resolved

```xml {filename:AndroidManifest.xml}
<application>
<meta-data android:name="io.sentry.ndk.scope-sync.enable" android:value="true" />
</application>
```

This file was deleted.

2 changes: 2 additions & 0 deletions src/includes/getting-started-install/android.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ Sentry's Android SDK depends on [Gson](https://github.com/google/gson) as a tran

If you are using multiple Sentry dependencies, you can add a [bill of materials](/platforms/android/configuration/bill-of-materials) to avoid specifying the version of each dependency.

If you are using Kotlin and coroutines in your Android project, you should read [Using Sentry SDK with Coroutines](/platforms/android/enriching-events/scopes/#kotlin-coroutines)
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
marandaneto marked this conversation as resolved.
Show resolved Hide resolved

</Note>
10 changes: 9 additions & 1 deletion src/platforms/common/enriching-events/attachments/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,22 @@ You'll first need to import the SDK, as usual:

</PlatformSection>

<PlatformSection notSupported={["unity"]} supported={["android", "apple", "java", "dotnet", "dart", "flutter"]}>
<PlatformSection notSupported={["unity", "android"]} supported={["apple", "java", "dotnet", "dart", "flutter"]}>

Attachments live on the <PlatformLink to="/enriching-events/scopes/">Scope</PlatformLink>. You can either add an attachment on the global scope to be sent with every event or add it on the <PlatformLink to="/enriching-events/scopes/#local-scopes">local Scope</PlatformLink> to just send the attachment with one specific event.

<PlatformContent includePath="enriching-events/attachment-upload" />

</PlatformSection>

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

Attachments live on the <PlatformLink to="/enriching-events/scopes/">Scope</PlatformLink>, and will be sent with all events.

<PlatformContent includePath="enriching-events/attachment-upload" />

</PlatformSection>

<PlatformSection supported={["unity"]}>

Unity features a global <PlatformLink to="/enriching-events/scopes/">scope</PlatformLink> to which you add an attachment that will be sent with every event.
Expand Down
47 changes: 32 additions & 15 deletions src/platforms/common/enriching-events/scopes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ routes or controllers.
## How the Scope and Hub Work

As you start using an SDK, a scope and hub are automatically created for you out
of the box. You are unlikely to be interacting with the hub directly unless you
of the box. It is unlikely that you will interact with the hub directly unless you
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
are writing an integration or you want to create or destroy scopes. Scopes, on the
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
other hand are more user facing. You can at any point in time call
`configure-scope` to modify data stored on the scope. This is for instance
used to [modify the context](../context/).
other hand are more user facing. You may call `configure-scope` at any point in
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
time to to modify data stored on the scope. This is useful for doing things like
[modifying the context](../context/).

<Alert>
If you are very curious about how thread locality works: On platforms such as .NET
Expand Down Expand Up @@ -80,28 +80,44 @@ You can also apply this configuration when unsetting a user at logout:

<PlatformContent includePath="enriching-events/unset-user" />

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

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

</PlatformSection>

To learn what useful information can be associated with scopes see
[the context documentation](../context/).

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

## Local Scopes

We also have support for pushing and configuring a scope in one go. This is typically called `with-scope` or `push-scope` which is also very helpful if you only want to send data with one specific event. In the following example we are using that function to attach a `level` and a `tag` to only one specific error:
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
`with-scope` to attach a `level` and a `tag` to only one specific error:

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

While this example looks similar to `configure-scope`, it's very different, in the sense that `configure-scope` actually changes the current active scope, all successive calls to `configure-scope`
will keep the changes.
While this example looks similar to `configure-scope`, it is actually very different.
Calls to `configure-scope` change the current active scope; all successive calls
to `configure-scope` will maintain previously set changes unless they are explicitly
unset.

While on the other hand using `with-scope` creates a clone of the current scope
and will stay isolated until the function call is completed. So you can either
set context information in there that you don't want to be somewhere else or not
attach any context information at all by calling `clear` on the scope, while the
"global" scope remains unchanged.
`with-scope` on the other hand creates a clone of the current scope and the changes
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
made will stay isolated within the `with-scope` callback function. This allows you to
easily isolate pieces of context information to specific locations in your code or
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
even call `clear` to briefly remove all context information.

Keep in mind that `with-scope` will not capture any exceptions that happen inside its callback function,
and every error that happens there will be silently ignored and *not* reported.
<Alert level="info" title="Important">
Any exceptions that occur within the callback function for `with-scope` will not be
caught and all errors that occur will be silently ignored and *not* repoted.
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
</Alert>

</PlatformSection>

<PlatformSection supported={["java"]} notSupported={["android"]}>
<PlatformSection supported={["java", "android"]}>
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we need to keep android as non supported due to https://github.com/getsentry/sentry-docs/pull/4747/files#r841714836

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh! with this did you mean that you wanted to remove the mention of Kotlin as well as the local scopes stuff?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, for Android, since there's a global hub/scope, it does not make sense to use that integration, so we can remove from the android page.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, that makes sense. As a counter point though, I was confused that it was mentioned for Java and not Android, which is why I added it to begin with. What do you think about mentioning somewhere in the Android setup docs that the Kotlin extensions aren't needed for Android?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure, the content is not for Android at all, it even says:

Sentry's SDK for Java stores the scope and the context in a thread-local variable

Instead of Sentry's SDK for Android, which is the case for all the other Android integrations, I guess explaining something from another platform would also make confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure, the content is not for Android at all, it even says:

Sentry's SDK for Java stores the scope and the context in a thread-local variable

Instead of Sentry's SDK for Android, which is the case for all the other Android integrations, I guess explaining something from another platform would also make confusion.

To go back to this thread though, I was confused and thought that the Kotlin extensions lib was needed for two reasons:

  1. The first is the general tendency for java focused libs to have a lib-ktx library with extension functions focused on making the library more kotlin-like in the Kotlin world.
  2. The platforms page for Kotlin explicitly says that it can be used for Android, and mentions the Kotlin Extensions, and links to the Java page There's nothing in either the Kotlin page or the using Sentry with coroutines part of the Java documents that says the extension functions library isn't needed for Android.
    image

Thus, it's really easy for someone who's not familiar with the platform but using Kotlin on Android to poke around the docs and accidentally think that they need the Kotlin extensions library, when they do not.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree, The goal is that sentry-kotlin-extensions get expanded and add extension functions to be "Kotlin friendly", but right now it only has the SentryContext and this only makes sense for non-Android.

E.g. https://github.com/getsentry/sentry-java/pull/1886/files
This would make sense for Android too.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd leave this specific part for another PR, this will get addressed automatically when we land the 1st Kotlin function in the sentry-kotlin-extensions library for Android, which is gonna be very soon :)
Thanks for the feedback.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, The goal is that sentry-kotlin-extensions get expanded and add extension functions to be "Kotlin friendly", but right now it only has the SentryContext and this only makes sense for non-Android.

Got it, that makes sense.

E.g. https://github.com/getsentry/sentry-java/pull/1886/files
This would make sense for Android too.

Oooh, that would be handy.

I'd leave this specific part for another PR, this will get addressed automatically when we land the 1st Kotlin function in the sentry-kotlin-extensions library for Android, which is gonna be very soon :)
Thanks for the feedback.

Awesome! That sounds good to me!


## Kotlin Coroutines

Expand Down Expand Up @@ -140,4 +156,5 @@ launch(SentryContext()) {
}
}
```

</PlatformSection>
marandaneto marked this conversation as resolved.
Show resolved Hide resolved