Skip to content

Commit

Permalink
Merge pull request #205 from Countly/apm_configs
Browse files Browse the repository at this point in the history
[Android] Rework apm configs
  • Loading branch information
ArtursKadikis authored Jan 23, 2024
2 parents b443c8c + 76cd92b commit 6008078
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 35 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
## XX.X.X
* ! Minor breaking change ! Tracking of foreground and background reporting for APM is disabled by default

* Added a way to enable tracking of foreground and background reporting for APM

* Deprecated the following calls from "Apm" interface:
* "triggerBackground"
* "triggerForeground"
* Deprecated following functions from "CountlyConfig":
* "setRecordAppStartTime(recordAppStartTime)" instead use "apm.enableAppStartTimeTracking()"
* "setAppStartTimestampOverride(appStartTimestampOverride)" instead use "apm.setAppStartTimestampOverride(appStartTimestampOverride)"
* "enableManualAppLoadedTrigger()" instead use "apm.enableManualAppLoadedTrigger()"
* "enableManualForegroundBackgroundTriggerAPM()" will be deleted in the future

## 23.12.0
* Mitigated a rare issue related to handling request headers
* Added 'addSegmentationToViewWithID' method for adding segmentation to an ongoing view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,16 @@ public boolean filterCrash(String crash) {
Assert.assertTrue(config.starRatingShownAutomatically);
Assert.assertTrue(config.starRatingDisableAskingForEachAppVersion);
Assert.assertEquals(app, config.application);
Assert.assertTrue(config.recordAppStartTime);
Assert.assertTrue(config.apm.trackAppStartTime);
Assert.assertTrue(config.disableLocation);
Assert.assertEquals("CC", config.locationCountyCode);
Assert.assertEquals("city", config.locationCity);
Assert.assertEquals("loc", config.locationLocation);
Assert.assertEquals("ip", config.locationIpAddress);
Assert.assertEquals(metricOverride, config.metricOverride);
Assert.assertEquals((Long) 123l, config.appStartTimestampOverride);
Assert.assertTrue(config.appLoadedManualTrigger);
Assert.assertTrue(config.manualForegroundBackgroundTrigger);
Assert.assertEquals((Long) 123l, config.apm.appStartTimestampOverride);
Assert.assertTrue(config.apm.appLoadedManualTrigger);
Assert.assertTrue(config.apm.manualForegroundBackgroundTrigger);
Assert.assertEquals(logCallback, config.providedLogCallback);

config.setLocation("CC", "city", "loc", "ip");
Expand Down Expand Up @@ -272,16 +272,16 @@ void assertDefaultValues(CountlyConfig config, boolean includeConstructorValues)
Assert.assertFalse(config.starRatingDialogIsCancellable);
Assert.assertFalse(config.starRatingShownAutomatically);
Assert.assertFalse(config.starRatingDisableAskingForEachAppVersion);
Assert.assertFalse(config.recordAppStartTime);
Assert.assertFalse(config.apm.trackAppStartTime);
Assert.assertFalse(config.disableLocation);
Assert.assertNull(config.locationCountyCode);
Assert.assertNull(config.locationCity);
Assert.assertNull(config.locationLocation);
Assert.assertNull(config.locationIpAddress);
Assert.assertNull(config.metricOverride);
Assert.assertNull(config.appStartTimestampOverride);
Assert.assertFalse(config.appLoadedManualTrigger);
Assert.assertFalse(config.manualForegroundBackgroundTrigger);
Assert.assertNull(config.apm.appStartTimestampOverride);
Assert.assertFalse(config.apm.appLoadedManualTrigger);
Assert.assertFalse(config.apm.manualForegroundBackgroundTrigger);
Assert.assertNull(config.providedLogCallback);
}
}
69 changes: 69 additions & 0 deletions sdk/src/main/java/ly/count/android/sdk/ConfigApm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ly.count.android.sdk;

