Skip to content

Commit 9a2e6a5

Browse files
authored
[Feat][SDK-574] Automatic navigation telemetry events (#361)
* feat: add flag to capture navigation events in AndroidConfiguration * doc: update javadoc for TelemetryEventTracker * doc: update javadoc for RollbarTelemetryEventTracker * feat: Add TelemetryNavigationCallbacks implementation * feat: init automatic capture of navigation telemetry events * ci: fix lint * ci: fix lint
1 parent 3a4ccb8 commit 9a2e6a5

6 files changed

Lines changed: 120 additions & 1 deletion

File tree

rollbar-android/src/main/java/com/rollbar/android/AndroidConfiguration.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@
44

55
public class AndroidConfiguration {
66
private final AnrConfiguration anrConfiguration;
7+
private final boolean mustCaptureNavigationEvents;
78

89
AndroidConfiguration(Builder builder) {
910
anrConfiguration = builder.anrConfiguration;
11+
mustCaptureNavigationEvents = builder.mustCaptureNavigationEvents;
1012
}
1113

1214
public AnrConfiguration getAnrConfiguration() {
1315
return anrConfiguration;
1416
}
1517

18+
public boolean mustCaptureNavigationEvents() {
19+
return mustCaptureNavigationEvents;
20+
}
21+
1622

1723
public static final class Builder {
1824
private AnrConfiguration anrConfiguration;
25+
private boolean mustCaptureNavigationEvents = true;
1926

2027
public Builder() {
2128
anrConfiguration = new AnrConfiguration.Builder().build();
@@ -31,6 +38,17 @@ public Builder setAnrConfiguration(AnrConfiguration anrConfiguration) {
3138
return this;
3239
}
3340

41+
/**
42+
* Enable or disable automatic capture of Telemetry Navigation events (only over new Activities).
43+
* Default is enabled.
44+
* @param mustCaptureNavigationEvents if automatic capture must be enabled or disabled.
45+
* @return the builder instance
46+
*/
47+
public Builder captureNewActivityTelemetryEvents(boolean mustCaptureNavigationEvents) {
48+
this.mustCaptureNavigationEvents = mustCaptureNavigationEvents;
49+
return this;
50+
}
51+
3452
public AndroidConfiguration build() {
3553
return new AndroidConfiguration(this);
3654
}

rollbar-android/src/main/java/com/rollbar/android/Rollbar.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.rollbar.android.util.Constants.ROLLBAR_NAMESPACE;
44

5+
import android.app.Application;
56
import android.content.Context;
67
import android.content.pm.ApplicationInfo;
78
import android.content.pm.PackageManager;
@@ -19,6 +20,7 @@
1920
import com.rollbar.api.payload.data.TelemetryType;
2021
import com.rollbar.api.payload.data.body.RollbarThread;
2122
import com.rollbar.notifier.config.ConfigProvider;
23+
import com.rollbar.notifier.telemetry.TelemetryEventTracker;
2224
import com.rollbar.notifier.uncaughtexception.RollbarUncaughtExceptionHandler;
2325
import com.rollbar.android.provider.NotifierProvider;
2426
import com.rollbar.android.provider.PersonProvider;
@@ -233,6 +235,7 @@ public static Rollbar init(
233235
suspendWhenNetworkIsUnavailable);
234236
if (androidConfiguration != null) {
235237
initAnrDetector(context, androidConfiguration);
238+
initAutomaticCaptureOfNavigationTelemetryEvents(context, androidConfiguration);
236239
}
237240
}
238241

@@ -273,6 +276,7 @@ public static Rollbar init(Context context, ConfigProvider provider) {
273276
notifier = new Rollbar(context, null, null, true, false, provider);
274277
AndroidConfiguration androidConfiguration = makeDefaultAndroidConfiguration();
275278
initAnrDetector(context, androidConfiguration);
279+
initAutomaticCaptureOfNavigationTelemetryEvents(context, androidConfiguration);
276280
}
277281
return notifier;
278282
}
@@ -1173,6 +1177,31 @@ public void run() {
11731177
});
11741178
}
11751179

