From 1d1c1760298a783a1b615b2b3169f8c3e1cccab5 Mon Sep 17 00:00:00 2001 From: turtledreams <62231246+turtledreams@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:46:24 +0900 Subject: [PATCH] HC log methods --- CHANGELOG.md | 6 + .../main/java/ly/count/sdk/java/Config.java | 20 + .../main/java/ly/count/sdk/java/Countly.java | 104 +-- .../sdk/java/internal/DefaultNetworking.java | 12 +- .../ly/count/sdk/java/internal/EventImpl.java | 15 +- .../sdk/java/internal/HealthCheckCounter.java | 492 ++++++++++++ .../java/internal/ImmediateRequestMaker.java | 8 +- .../sdk/java/internal/InternalConfig.java | 9 + .../sdk/java/internal/JsonFileStorage.java | 2 + .../java/ly/count/sdk/java/internal/Log.java | 220 ++++++ .../sdk/java/internal/ModuleCrashes.java | 52 +- .../sdk/java/internal/ModuleDeviceIdCore.java | 6 +- .../count/sdk/java/internal/ModuleEvents.java | 35 +- .../sdk/java/internal/ModuleHealthCheck.java | 194 +++++ .../sdk/java/internal/ModuleRequests.java | 4 +- .../sdk/java/internal/ModuleSessions.java | 9 +- .../count/sdk/java/internal/ModuleViews.java | 36 +- .../ly/count/sdk/java/internal/SDKCore.java | 42 +- .../count/sdk/java/internal/SDKStorage.java | 44 +- .../count/sdk/java/internal/SessionImpl.java | 93 ++- .../sdk/java/internal/StorageProvider.java | 27 + .../ly/count/sdk/java/internal/Transport.java | 20 +- .../ly/count/sdk/java/internal/Utils.java | 1 + .../java/internal/UtilsInternalLimits.java | 5 + .../internal/ModuleConnectionTestTests.java | 3 +- .../java/internal/ModuleHealthCheckTests.java | 712 ++++++++++++++++++ .../ly/count/sdk/java/internal/TestUtils.java | 3 + 27 files changed, 2061 insertions(+), 113 deletions(-) create mode 100644 sdk-java/src/main/java/ly/count/sdk/java/internal/HealthCheckCounter.java create mode 100644 sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleHealthCheck.java create mode 100644 sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleHealthCheckTests.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f6f15d0f..e5d92d364 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ Before initialization, while the consent of a feature is not given, or when a feature is not enabled, they return an interface that ignores calls until the feature runs and passes an error to the callbacks of the calls it ignores. An interface you keep sends its calls to the feature once it runs. Use "Countly.isTracking" to check whether a feature runs. +* Added the health check feature. The SDK now sends a health check request once after each initialization, except in backend mode. * Added support for SDK behavior settings that let the server control the SDK: * Switches for tracking, networking, sessions, views, custom events, crash reporting, automatic crash reporting, location, and console logging * The event queue size, request queue size, session update interval, and settings refresh interval @@ -40,6 +41,9 @@ * Added support for server requested SDK log gathering. When the server asks for it, the SDK uploads its own internal log lines, even if logging is disabled. While consent is required, a line is uploaded only while the consent of the feature it belongs to is given. * Added support for the SDK connection test. When the server asks for it, the SDK checks that it can reach every endpoint it depends on and reports the results. * Added "setMaxKeyLength(int)" init config method to limit the length of event keys, view names, segmentation keys and custom user property keys. Longer keys are truncated. There is no limit by default, and the key length limit of the SDK behavior settings overrides it. +* Added "disableHealthCheck()" init config method to turn off the health check request. +* The deprecated "event", "timedEvent", "view" and "addCrashReport" calls of the session interface, and "record" and "endAndRecord" of a legacy event, now log a warning that names the call to use instead. +* Failed requests are now logged at debug level, and wrong uses of the session, event, view and crash methods at warning level instead of error level. * Mitigated an issue where saving the SDK storage while remote config values were changing could empty the storage file. * Mitigated an issue where a storage file cut short by a stopped process made initialization fail. @@ -58,6 +62,8 @@ * Mitigated an issue where backend mode requests carried no device ID type, so the server did not count the users recorded in backend mode as identified. * Mitigated an issue where a backend mode direct request sent a key twice when the given map contained a key the SDK also sets. The value in the map now replaces the SDK's value, as documented. * Mitigated an issue where a backend mode direct request changed the given map, and failed with an unmodifiable map. +* Mitigated an issue where "onConsent" and "onConsentRemoval" threw a NullPointerException when one of the given features was null. +* Mitigated an issue where the session update timer logged an error every minute for a session that was created but not begun. ## 26.8.0 * Added support for the Content feature, accessible through the "Countly.instance().content()" interface: diff --git a/sdk-java/src/main/java/ly/count/sdk/java/Config.java b/sdk-java/src/main/java/ly/count/sdk/java/Config.java index 732514be2..91603dabf 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/Config.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/Config.java @@ -278,6 +278,11 @@ public class Config { */ protected boolean backoffMechanismEnabled = true; + /** + * Whether the SDK sends its health check after init. + */ + protected boolean healthCheckEnabled = true; + // TODO: storage limits & configuration // protected int maxRequestsStored = 0; // protected int storageDirectory = ""; @@ -1595,4 +1600,19 @@ public Config disableBackoffMechanism() { this.backoffMechanismEnabled = false; return this; } + + /** + * Disable the SDK health check. Once after init the SDK sends the server a short report about + * its own state: how many warnings and errors it logged, the last request the server refused, + * how often it had to wait for a slow server, how often its session, event, view and crash + * methods were called, and which known problems occurred. It carries no data the app recorded. + * The server shows it in the SDK Manager. With this option the report is not sent; the SDK + * keeps counting, as before. + * + * @return {@code this} instance for method chaining + */ + public Config disableHealthCheck() { + this.healthCheckEnabled = false; + return this; + } } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/Countly.java b/sdk-java/src/main/java/ly/count/sdk/java/Countly.java index 30f5bf9c0..6a5a40a36 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/Countly.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/Countly.java @@ -1,6 +1,7 @@ package ly.count.sdk.java; import java.io.File; +import java.util.Arrays; import java.util.Map; import ly.count.sdk.java.internal.Device; import ly.count.sdk.java.internal.DeviceIdType; @@ -100,7 +101,9 @@ public void init(final Config config) { } if (isInitialized()) { - L.e("[Countly] Countly shouldn't be initialized twice. Please either use Countly.isInitialized() to check status or call Countly.stop() before second Countly.init(). Calling 'init' the second time will now do nothing"); + Log running = cly.L != null ? cly.L : L; + running.w("[Countly] Countly shouldn't be initialized twice. Please either use Countly.isInitialized() to check status or call Countly.stop() before second Countly.init(). Calling 'init' the second time will now do nothing"); + running.recordLogCode("w110"); return; } internalConfig.setLogger(L); @@ -110,7 +113,8 @@ public void init(final Config config) { } if (internalConfig.requestQueueMaxSize < 1) { - L.e("[Countly] init: Request queue max size can not be less than 1."); + L.w("[Countly] init: Request queue max size can not be less than 1."); + L.recordLogCode("w121"); internalConfig.requestQueueMaxSize = 1; } @@ -211,9 +215,7 @@ public static boolean isTracking(Config.Feature feature) { */ public static Session session() { if (!isInitialized()) { - if (cly.L != null) { - cly.L.e("[Countly] SDK is not initialized yet."); - } + Log.recordLogCodeWhileNotInitialized("w101"); return null; } @@ -237,9 +239,7 @@ public static ModuleBackendMode.BackendMode backendMode() { */ public ModuleBackendMode.BackendMode backendM() { if (!isInitialized()) { - if (cly.L != null) { - cly.L.e("[Countly] SDK is not initialized yet."); - } + Log.recordLogCodeWhileNotInitialized("w101"); return null; } else { ModuleBackendMode mbm = cly.sdk.module(ModuleBackendMode.class); @@ -353,19 +353,44 @@ public Usage changeDeviceIdWithoutMerge(String id) { */ public static void onConsent(Config.Feature... features) { if (!isInitialized()) { + Log.recordLogCodeWhileNotInitialized("w101"); + } else { if (cly.L != null) { - cly.L.e("[Countly] SDK is not initialized yet."); + cly.L.i("[Countly] onConsent: features = " + Arrays.toString(features)); } - } else { + cly.sdk.onConsent(consentFeatureIndexes("onConsent", features)); + } + } + + /** + * The feature bits a consent call names. A call that names no feature, or a {@code null} + * feature, is reported as log code w402; such a name is ignored. + * + * @param call the consent call, for the log + * @param features the features the call names + * @return the bits of the named features + */ + private static int consentFeatureIndexes(String call, Config.Feature... features) { + int ftrs = 0; + if (features == null || features.length == 0) { if (cly.L != null) { - cly.L.e("[Countly] onConsent: features = " + features); + cly.L.w("[Countly] " + call + ", no feature is given, the call does nothing"); + cly.L.recordLogCode("w402"); } - int ftrs = 0; - for (Config.Feature f : features) { - ftrs = ftrs | f.getIndex(); + return ftrs; + } + + for (Config.Feature f : features) { + if (f == null) { + if (cly.L != null) { + cly.L.w("[Countly] " + call + ", a 'null' feature is given, it is ignored"); + cly.L.recordLogCode("w402"); + } + continue; } - cly.sdk.onConsent(ftrs); + ftrs = ftrs | f.getIndex(); } + return ftrs; } /** @@ -377,18 +402,12 @@ public static void onConsent(Config.Feature... features) { */ public static void onConsentRemoval(Config.Feature... features) { if (cly.L != null) { - cly.L.e("[Countly] onConsentRemoval: features = " + features); + cly.L.i("[Countly] onConsentRemoval: features = " + Arrays.toString(features)); } if (!isInitialized()) { - if (cly.L != null) { - cly.L.e("[Countly] onConsentRemoval: SDK is not initialized yet."); - } + Log.recordLogCodeWhileNotInitialized("w101"); } else { - int ftrs = 0; - for (Config.Feature f : features) { - ftrs = ftrs | f.getIndex(); - } - cly.sdk.onConsentRemoval(cly.sdk.config, ftrs); + cly.sdk.onConsentRemoval(cly.sdk.config, consentFeatureIndexes("onConsentRemoval", features)); } } @@ -401,9 +420,7 @@ public static void onConsentRemoval(Config.Feature... features) { */ public ModuleFeedback.Feedback feedback() { if (!isInitialized()) { - if (L != null) { - L.e("[Countly] SDK is not initialized yet."); - } + Log.recordLogCodeWhileNotInitialized("w101"); return SDKCore.dormantFeedback; } return sdk.feedback(); @@ -420,9 +437,7 @@ public ModuleFeedback.Feedback feedback() { */ public ModuleContent.Content content() { if (!isInitialized()) { - if (L != null) { - L.e("[Countly] content, SDK is not initialized yet."); - } + Log.recordLogCodeWhileNotInitialized("w101"); return SDKCore.dormantContent; } return sdk.content(); @@ -436,9 +451,7 @@ public ModuleContent.Content content() { */ public ModuleRemoteConfig.RemoteConfig remoteConfig() { if (!isInitialized()) { - if (L != null) { - L.e("[Countly] remoteConfig, SDK is not initialized yet."); - } + Log.recordLogCodeWhileNotInitialized("w101"); return SDKCore.dormantRemoteConfig; } return sdk.remoteConfig(); @@ -452,9 +465,7 @@ public ModuleRemoteConfig.RemoteConfig remoteConfig() { */ public ModuleDeviceIdCore.DeviceId deviceId() { if (!isInitialized()) { - if (L != null) { - L.e("[Countly] SDK is not initialized yet."); - } + Log.recordLogCodeWhileNotInitialized("w101"); return SDKCore.dormantDeviceId; } return sdk.deviceId(); @@ -481,9 +492,7 @@ public Event event(String key) { */ public ModuleEvents.Events events() { if (!isInitialized()) { - if (L != null) { - L.e("[Countly] SDK is not initialized yet."); - } + Log.recordLogCodeWhileNotInitialized("w101"); return SDKCore.dormantEvents; } return sdk.events(); @@ -497,9 +506,7 @@ public ModuleEvents.Events events() { */ public ModuleCrashes.Crashes crashes() { if (!isInitialized()) { - if (L != null) { - L.e("[Countly] SDK is not initialized yet."); - } + Log.recordLogCodeWhileNotInitialized("w101"); return SDKCore.dormantCrashes; } return sdk.crashes(); @@ -513,9 +520,7 @@ public ModuleCrashes.Crashes crashes() { */ public ModuleViews.Views views() { if (!isInitialized()) { - if (L != null) { - L.e("[Countly] SDK is not initialized yet."); - } + Log.recordLogCodeWhileNotInitialized("w101"); return SDKCore.dormantViews; } return sdk.views(); @@ -542,9 +547,7 @@ public Event timedEvent(String key) { */ public ModuleUserProfile.UserProfile userProfile() { if (!isInitialized()) { - if (L != null) { - L.e("[Countly] userProfile, SDK is not initialized yet."); - } + Log.recordLogCodeWhileNotInitialized("w101"); return SDKCore.dormantUserProfile; } return sdk.userProfile(); @@ -558,10 +561,7 @@ public ModuleUserProfile.UserProfile userProfile() { */ public ModuleLocation.Location location() { if (!isInitialized()) { - // L is null before init, so this has to be guarded like every sibling accessor. - if (L != null) { - L.e("Countly.sharedInstance().init must be called before accessing location"); - } + Log.recordLogCodeWhileNotInitialized("w101"); return SDKCore.dormantLocation; } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/DefaultNetworking.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/DefaultNetworking.java index f1e9ee222..f6eade49d 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/DefaultNetworking.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/DefaultNetworking.java @@ -231,6 +231,9 @@ private boolean prepareAndSend(final InternalConfig config, final Request reques if (uploadsGatheredLogs) { L.onLogBatchSent(); } + if (!backendMode && responseTimeMs.get() >= 0 && responseTimeMs.get() < config.getConfigurationProvider().getBOMAcceptedTimeoutSeconds() * 1000L) { + L.logConsecutiveBackoffRequest(); + } if (!backOffIfTheServerIsSlow(config, request, responseTimeMs.get())) { check(config); } @@ -309,6 +312,7 @@ private boolean isOlderThanDropAge(@Nonnull InternalConfig config, @Nonnull Requ */ private boolean dropWithoutSending(@Nonnull InternalConfig config, @Nonnull Request request) { L.w("[Networking] dropWithoutSending, request [" + request.storageId() + "] is older than the request drop age of [" + config.getConfigurationProvider().getRequestDropAgeHours() + "] hours set by the SDK behavior settings, dropping it without sending"); + L.recordLogCode("w330"); if (!Boolean.TRUE.equals(storageForRequestQueue.removeRequest(request))) { L.e("[Networking] dropWithoutSending, failed to remove request [" + request.storageId() + "], the queue drain stops here"); return false; @@ -390,8 +394,12 @@ private boolean backOffIfTheServerIsSlow(@Nonnull InternalConfig config, @Nonnul } int durationSeconds = provider.getBOMDuration(); - L.i("[Networking] backOffIfTheServerIsSlow, the server took [" + responseTimeMs + "] ms to answer request [" + request.storageId() + "] and [" + queuedRequests + "] requests remain queued, holding the request queue back for [" + durationSeconds + "] seconds"); - return startBackoff(config, durationSeconds); + if (!startBackoff(config, durationSeconds)) { + return false; + } + L.w("[Networking] backOffIfTheServerIsSlow, the server took [" + responseTimeMs + "] ms to answer request [" + request.storageId() + "] and [" + queuedRequests + "] requests remain queued, holding the request queue back for [" + durationSeconds + "] seconds"); + L.logBackoffRequest(); + return true; } /** diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/EventImpl.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/EventImpl.java index 211628f56..b0913d623 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/EventImpl.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/EventImpl.java @@ -102,6 +102,16 @@ public void record() { return; } + L.w("[EventImpl] record, this function is deprecated, use 'events().recordEvent' instead"); + L.recordLogCode("w120"); + L.recordUsage(HealthCheckCounter.AREA_EVENTS, "record:d"); + recordInternal(); + } + + /** + * Hands the event to its recorder once. + */ + private void recordInternal() { if (recorder != null && !invalid) { invalid = true; recorder.recordEvent(this); @@ -117,8 +127,11 @@ public void endAndRecord() { return; } + L.w("[EventImpl] endAndRecord, this function is deprecated, use 'events().endEvent' instead"); + L.recordLogCode("w120"); + L.recordUsage(HealthCheckCounter.AREA_EVENTS, "endAndRecord:d"); setDuration((TimeUtils.timestampMs() - timestamp) / 1000); - record(); + recordInternal(); } @Override diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/HealthCheckCounter.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/HealthCheckCounter.java new file mode 100644 index 000000000..f88391822 --- /dev/null +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/HealthCheckCounter.java @@ -0,0 +1,492 @@ +package ly.count.sdk.java.internal; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.function.Predicate; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.json.JSONException; +import org.json.JSONObject; + +/** + * The counters of the SDK health check: warnings and errors logged, the last failed request, + * backoffs, method usage ("fu") and log codes ("lc"), kept since the last health check the server + * accepted. Logs at debug level only, as every warning and error the SDK logs is counted here. + */ +public class HealthCheckCounter { + static final int COUNT_CAP = 65535; + static final int MAX_ERROR_MESSAGE_LENGTH = 1000; + + static final String KEY_LOG_ERROR = "LErr"; + static final String KEY_LOG_WARNING = "LWar"; + static final String KEY_STATUS_CODE = "RStatC"; + static final String KEY_ERROR_MESSAGE = "REMsg"; + static final String KEY_BACKOFF_REQUEST = "BReq"; + static final String KEY_CONSECUTIVE_BACKOFF_REQUEST = "CBReq"; + static final String KEY_METHOD_USAGE = "MU"; + static final String KEY_LOG_CODES = "LC"; + + static final String REQUEST_KEY_ERROR_COUNT = "el"; + static final String REQUEST_KEY_WARNING_COUNT = "wl"; + static final String REQUEST_KEY_STATUS_CODE = "sc"; + static final String REQUEST_KEY_ERROR_MESSAGE = "em"; + static final String REQUEST_KEY_BACKOFF_REQUEST = "bom"; + static final String REQUEST_KEY_CONSECUTIVE_BACKOFF_REQUEST = "cbom"; + static final String REQUEST_KEY_METHOD_USAGE = "fu"; + static final String REQUEST_KEY_LOG_CODES = "lc"; + + static final String AREA_SESSIONS = "sessions"; + static final String AREA_EVENTS = "events"; + static final String AREA_VIEWS = "views"; + static final String AREA_CRASHES = "crashes"; + + long countLogWarning = 0; + long countLogError = 0; + int statusCode = -1; + String errorMessage = ""; + long countBackoffRequest = 0; + int consecutiveBackoffRequest = 0; + private int consecutiveBackoffRequestCounter = 0; + final Map> methodUsage = new HashMap<>(); + final Map logCodes = new HashMap<>(); + + private volatile boolean recordingStoppedForCrash = false; + private final StorageProvider storageProvider; + private final Log L; + private final Predicate consentGiven; + + /** + * Creates the counters from the state a previous run stored, and adds the automatically + * detected crashes that run counted to "crashes" / "record:a". + * + * @param storageProvider the storage the state is kept in + * @param logger the SDK logger, used at debug level only + * @param consentGiven whether a feature has consent now, {@code true} for every feature while no consent is required + */ + public HealthCheckCounter(@Nonnull StorageProvider storageProvider, @Nonnull Log logger, @Nonnull Predicate consentGiven) { + this.storageProvider = storageProvider; + this.L = logger; + this.consentGiven = consentGiven; + + setupInitialCounters(storageProvider.getHealthCheckState()); + + int automaticCrashes = storageProvider.takeAutomaticCrashCount(); + if (automaticCrashes > 0) { + synchronized (this) { + addUsage(AREA_CRASHES, "record:a", automaticCrashes); + } + saveState(); + } + } + + /** + * Adds the counters a previous run stored to the current ones. A stored state that is not valid + * JSON is reset and reported as log code e503. An entry of the wrong type is skipped. + * + * @param initialState the stored state, {@code null} or empty when nothing was stored + */ + void setupInitialCounters(@Nullable String initialState) { + if (initialState == null || initialState.isEmpty()) { + return; + } + + JSONObject stored; + try { + stored = new JSONObject(initialState); + } catch (JSONException e) { + clearAndSave(); + synchronized (this) { + addLogCode("e503", 1); + } + L.d("[HealthCheckCounter] setupInitialCounters, the stored health check state could not be read and is reset, [" + e + "]"); + return; + } + + synchronized (this) { + countLogWarning += optLong(stored, KEY_LOG_WARNING); + countLogError += optLong(stored, KEY_LOG_ERROR); + if (statusCode == -1 && stored.opt(KEY_STATUS_CODE) instanceof Number) { + statusCode = ((Number) stored.opt(KEY_STATUS_CODE)).intValue(); + } + if (errorMessage.isEmpty() && stored.opt(KEY_ERROR_MESSAGE) instanceof String) { + errorMessage = (String) stored.opt(KEY_ERROR_MESSAGE); + } + countBackoffRequest += optLong(stored, KEY_BACKOFF_REQUEST); + consecutiveBackoffRequest = (int) Math.max(consecutiveBackoffRequest, optLong(stored, KEY_CONSECUTIVE_BACKOFF_REQUEST)); + + JSONObject storedUsage = stored.optJSONObject(KEY_METHOD_USAGE); + if (storedUsage != null) { + Iterator areas = storedUsage.keys(); + while (areas.hasNext()) { + String area = areas.next(); + JSONObject methods = storedUsage.optJSONObject(area); + if (methods == null) { + continue; + } + Iterator methodNames = methods.keys(); + while (methodNames.hasNext()) { + String method = methodNames.next(); + if (methods.opt(method) instanceof Number) { + addUsage(area, method, ((Number) methods.opt(method)).intValue()); + } + } + } + } + + JSONObject storedCodes = stored.optJSONObject(KEY_LOG_CODES); + if (storedCodes != null) { + Iterator codes = storedCodes.keys(); + while (codes.hasNext()) { + String code = codes.next(); + if (storedCodes.opt(code) instanceof Number) { + addLogCode(code, ((Number) storedCodes.opt(code)).intValue()); + } + } + } + } + + L.d("[HealthCheckCounter] setupInitialCounters, loaded the stored health check state [" + stored + "]"); + } + + private static long optLong(@Nonnull JSONObject json, @Nonnull String key) { + Object value = json.opt(key); + return value instanceof Number ? Math.max(0, ((Number) value).longValue()) : 0; + } + + /** + * Counts one warning the SDK logged. + */ + void logWarning() { + synchronized (this) { + countLogWarning++; + } + } + + /** + * Counts one error the SDK logged. + */ + void logError() { + synchronized (this) { + countLogError++; + } + } + + /** + * Keeps the answer of the server to a queued request it did not accept. Only the most recent + * one is kept. + * + * @param statusCode the HTTP status code, ignored unless above 0 and below 1000 + * @param errorResponse the body of the answer, cut to 1000 characters + */ + void logFailedNetworkRequest(int statusCode, @Nullable String errorResponse) { + if (statusCode <= 0 || statusCode >= 1000) { + return; + } + + String response = errorResponse == null ? "" : errorResponse; + synchronized (this) { + this.statusCode = statusCode; + this.errorMessage = response.length() > MAX_ERROR_MESSAGE_LENGTH ? response.substring(0, MAX_ERROR_MESSAGE_LENGTH) : response; + } + } + + /** + * Counts a backoff of the request queue and extends the current run of backoffs in a row. The + * longest run ("cbom") is raised right away, so no save has to end the run to keep it. Records + * log code w320, and w321 when the backoff continues a run. The run is kept in memory only, so + * a restart ends it. + */ + void logBackoffRequest() { + synchronized (this) { + boolean continuesRun = consecutiveBackoffRequestCounter > 0; + countBackoffRequest++; + consecutiveBackoffRequestCounter++; + consecutiveBackoffRequest = Math.max(consecutiveBackoffRequest, consecutiveBackoffRequestCounter); + + if (!recordingStoppedForCrash) { + addLogCode("w320", 1); + if (continuesRun) { + addLogCode("w321", 1); + } + } + } + } + + /** + * Ends the current run of backoffs in a row, when a queued request was answered in time. The + * longest run is already kept by {@link #logBackoffRequest()}. + */ + void logConsecutiveBackoffRequest() { + synchronized (this) { + consecutiveBackoffRequestCounter = 0; + } + } + + /** + * Counts one call of a method for the "fu" map, under its feature area and method name. A call + * the SDK made by itself carries ":a", a deprecated method ":d". Nothing is counted while consent + * is required and the feature of the area has no consent, or while an automatic crash is reported. + * + * @param area the feature area: "sessions", "events", "views" or "crashes" + * @param method the method name with its modifiers + */ + void recordUsage(@Nullable String area, @Nullable String method) { + if (area == null || method == null || area.isEmpty() || method.isEmpty() || recordingStoppedForCrash) { + return; + } + + if (!hasConsentForUsageArea(area)) { + return; + } + + synchronized (this) { + addUsage(area, method, 1); + } + } + + /** + * Counts occurrences of a log code for the "lc" map, a level letter and three digits such as "w740". + * + * @param code the log code + * @param count how many times the condition occurred + */ + void recordLogCode(@Nullable String code, int count) { + if (code == null || code.isEmpty() || count <= 0 || recordingStoppedForCrash) { + return; + } + + synchronized (this) { + addLogCode(code, count); + } + } + + /** + * Counts one occurrence of a log code that a public method of a feature area records. Like + * {@link #recordUsage(String, String)}, nothing is counted while consent is required and the + * feature of the area has no consent. + * + * @param code the log code + * @param area the feature area of the method that recorded it + */ + void recordLogCode(@Nullable String code, @Nullable String area) { + if (!hasConsentForUsageArea(area)) { + return; + } + + recordLogCode(code, 1); + } + + /** + * Makes method usage and log codes ignore their calls while an automatically detected crash is + * reported, so the report never counts into these maps. + */ + void stopRecordingForCrash() { + recordingStoppedForCrash = true; + } + + /** + * Lets method usage and log codes count again after an automatically detected crash was + * reported. A Java process outlives a crashed thread, so counting goes on once the report is stored. + */ + void resumeRecordingAfterCrash() { + recordingStoppedForCrash = false; + } + + /** + * Whether calls of a method usage area are counted: the feature behind it has consent, or no + * consent is required. An unknown area is never counted. + * + * @param area the method usage area + * @return {@code true} when calls of the area are counted + */ + boolean hasConsentForUsageArea(@Nullable String area) { + if (area == null) { + return false; + } + + switch (area) { + case AREA_SESSIONS: + return consentGiven.test(CoreFeature.Sessions); + case AREA_EVENTS: + return consentGiven.test(CoreFeature.Events); + case AREA_VIEWS: + return consentGiven.test(CoreFeature.Views); + case AREA_CRASHES: + return consentGiven.test(CoreFeature.CrashReporting); + default: + return false; + } + } + + /** + * Adds to the count of a method, up to the cap a health check reports. Call while holding this + * object's lock. + */ + private void addUsage(@Nonnull String area, @Nonnull String method, int count) { + if (count <= 0) { + return; + } + + Map methods = methodUsage.computeIfAbsent(area, key -> new HashMap<>()); + Integer current = methods.get(method); + methods.put(method, (int) Math.min((long) (current == null ? 0 : current) + count, COUNT_CAP)); + } + + /** + * Adds to the count of a log code, up to the cap a health check reports. Call while holding this + * object's lock. + */ + private void addLogCode(@Nonnull String code, int count) { + if (count <= 0) { + return; + } + + Integer current = logCodes.get(code); + logCodes.put(code, (int) Math.min((long) (current == null ? 0 : current) + count, COUNT_CAP)); + } + + /** + * Resets every counter to its default and clears the stored state. + */ + void clearAndSave() { + synchronized (this) { + countLogWarning = 0; + countLogError = 0; + statusCode = -1; + errorMessage = ""; + countBackoffRequest = 0; + consecutiveBackoffRequest = 0; + consecutiveBackoffRequestCounter = 0; + methodUsage.clear(); + logCodes.clear(); + } + storageProvider.setHealthCheckState(""); + } + + /** + * Stores the counters. The current run of backoffs in a row goes on, as "cbom" already holds + * the longest run. The storage is written outside this object's lock, since writing can log. + */ + void saveState() { + String state; + synchronized (this) { + JSONObject json = new JSONObject(); + json.put(KEY_LOG_WARNING, countLogWarning); + json.put(KEY_LOG_ERROR, countLogError); + json.put(KEY_STATUS_CODE, statusCode); + json.put(KEY_ERROR_MESSAGE, errorMessage); + json.put(KEY_BACKOFF_REQUEST, countBackoffRequest); + json.put(KEY_CONSECUTIVE_BACKOFF_REQUEST, consecutiveBackoffRequest); + json.put(KEY_METHOD_USAGE, usageToJson()); + json.put(KEY_LOG_CODES, new JSONObject(logCodes)); + state = json.toString(); + } + + storageProvider.setHealthCheckState(state); + } + + /** + * The method usage counts as a JSON object. Call while holding this object's lock. + */ + @Nonnull + private JSONObject usageToJson() { + JSONObject usage = new JSONObject(); + for (Map.Entry> area : methodUsage.entrySet()) { + usage.put(area.getKey(), new JSONObject(area.getValue())); + } + return usage; + } + + /** + * The health check parameter as it is sent, taken from the current counters. "fu" and "lc" are + * always present, empty when nothing was counted, so the server can tell an SDK that reports + * them from one that does not. + * + * @return a copy of the counters that later counting does not change + */ + @Nonnull + JSONObject snapshot() { + JSONObject json = new JSONObject(); + synchronized (this) { + json.put(REQUEST_KEY_ERROR_COUNT, countLogError); + json.put(REQUEST_KEY_WARNING_COUNT, countLogWarning); + json.put(REQUEST_KEY_STATUS_CODE, statusCode); + json.put(REQUEST_KEY_ERROR_MESSAGE, errorMessage); + json.put(REQUEST_KEY_BACKOFF_REQUEST, countBackoffRequest); + json.put(REQUEST_KEY_CONSECUTIVE_BACKOFF_REQUEST, consecutiveBackoffRequest); + json.put(REQUEST_KEY_METHOD_USAGE, usageToJson()); + json.put(REQUEST_KEY_LOG_CODES, new JSONObject(logCodes)); + } + return json; + } + + /** + * Takes away what a health check the server accepted reported, so what was counted while it was + * on its way is reported next time. The last failed request and the longest backoff run are + * cleared only when they did not change since. Stores what is left. + * + * @param sent the snapshot the delivered health check was built from + */ + void clearSentValues(@Nonnull JSONObject sent) { + synchronized (this) { + countLogError = Math.max(0, countLogError - sent.optLong(REQUEST_KEY_ERROR_COUNT, 0)); + countLogWarning = Math.max(0, countLogWarning - sent.optLong(REQUEST_KEY_WARNING_COUNT, 0)); + countBackoffRequest = Math.max(0, countBackoffRequest - sent.optLong(REQUEST_KEY_BACKOFF_REQUEST, 0)); + + if (consecutiveBackoffRequest == sent.optInt(REQUEST_KEY_CONSECUTIVE_BACKOFF_REQUEST, -1)) { + consecutiveBackoffRequest = 0; + } + + if (statusCode == sent.optInt(REQUEST_KEY_STATUS_CODE, -2) && errorMessage.equals(sent.optString(REQUEST_KEY_ERROR_MESSAGE, ""))) { + statusCode = -1; + errorMessage = ""; + } + + JSONObject sentUsage = sent.optJSONObject(REQUEST_KEY_METHOD_USAGE); + if (sentUsage != null) { + Iterator areas = sentUsage.keys(); + while (areas.hasNext()) { + String area = areas.next(); + JSONObject sentMethods = sentUsage.optJSONObject(area); + Map counted = methodUsage.get(area); + if (sentMethods == null || counted == null) { + continue; + } + Iterator methods = sentMethods.keys(); + while (methods.hasNext()) { + String method = methods.next(); + subtract(counted, method, sentMethods.optInt(method, 0)); + } + if (counted.isEmpty()) { + methodUsage.remove(area); + } + } + } + + JSONObject sentCodes = sent.optJSONObject(REQUEST_KEY_LOG_CODES); + if (sentCodes != null) { + Iterator codes = sentCodes.keys(); + while (codes.hasNext()) { + String code = codes.next(); + subtract(logCodes, code, sentCodes.optInt(code, 0)); + } + } + } + + saveState(); + } + + /** + * Takes a reported amount away from a count, removing the count when nothing is left. + */ + private static void subtract(@Nonnull Map counts, @Nonnull String key, int reported) { + Integer current = counts.get(key); + int left = (current == null ? 0 : current) - reported; + if (left > 0) { + counts.put(key, left); + } else { + counts.remove(key); + } + } +} diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/ImmediateRequestMaker.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/ImmediateRequestMaker.java index 36683c06a..66a8326ae 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/ImmediateRequestMaker.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/ImmediateRequestMaker.java @@ -69,7 +69,7 @@ private JSONObject doInBackground(String requestData, String customEndpoint, Tra try { connection = cp.connection(request); } catch (IOException e) { - L.e("[ImmediateRequestMaker] IOException while preparing remote config update request :[" + e + "]"); + L.d("[ImmediateRequestMaker] IOException while preparing remote config update request :[" + e + "]"); return null; } //connecting @@ -79,7 +79,7 @@ private JSONObject doInBackground(String requestData, String customEndpoint, Tra String receivedBuffer = cp.response(connection); if (receivedBuffer == null) { - L.e("[ImmediateRequestMaker] Encountered problem while making a immediate server request, received result was null"); + L.d("[ImmediateRequestMaker] Encountered problem while making a immediate server request, received result was null"); return null; } @@ -92,11 +92,11 @@ private JSONObject doInBackground(String requestData, String customEndpoint, Tra } return new JSONObject(receivedBuffer); } else { - L.e("[ImmediateRequestMaker] Encountered problem while making a immediate server request, :[" + receivedBuffer + "]"); + L.d("[ImmediateRequestMaker] Encountered problem while making a immediate server request, :[" + receivedBuffer + "]"); return null; } } catch (Exception e) { - L.e("[ImmediateRequestMaker] Received exception while making a immediate server request " + e.getMessage()); + L.d("[ImmediateRequestMaker] Received exception while making a immediate server request " + e.getMessage()); } finally { if (connection != null) { connection.disconnect(); diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/InternalConfig.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/InternalConfig.java index 4fb0c8e13..12ce391fb 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/InternalConfig.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/InternalConfig.java @@ -183,6 +183,15 @@ public boolean isSdkBehaviorSettingsRequestsDisabled() { return sdkBehaviorSettingsRequestsDisabled; } + /** + * Whether the SDK sends its health check, see {@link Config#disableHealthCheck()}. + * + * @return {@code true} unless the health check was disabled + */ + boolean isHealthCheckEnabled() { + return healthCheckEnabled; + } + /** * Whether the developer left the backoff mechanism on, the value the {@code bom} setting of the * SDK behavior settings starts from. diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/JsonFileStorage.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/JsonFileStorage.java index 51d17cc54..e823d540f 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/JsonFileStorage.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/JsonFileStorage.java @@ -66,6 +66,7 @@ public synchronized void save() { String content = json.toString(); if (content == null) { logger.e("[JsonFileStorage] save, Failed to serialize the data, the json file is left as it is: [" + file.getAbsolutePath() + "]"); + logger.recordLogCode("e501"); return; } @@ -77,6 +78,7 @@ public synchronized void save() { replaceFile(temporary, file); } catch (IOException e) { logger.e("[JsonFileStorage] save, Failed to save json file, reason: [" + e.getMessage() + "]"); + logger.recordLogCode("e501"); } } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/Log.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/Log.java index aee71824b..8429c687c 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/Log.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/Log.java @@ -122,6 +122,15 @@ public class Log { private volatile IntSupplier givenConsents = null; private volatile LogBatchQueue logBatchQueue = null; + //guards the counts kept until the health check counter exists and the handover to it + private final Object healthCountLock = new Object(); + private volatile HealthCheckCounter healthCheckCounter = null; + private long warningsBeforeHealthCheck = 0; + private long errorsBeforeHealthCheck = 0; + private final Map logCodesBeforeHealthCheck = new HashMap<>(); + //log codes recorded while no SDK runs, such as calls made before init, handed to the next counter + private static final Map logCodesWhileNotInitialized = new HashMap<>(); + /** * The request queue as the log gathering uses it: one request per batch of gathered log lines. */ @@ -207,6 +216,7 @@ public void i(String logMessage) { * @param logMessage string to log */ public void w(String logMessage) { + countWarning(); print("[WARN] [Countly]\t" + logMessage, Config.LoggingLevel.WARN); informListener(logMessage, Config.LoggingLevel.WARN); captureLogLine(logMessage, 'w'); @@ -218,11 +228,221 @@ public void w(String logMessage) { * @param logMessage string to log */ public void e(String logMessage) { + countError(); print("[ERROR] [Countly]\t" + logMessage, Config.LoggingLevel.ERROR); informListener(logMessage, Config.LoggingLevel.ERROR); captureLogLine(logMessage, 'e'); } + /** + * Counts a warning for the health check, whether or not it is printed. Kept here until the + * health check counter exists. + */ + private void countWarning() { + HealthCheckCounter counter = healthCheckCounter; + if (counter != null) { + counter.logWarning(); + return; + } + synchronized (healthCountLock) { + counter = healthCheckCounter; + if (counter == null) { + warningsBeforeHealthCheck++; + return; + } + } + counter.logWarning(); + } + + /** + * Counts an error for the health check, whether or not it is printed. Kept here until the + * health check counter exists. + */ + private void countError() { + HealthCheckCounter counter = healthCheckCounter; + if (counter != null) { + counter.logError(); + return; + } + synchronized (healthCountLock) { + counter = healthCheckCounter; + if (counter == null) { + errorsBeforeHealthCheck++; + return; + } + } + counter.logError(); + } + + /** + * Hands the health check counter to the logger, with the warnings, errors and log codes counted + * before it existed, including the codes recorded while no SDK ran. {@code null} detaches it, + * after which counts are kept here again. + * + * @param counter the counter, {@code null} for none + */ + void setHealthCheckCounter(@Nullable HealthCheckCounter counter) { + long warnings; + long errors; + Map codes = new HashMap<>(); + synchronized (healthCountLock) { + healthCheckCounter = counter; + if (counter == null) { + return; + } + warnings = warningsBeforeHealthCheck; + errors = errorsBeforeHealthCheck; + warningsBeforeHealthCheck = 0; + errorsBeforeHealthCheck = 0; + codes.putAll(logCodesBeforeHealthCheck); + logCodesBeforeHealthCheck.clear(); + } + synchronized (logCodesWhileNotInitialized) { + for (Map.Entry entry : logCodesWhileNotInitialized.entrySet()) { + codes.merge(entry.getKey(), entry.getValue(), Integer::sum); + } + logCodesWhileNotInitialized.clear(); + } + + for (long a = 0; a < warnings; a++) { + counter.logWarning(); + } + for (long a = 0; a < errors; a++) { + counter.logError(); + } + for (Map.Entry entry : codes.entrySet()) { + counter.recordLogCode(entry.getKey(), entry.getValue()); + } + } + + /** + * Records a health check log code, such as "w740", next to the log line of its condition. A + * code recorded before the health check counter exists is kept until it does. + * + * @param code the log code + */ + public void recordLogCode(@Nonnull String code) { + recordLogCode(code, 1); + } + + /** + * Records several occurrences of a health check log code at once, for a condition several + * items met together, such as requests dropped from the queue in one go. + * + * @param code the log code + * @param count how many times the condition occurred + */ + public void recordLogCode(@Nonnull String code, int count) { + if (count <= 0) { + return; + } + HealthCheckCounter counter = healthCheckCounter; + if (counter == null) { + synchronized (healthCountLock) { + counter = healthCheckCounter; + if (counter == null) { + logCodesBeforeHealthCheck.merge(code, count, Integer::sum); + return; + } + } + } + counter.recordLogCode(code, count); + } + + /** + * Records a health check log code that a public method of a feature area records, so it is + * ignored while that feature has no consent. Ignored before the health check counter exists, + * as no consent is known then. + * + * @param code the log code + * @param area the feature area: "sessions", "events", "views" or "crashes" + */ + public void recordLogCode(@Nonnull String code, @Nonnull String area) { + HealthCheckCounter counter = healthCheckCounter; + if (counter != null) { + counter.recordLogCode(code, area); + } + } + + /** + * Counts one call of a method for the health check method usage, under its feature area. A call + * the SDK made by itself carries ":a", a deprecated method ":d". + * + * @param area the feature area: "sessions", "events", "views" or "crashes" + * @param method the method name with its modifiers + */ + public void recordUsage(@Nonnull String area, @Nonnull String method) { + HealthCheckCounter counter = healthCheckCounter; + if (counter != null) { + counter.recordUsage(area, method); + } + } + + /** + * Keeps the answer of the server to a queued request it did not accept for the health check. + * + * @param statusCode the HTTP status code + * @param response the body of the answer + */ + void logFailedNetworkRequest(int statusCode, @Nullable String response) { + HealthCheckCounter counter = healthCheckCounter; + if (counter != null) { + counter.logFailedNetworkRequest(statusCode, response); + } + } + + /** + * Counts a backoff of the request queue for the health check. + */ + void logBackoffRequest() { + HealthCheckCounter counter = healthCheckCounter; + if (counter != null) { + counter.logBackoffRequest(); + } + } + + /** + * Ends the run of backoffs in a row of the health check, as a queued request was answered in time. + */ + void logConsecutiveBackoffRequest() { + HealthCheckCounter counter = healthCheckCounter; + if (counter != null) { + counter.logConsecutiveBackoffRequest(); + } + } + + /** + * Stops method usage and log codes from counting while an automatically detected crash is reported. + */ + void stopHealthCheckRecordingForCrash() { + HealthCheckCounter counter = healthCheckCounter; + if (counter != null) { + counter.stopRecordingForCrash(); + } + } + + /** + * Lets method usage and log codes count again once an automatically detected crash was reported. + */ + void resumeHealthCheckRecordingAfterCrash() { + HealthCheckCounter counter = healthCheckCounter; + if (counter != null) { + counter.resumeRecordingAfterCrash(); + } + } + + /** + * Records a health check log code while no SDK runs, for a call made before init or after the + * SDK stopped, which no logger can print. The next SDK that starts reports it. + * + * @param code the log code + */ + public static void recordLogCodeWhileNotInitialized(@Nonnull String code) { + synchronized (logCodesWhileNotInitialized) { + logCodesWhileNotInitialized.merge(code, 1, (current, added) -> Math.min(current + added, HealthCheckCounter.COUNT_CAP)); + } + } + /** * {@link Config.LoggingLevel#VERBOSE} level logging * diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleCrashes.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleCrashes.java index e2d071282..8cd21157b 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleCrashes.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleCrashes.java @@ -130,6 +130,8 @@ private boolean isAutomaticCrashReportingEnabled() { /** * Builds the handler that records an unhandled crash, while the module runs and the SDK behavior * settings enable automatic crash reporting, and then always hands the crash to the handler it replaced. + * While the crash is reported, the health check counts no method usage and no log codes; the + * crash itself is counted as a stored number that the health check of the next start reports. * * @param previous the handler to hand every crash to, {@code null} for none * @return the handler @@ -142,7 +144,14 @@ private Thread.UncaughtExceptionHandler createUncaughtExceptionHandler(@Nullable try { if (isActive() && !stopped && isAutomaticCrashReportingEnabled()) { - recordExceptionInternal(throwable, false, null, null); + L.stopHealthCheckRecordingForCrash(); + try { + if (recordExceptionInternal(throwable, false, null, null)) { + internalConfig.storageProvider.incrementAutomaticCrashCount(); + } + } finally { + L.resumeHealthCheckRecordingAfterCrash(); + } } } catch (Throwable recordFailure) { L.e("[ModuleCrash] uncaughtException, failed to record the crash, [" + recordFailure + "]"); @@ -180,20 +189,36 @@ private void restoreUncaughtExceptionHandler() { } } - protected void recordExceptionInternal(Throwable t, boolean handled, Map segments, String legacyCrashName) { + /** + * Records a crash, unless the SDK behavior settings turn crash reporting off, backend mode is + * on, or there is no throwable. + * + * @param t the throwable + * @param handled whether the app handled it + * @param segments custom segmentation, {@code null} for none + * @param legacyCrashName the name of the deprecated crash report call, {@code null} for none + * @return whether the crash was stored for sending + */ + protected boolean recordExceptionInternal(Throwable t, boolean handled, Map segments, String legacyCrashName) { ConfigurationProvider configProvider = internalConfig.getConfigurationProvider(); - if (!configProvider.getTrackingEnabled() || !configProvider.getCrashReportingEnabled()) { + if (!configProvider.getTrackingEnabled()) { + L.w("[ModuleCrash] recordExceptionInternal, tracking disabled by SDK behavior settings; ignoring"); + L.recordLogCode("w505", HealthCheckCounter.AREA_CRASHES); + return false; + } + if (!configProvider.getCrashReportingEnabled()) { L.d("[ModuleCrash] recordExceptionInternal, crash reporting disabled by SDK behavior settings; ignoring"); - return; + return false; } if (config.isBackendModeEnabled()) { L.w("[ModuleCrash] recordExceptionInternal, Skipping crash, backend mode is enabled!"); - return; + return false; } if (t == null) { - L.e("[ModuleCrash] recordExceptionInternal, Throwable cannot be null"); - return; + L.w("[ModuleCrash] recordExceptionInternal, Throwable cannot be null"); + L.recordLogCode("w700", HealthCheckCounter.AREA_CRASHES); + return false; } CrashImpl crash = new CrashImpl(L).addThrowable(t).setFatal(!handled).addSegments(segments); @@ -203,7 +228,7 @@ protected void recordExceptionInternal(Throwable t, boolean handled, Map segmentation, String eventIdOverride) { ConfigurationProvider configProvider = internalConfig.getConfigurationProvider(); if (!configProvider.getTrackingEnabled()) { - L.d("[ModuleEvents] recordEventInternal, tracking is disabled by the SDK behavior settings, event [" + key + "] will not be recorded"); + L.w("[ModuleEvents] recordEventInternal, tracking is disabled by the SDK behavior settings, event [" + key + "] will not be recorded"); + L.recordLogCode("w505", HealthCheckCounter.AREA_EVENTS); return; } if (count <= 0) { L.w("[ModuleEvents] recordEventInternal, Count can't be less than 1, ignoring this event."); + L.recordLogCode("w750", HealthCheckCounter.AREA_EVENTS); return; } if (key == null || key.isEmpty()) { L.w("[ModuleEvents] recordEventInternal, Key can't be null or empty, ignoring this event."); + L.recordLogCode("w701", HealthCheckCounter.AREA_EVENTS); return; } @@ -346,10 +349,13 @@ private void checkEventQueueToSend(boolean forceSend, boolean journeyTrigger) { boolean startEventInternal(final String key) { if (key == null || key.isEmpty()) { - L.e("[ModuleEvents] Can't start event with a null or empty key"); + L.w("[ModuleEvents] Can't start event with a null or empty key"); + L.recordLogCode("w700", HealthCheckCounter.AREA_EVENTS); return false; } if (timedEvents.containsKey(key)) { + L.w("[ModuleEvents] startEventInternal, a timed event with the key [" + key + "] is already running, the call is ignored"); + L.recordLogCode("w722", HealthCheckCounter.AREA_EVENTS); return false; } @@ -371,19 +377,22 @@ boolean endEventInternal(final String key, final Map segmentatio L.d("[ModuleEvents] Ending event: [" + key + "]"); if (key == null || key.isEmpty()) { - L.e("[ModuleEvents] Can't end event with a null or empty key"); + L.w("[ModuleEvents] Can't end event with a null or empty key"); + L.recordLogCode("w700", HealthCheckCounter.AREA_EVENTS); return false; } EventImpl event = timedEvents.remove(key); if (event == null) { - L.w("endEventInternal, No timed event with the name [" + key + "] is started, nothing to end. Will ignore call."); + L.w("[ModuleEvents] endEventInternal, No timed event with the name [" + key + "] is started, nothing to end. Will ignore call."); + L.recordLogCode("w720", HealthCheckCounter.AREA_EVENTS); return false; } if (count < 1) { - L.e("endEventInternal, Countly event count should be greater than zero [" + count + "]. Changing value to 1"); + L.w("[ModuleEvents] endEventInternal, Countly event count should be greater than zero [" + count + "]. Changing value to 1"); + L.recordLogCode("w750", HealthCheckCounter.AREA_EVENTS); count = 1; } @@ -398,13 +407,20 @@ boolean endEventInternal(final String key, final Map segmentatio boolean cancelEventInternal(final String key) { if (key == null || key.isEmpty()) { - L.e("[ModuleEvents] Can't cancel event with a null or empty key"); + L.w("[ModuleEvents] Can't cancel event with a null or empty key"); + L.recordLogCode("w700", HealthCheckCounter.AREA_EVENTS); return false; } EventImpl event = timedEvents.remove(key); - return event != null; + if (event == null) { + L.w("[ModuleEvents] cancelEventInternal, no timed event with the key [" + key + "] is running, the call is ignored"); + L.recordLogCode("w721", HealthCheckCounter.AREA_EVENTS); + return false; + } + + return true; } public class Events { @@ -440,6 +456,7 @@ public void recordEvent(String key, Map segmentation, int count, return; } L.i("[Events] recordEvent: key = " + key + ", count = " + count + ", sum = " + sum + ", segmentation = " + segmentation + ", dur = " + dur); + L.recordUsage(HealthCheckCounter.AREA_EVENTS, "record"); recordEventInternal(key, count, sum, dur, segmentation, null); } @@ -524,6 +541,7 @@ public boolean startEvent(final String key) { return target != null && target.startEvent(key); } L.i("[Events] startEvent: key = " + key); + L.recordUsage(HealthCheckCounter.AREA_EVENTS, "start"); return startEventInternal(key); } @@ -539,6 +557,7 @@ public boolean endEvent(final String key) { return target != null && target.endEvent(key); } L.i("[Events] endEvent: key = " + key); + L.recordUsage(HealthCheckCounter.AREA_EVENTS, "end"); return endEventInternal(key, null, 1, null); } @@ -559,6 +578,7 @@ public boolean endEvent(final String key, final Map segmentation return target != null && target.endEvent(key, segmentation, count, sum); } L.i("[Events] endEvent: key = " + key + ", segmentation = " + segmentation + ", count = " + count + ", sum = " + sum); + L.recordUsage(HealthCheckCounter.AREA_EVENTS, "end"); return endEventInternal(key, segmentation, count, sum); } @@ -573,6 +593,7 @@ public boolean cancelEvent(final String key) { return target != null && target.cancelEvent(key); } L.i("[Events] cancelEvent: key = " + key); + L.recordUsage(HealthCheckCounter.AREA_EVENTS, "cancel"); return cancelEventInternal(key); } } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleHealthCheck.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleHealthCheck.java new file mode 100644 index 000000000..d47ee0a67 --- /dev/null +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleHealthCheck.java @@ -0,0 +1,194 @@ +package ly.count.sdk.java.internal; + +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import ly.count.sdk.java.Session; +import org.json.JSONObject; + +/** + * Sends the SDK health check once per initialization, as an immediate request to {@code /i}, and + * stores the health check counters on the update timer, at the end of a session and when the SDK + * stops. Logs at debug level only, as every warning and error the SDK logs is counted. + */ +public class ModuleHealthCheck extends ModuleBase { + static final String ENDPOINT = "/i?"; + static final String KEY_HEALTH_CHECK = "hc"; + static final String KEY_METRICS = "metrics"; + static final String KEY_APP_VERSION = "_app_version"; + + private final AtomicBoolean healthCheckSent = new AtomicBoolean(false); + @Nullable HealthCheckCounter counter; + + @Override + public void init(InternalConfig config) { + super.init(config); + counter = config.sdk == null ? null : config.sdk.healthCheckCounter; + L.v("[ModuleHealthCheck] init, health check enabled:[" + config.isHealthCheckEnabled() + "]"); + } + + @Override + protected void initFinished(InternalConfig config) { + sendHealthCheck(); + } + + @Override + protected void onTimer() { + HealthCheckCounter current = counter; + if (current != null) { + current.saveState(); + } + } + + @Override + public void onSessionEnded(Session session, InternalConfig config) { + HealthCheckCounter current = counter; + if (current != null) { + current.saveState(); + } + } + + /** + * Stores the counters when the SDK stops and keeps its data. When it clears its data, the + * stored state goes with it. + * + * @param config configuration of the SDK being stopped + * @param clear whether the SDK clears its data + */ + @Override + public void stop(InternalConfig config, boolean clear) { + HealthCheckCounter current = counter; + if (current != null && !clear) { + current.saveState(); + } + counter = null; + } + + /** + * Sends the health check, unless it is disabled, offline mode is on, the SDK behavior settings + * turned networking off, or it was sent already in this initialization. The request carries + * the base request parameters, the app version as metrics while the sessions consent allows + * metrics, and a snapshot of the counters. Only what the snapshot reported is taken away from + * the counters, and only once the server accepted it: a 2xx answer with a JSON object that has + * a "result". Otherwise the counters wait for the next initialization. + */ + void sendHealthCheck() { + L.v("[ModuleHealthCheck] sendHealthCheck, preparing the health check"); + + if (!internalConfig.isHealthCheckEnabled()) { + L.d("[ModuleHealthCheck] sendHealthCheck, the health check is disabled, nothing is sent"); + return; + } + + if (internalConfig.isTemporaryIdEnabled()) { + L.d("[ModuleHealthCheck] sendHealthCheck, offline mode is on, nothing is sent"); + return; + } + + if (!internalConfig.getNetworkingEnabled()) { + L.d("[ModuleHealthCheck] sendHealthCheck, networking is disabled by the SDK behavior settings, the counters wait for the next initialization"); + return; + } + + final HealthCheckCounter current = counter; + SDKCore core = internalConfig.sdk; + Networking networking = core == null ? null : core.networking; + if (current == null || networking == null || networking.getTransport() == null) { + L.d("[ModuleHealthCheck] sendHealthCheck, the SDK has no counters or no networking, nothing is sent"); + return; + } + + if (!healthCheckSent.compareAndSet(false, true)) { + L.d("[ModuleHealthCheck] sendHealthCheck, the health check was already sent in this initialization"); + return; + } + + final JSONObject snapshot = current.snapshot(); + Params params = ModuleRequests.prepareRequiredParams(internalConfig); + String metrics = healthCheckMetrics(); + if (metrics != null) { + params.add(KEY_METRICS, metrics); + } + params.add(KEY_HEALTH_CHECK, snapshot.toString()); + + L.d("[ModuleHealthCheck] sendHealthCheck, sending the health check [" + snapshot + "]"); + internalConfig.immediateRequestGenerator.createImmediateRequestMaker().doWork(params.toString(), ENDPOINT, networking.getTransport(), false, true, response -> { + if (response == null || !response.has("result")) { + L.d("[ModuleHealthCheck] sendHealthCheck, the server did not accept the health check, the counters are kept for the next initialization, response:[" + response + "]"); + return; + } + + if (counter != current) { + L.d("[ModuleHealthCheck] sendHealthCheck, the SDK stopped before the answer arrived, the counters are left as they are"); + return; + } + + L.d("[ModuleHealthCheck] sendHealthCheck, the server accepted the health check"); + current.clearSentValues(snapshot); + }, L); + } + + /** + * The metrics of the health check: only the app version, the overridden value when the + * developer set one. Sent only while the sessions consent allows metrics, as for a session + * start, since this SDK has no consent of its own for metrics. + * + * @return the metrics as JSON text, {@code null} when there is no app version or no consent + */ + @Nullable + String healthCheckMetrics() { + SDKCore core = internalConfig.sdk; + if (core == null || !core.hasConsentForFeature(CoreFeature.Sessions)) { + return null; + } + + String appVersion = internalConfig.getApplicationVersion(); + Map overrides = internalConfig.getMetricOverride(); + if (overrides != null && !Utils.isEmptyOrNull(overrides.get(KEY_APP_VERSION))) { + appVersion = overrides.get(KEY_APP_VERSION); + } + + if (Utils.isEmptyOrNull(appVersion)) { + return null; + } + + return new JSONObject().put(KEY_APP_VERSION, appVersion).toString(); + } + + /** + * Whether the health check was sent in this initialization. + * + * @return {@code true} once it was sent + */ + boolean isHealthCheckSent() { + return healthCheckSent.get(); + } + + /** + * The counters this module sends, for the tests. + * + * @return the counters, {@code null} once the module stopped + */ + @Nullable + HealthCheckCounter counter() { + return counter; + } + + /** + * Creates the counters of a starting SDK, unless it runs in backend mode, where no health + * check is sent. + * + * @param config configuration of the starting SDK + * @param storageProvider where the counters are stored + * @param core the starting SDK, which tells which feature has consent + * @return the counters, {@code null} in backend mode + */ + @Nullable + static HealthCheckCounter createCounter(@Nonnull InternalConfig config, @Nonnull StorageProvider storageProvider, @Nonnull SDKCore core) { + if (config.isBackendModeEnabled()) { + return null; + } + return new HealthCheckCounter(storageProvider, config.getLogger(), core::hasConsentForUsageArea); + } +} diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleRequests.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleRequests.java index 9d0479020..f16107db7 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleRequests.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleRequests.java @@ -224,7 +224,8 @@ public static Future pushAsync(final InternalConfig config, final Reque config.getLogger().d("New request " + request.storageId() + ": " + request); if (!config.getConfigurationProvider().getTrackingEnabled()) { - config.getLogger().d("[ModuleRequests] pushAsync, tracking disabled by SDK behavior settings; dropping request"); + config.getLogger().w("[ModuleRequests] pushAsync, tracking disabled by SDK behavior settings; dropping request"); + config.getLogger().recordLogCode("w505"); if (callback != null) { try { callback.call(false); @@ -377,6 +378,7 @@ static int dropOldestRequestsOverQueueLimit(@Nonnull InternalConfig config) { } config.getLogger().w("[ModuleRequests] dropOldestRequestsOverQueueLimit, the request queue went over its size of [" + maxSize + "] set by the SDK behavior settings, dropped the [" + droppedIds.size() + "] oldest requests"); + config.getLogger().recordLogCode("w310", droppedIds.size()); } catch (Exception e) { config.getLogger().e("[ModuleRequests] dropOldestRequestsOverQueueLimit, failed to keep the request queue within its size, [" + e + "]"); } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleSessions.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleSessions.java index e4bfdcee9..be3139ff0 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleSessions.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleSessions.java @@ -44,9 +44,10 @@ protected void onTimer() { L.d("[ModuleSessions] onTimer, session tracking disabled by SDK behavior settings; skipping update"); return; } - if (!internalConfig.isBackendModeEnabled() && isActive() && getSession() != null) { + SessionImpl running = getSession(); + if (!internalConfig.isBackendModeEnabled() && isActive() && running != null && running.isActive()) { L.i("[ModuleSessions] onTimer, updating session"); - getSession().update(); + running.updateAutomatically(); } } @@ -73,11 +74,11 @@ public void deviceIdChanged(String oldDeviceId, boolean withMerge) { L.d("[ModuleSessions] deviceIdChanged, " + deviceId + ", old " + oldDeviceId); if (!withMerge && session != null && session.isActive()) { L.d("[ModuleSessions] deviceIdChanged, Ending session because device id was unset from [" + oldDeviceId + "]"); - session.end(null, null, oldDeviceId); + session.endAutomatically(null, null, oldDeviceId); } if (deviceId != null && oldDeviceId != null && (session == null || !session.isActive()) && !withMerge) { - session(internalConfig, null).begin(); + session(internalConfig, null).beginAutomatically(); } } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleViews.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleViews.java index 378c3f6c9..165528a70 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleViews.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleViews.java @@ -186,13 +186,19 @@ private void autoCloseRequiredViews(boolean closeAllViews, Map c */ @Nullable String startViewInternal(@Nullable String viewName, @Nullable Map customViewSegmentation, boolean viewShouldBeAutomaticallyStopped) { ConfigurationProvider configProvider = internalConfig.getConfigurationProvider(); - if (!configProvider.getTrackingEnabled() || !configProvider.getViewTrackingEnabled()) { + if (!configProvider.getTrackingEnabled()) { + L.w("[ModuleViews] startViewInternal, tracking disabled by SDK behavior settings; ignoring"); + L.recordLogCode("w505", HealthCheckCounter.AREA_VIEWS); + return null; + } + if (!configProvider.getViewTrackingEnabled()) { L.d("[ModuleViews] startViewInternal, view tracking disabled by SDK behavior settings; ignoring"); return null; } if (viewName == null || viewName.isEmpty()) { - L.e("[ModuleViews] startViewInternal, Trying to record view with null or empty view name, ignoring request"); + L.w("[ModuleViews] startViewInternal, Trying to record view with null or empty view name, ignoring request"); + L.recordLogCode("w710", HealthCheckCounter.AREA_VIEWS); return null; } @@ -304,6 +310,7 @@ void pauseViewWithIDInternal(String viewID) { if (vd.viewStartTimeSeconds == 0) { L.w("[ModuleViews] pauseViewWithIDInternal, pausing a view that is already paused. ID:[" + viewID + "], name:[" + vd.viewName + "]"); + L.recordLogCode("w731", HealthCheckCounter.AREA_VIEWS); return; } @@ -326,6 +333,7 @@ void resumeViewWithIDInternal(String viewID) { if (vd.viewStartTimeSeconds > 0) { L.w("[ModuleViews] resumeViewWithIDInternal, resuming a view that is already running. ID:[" + viewID + "], name:[" + vd.viewName + "]"); + L.recordLogCode("w731", HealthCheckCounter.AREA_VIEWS); return; } @@ -359,12 +367,14 @@ private boolean isViewTrackingEnabled(@Nonnull String function) { private ViewData validateViewID(String viewID, String function) { if (viewID == null || viewID.isEmpty()) { - L.e("[ModuleViews] validateViewID, " + function + ", Trying to process view with null or empty view ID, ignoring request"); + L.w("[ModuleViews] validateViewID, " + function + ", Trying to process view with null or empty view ID, ignoring request"); + L.recordLogCode("w700", HealthCheckCounter.AREA_VIEWS); return null; } if (!viewDataMap.containsKey(viewID)) { L.w("[ModuleViews] validateViewID, " + function + ", there is no view with the provided view id"); + L.recordLogCode("w730", HealthCheckCounter.AREA_VIEWS); return null; } @@ -378,7 +388,8 @@ private ViewData validateViewID(String viewID, String function) { private String validateViewWithName(String viewName, String function) { if (viewName == null || viewName.isEmpty()) { - L.e("[ModuleViews] " + function + ", Trying to process the view with null or empty view name, ignoring request"); + L.w("[ModuleViews] " + function + ", Trying to process the view with null or empty view name, ignoring request"); + L.recordLogCode("w700", HealthCheckCounter.AREA_VIEWS); return null; } @@ -392,7 +403,8 @@ private String validateViewWithName(String viewName, String function) { } if (viewID == null) { - L.e("[ModuleViews] " + function + ", No view entry found with the provided name :[" + viewName + "]"); + L.w("[ModuleViews] " + function + ", No view entry found with the provided name :[" + viewName + "]"); + L.recordLogCode("w730", HealthCheckCounter.AREA_VIEWS); } return viewID; @@ -413,7 +425,8 @@ private void addSegmentationToViewWithIDInternal(String viewID, Map } synchronized (Countly.instance()) { L.i("[Views] Calling stopViewWithID vi[" + viewID + "] sg[" + viewSegmentation + "]"); + L.recordUsage(HealthCheckCounter.AREA_VIEWS, "stop"); stopViewWithIDInternal(viewID, viewSegmentation); } } @@ -622,6 +642,7 @@ public void pauseViewWithID(@Nonnull String viewID) { } synchronized (Countly.instance()) { L.i("[Views] Calling pauseViewWithID vi[" + viewID + "]"); + L.recordUsage(HealthCheckCounter.AREA_VIEWS, "pause"); pauseViewWithIDInternal(viewID); } } @@ -641,6 +662,7 @@ public void resumeViewWithID(@Nonnull String viewID) { } synchronized (Countly.instance()) { L.i("[Views] Calling resumeViewWithID vi[" + viewID + "]"); + L.recordUsage(HealthCheckCounter.AREA_VIEWS, "resume"); resumeViewWithIDInternal(viewID); } } @@ -660,6 +682,7 @@ public void stopAllViews(@Nullable Map viewSegmentation) { } synchronized (Countly.instance()) { L.i("[Views] Calling stopAllViews sg[" + viewSegmentation + "]"); + L.recordUsage(HealthCheckCounter.AREA_VIEWS, "stopAll"); stopAllViewsInternal(viewSegmentation); } } @@ -742,6 +765,7 @@ public void updateGlobalViewSegmentation(@Nullable Map segmentat if (segmentation == null) { L.w("[View] When updating segmentation values, they can't be 'null'."); + L.recordLogCode("w700", HealthCheckCounter.AREA_VIEWS); return; } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKCore.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKCore.java index 88ecd0767..a532399b4 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKCore.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKCore.java @@ -29,6 +29,8 @@ public class SDKCore { private ModuleConfiguration moduleConfiguration; protected Log L = null; + //the health check counters, null in backend mode + volatile HealthCheckCounter healthCheckCounter = null; protected static ModuleBase testDummyModule = null;//set during testing when trying to check the SDK's lifecycle /** * Currently enabled features with consents @@ -195,6 +197,8 @@ public void stop(final boolean clear) { modules.clear(); moduleMappings.clear(); + L.setHealthCheckCounter(null); + healthCheckCounter = null; L.endLogGathering(!clear); sdkStorage.stop(config, clear);//from original super class @@ -232,13 +236,13 @@ public void onConsent(final int consent) { if (addingConsent(consent, CoreFeature.Sessions)) { SessionImpl session = module(ModuleSessions.class).getSession(); - if (session != null) { - session.end(); + if (session != null && session.isActive()) { + session.endAutomatically(null, null, null); } consents = consents | (consent & config.getFeatures1()); - module(ModuleSessions.class).session(config, null).begin(); + module(ModuleSessions.class).session(config, null).beginAutomatically(); } consents = consents | (consent & config.getFeatures1()); @@ -273,8 +277,8 @@ public void onConsentRemoval(final InternalConfig config, int noConsent) { if (removingConsent(noConsent, CoreFeature.Sessions)) { SessionImpl session = module(ModuleSessions.class).getSession(); - if (session != null) { - session.end(); + if (session != null && session.isActive()) { + session.endAutomatically(null, null, null); } } @@ -371,6 +375,11 @@ protected void buildModules(InternalConfig config, int features) throws IllegalA } modules.put(CoreFeature.BackendMode.getIndex(), new ModuleBackendMode()); + //last, so the health check sent at the end of init counts what the other modules logged while starting + if (healthCheckCounter != null) { + modules.put(Integer.MAX_VALUE, new ModuleHealthCheck()); + } + // dummy module for tests if any if (testDummyModule != null) { modules.put(CoreFeature.TestDummy.getIndex(), testDummyModule); @@ -486,6 +495,8 @@ public void init(final InternalConfig givenConfig) { config.sdk = this; sdkStorage.init(config); config.storageProvider = sdkStorage; + healthCheckCounter = ModuleHealthCheck.createCounter(config, sdkStorage, this); + L.setHealthCheckCounter(healthCheckCounter); if (config.immediateRequestGenerator == null) { config.immediateRequestGenerator = ImmediateRequestMaker::new; @@ -858,6 +869,24 @@ public static boolean enabled(CoreFeature feature) { return enabled(feature.getIndex()); } + /** + * Whether calls of a feature are counted in the health check method usage: the feature has + * consent, or no consent is required. Reads only this instance, so it is safe while the SDK stops. + * + * @param feature the feature behind a method usage area + * @return {@code true} when its calls are counted + */ + boolean hasConsentForUsageArea(@Nonnull CoreFeature feature) { + InternalConfig current = config; + if (current == null) { + return false; + } + if (!current.requiresConsent()) { + return true; + } + return (consents & feature.getIndex()) == feature.getIndex(); + } + public boolean hasConsentForFeature(CoreFeature feature) { if (!instance.config.requiresConsent()) { //if no consent required, return true @@ -949,7 +978,8 @@ private boolean processCrash(InternalConfig config, Long id) { crash = Storage.read(config, crash); if (crash == null) { - L.e("Cannot read crash from storage, skipping"); + L.w("[SDKCore] processCrash, Cannot read crash from storage, skipping"); + L.recordLogCode("w504"); return false; } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKStorage.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKStorage.java index 0f5814a8f..45f0531b8 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKStorage.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKStorage.java @@ -34,6 +34,8 @@ public class SDKStorage implements StorageProvider { protected static final String key_remote_config = "rc"; protected static final String key_migration_version = "dv"; protected static final String key_server_config = "sc"; + protected static final String key_health_check = "HEALTH_CHECK"; + protected static final String key_health_check_automatic_crashes = "HEALTH_CHECK_AUTOMATIC_CRASHES"; private JsonFileStorage jsonFileStorage; @@ -120,7 +122,8 @@ public Boolean storableWrite(ly.count.sdk.java.internal.InternalConfig config, S stream.close(); return true; } catch (IOException e) { - L.w("[SDKStorage] Cannot write data to " + filename + " " + e); + L.e("[SDKStorage] Cannot write data to " + filename + " " + e); + L.recordLogCode("e501"); } finally { if (stream != null) { try { @@ -419,6 +422,45 @@ public String getServerConfig() { return jsonFileStorage.getString(key_server_config); } + @Override + public String getHealthCheckState() { + JsonFileStorage storage = jsonFileStorage; + return storage == null ? null : storage.getString(key_health_check); + } + + @Override + public void setHealthCheckState(String state) { + JsonFileStorage storage = jsonFileStorage; + if (storage != null) { + storage.addAndSave(key_health_check, state == null ? "" : state); + } + } + + @Override + public void incrementAutomaticCrashCount() { + JsonFileStorage storage = jsonFileStorage; + if (storage != null) { + synchronized (storage) { + storage.addAndSave(key_health_check_automatic_crashes, storage.getInt(key_health_check_automatic_crashes, 0) + 1); + } + } + } + + @Override + public int takeAutomaticCrashCount() { + JsonFileStorage storage = jsonFileStorage; + if (storage == null) { + return 0; + } + synchronized (storage) { + int count = storage.getInt(key_health_check_automatic_crashes, 0); + if (count != 0) { + storage.deleteAndSave(key_health_check_automatic_crashes); + } + return Math.max(count, 0); + } + } + @Override public boolean isCountlyStorageEmpty() { String[] files = getCountlyFileList(config); diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/SessionImpl.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/SessionImpl.java index b4e881f7c..49c549efb 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/SessionImpl.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/SessionImpl.java @@ -99,21 +99,39 @@ public Session begin() { } L.d("[SessionImpl] begin"); + L.recordUsage(HealthCheckCounter.AREA_SESSIONS, "begin"); begin(null); return this; } + /** + * Begins the session on the SDK's own behalf, counted as "begin:a" in the health check. + */ + void beginAutomatically() { + if (config.isBackendModeEnabled()) { + L.w("[SessionImpl] beginAutomatically: Skipping session begin, backend mode is enabled!"); + return; + } + + L.d("[SessionImpl] beginAutomatically"); + L.recordUsage(HealthCheckCounter.AREA_SESSIONS, "begin:a"); + begin(null); + } + Future begin(Long now) { if (SDKCore.instance == null) { - L.e("[SessionImpl] Countly is not initialized"); + L.w("[SessionImpl] Countly is not initialized"); + Log.recordLogCodeWhileNotInitialized("w101"); return null; } else if (!isSessionTrackingEnabled("begin")) { return null; } else if (began != null) { - L.e("[SessionImpl] Session already began"); + L.w("[SessionImpl] Session already began"); + L.recordLogCode("w760", HealthCheckCounter.AREA_SESSIONS); return null; } else if (ended != null) { - L.e("[SessionImpl] Session already ended"); + L.w("[SessionImpl] Session already ended"); + L.recordLogCode("w760", HealthCheckCounter.AREA_SESSIONS); return null; } else { began = now == null ? System.nanoTime() : now; @@ -144,21 +162,39 @@ public Session update() { } L.d("[SessionImpl] update"); + L.recordUsage(HealthCheckCounter.AREA_SESSIONS, "update"); update(null); return this; } + /** + * Updates the running session on the SDK's own behalf, from the update timer, counted as + * "update:a" in the health check. A session that does not run is left alone. + */ + void updateAutomatically() { + if (config.isBackendModeEnabled() || !isActive()) { + return; + } + + L.d("[SessionImpl] updateAutomatically"); + L.recordUsage(HealthCheckCounter.AREA_SESSIONS, "update:a"); + update(null); + } + Future update(Long now) { if (SDKCore.instance == null) { - L.e("[SessionImpl] Countly is not initialized"); + L.w("[SessionImpl] Countly is not initialized"); + Log.recordLogCodeWhileNotInitialized("w101"); return null; } else if (!isSessionTrackingEnabled("update")) { return null; } else if (began == null) { - L.e("[SessionImpl] Session is not began to update it"); + L.w("[SessionImpl] Session is not began to update it"); + L.recordLogCode("w760", HealthCheckCounter.AREA_SESSIONS); return null; } else if (ended != null) { - L.e("[SessionImpl] Session is already ended to update it"); + L.w("[SessionImpl] Session is already ended to update it"); + L.recordLogCode("w760", HealthCheckCounter.AREA_SESSIONS); return null; } @@ -182,20 +218,43 @@ public void end() { } L.d("[SessionImpl] end"); + L.recordUsage(HealthCheckCounter.AREA_SESSIONS, "end"); end(null, null, null); } + /** + * Ends the session on the SDK's own behalf, counted as "end:a" in the health check. + * + * @param now the time the session ends at, {@code null} for now + * @param callback called once the end request is handled, {@code null} for none + * @param did the device ID the session belongs to, {@code null} for the current one + * @return the end request, {@code null} when nothing was sent + */ + Future endAutomatically(Long now, final Tasks.Callback callback, String did) { + if (config.isBackendModeEnabled()) { + L.w("[SessionImpl] endAutomatically: Skipping session end, backend mode is enabled!"); + return null; + } + + L.d("[SessionImpl] endAutomatically"); + L.recordUsage(HealthCheckCounter.AREA_SESSIONS, "end:a"); + return end(now, callback, did); + } + Future end(Long now, final Tasks.Callback callback, String did) { if (SDKCore.instance == null) { - L.e("[SessionImpl] Countly is not initialized"); + L.w("[SessionImpl] Countly is not initialized"); + Log.recordLogCodeWhileNotInitialized("w101"); return null; } else if (!isSessionTrackingEnabled("end")) { return null; } else if (began == null) { - L.e("[SessionImpl] Session is not began to end it"); + L.w("[SessionImpl] Session is not began to end it"); + L.recordLogCode("w760", HealthCheckCounter.AREA_SESSIONS); return null; } else if (ended != null) { - L.e("[SessionImpl] Session already ended"); + L.w("[SessionImpl] Session already ended"); + L.recordLogCode("w760", HealthCheckCounter.AREA_SESSIONS); return null; } ended = now == null ? System.nanoTime() : now; @@ -244,9 +303,9 @@ Boolean recover(InternalConfig config) { if (began == null) { return Storage.remove(config, this); } else if (ended == null && updated == null) { - future = end(began, null, null); + future = endAutomatically(began, null, null); } else if (ended == null) { - future = end(updated, null, null); + future = endAutomatically(updated, null, null); } else { // began != null && ended != null return Storage.remove(config, this); @@ -318,6 +377,9 @@ public Event event(String key) { * @deprecated use {@link ModuleEvents.Events#startEvent(String)}} instead via instance().events() call */ public Event timedEvent(String key) { + L.w("[SessionImpl] timedEvent, this function is deprecated, use 'events().startEvent' instead"); + L.recordLogCode("w120"); + L.recordUsage(HealthCheckCounter.AREA_EVENTS, "timedEvent:d"); return timedEvents().event(config, key); } @@ -344,7 +406,7 @@ public void recordEvent(Event event) { L.d("[SessionImpl] recordEvent"); if (began == null) { - begin(); + beginAutomatically(); } ModuleEvents eventsModule = (ModuleEvents) SDKCore.instance.module(CoreFeature.Events); @@ -374,6 +436,10 @@ public Session addCrashReport(Throwable t, boolean fatal, String name, Map crashSegments = null; @@ -420,6 +486,9 @@ public View view(String name, boolean start) { return null; } L.d("[SessionImpl] view: start = " + start); + L.w("[SessionImpl] view, this function is deprecated, use 'views().startView' instead"); + L.recordLogCode("w120"); + L.recordUsage(HealthCheckCounter.AREA_VIEWS, "view:d"); if (currentView != null) { currentView.stop(false); } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/StorageProvider.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/StorageProvider.java index da2d7bd4d..e0308a40e 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/StorageProvider.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/StorageProvider.java @@ -83,4 +83,31 @@ public interface StorageProvider { * @return serialized configuration, or {@code null} if none persisted yet */ String getServerConfig(); + + /** + * Read the stored health check counters. + * + * @return the counters as JSON text, {@code null} or empty when none are stored + */ + String getHealthCheckState(); + + /** + * Store the health check counters. + * + * @param state the counters as JSON text, empty to clear them + */ + void setHealthCheckState(String state); + + /** + * Add one automatically detected crash to the count the next start reports in the health check. + * Written right away, without the health check counters, as it runs while the crash is reported. + */ + void incrementAutomaticCrashCount(); + + /** + * Read the automatically detected crashes counted since the last call, and set the stored count back to 0. + * + * @return the stored count of automatically detected crashes + */ + int takeAutomaticCrashCount(); } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java index 315d7e9f2..281b5abab 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java @@ -445,7 +445,7 @@ public Boolean send() { return processResponse(code, response, request.storageId()); } catch (IOException e) { - L.w("[network] Error while sending request " + request + " " + e); + L.d("[network] Error while sending request " + request + " " + e); notifyOwnerOfAttemptWithoutResponse(request, requestOwner); return false; } catch (Exception e) { @@ -511,11 +511,21 @@ private static boolean isSuccessfulResponse(int code, JSONObject response) { return code >= 200 && code < 300 && response.has("result"); } + /** + * Whether the server accepted a queued request. An answer it did not accept is kept for the + * health check, which reports the last one; a request without an answer changes nothing there. + * A failed request is logged at debug level, as the health check reports it on its own. + * + * @param code the response code + * @param response the response body, {@code null} when none could be read + * @param requestId the storage ID of the request, for the log + * @return {@code true} when the server accepted the request + */ Boolean processResponse(int code, String response, Long requestId) { L.i("[network] [processResponse] Code [" + code + "] response [" + response + "] for request[" + requestId + "]"); if (response == null) { - L.w("[network] Null response for request [" + requestId + "]"); + L.d("[network] Null response for request [" + requestId + "]"); return false; } @@ -525,11 +535,13 @@ Boolean processResponse(int code, String response, Long requestId) { L.d("[network] Success"); return true; } else { - L.w("[network] Fail: code :" + code + ", result: " + response); + L.d("[network] Fail: code :" + code + ", result: " + response); + L.logFailedNetworkRequest(code, response); return false; } } catch (Exception e) { - L.w("[network] Failed to parse response as JSON for request [" + requestId + "], response: [" + response + "], error: [" + e.getMessage() + "]"); + L.d("[network] Failed to parse response as JSON for request [" + requestId + "], response: [" + response + "], error: [" + e.getMessage() + "]"); + L.logFailedNetworkRequest(code, response); return false; } } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/Utils.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/Utils.java index 20e784a6f..b7ac34c8f 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/Utils.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/Utils.java @@ -423,6 +423,7 @@ public static void removeInvalidDataFromSegments(Map segments, L toRemove.forEach(key -> { L.w("[Utils] removeInvalidDataFromSegments, In segmentation Data type '" + segments.get(key) + "' of item '" + key + "' isn't valid."); + L.recordLogCode("w206"); segments.remove(key); }); } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/UtilsInternalLimits.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/UtilsInternalLimits.java index 010027f73..9810b2d56 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/UtilsInternalLimits.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/UtilsInternalLimits.java @@ -52,6 +52,7 @@ static String truncateKey(@Nullable String key, int maxKeyLength, @Nonnull Log L String truncated = truncate(key, maxKeyLength); L.w(tag + ", the key [" + key + "] is longer than the key length limit of [" + maxKeyLength + "] of the SDK behavior settings, it is truncated to [" + truncated + "]"); + L.recordLogCode("w201"); return truncated; } @@ -72,6 +73,7 @@ static String truncateValue(@Nullable String value, int maxValueSize, @Nonnull L String truncated = truncate(value, maxValueSize); L.w(tag + ", a value of [" + value.length() + "] characters is longer than the value size limit of [" + maxValueSize + "] of the SDK behavior settings, it is truncated to [" + truncated + "]"); + L.recordLogCode("w202"); return truncated; } @@ -162,6 +164,7 @@ static Map limitSegmentationEntries(@Nullable Map logs.add(message)); + .setLogListener((message, level) -> logs.add(message)) + .disableHealthCheck(); } /** diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleHealthCheckTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleHealthCheckTests.java new file mode 100644 index 000000000..51d79d88b --- /dev/null +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleHealthCheckTests.java @@ -0,0 +1,712 @@ +package ly.count.sdk.java.internal; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Predicate; +import ly.count.sdk.java.Config; +import ly.count.sdk.java.Countly; +import org.json.JSONObject; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import static org.mockito.Mockito.mock; + +@RunWith(JUnit4.class) +public class ModuleHealthCheckTests { + + /** + * One immediate request the SDK made. + */ + static class SentRequest { + final String data; + final String endpoint; + + SentRequest(String data, String endpoint) { + this.data = data; + this.endpoint = endpoint; + } + + Map params() { + Map decoded = new HashMap<>(); + for (Map.Entry param : TestUtils.parseQueryParams(data).entrySet()) { + decoded.put(param.getKey(), Utils.urldecode(param.getValue())); + } + return decoded; + } + + JSONObject healthCheck() { + return new JSONObject(params().get("hc")); + } + } + + /** + * An in-memory storage for the counter tests. + */ + static class MemoryStorage implements StorageProvider { + String healthCheckState = null; + int automaticCrashes = 0; + + @Override public String getDeviceID() { + return null; + } + + @Override public void setDeviceID(String deviceID) { + } + + @Override public String getDeviceIdType() { + return null; + } + + @Override public void setDeviceIdType(String deviceIdTypeString) { + } + + @Override public void setRemoteConfigValues(JSONObject remoteConfigValues) { + } + + @Override public JSONObject getRemoteConfigValues() { + return new JSONObject(); + } + + @Override public Integer getMigrationVersion() { + return 0; + } + + @Override public void setMigrationVersion(Integer migrationVersion) { + } + + @Override public boolean isCountlyStorageEmpty() { + return true; + } + + @Override public void setServerConfig(String config) { + } + + @Override public String getServerConfig() { + return null; + } + + @Override public String getHealthCheckState() { + return healthCheckState; + } + + @Override public void setHealthCheckState(String state) { + healthCheckState = state; + } + + @Override public void incrementAutomaticCrashCount() { + automaticCrashes++; + } + + @Override public int takeAutomaticCrashCount() { + int count = automaticCrashes; + automaticCrashes = 0; + return count; + } + } + + private final List sentRequests = new ArrayList<>(); + private JSONObject serverAnswer = new JSONObject().put("result", "Success"); + + @Before + public void beforeTest() { + TestUtils.createCleanTestState(); + //a start takes the log codes other tests recorded while no SDK ran, so each test begins without them + Countly.instance().init(TestUtils.getBaseConfig()); + TestUtils.createCleanTestState(); + sentRequests.clear(); + serverAnswer = new JSONObject().put("result", "Success"); + } + + @After + public void stop() { + Countly.instance().halt(); + } + + private HealthCheckCounter counter(MemoryStorage storage, Predicate consent) { + return new HealthCheckCounter(storage, mock(Log.class), consent); + } + + private HealthCheckCounter counter(MemoryStorage storage) { + return counter(storage, feature -> true); + } + + /** + * A configuration whose health check is on, as the test base configuration turns it off, with + * an immediate request generator that keeps every request and answers with {@link #serverAnswer}. + */ + private InternalConfig healthCheckConfig() { + Config config = new Config(TestUtils.SERVER_URL, TestUtils.SERVER_APP_KEY, TestUtils.getTestSDirectory()) + .setApplicationVersion(TestUtils.APPLICATION_VERSION) + .setCustomDeviceId(TestUtils.DEVICE_ID) + .disableSdkBehaviorSettingsUpdates() + .enableFeatures(Config.Feature.Events, Config.Feature.Sessions, Config.Feature.Views, Config.Feature.CrashReporting); + return withRecordingServer(new InternalConfig(config)); + } + + private InternalConfig withRecordingServer(InternalConfig config) { + config.immediateRequestGenerator = () -> (requestData, customEndpoint, cp, requestShouldBeDelayed, networkingIsEnabled, callback, log) -> { + sentRequests.add(new SentRequest(requestData, customEndpoint)); + callback.callback(serverAnswer); + }; + return config; + } + + private List healthChecks() { + List found = new ArrayList<>(); + for (SentRequest request : sentRequests) { + if (request.params().containsKey("hc")) { + found.add(request); + } + } + return found; + } + + private HealthCheckCounter runningCounter() { + return SDKCore.instance.module(ModuleHealthCheck.class).counter(); + } + + private static int usage(JSONObject snapshot, String area, String method) { + JSONObject methods = snapshot.getJSONObject("fu").optJSONObject(area); + return methods == null ? 0 : methods.optInt(method, 0); + } + + private static int code(JSONObject snapshot, String code) { + return snapshot.getJSONObject("lc").optInt(code, 0); + } + + /** + * A new counter reports the defaults, with "fu" and "lc" present as empty objects. + */ + @Test + public void counter_defaults() { + JSONObject snapshot = counter(new MemoryStorage()).snapshot(); + Assert.assertEquals(0, snapshot.getLong("el")); + Assert.assertEquals(0, snapshot.getLong("wl")); + Assert.assertEquals(-1, snapshot.getInt("sc")); + Assert.assertEquals("", snapshot.getString("em")); + Assert.assertEquals(0, snapshot.getLong("bom")); + Assert.assertEquals(0, snapshot.getInt("cbom")); + Assert.assertEquals(0, snapshot.getJSONObject("fu").length()); + Assert.assertEquals(0, snapshot.getJSONObject("lc").length()); + } + + /** + * A stored state is restored, entries of the wrong type are skipped, and the saved state uses + * the storage keys of the guide. + */ + @Test + public void counter_restoresAndSavesState() { + MemoryStorage storage = new MemoryStorage(); + storage.healthCheckState = "{\"LErr\":2,\"LWar\":3,\"RStatC\":503,\"REMsg\":\"Service Unavailable\",\"BReq\":4,\"CBReq\":2," + + "\"MU\":{\"events\":{\"record\":5,\"start\":\"x\"},\"views\":7},\"LC\":{\"w740\":1,\"w720\":\"y\"}}"; + + HealthCheckCounter counter = counter(storage); + JSONObject snapshot = counter.snapshot(); + Assert.assertEquals(2, snapshot.getLong("el")); + Assert.assertEquals(3, snapshot.getLong("wl")); + Assert.assertEquals(503, snapshot.getInt("sc")); + Assert.assertEquals("Service Unavailable", snapshot.getString("em")); + Assert.assertEquals(4, snapshot.getLong("bom")); + Assert.assertEquals(2, snapshot.getInt("cbom")); + Assert.assertEquals(5, usage(snapshot, "events", "record")); + Assert.assertEquals(0, usage(snapshot, "events", "start")); + Assert.assertNull(snapshot.getJSONObject("fu").optJSONObject("views")); + Assert.assertEquals(1, code(snapshot, "w740")); + Assert.assertFalse(snapshot.getJSONObject("lc").has("w720")); + + counter.logError(); + counter.saveState(); + JSONObject stored = new JSONObject(storage.healthCheckState); + Assert.assertEquals(3, stored.getLong("LErr")); + Assert.assertEquals(3, stored.getLong("LWar")); + Assert.assertEquals(503, stored.getInt("RStatC")); + Assert.assertEquals("Service Unavailable", stored.getString("REMsg")); + Assert.assertEquals(4, stored.getLong("BReq")); + Assert.assertEquals(2, stored.getInt("CBReq")); + Assert.assertEquals(5, stored.getJSONObject("MU").getJSONObject("events").getInt("record")); + Assert.assertEquals(1, stored.getJSONObject("LC").getInt("w740")); + } + + /** + * A stored state that is not valid JSON is cleared, the counters start from zero and the + * condition is reported as log code e503. + */ + @Test + public void counter_malformedStateIsResetWithE503() { + MemoryStorage storage = new MemoryStorage(); + storage.healthCheckState = "{not json"; + + JSONObject snapshot = counter(storage).snapshot(); + Assert.assertEquals("", storage.healthCheckState); + Assert.assertEquals(0, snapshot.getLong("el")); + Assert.assertEquals(1, code(snapshot, "e503")); + } + + /** + * The automatically detected crashes the previous run counted are reported as "crashes" / + * "record:a" and their stored count goes back to 0. + */ + @Test + public void counter_addsAutomaticCrashesOfThePreviousRun() { + MemoryStorage storage = new MemoryStorage(); + storage.automaticCrashes = 2; + + JSONObject snapshot = counter(storage).snapshot(); + Assert.assertEquals(2, usage(snapshot, "crashes", "record:a")); + Assert.assertEquals(0, storage.automaticCrashes); + Assert.assertEquals(2, new JSONObject(storage.healthCheckState).getJSONObject("MU").getJSONObject("crashes").getInt("record:a")); + } + + /** + * Only the last refused request is kept, status codes outside 1..999 are ignored and the + * message is cut to 1000 characters. + */ + @Test + public void counter_failedRequests() { + HealthCheckCounter counter = counter(new MemoryStorage()); + counter.logFailedNetworkRequest(500, "first"); + counter.logFailedNetworkRequest(0, "ignored"); + counter.logFailedNetworkRequest(1000, "ignored"); + StringBuilder longBody = new StringBuilder(); + for (int a = 0; a < 1200; a++) { + longBody.append('x'); + } + counter.logFailedNetworkRequest(400, longBody.toString()); + + JSONObject snapshot = counter.snapshot(); + Assert.assertEquals(400, snapshot.getInt("sc")); + Assert.assertEquals(1000, snapshot.getString("em").length()); + } + + /** + * "bom" counts every backoff, "cbom" keeps the longest run of backoffs in a row, w320 is + * recorded for every backoff and w321 for every backoff that continues a run. + */ + @Test + public void counter_backoffRuns() { + HealthCheckCounter counter = counter(new MemoryStorage()); + counter.logBackoffRequest(); + counter.logBackoffRequest(); + counter.logBackoffRequest(); + counter.saveState(); + counter.logConsecutiveBackoffRequest(); + counter.logBackoffRequest(); + counter.logBackoffRequest(); + + JSONObject snapshot = counter.snapshot(); + Assert.assertEquals(5, snapshot.getLong("bom")); + Assert.assertEquals(3, snapshot.getInt("cbom")); + Assert.assertEquals(5, code(snapshot, "w320")); + Assert.assertEquals(3, code(snapshot, "w321")); + } + + /** + * Method usage and the log codes of a feature area count only while that feature has consent, + * an unknown area never counts, and every count stops at 65535. + */ + @Test + public void counter_consentAndCap() { + HealthCheckCounter counter = counter(new MemoryStorage(), feature -> feature != CoreFeature.Views); + counter.recordUsage("views", "start"); + counter.recordUsage("events", "record"); + counter.recordUsage("unknown", "call"); + counter.recordUsage("events", ""); + counter.recordLogCode("w730", "views"); + counter.recordLogCode("w720", "events"); + counter.recordLogCode("w101", 70000); + counter.recordLogCode("w101", 1); + + JSONObject snapshot = counter.snapshot(); + Assert.assertEquals(0, usage(snapshot, "views", "start")); + Assert.assertEquals(1, usage(snapshot, "events", "record")); + Assert.assertNull(snapshot.getJSONObject("fu").optJSONObject("unknown")); + Assert.assertEquals(0, code(snapshot, "w730")); + Assert.assertEquals(1, code(snapshot, "w720")); + Assert.assertEquals(HealthCheckCounter.COUNT_CAP, code(snapshot, "w101")); + } + + /** + * While an automatic crash is reported, method usage and log codes are ignored; warnings and + * errors are still counted. Counting resumes afterwards. + */ + @Test + public void counter_crashStop() { + HealthCheckCounter counter = counter(new MemoryStorage()); + counter.stopRecordingForCrash(); + counter.recordUsage("events", "record"); + counter.recordLogCode("w202", 1); + counter.logError(); + counter.resumeRecordingAfterCrash(); + counter.recordUsage("events", "record"); + + JSONObject snapshot = counter.snapshot(); + Assert.assertEquals(1, usage(snapshot, "events", "record")); + Assert.assertEquals(0, code(snapshot, "w202")); + Assert.assertEquals(1, snapshot.getLong("el")); + } + + /** + * An accepted health check takes away only what it reported: what was counted while it was on + * its way stays, and a failed request or a backoff run that changed meanwhile is kept. + */ + @Test + public void counter_clearSentValuesSubtractsWhatWasReported() { + MemoryStorage storage = new MemoryStorage(); + HealthCheckCounter counter = counter(storage); + counter.logWarning(); + counter.logWarning(); + counter.logFailedNetworkRequest(500, "down"); + counter.logBackoffRequest(); + counter.recordUsage("events", "record"); + counter.recordLogCode("w720", 1); + + JSONObject snapshot = counter.snapshot(); + + counter.logWarning(); + counter.logFailedNetworkRequest(400, "bad"); + counter.logBackoffRequest(); + counter.recordUsage("events", "record"); + counter.recordUsage("views", "start"); + + counter.clearSentValues(snapshot); + JSONObject left = counter.snapshot(); + Assert.assertEquals(1, left.getLong("wl")); + Assert.assertEquals(400, left.getInt("sc")); + Assert.assertEquals("bad", left.getString("em")); + Assert.assertEquals(1, left.getLong("bom")); + Assert.assertEquals(2, left.getInt("cbom")); + Assert.assertEquals(1, usage(left, "events", "record")); + Assert.assertEquals(1, usage(left, "views", "start")); + Assert.assertFalse(left.getJSONObject("lc").has("w720")); + Assert.assertEquals(1, new JSONObject(storage.healthCheckState).getLong("LWar")); + + counter.clearSentValues(counter.snapshot()); + JSONObject empty = counter.snapshot(); + Assert.assertEquals(-1, empty.getInt("sc")); + Assert.assertEquals("", empty.getString("em")); + Assert.assertEquals(0, empty.getInt("cbom")); + Assert.assertEquals(0, empty.getJSONObject("fu").length()); + } + + /** + * The health check is sent once, right after init, as an immediate request to /i with the base + * request parameters, the app version as metrics and the counters. The warnings logged before + * the counter existed, such as an invalid init value, are in it. + */ + @Test + public void healthCheck_sentOnceAfterInit() { + InternalConfig config = healthCheckConfig(); + config.setRequestQueueMaxSize(0); + config.setLoggingLevel(Config.LoggingLevel.OFF); + Countly.instance().init(config); + + List healthChecks = healthChecks(); + Assert.assertEquals(1, healthChecks.size()); + SentRequest request = healthChecks.get(0); + Assert.assertEquals("/i?", request.endpoint); + + Map params = request.params(); + TestUtils.validateRequiredParams(params); + Assert.assertEquals("{\"_app_version\":\"" + TestUtils.APPLICATION_VERSION + "\"}", params.get("metrics")); + + JSONObject hc = request.healthCheck(); + for (String key : new String[] { "el", "wl", "sc", "em", "bom", "cbom", "fu", "lc" }) { + Assert.assertTrue("missing " + key, hc.has(key)); + } + Assert.assertTrue(hc.getLong("wl") >= 1); + Assert.assertEquals(1, code(hc, "w121")); + + SDKCore.instance.module(ModuleHealthCheck.class).sendHealthCheck(); + Assert.assertEquals(1, healthChecks().size()); + } + + /** + * An accepted health check takes the reported counts away and stores what is left; a refused + * one leaves the counters for the next initialization. + */ + @Test + public void healthCheck_acceptedClearsRefusedKeeps() { + serverAnswer = new JSONObject().put("result", "Success"); + InternalConfig first = healthCheckConfig(); + first.setRequestQueueMaxSize(0); + Countly.instance().init(first); + Assert.assertEquals(1, code(healthChecks().get(0).healthCheck(), "w121")); + Countly.instance().events().endEvent("never started"); + JSONObject afterAccepted = runningCounter().snapshot(); + Assert.assertEquals(1, code(afterAccepted, "w720")); + Assert.assertEquals(0, code(afterAccepted, "w121")); + Countly.instance().stop(); + + serverAnswer = new JSONObject().put("error", "App does not exist"); + Countly.instance().init(healthCheckConfig()); + Assert.assertEquals(1, code(healthChecks().get(1).healthCheck(), "w720")); + Assert.assertEquals(1, code(runningCounter().snapshot(), "w720")); + + serverAnswer = null; + Countly.instance().stop(); + Countly.instance().init(healthCheckConfig()); + Assert.assertEquals(1, code(runningCounter().snapshot(), "w720")); + } + + /** + * With the health check disabled nothing is sent, but warnings are still counted. + */ + @Test + public void healthCheck_disabled() { + InternalConfig config = healthCheckConfig(); + config.disableHealthCheck(); + Countly.instance().init(config); + + Assert.assertTrue(healthChecks().isEmpty()); + long before = runningCounter().snapshot().getLong("wl"); + Countly.instance().events().cancelEvent("never started"); + Assert.assertEquals(before + 1, runningCounter().snapshot().getLong("wl")); + } + + /** + * While the SDK behavior settings turn networking off, nothing is sent and the counters wait + * for the next initialization. + */ + @Test + public void healthCheck_networkingOff() { + InternalConfig config = healthCheckConfig(); + config.setSdkBehaviorSettings("{\"v\":1,\"t\":1700000000000,\"c\":{\"networking\":false}}"); + Countly.instance().init(config); + + Assert.assertTrue(healthChecks().isEmpty()); + Assert.assertFalse(SDKCore.instance.module(ModuleHealthCheck.class).isHealthCheckSent()); + } + + /** + * Backend mode sends no health check and runs no health check module. + */ + @Test + public void healthCheck_backendMode() { + InternalConfig config = healthCheckConfig(); + config.enableBackendMode(); + Countly.instance().init(config); + + Assert.assertTrue(healthChecks().isEmpty()); + Assert.assertNull(SDKCore.instance.module(ModuleHealthCheck.class)); + } + + /** + * Without the sessions consent, which carries the metrics in this SDK, the health check is sent + * without metrics; the health check itself needs no consent. + */ + @Test + public void healthCheck_withoutConsent() { + InternalConfig config = healthCheckConfig(); + config.setRequiresConsent(true); + Countly.instance().init(config); + + Assert.assertEquals(1, healthChecks().size()); + Assert.assertNull(healthChecks().get(0).params().get("metrics")); + } + + /** + * The overridden app version is the one the health check metrics carry. + */ + @Test + public void healthCheck_appVersionOverride() { + InternalConfig config = healthCheckConfig(); + Map overrides = new HashMap<>(); + overrides.put("_app_version", "9.9.9"); + config.setMetricOverride(overrides); + Countly.instance().init(config); + + Assert.assertEquals("{\"_app_version\":\"9.9.9\"}", healthChecks().get(0).params().get("metrics")); + } + + /** + * Every listed method of the session, event, view and crash features is counted once per call, + * under the names of the guide. + */ + @Test + public void methodUsage_counted() { + Countly.instance().init(healthCheckConfig()); + + Countly.session().begin(); + Countly.session().update(); + Countly.session().end(); + + Countly.instance().events().recordEvent("a"); + Countly.instance().events().recordEvent("b", 2); + Countly.instance().events().startEvent("timed"); + Countly.instance().events().endEvent("timed"); + Countly.instance().events().startEvent("cancelled"); + Countly.instance().events().cancelEvent("cancelled"); + + String first = Countly.instance().views().startView("first"); + Countly.instance().views().startAutoStoppedView("auto"); + Countly.instance().views().pauseViewWithID(first); + Countly.instance().views().resumeViewWithID(first); + Countly.instance().views().stopViewWithID(first); + Countly.instance().views().startView("named"); + Countly.instance().views().stopViewWithName("named"); + Countly.instance().views().stopAllViews(null); + + Countly.instance().crashes().addCrashBreadcrumb("crumb"); + Countly.instance().crashes().recordHandledException(new RuntimeException("handled")); + + JSONObject snapshot = runningCounter().snapshot(); + Assert.assertEquals(1, usage(snapshot, "sessions", "begin")); + Assert.assertEquals(1, usage(snapshot, "sessions", "update")); + Assert.assertEquals(1, usage(snapshot, "sessions", "end")); + Assert.assertEquals(2, usage(snapshot, "events", "record")); + Assert.assertEquals(2, usage(snapshot, "events", "start")); + Assert.assertEquals(1, usage(snapshot, "events", "end")); + Assert.assertEquals(1, usage(snapshot, "events", "cancel")); + Assert.assertEquals(2, usage(snapshot, "views", "start")); + Assert.assertEquals(1, usage(snapshot, "views", "startAuto")); + Assert.assertEquals(1, usage(snapshot, "views", "pause")); + Assert.assertEquals(1, usage(snapshot, "views", "resume")); + Assert.assertEquals(2, usage(snapshot, "views", "stop")); + Assert.assertEquals(1, usage(snapshot, "views", "stopAll")); + Assert.assertEquals(1, usage(snapshot, "crashes", "log")); + Assert.assertEquals(1, usage(snapshot, "crashes", "record")); + } + + /** + * Calls the SDK makes by itself carry ":a", and deprecated calls carry ":d" with log code w120. + */ + @Test + public void methodUsage_automaticAndDeprecated() { + Countly.instance().init(healthCheckConfig()); + + Countly.api().event("legacy").record(); + Countly.api().addCrashReport(new RuntimeException("legacy"), false); + Countly.api().view("legacy view"); + + JSONObject snapshot = runningCounter().snapshot(); + Assert.assertEquals(1, usage(snapshot, "events", "record:d")); + Assert.assertEquals(1, usage(snapshot, "sessions", "begin:a")); + Assert.assertEquals(1, usage(snapshot, "crashes", "addCrashReport:d")); + Assert.assertEquals(1, usage(snapshot, "views", "view:d")); + Assert.assertEquals(3, code(snapshot, "w120")); + } + + /** + * While consent is required, the calls of a feature without consent are not counted, and + * neither are the log codes they record. + */ + @Test + public void methodUsage_followsConsent() { + InternalConfig config = healthCheckConfig(); + config.setRequiresConsent(true); + Countly.instance().init(config); + + Countly.instance().events().recordEvent("no consent"); + Countly.instance().events().endEvent("no consent"); + Assert.assertEquals(0, usage(runningCounter().snapshot(), "events", "record")); + + Countly.onConsent(Config.Feature.Events); + Countly.instance().events().recordEvent("consent"); + Countly.instance().events().endEvent("consent"); + JSONObject snapshot = runningCounter().snapshot(); + Assert.assertEquals(1, usage(snapshot, "events", "record")); + Assert.assertEquals(1, code(snapshot, "w720")); + } + + /** + * Misuse of the event, view and session methods records the codes of the guide. + */ + @Test + public void logCodes_misuse() { + Countly.instance().init(healthCheckConfig()); + + Countly.instance().events().recordEvent(""); + Countly.instance().events().recordEvent("count", 0); + Countly.instance().events().startEvent("twice"); + Countly.instance().events().startEvent("twice"); + Countly.instance().events().endEvent("not running"); + Countly.instance().events().cancelEvent("not running"); + Countly.instance().events().startEvent(""); + Countly.instance().views().startView(""); + Countly.instance().views().stopViewWithID("unknown"); + String view = Countly.instance().views().startView("view"); + Countly.instance().views().pauseViewWithID(view); + Countly.instance().views().pauseViewWithID(view); + Countly.session().begin(); + Countly.session().begin(); + Countly.onConsent(); + + JSONObject snapshot = runningCounter().snapshot(); + Assert.assertEquals(1, code(snapshot, "w701")); + Assert.assertEquals(1, code(snapshot, "w750")); + Assert.assertEquals(1, code(snapshot, "w722")); + Assert.assertEquals(1, code(snapshot, "w720")); + Assert.assertEquals(1, code(snapshot, "w721")); + Assert.assertEquals(1, code(snapshot, "w700")); + Assert.assertEquals(1, code(snapshot, "w710")); + Assert.assertEquals(1, code(snapshot, "w730")); + Assert.assertEquals(1, code(snapshot, "w731")); + Assert.assertEquals(1, code(snapshot, "w760")); + Assert.assertEquals(1, code(snapshot, "w402")); + } + + /** + * A call made before init is reported as w101 by the health check of the next start. + */ + @Test + public void logCodes_callBeforeInit() { + Countly.instance().events().recordEvent("too early"); + Countly.instance().init(healthCheckConfig()); + + Assert.assertEquals(1, code(healthChecks().get(0).healthCheck(), "w101")); + } + + /** + * A queued request the server refuses is reported in "sc" and "em" as plain text; a request + * the server accepts changes neither. + */ + @Test + public void failedQueuedRequest_reported() { + Countly.instance().init(healthCheckConfig()); + Transport transport = SDKCore.instance.networking.getTransport(); + + Assert.assertFalse(transport.processResponse(400, "{\"result\":\"App does not exist\"}", 1L)); + Assert.assertTrue(transport.processResponse(200, "{\"result\":\"Success\"}", 2L)); + + JSONObject snapshot = runningCounter().snapshot(); + Assert.assertEquals(400, snapshot.getInt("sc")); + Assert.assertEquals("{\"result\":\"App does not exist\"}", snapshot.getString("em")); + } + + /** + * The counters are stored when the SDK stops and keeps its data, and the next start reports them. + */ + @Test + public void state_storedOnStopAndReportedNextStart() { + serverAnswer = null; + Countly.instance().init(healthCheckConfig()); + Countly.instance().events().cancelEvent("not running"); + Countly.instance().stop(); + + JSONObject stored = new JSONObject((String) TestUtils.getJsonStorageProperty("HEALTH_CHECK")); + Assert.assertEquals(1, stored.getJSONObject("LC").getInt("w721")); + + Countly.instance().init(healthCheckConfig()); + Assert.assertEquals(1, code(healthChecks().get(1).healthCheck(), "w721")); + } + + /** + * An automatically detected crash is counted as a stored number, which the next start reports + * as "crashes" / "record:a". + */ + @Test + public void automaticCrash_reportedNextStart() { + Countly.instance().init(healthCheckConfig()); + SDKCore.instance.config.storageProvider.incrementAutomaticCrashCount(); + Countly.instance().stop(); + + Countly.instance().init(healthCheckConfig()); + Assert.assertEquals(1, usage(healthChecks().get(1).healthCheck(), "crashes", "record:a")); + } +} diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java index ebc0d03af..1d9006fea 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java @@ -89,6 +89,9 @@ static Config getConfigSdkBehaviorSettings(String deviceID) { Config config = new Config(SERVER_URL, SERVER_APP_KEY, sdkStorageRootDirectory); config.setApplicationVersion(APPLICATION_VERSION); config.setCustomDeviceId(deviceID); + // Off by default in tests, so no init sends a health check to the test host or counts as an + // immediate request; ModuleHealthCheckTests builds its own configuration with it on. + config.disableHealthCheck(); return config; }