public class ConfigApm {
/**
* Whether to track the app start time
*/
protected boolean trackAppStartTime = false;
/**
* Whether to track the app foreground / background state
*/
protected boolean trackForegroundBackground = false;
/**
* Whether to track the app foreground / background state manually
*/
protected boolean manualForegroundBackgroundTrigger = false;
/**
* Whether to track the app start time manually
*/
protected boolean appLoadedManualTrigger = false;
/**
* Whether to track the app start time manually
*/
protected Long appStartTimestampOverride = null;

//we enable features

/**
* Enable the recording of the app start time
*
* @return Returns the same config object for convenient linking
*/
public synchronized ConfigApm enableAppStartTimeTracking() {
this.trackAppStartTime = true;
return this;
}

/**
* Enable the recording of the app foreground / background state
*
* @return Returns the same config object for convenient linking
*/
public synchronized ConfigApm enableForegroundBackgroundTracking() {
this.trackForegroundBackground = true;
return this;
}

//we configure features / set manual overrides

/**
* Set to manually trigger the moment when the app has finished loading
*
* @return Returns the same config object for convenient linking
*/
public synchronized ConfigApm enableManualAppLoadedTrigger() {
appLoadedManualTrigger = true;
return this;
}

/**
* Override the app start timestamp in case you have a more precise way to measure it
*
* @param appStartTimestampOverride The timestamp to use as the app start timestamp
* @return Returns the same config object for convenient linking
*/
public synchronized ConfigApm setAppStartTimestampOverride(long appStartTimestampOverride) {
this.appStartTimestampOverride = appStartTimestampOverride;
return this;
}
}
29 changes: 15 additions & 14 deletions sdk/src/main/java/ly/count/android/sdk/CountlyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ public class CountlyConfig {

protected Application application = null;

protected boolean recordAppStartTime = false;

boolean disableLocation = false;

String locationCountyCode = null;
Expand All @@ -192,12 +190,6 @@ public class CountlyConfig {

Map<String, String> metricOverride = null;

Long appStartTimestampOverride = null;

boolean appLoadedManualTrigger = false;

boolean manualForegroundBackgroundTrigger = false;

int maxRequestQueueSize = 1000;

ModuleLog.LogCallback providedLogCallback;
Expand Down Expand Up @@ -884,11 +876,12 @@ public synchronized CountlyConfig setApplication(Application application) {
/**
* Enable the recording of the app start time
*
* @param recordAppStartTime
* @param recordAppStartTime set true if you want to enable the recording of the app start time
* @return Returns the same config object for convenient linking
* @deprecated this call is deprecated, use <pre>apm.enableAppStartTracking()</pre> instead
*/
public synchronized CountlyConfig setRecordAppStartTime(boolean recordAppStartTime) {
this.recordAppStartTime = recordAppStartTime;
apm.trackAppStartTime = recordAppStartTime;
return this;
}

Expand Down Expand Up @@ -933,31 +926,34 @@ public synchronized CountlyConfig setMetricOverride(Map<String, String> provided
/**
* Override the app start timestamp in case you have a more precise way to measure it
*
* @param appStartTimestampOverride
* @param appStartTimestampOverride The timestamp to use as the app start timestamp
* @return Returns the same config object for convenient linking
* @deprecated this call is deprecated, use <pre>apm.setAppStartTimestampOverride()</pre> instead
*/
public synchronized CountlyConfig setAppStartTimestampOverride(long appStartTimestampOverride) {
this.appStartTimestampOverride = appStartTimestampOverride;
apm.setAppStartTimestampOverride(appStartTimestampOverride);
return this;
}

/**
* Set to manually trigger the moment when the app has finished loading
*
* @return Returns the same config object for convenient linking
* @deprecated this call is deprecated, use <pre>apm.enableManualAppLoadedTrigger()</pre> instead
*/
public synchronized CountlyConfig enableManualAppLoadedTrigger() {
appLoadedManualTrigger = true;
apm.enableManualAppLoadedTrigger();
return this;
}

/**
* Set this in case you want to control these triggers manually
*
* @return Returns the same config object for convenient linking
* @deprecated this call is deprecated and will be removed in the future
*/
public synchronized CountlyConfig enableManualForegroundBackgroundTriggerAPM() {
manualForegroundBackgroundTrigger = true;
apm.manualForegroundBackgroundTrigger = true;
return this;
}

Expand Down Expand Up @@ -1047,4 +1043,9 @@ protected synchronized CountlyConfig disableHealthCheck() {
healthCheckEnabled = false;
return this;
}

/**
* APM configuration interface to be used with CountlyConfig
*/
public final ConfigApm apm = new ConfigApm();
}
51 changes: 38 additions & 13 deletions sdk/src/main/java/ly/count/android/sdk/ModuleAPM.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public class ModuleAPM extends ModuleBase {

int activitiesOpen;

boolean useManualAppLoadedTrigger = false;
boolean useManualAppLoadedTrigger;
long appStartTimestamp;

boolean manualForegroundBackgroundTriggers = false;
boolean manualForegroundBackgroundTriggers;
boolean trackForegroundBackground;
boolean manualOverrideInForeground = false;//app starts in background

ModuleAPM(Countly cly, CountlyConfig config) {
Expand All @@ -39,26 +39,31 @@ public class ModuleAPM extends ModuleBase {

activitiesOpen = 0;

useManualAppLoadedTrigger = config.appLoadedManualTrigger;
if (config.appStartTimestampOverride != null) {
useManualAppLoadedTrigger = config.apm.appLoadedManualTrigger;
if (config.apm.appStartTimestampOverride != null) {
//if there is a app start time override, use it
appStartTimestamp = config.appStartTimestampOverride;
appStartTimestamp = config.apm.appStartTimestampOverride;

L.d("[ModuleAPM] Using app start timestamp override");
} else {
//otherwise use the statically generated timestamp
appStartTimestamp = Countly.applicationStart;
}

if (config.appLoadedManualTrigger) {
if (config.apm.appLoadedManualTrigger) {
L.d("[ModuleAPM] Using manual app finished loading trigger for app start");
}

manualForegroundBackgroundTriggers = config.manualForegroundBackgroundTrigger;
manualForegroundBackgroundTriggers = config.apm.manualForegroundBackgroundTrigger;
if (manualForegroundBackgroundTriggers) {
L.d("[ModuleAPM] Using manual foreground/background triggers");
}

trackForegroundBackground = config.apm.trackForegroundBackground;
if (trackForegroundBackground) {
L.d("[ModuleAPM] tracking foreground/background is enabled");
}

apmInterface = new Apm();
}

Expand Down Expand Up @@ -332,7 +337,7 @@ void clearNetworkTraces() {

void recordAppStart(long appLoadedTimestamp) {
L.d("[ModuleAPM] Calling 'recordAppStart'");
if (_cly.config_.recordAppStartTime) {
if (_cly.config_.apm.trackAppStartTime) {
long durationMs = appLoadedTimestamp - appStartTimestamp;

if (durationMs <= 0) {
Expand Down Expand Up @@ -418,7 +423,7 @@ void callbackOnActivityResumed(Activity activity) {

long currentTimestamp = System.currentTimeMillis();

if (!manualForegroundBackgroundTriggers) {
if (trackForegroundBackground && !manualForegroundBackgroundTriggers) {
calculateAppRunningTimes(activitiesOpen, activitiesOpen + 1);
}
activitiesOpen++;
Expand All @@ -440,7 +445,7 @@ void callbackOnActivityResumed(Activity activity) {
void callbackOnActivityStopped(Activity activity) {
L.d("[Apm] Calling 'callbackOnActivityStopped', [" + activitiesOpen + "] -> [" + (activitiesOpen - 1) + "]");

if (!manualForegroundBackgroundTriggers) {
if (trackForegroundBackground & !manualForegroundBackgroundTriggers) {
calculateAppRunningTimes(activitiesOpen, activitiesOpen - 1);
}
activitiesOpen--;
Expand All @@ -460,7 +465,7 @@ void onConsentChanged(@NonNull final List<String> consentChangeDelta, final bool
@Override
void initFinished(@NonNull CountlyConfig config) {
// we only do this adjustment if we track it automatically
if (!manualForegroundBackgroundTriggers && _cly.lifecycleStateAtLeastStarted()) {
if (trackForegroundBackground && !manualForegroundBackgroundTriggers && _cly.lifecycleStateAtLeastStarted()) {
L.d("[ModuleAPM] SDK detects that the app is in the foreground. Increasing the activity counter.");

calculateAppRunningTimes(activitiesOpen, activitiesOpen + 1);
Expand Down Expand Up @@ -583,10 +588,20 @@ public void setAppIsLoaded() {
}
}

/**
* Manually trigger that the app has gone to the foreground
*
* @deprecated this call is deprecated and will be removed in the future
*/
public void triggerForeground() {
synchronized (_cly) {
L.i("[Apm] Calling 'triggerForeground'");

if (!trackForegroundBackground) {
L.w("[Apm] triggerForeground, tracking foreground is disabled");
return;
}

if (!manualForegroundBackgroundTriggers) {
L.w("[Apm] trying to use manual foreground triggers without enabling them");
return;
Expand All @@ -596,12 +611,22 @@ public void triggerForeground() {
}
}

/**
* Manually trigger that the app has gone to the background
*
* @deprecated this call is deprecated and will be removed in the future
*/
public void triggerBackground() {
synchronized (_cly) {
L.i("[Apm] Calling 'triggerBackground'");

if (!trackForegroundBackground) {
L.w("[Apm] triggerBackground, tracking background is disabled");
return;
}

if (!manualForegroundBackgroundTriggers) {
L.w("[Apm] trying to use manual background triggers without enabling them");
L.w("[Apm] triggerBackground, trying to use manual background triggers without enabling them");
return;
}

Expand Down

0 comments on commit 6008078

Please sign in to comment.