Skip to content

Commit

Permalink
Merge pull request #122 from backtrace-labs/metrics-fix-unsupported-s…
Browse files Browse the repository at this point in the history
…erver

Metrics fix unsupported server
  • Loading branch information
BartoszLitwiniuk authored May 13, 2024
2 parents 0c9ebfd + 06130f3 commit 091988d
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -76,16 +75,75 @@ public void addAttributesUniqueEvent() {

@Test
public void testDefaultUrl() {
BacktraceMetrics metrics = new BacktraceMetrics(context, new HashMap<String, Object>(), 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<String, Object>(), 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<String, Object>() {});
}

@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<String, Object>() {});
}

@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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand All @@ -79,17 +83,17 @@ public final class BacktraceMetrics implements Metrics {
/**
* Custom attributes provided by the user to BacktraceBase
*/
protected Map<String, Object> customReportAttributes;
Map<String, Object> 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
Expand All @@ -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
*/
Expand All @@ -131,6 +130,10 @@ public BacktraceMetrics(Context context, @NotNull Map<String, Object> customRepo
this.credentials = credentials;
}

public Context getContext() {
return context;
}

/**
* Enables metrics with BacktraceClient's credentials.
*/
Expand All @@ -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();
Expand All @@ -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;
}
Expand All @@ -190,6 +206,7 @@ public String getBaseUrl() {
* Send startup event to Backtrace
*/
public void sendStartupEvent() {
verifyIfMetricsAvailable();
addUniqueEvent(startupUniqueEventName);
addSummedEvent(startupSummedEventName);
uniqueEventsHandler.send();
Expand All @@ -200,6 +217,7 @@ public void sendStartupEvent() {
* Send all outgoing messages (unique and summed) currently queued
*/
public void send() {
verifyIfMetricsAvailable();
uniqueEventsHandler.send();
summedEventsHandler.send();
}
Expand All @@ -222,6 +240,8 @@ public boolean addUniqueEvent(String attributeName) {
* @return true if success
*/
public boolean addUniqueEvent(String attributeName, Map<String, Object> attributes) {
verifyIfMetricsAvailable();

if (!shouldProcessEvent(attributeName)) {
BacktraceLogger.w(LOG_TAG, "Skipping report");
return false;
Expand Down Expand Up @@ -295,12 +315,14 @@ public boolean addSummedEvent(String metricGroupName) {
* @return true if success
*/
public boolean addSummedEvent(String metricGroupName, Map<String, Object> attributes) {
verifyIfMetricsAvailable();

if (!shouldProcessEvent(metricGroupName)) {
BacktraceLogger.w(LOG_TAG, "Skipping report");
return false;
}

Map<String, Object> localAttributes = new HashMap<String, Object>();
Map<String, Object> localAttributes = new HashMap<>();
if (attributes != null) {
localAttributes.putAll(attributes);
}
Expand Down Expand Up @@ -361,7 +383,7 @@ private boolean shouldProcessEvent(String name) {
}

protected Map<String, Object> createLocalAttributes(Map<String, Object> attributes) {
Map<String, Object> localAttributes = new HashMap<String, Object>();
Map<String, Object> localAttributes = new HashMap<>();

if (attributes != null) {
localAttributes.putAll(attributes);
Expand Down

0 comments on commit 091988d

Please sign in to comment.