1180+
private static void initAutomaticCaptureOfNavigationTelemetryEvents(
1181+
Context context,
1182+
AndroidConfiguration androidConfiguration
1183+
) {
1184+
if (!androidConfiguration.mustCaptureNavigationEvents()) {
1185+
return;
1186+
}
1187+
1188+
com.rollbar.notifier.Rollbar rollbarNotifier = notifier.rollbar;
1189+
if (rollbarNotifier == null) {
1190+
return;
1191+
}
1192+
1193+
TelemetryEventTracker telemetryEventTracker = rollbarNotifier.getTelemetryEventTracker();
1194+
if (telemetryEventTracker == null) {
1195+
return;
1196+
}
1197+
1198+
Context appContext = context.getApplicationContext();
1199+
if (appContext instanceof Application) {
1200+
Application application = (Application) appContext;
1201+
application.registerActivityLifecycleCallbacks(new TelemetryNavigationCallbacks(telemetryEventTracker));
1202+
}
1203+
}
1204+
11761205
private String loadAccessTokenFromManifest(Context context) throws NameNotFoundException {
11771206
Context appContext = context.getApplicationContext();
11781207
ApplicationInfo ai = appContext.getPackageManager().getApplicationInfo(appContext.getPackageName(), PackageManager.GET_META_DATA);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.rollbar.android;
2+
3+
import android.app.Activity;
4+
import android.app.Application;
5+
import android.os.Bundle;
6+
7+
import com.rollbar.api.payload.data.Level;
8+
import com.rollbar.api.payload.data.Source;
9+
import com.rollbar.notifier.telemetry.TelemetryEventTracker;
10+
11+
class TelemetryNavigationCallbacks implements Application.ActivityLifecycleCallbacks {
12+
private final TelemetryEventTracker telemetryEventTracker;
13+
private String lastActivity;
14+
15+
TelemetryNavigationCallbacks(TelemetryEventTracker telemetryEventTracker) {
16+
this.telemetryEventTracker = telemetryEventTracker;
17+
}
18+
19+
@Override
20+
public void onActivityResumed(Activity activity) {
21+
String current = activity.getClass().getSimpleName();
22+
23+
if (lastActivity != null && !lastActivity.equals(current)) {
24+
telemetryEventTracker.recordNavigationEventFor(
25+
Level.INFO,
26+
Source.CLIENT,
27+
lastActivity,
28+
current
29+
);
30+
}
31+
32+
lastActivity = current;
33+
}
34+
35+
@Override
36+
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
37+
38+
@Override
39+
public void onActivityStarted(Activity activity) {}
40+
41+
@Override
42+
public void onActivityPaused(Activity activity) {}
43+
44+
@Override
45+
public void onActivityStopped(Activity activity) {}
46+
47+
@Override
48+
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
49+
50+
@Override
51+
public void onActivityDestroyed(Activity activity) {}
52+
}

rollbar-java/src/main/java/com/rollbar/notifier/RollbarBase.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ public void recordNetworkEventFor(Level level, String method, String url, String
103103
telemetryEventTracker.recordNetworkEventFor(level, getSource(), method, url, statusCode);
104104
}
105105

106+
/**
107+
* Gives the current TelemetryEventTracker.
108+
* @return the {@link TelemetryEventTracker}.
109+
*/
110+
public TelemetryEventTracker getTelemetryEventTracker() {
111+
return telemetryEventTracker;
112+
}
113+
106114
/**
107115
* Replace the configuration of this instance directly.
108116
*

rollbar-java/src/main/java/com/rollbar/notifier/telemetry/RollbarTelemetryEventTracker.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
import java.util.concurrent.ConcurrentLinkedQueue;
1515

1616
/**
17-
* Default {@link TelemetryEventTracker}.
17+
* Default implementation of {@link TelemetryEventTracker}.
18+
*
19+
* <p>This tracker records telemetry events in memory using a bounded, thread-safe
20+
* buffer. When the configured maximum capacity is reached, the oldest events
21+
* are discarded to make room for new ones.
22+
*
23+
* <p>Recorded events are returned and cleared when {@link #dump()} is called.
1824
*/
1925
public class RollbarTelemetryEventTracker implements TelemetryEventTracker {
2026
public static final int MAXIMUM_CAPACITY_FOR_TELEMETRY_EVENTS = 100;

rollbar-java/src/main/java/com/rollbar/notifier/telemetry/TelemetryEventTracker.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
import java.util.List;
99

10+
/**
11+
* Collects and manages telemetry events that provide additional runtime
12+
* context for error and message payloads.
13+
* Telemetry events are typically buffered and cleared after they are
14+
* {@link #dump() dumped}.
15+
*/
1016
public interface TelemetryEventTracker {
1117

1218
/**

0 commit comments

Comments
 (0)