diff --git a/backtrace-library/src/androidTest/java/backtraceio/library/metrics/BacktraceMetricsTest.java b/backtrace-library/src/androidTest/java/backtraceio/library/metrics/BacktraceMetricsTest.java index db5477a7..0c69dc60 100644 --- a/backtrace-library/src/androidTest/java/backtraceio/library/metrics/BacktraceMetricsTest.java +++ b/backtrace-library/src/androidTest/java/backtraceio/library/metrics/BacktraceMetricsTest.java @@ -36,7 +36,6 @@ public class BacktraceMetricsTest { private final String[] uniqueAttributeName = {"uname.version", "cpu.boottime", "screen.orientation", "battery.state", "device.airplane_mode", "device.sdk", "device.brand", "system.memory.total", "uname.sysname", "application.package"}; private final String token = "aaaaabbbbbccccf82668682e69f59b38e0a853bed941e08e85f4bf5eb2c5458"; - private final String universeName = "testing-universe-name"; /** * NOTE: Some of these tests are very time-sensitive so you may occasionally get false negative results. @@ -76,16 +75,75 @@ public void addAttributesUniqueEvent() { @Test public void testDefaultUrl() { - BacktraceMetrics metrics = new BacktraceMetrics(context, new HashMap(), null, credentials); - metrics.enable(new BacktraceMetricsSettings(credentials)); + // GIVEN + BacktraceMetrics metrics = new BacktraceMetrics(context, new HashMap<>(), null, credentials); + BacktraceMetricsSettings settings = new BacktraceMetricsSettings(credentials); + // WHEN + metrics.enable(settings); + + // THEN TestCase.assertEquals(BacktraceMetrics.defaultBaseUrl, metrics.getBaseUrl()); + TestCase.assertTrue(settings.isBacktraceServer()); } - @Test - public void testCustomUrl() { - String customUrl = "https://my.custom.url"; - BacktraceMetrics metrics = new BacktraceMetrics(context, new HashMap(), null, credentials); - metrics.enable(new BacktraceMetricsSettings(credentials, customUrl)); - TestCase.assertEquals(customUrl, metrics.getBaseUrl()); + @Test(expected = IllegalStateException.class) + public void tryToEnableMetricsTwoTimes() { + // GIVEN + BacktraceMetrics metrics = new BacktraceMetrics(context, new HashMap<>(), null, credentials); + BacktraceMetricsSettings settings = new BacktraceMetricsSettings(credentials); + // WHEN + metrics.enable(settings); + metrics.enable(settings); + } + + @Test(expected = IllegalArgumentException.class) + public void tryToEnableMetricsOnCustomServer() { + // GIVEN + BacktraceCredentials customCredentials = new BacktraceCredentials("https://custom.on.premise.server.io:6098", token); + BacktraceMetrics metrics = new BacktraceMetrics(context, new HashMap<>(), null, customCredentials); + + // WHEN + metrics.enable(); + } + + @Test(expected = IllegalArgumentException.class) + public void tryToAddSummedEventOnCustomServer() { + // GIVEN + BacktraceCredentials customCredentials = new BacktraceCredentials("https://custom.on.premise.server.io:6098", token); + BacktraceMetrics metrics = new BacktraceMetrics(context, new HashMap<>(), null, customCredentials); + + // WHEN + metrics.addSummedEvent("demo", new HashMap() {}); + } + + @Test(expected = IllegalArgumentException.class) + public void tryToAddUniqueEventOnCustomServer() { + // GIVEN + BacktraceCredentials customCredentials = new BacktraceCredentials("https://custom.on.premise.server.io:6098", token); + BacktraceMetrics metrics = new BacktraceMetrics(context, new HashMap<>(), null, customCredentials); + + // WHEN + metrics.addUniqueEvent("demo", new HashMap() {}); + } + + @Test(expected = IllegalArgumentException.class) + public void tryToSendOnCustomServer() { + // GIVEN + BacktraceCredentials customCredentials = new BacktraceCredentials("https://custom.on.premise.server.io:6098", token); + BacktraceMetrics metrics = new BacktraceMetrics(context, new HashMap<>(), null, customCredentials); + + // WHEN + metrics.send(); } + + @Test(expected = IllegalArgumentException.class) + public void tryToSendStartupEventOnCustomServer() { + // GIVEN + BacktraceCredentials customCredentials = new BacktraceCredentials("https://custom.on.premise.server.io:6098", token); + BacktraceMetrics metrics = new BacktraceMetrics(context, new HashMap<>(), null, customCredentials); + + // WHEN + metrics.sendStartupEvent(); + } + } diff --git a/backtrace-library/src/main/java/backtraceio/library/BacktraceCredentials.java b/backtrace-library/src/main/java/backtraceio/library/BacktraceCredentials.java index 8e8e48a4..f9c2a485 100644 --- a/backtrace-library/src/main/java/backtraceio/library/BacktraceCredentials.java +++ b/backtrace-library/src/main/java/backtraceio/library/BacktraceCredentials.java @@ -2,10 +2,15 @@ import android.net.Uri; +import backtraceio.library.logger.BacktraceLogger; + /** * Backtrace credentials information */ public class BacktraceCredentials { + + private final static transient String LOG_TAG = BacktraceCredentials.class.getSimpleName(); + /** * Data format */ @@ -91,13 +96,15 @@ public String getUniverseName() { int universeIndexStart = backtraceSubmitUrl.length(); int universeIndexEnd = submissionUrl.indexOf('/', universeIndexStart); if (universeIndexEnd == -1) { - throw new IllegalArgumentException("Invalid Backtrace URL"); + BacktraceLogger.w(LOG_TAG, "Missed universe in server URL: " + submissionUrl); + return null; } return submissionUrl.substring(universeIndexStart, universeIndexEnd); } else { final String backtraceDomain = "backtrace.io"; if (submissionUrl.indexOf(backtraceDomain) == -1) { - throw new IllegalArgumentException("Invalid Backtrace URL"); + BacktraceLogger.w(LOG_TAG, "Missed universe in server URL: " + submissionUrl); + return null; } Uri uri = Uri.parse(submissionUrl); @@ -108,8 +115,8 @@ public String getUniverseName() { /** * Get an access token to Backtrace server API * - * @return: access token - * @note: Using algorithm from backtrace-unity https://github.com/backtrace-labs/backtrace-unity/blob/553aab2b39c318ff96ebed4bc739bf2c87304649/Runtime/Model/BacktraceConfiguration.cs#L320 + * Note: Using algorithm from backtrace-unity https://github.com/backtrace-labs/backtrace-unity/blob/553aab2b39c318ff96ebed4bc739bf2c87304649/Runtime/Model/BacktraceConfiguration.cs#L320 + * @return access token */ public String getSubmissionToken() { if (submissionToken != null) diff --git a/backtrace-library/src/main/java/backtraceio/library/models/BacktraceMetricsSettings.java b/backtrace-library/src/main/java/backtraceio/library/models/BacktraceMetricsSettings.java index 91b783e1..272023b1 100644 --- a/backtrace-library/src/main/java/backtraceio/library/models/BacktraceMetricsSettings.java +++ b/backtrace-library/src/main/java/backtraceio/library/models/BacktraceMetricsSettings.java @@ -89,4 +89,8 @@ public int getTimeBetweenRetriesMillis() { public String getSubmissionUrl(String urlPrefix) { return this.getBaseUrl() + "/" + urlPrefix + "/submit?token=" + this.getToken() + "&universe=" + this.getUniverseName(); } + + public boolean isBacktraceServer() { + return this.getUniverseName() != null; + } } diff --git a/backtrace-library/src/main/java/backtraceio/library/services/BacktraceEventsHandler.java b/backtrace-library/src/main/java/backtraceio/library/services/BacktraceEventsHandler.java index 07009a50..50e6ff60 100644 --- a/backtrace-library/src/main/java/backtraceio/library/services/BacktraceEventsHandler.java +++ b/backtrace-library/src/main/java/backtraceio/library/services/BacktraceEventsHandler.java @@ -90,7 +90,7 @@ public BacktraceEventsHandler(BacktraceMetrics backtraceMetrics, long timeIntervalMillis = backtraceMetrics.settings.getTimeIntervalMillis(); - BacktraceAttributes backtraceAttributes = new BacktraceAttributes(backtraceMetrics.context, null, null); + BacktraceAttributes backtraceAttributes = new BacktraceAttributes(backtraceMetrics.getContext(), null, null); this.application = backtraceAttributes.getApplicationName(); this.appVersion = backtraceAttributes.getApplicationVersionOrEmpty(); diff --git a/backtrace-library/src/main/java/backtraceio/library/services/BacktraceMetrics.java b/backtrace-library/src/main/java/backtraceio/library/services/BacktraceMetrics.java index d1fc1043..3ffd2f97 100644 --- a/backtrace-library/src/main/java/backtraceio/library/services/BacktraceMetrics.java +++ b/backtrace-library/src/main/java/backtraceio/library/services/BacktraceMetrics.java @@ -13,7 +13,6 @@ import backtraceio.library.common.BacktraceTimeHelper; import backtraceio.library.events.EventsOnServerResponseEventListener; import backtraceio.library.events.EventsRequestHandler; -import backtraceio.library.events.RequestHandler; import backtraceio.library.interfaces.Api; import backtraceio.library.interfaces.Metrics; import backtraceio.library.logger.BacktraceLogger; @@ -66,6 +65,11 @@ public final class BacktraceMetrics implements Metrics { */ private final String startupSummedEventName = "Application Launches"; + /** + * Is gathering and sending metrics enabled, supported only on Backtrace servers + */ + private boolean enabled; + /** * Unique Events handler */ @@ -79,17 +83,17 @@ public final class BacktraceMetrics implements Metrics { /** * Custom attributes provided by the user to BacktraceBase */ - protected Map customReportAttributes; + Map customReportAttributes; /** * The application context */ - protected Context context; + private final Context context; /** * Backtrace metrics settings */ - protected BacktraceMetricsSettings settings = null; + BacktraceMetricsSettings settings = null; /** * Name of the unique event that will be generated on app startup @@ -102,11 +106,6 @@ public final class BacktraceMetrics implements Metrics { */ private int maximumNumberOfEvents = 350; - /** - * User custom request method - */ - private final RequestHandler requestHandler = null; - /** * Backtrace API class for metrics sending */ @@ -131,6 +130,10 @@ public BacktraceMetrics(Context context, @NotNull Map customRepo this.credentials = credentials; } + public Context getContext() { + return context; + } + /** * Enables metrics with BacktraceClient's credentials. */ @@ -155,11 +158,21 @@ public void enable(BacktraceMetricsSettings settings) { } public void enable(BacktraceMetricsSettings settings, String uniqueEventName) { + if (this.enabled) { + throw new IllegalStateException("Metrics already enabled"); + } + + if (!settings.isBacktraceServer()) { + throw new IllegalArgumentException("Unsupported metrics server"); + } + if (uniqueEventName == null || uniqueEventName.length() == 0) { throw new IllegalArgumentException("Unique event name must be defined!"); } + setStartupUniqueEventName(uniqueEventName); this.settings = settings; + this.enabled = true; try { startMetricsEventHandlers(backtraceApi); sendStartupEvent(); @@ -169,15 +182,18 @@ public void enable(BacktraceMetricsSettings settings, String uniqueEventName) { } } + private void verifyIfMetricsAvailable() { + if (!enabled) { + throw new IllegalArgumentException("Metrics are not available!"); + } + } + private void startMetricsEventHandlers(Api backtraceApi) { + verifyIfMetricsAvailable(); uniqueEventsHandler = backtraceApi.enableUniqueEvents(this); summedEventsHandler = backtraceApi.enableSummedEvents(this); } - protected String getStartupUniqueEventName() { - return this.startupUniqueEventName; - } - public void setStartupUniqueEventName(String startupUniqueEventName) { this.startupUniqueEventName = startupUniqueEventName; } @@ -190,6 +206,7 @@ public String getBaseUrl() { * Send startup event to Backtrace */ public void sendStartupEvent() { + verifyIfMetricsAvailable(); addUniqueEvent(startupUniqueEventName); addSummedEvent(startupSummedEventName); uniqueEventsHandler.send(); @@ -200,6 +217,7 @@ public void sendStartupEvent() { * Send all outgoing messages (unique and summed) currently queued */ public void send() { + verifyIfMetricsAvailable(); uniqueEventsHandler.send(); summedEventsHandler.send(); } @@ -222,6 +240,8 @@ public boolean addUniqueEvent(String attributeName) { * @return true if success */ public boolean addUniqueEvent(String attributeName, Map attributes) { + verifyIfMetricsAvailable(); + if (!shouldProcessEvent(attributeName)) { BacktraceLogger.w(LOG_TAG, "Skipping report"); return false; @@ -295,12 +315,14 @@ public boolean addSummedEvent(String metricGroupName) { * @return true if success */ public boolean addSummedEvent(String metricGroupName, Map attributes) { + verifyIfMetricsAvailable(); + if (!shouldProcessEvent(metricGroupName)) { BacktraceLogger.w(LOG_TAG, "Skipping report"); return false; } - Map localAttributes = new HashMap(); + Map localAttributes = new HashMap<>(); if (attributes != null) { localAttributes.putAll(attributes); } @@ -361,7 +383,7 @@ private boolean shouldProcessEvent(String name) { } protected Map createLocalAttributes(Map attributes) { - Map localAttributes = new HashMap(); + Map localAttributes = new HashMap<>(); if (attributes != null) { localAttributes.putAll(attributes);