Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
removing session tracking guard on hub and client (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Apr 2, 2020
1 parent 314952b commit 3ca016c
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 88 deletions.
3 changes: 3 additions & 0 deletions sentry-android-core/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@
# don't warn jetbrains annotations
-dontwarn org.jetbrains.annotations.**

# R8: Attribute Signature requires InnerClasses attribute. Check -keepattributes directive.
-keepattributes InnerClasses

##---------------End: proguard configuration for Gson ----------
12 changes: 0 additions & 12 deletions sentry-core/src/main/java/io/sentry/core/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,6 @@ public void startSession() {
.getLogger()
.log(
SentryLevel.WARNING, "Instance is disabled and this 'startSession' call is a no-op.");
} else if (!options.isEnableSessionTracking()) {
options
.getLogger()
.log(
SentryLevel.INFO,
"Session tracking is disabled and this 'startSession' call is a no-op.");
} else {
final StackItem item = this.stack.peek();
if (item != null) {
Expand All @@ -227,12 +221,6 @@ public void endSession() {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'endSession' call is a no-op.");
} else if (!options.isEnableSessionTracking()) {
options
.getLogger()
.log(
SentryLevel.INFO,
"Session tracking is disabled and this 'endSession' call is a no-op.");
} else {
final StackItem item = this.stack.peek();
if (item != null) {
Expand Down
57 changes: 27 additions & 30 deletions sentry-core/src/main/java/io/sentry/core/SentryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,41 +147,38 @@ public SentryClient(final @NotNull SentryOptions options, @Nullable Connection c
void updateSessionData(
final @NotNull SentryEvent event, final @Nullable Object hint, final @Nullable Scope scope) {
if (ApplyScopeUtils.shouldApplyScopeData(hint)) {
// safe guard
if (options.isEnableSessionTracking()) {
if (scope != null) {
scope.withSession(
session -> {
if (session != null) {
Session.State status = null;
if (event.isCrashed()) {
status = Session.State.Crashed;
}
if (scope != null) {
scope.withSession(
session -> {
if (session != null) {
Session.State status = null;
if (event.isCrashed()) {
status = Session.State.Crashed;
}

boolean crashedOrErrored = false;
if (Session.State.Crashed == status || event.isErrored()) {
crashedOrErrored = true;
}
boolean crashedOrErrored = false;
if (Session.State.Crashed == status || event.isErrored()) {
crashedOrErrored = true;
}

String userAgent = null;
if (event.getRequest() != null && event.getRequest().getHeaders() != null) {
if (event.getRequest().getHeaders().containsKey("user-agent")) {
userAgent = event.getRequest().getHeaders().get("user-agent");
}
String userAgent = null;
if (event.getRequest() != null && event.getRequest().getHeaders() != null) {
if (event.getRequest().getHeaders().containsKey("user-agent")) {
userAgent = event.getRequest().getHeaders().get("user-agent");
}
}

if (session.update(status, userAgent, crashedOrErrored)) {
// a session update hint means its gonna only flush to the disk, but not to the
// network
captureSession(session, new SessionUpdateHint());
}
} else {
options.getLogger().log(SentryLevel.INFO, "Session is null on scope.withSession");
if (session.update(status, userAgent, crashedOrErrored)) {
// a session update hint means its gonna only flush to the disk, but not to the
// network
captureSession(session, new SessionUpdateHint());
}
});
} else {
options.getLogger().log(SentryLevel.INFO, "Scope is null on client.captureEvent");
}
} else {
options.getLogger().log(SentryLevel.INFO, "Session is null on scope.withSession");
}
});
} else {
options.getLogger().log(SentryLevel.INFO, "Scope is null on client.captureEvent");
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions sentry-core/src/main/java/io/sentry/core/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -671,16 +671,16 @@ public void setAttachThreads(boolean attachThreads) {
}

/**
* Returns if the session tracking is enabled or not
* Returns if the automatic session tracking is enabled or not
*
* @return trye if enabled or false otherwise
* @return true if enabled or false otherwise
*/
public boolean isEnableSessionTracking() {
return enableSessionTracking;
}

/**
* Enable or disable the session tracking
* Enable or disable the automatic session tracking
*
* @param enableSessionTracking true if enabled or false otherwise
*/
Expand Down
25 changes: 3 additions & 22 deletions sentry-core/src/test/java/io/sentry/core/HubTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,8 @@ class HubTest {
}

@Test
fun `when captureEvent is called session tracking is enabled but no session started, it should not capture a session`() {
fun `when captureEvent is called but no session started, it should not capture a session`() {
val options = SentryOptions()
options.isEnableSessionTracking = true
options.cacheDirPath = file.absolutePath
options.dsn = "https://key@sentry.io/proj"
options.setSerializer(mock())
Expand Down Expand Up @@ -864,24 +863,9 @@ class HubTest {
verify(mockClient, never()).captureSession(any(), any())
}

@Test
fun `when startSession is called and session tracking is disabled, do nothing`() {
val options = SentryOptions()
options.cacheDirPath = file.absolutePath
options.dsn = "https://key@sentry.io/proj"
options.setSerializer(mock())
val sut = Hub(options)
val mockClient = mock<ISentryClient>()
sut.bindClient(mockClient)

sut.startSession()
verify(mockClient, never()).captureSession(any(), any())
}

@Test
fun `when startSession is called, starts a session`() {
val options = SentryOptions()
options.isEnableSessionTracking = true
options.cacheDirPath = file.absolutePath
options.dsn = "https://key@sentry.io/proj"
options.setSerializer(mock())
Expand All @@ -894,9 +878,8 @@ class HubTest {
}

@Test
fun `when startSession is called and theres a session, stops it and starts a new one`() {
fun `when startSession is called and there's a session, stops it and starts a new one`() {
val options = SentryOptions()
options.isEnableSessionTracking = true
options.cacheDirPath = file.absolutePath
options.dsn = "https://key@sentry.io/proj"
options.setSerializer(mock())
Expand Down Expand Up @@ -944,7 +927,6 @@ class HubTest {
@Test
fun `when endSession is called, end a session`() {
val options = SentryOptions()
options.isEnableSessionTracking = true
options.cacheDirPath = file.absolutePath
options.dsn = "https://key@sentry.io/proj"
options.setSerializer(mock())
Expand All @@ -959,9 +941,8 @@ class HubTest {
}

@Test
fun `when endSession is called and theres no session, do nothing`() {
fun `when endSession is called and there's no session, do nothing`() {
val options = SentryOptions()
options.isEnableSessionTracking = true
options.cacheDirPath = file.absolutePath
options.dsn = "https://key@sentry.io/proj"
options.setSerializer(mock())
Expand Down
21 changes: 2 additions & 19 deletions sentry-core/src/test/java/io/sentry/core/SentryClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,6 @@ class SentryClientTest {

@Test
fun `When event is Fatal or not handled, mark session as Crashed`() {
fixture.sentryOptions.isEnableSessionTracking = true
val scope = Scope(fixture.sentryOptions)
val session = scope.startSession().current
val event = SentryEvent().apply {
Expand All @@ -466,7 +465,6 @@ class SentryClientTest {

@Test
fun `When event is non fatal, keep level as it is`() {
fixture.sentryOptions.isEnableSessionTracking = true
val scope = Scope(fixture.sentryOptions)
val session = scope.startSession().current
val level = session.status
Expand All @@ -477,7 +475,6 @@ class SentryClientTest {

@Test
fun `When event is Fatal, increase errorCount`() {
fixture.sentryOptions.isEnableSessionTracking = true
val scope = Scope(fixture.sentryOptions)
val session = scope.startSession().current
val event = SentryEvent().apply {
Expand All @@ -489,7 +486,6 @@ class SentryClientTest {

@Test
fun `When event is Errored, increase errorCount`() {
fixture.sentryOptions.isEnableSessionTracking = true
val scope = Scope(fixture.sentryOptions)
val session = scope.startSession().current
val exceptions = mutableListOf<SentryException>()
Expand All @@ -502,8 +498,7 @@ class SentryClientTest {
}

@Test
fun `When event is non fatal or error, do not increase errorsCount`() {
fixture.sentryOptions.isEnableSessionTracking = true
fun `When event is non fatal nor errored, do not increase errorsCount`() {
val scope = Scope(fixture.sentryOptions)
val session = scope.startSession().current
val errorCount = session.errorCount()
Expand All @@ -514,7 +509,6 @@ class SentryClientTest {

@Test
fun `When event has userAgent, set it into session`() {
fixture.sentryOptions.isEnableSessionTracking = true
val scope = Scope(fixture.sentryOptions)
val session = scope.startSession().current
val event = SentryEvent().apply {
Expand All @@ -528,7 +522,6 @@ class SentryClientTest {

@Test
fun `When event has no userAgent, keep as it is`() {
fixture.sentryOptions.isEnableSessionTracking = true
val scope = Scope(fixture.sentryOptions)
val session = scope.startSession().current
val userAgent = session.userAgent
Expand All @@ -541,8 +534,7 @@ class SentryClientTest {
}

@Test
fun `When capture an event and theres no session, do nothing`() {
fixture.sentryOptions.isEnableSessionTracking = true
fun `When capture an event and there's no session, do nothing`() {
val scope = Scope(fixture.sentryOptions)
val event = SentryEvent()
fixture.getSut().updateSessionData(event, null, scope)
Expand All @@ -551,18 +543,9 @@ class SentryClientTest {
}
}

@Test
fun `When capture an event and session tracking is disabled, do nothing`() {
val scope = mock<Scope>()
val event = SentryEvent()
fixture.getSut().updateSessionData(event, null, scope)
verify(scope, never()).withSession(any())
}

@Test
fun `when captureEvent with sampling, session is still updated`() {
fixture.sentryOptions.sampleRate = 1.0
fixture.sentryOptions.isEnableSessionTracking = true
val sut = fixture.getSut()

val event = SentryEvent().apply {
Expand Down
2 changes: 1 addition & 1 deletion sentry-native-sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
id("com.ydq.android.gradle.native-aar.import")

// apply sentry android gradle plugin
// id("io.sentry.android.gradle")
// id("io.sentry.android.gradle")
}

android {
Expand Down
3 changes: 3 additions & 0 deletions sentry-native-sample/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

# R8: Attribute Signature requires InnerClasses attribute. Check -keepattributes directive.
-keepattributes InnerClasses
2 changes: 1 addition & 1 deletion sentry-sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<!-- how to set a custom release-->
<!-- <meta-data android:name="io.sentry.release" android:value="io.sentry.sample@1.0.0+10001" />-->

<!-- how to enable session tracking-->
<!-- how to enable automatic session tracking-->
<meta-data android:name="io.sentry.session-tracking.enable" android:value="true" />

<!-- how to set an environment-->
Expand Down

0 comments on commit 3ca016c

Please sign in to comment.