Skip to content

Commit

Permalink
refactor(telemetry): replace static factories for TelemetryEventTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbuon committed Jul 10, 2024
1 parent 49177f8 commit 935c933
Show file tree
Hide file tree
Showing 7 changed files with 472 additions and 166 deletions.
60 changes: 50 additions & 10 deletions rollbar-android/src/main/java/com/rollbar/android/Rollbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import android.util.Log;
import com.rollbar.android.notifier.sender.ConnectionAwareSenderFailureStrategy;
import com.rollbar.android.provider.ClientProvider;
import com.rollbar.api.payload.data.TelemetryEvent;
import com.rollbar.api.payload.data.TelemetryType;
import com.rollbar.notifier.config.ConfigProvider;
import com.rollbar.notifier.uncaughtexception.RollbarUncaughtExceptionHandler;
import com.rollbar.android.provider.NotifierProvider;
Expand Down Expand Up @@ -499,9 +499,6 @@ public void configure(Config config) {
this.rollbar.configure(config);
}

public void addEvent(TelemetryEvent telemetryEvent) {
rollbar.addEvent(telemetryEvent);
}
/**
* Record a critical error.
*
Expand Down Expand Up @@ -798,7 +795,7 @@ public void debug(Throwable error, Map<String, Object> custom, String descriptio
}

/**
* Log an error at the level returned by {@link com.rollbar.notifier.Rollbar#level}.
* Log an error at the level returned by {@link Level}.
*
* @param error the error.
*/
Expand All @@ -808,7 +805,7 @@ public void log(Throwable error) {

/**
* Record an error with human readable description at the default level returned by {@link
* com.rollbar.notifier.Rollbar#level}.
* Level}.
*
* @param error the error.
* @param description human readable description of error.
Expand All @@ -819,7 +816,7 @@ public void log(Throwable error, String description) {

/**
* Record an error with extra information attached at the default level returned by {@link
* com.rollbar.notifier.Rollbar#level}.
* Level}.
*
* @param error the error.
* @param custom the extra information.
Expand Down Expand Up @@ -862,7 +859,7 @@ public void log(Throwable error, String description, Level level) {

/**
* Record an error with custom parameters and human readable description at the default level
* returned by {@link com.rollbar.notifier.Rollbar#level}.
* returned by {@link Level}.
*
* @param error the error.
* @param custom the custom data.
Expand All @@ -873,7 +870,7 @@ public void log(Throwable error, Map<String, Object> custom, String description)
}

/**
* Record a debugging message at the level returned by {@link com.rollbar.notifier.Rollbar#level} (WARNING unless level
* Record a debugging message at the level returned by {@link Level} (WARNING unless level
* is overridden).
*
* @param message the message.
Expand All @@ -884,7 +881,7 @@ public void log(String message) {

/**
* Record a message with extra information attached at the default level returned by {@link
* com.rollbar.notifier.Rollbar#level}, (WARNING unless level overridden).
* Level}, (WARNING unless level overridden).
*
* @param message the message.
* @param custom the extra information.
Expand Down Expand Up @@ -930,6 +927,49 @@ public void log(final Throwable error, final Map<String, Object> custom, final S
rollbar.log(error, custom, description, level);
}

/**
* Record log telemetry event. ({@link TelemetryType#LOG}).
*
* @param level the TelemetryEvent severity (e.g. {@link Level#DEBUG}).
* @param message the message sent for this event (e.g. "hello world").
*/
public void recordLogEventFor(Level level, final String message) {
rollbar.recordLogEventFor(level, message);
}

/**
* Record manual telemetry event. ({@link TelemetryType#MANUAL})
*
* @param level the TelemetryEvent severity (e.g. {@link Level#DEBUG}).
* @param message the message sent for this event (e.g. "hello world").
*/
public void recordManualEventFor(Level level, final String message) {
rollbar.recordManualEventFor(level, message);
}

/**
* Record navigation telemetry event with from (origin) and to (destination).({@link TelemetryType#NAVIGATION})
*
* @param level the TelemetryEvent severity (e.g. {@link Level#DEBUG}).
* @param from the starting point (e.g. "SettingView").
* @param to the destination point (e.g. "HomeView").
*/
public void recordNavigationEventFor(Level level, final String from, final String to) {
rollbar.recordNavigationEventFor(level, from, to);
}

/**
* Record network telemetry event with method, url, and status code.({@link TelemetryType#NETWORK})
*
* @param level the TelemetryEvent severity (e.g. {@link Level#DEBUG}).
* @param method the verb used (e.g. "POST").
* @param url the api url (e.g. "<a href="http://rollbar.com/test/api">http://rollbar.com/test/api</a>").
* @param statusCode the response status code (e.g. "404").
*/
public void recordNetworkEventFor(Level level, final String method, final String url, final String statusCode) {
rollbar.recordNetworkEventFor(level, method, url, statusCode);
}

/**
* Send payload to Rollbar.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,161 +8,80 @@
import java.util.Map;
import java.util.Objects;

public class TelemetryEvent implements JsonSerializable, StringTruncatable<TelemetryEvent> {
/**
* Represents an event that allows you to leave a 'breadcrumb' leading up to an exception.
*/
public class TelemetryEvent implements JsonSerializable, StringTruncatable<TelemetryEvent> {

private final TelemetryType type;
private final Level level;
private final Long timestamp;
private final Map<String, String> body;
private static final long serialVersionUID = 2843361810242481727L;
private static final String SOURCE = "client";
private static final String LOG_KEY_MESSAGE = "message";
private static final String NAVIGATION_KEY_FROM = "from";
private static final String NAVIGATION_KEY_TO = "to";
private static final String NETWORK_KEY_METHOD = "method";
private static final String NETWORK_KEY_URL = "url";
private static final String NETWORK_KEY_STATUS_CODE = "status_code";
private final TelemetryType type;
private final Level level;
private final Long timestamp;
private final Map<String, String> body;
private final String source;
private static final long serialVersionUID = 2843361810242481727L;

private TelemetryEvent(
public TelemetryEvent(
TelemetryType telemetryType,
Level level,
Long timestamp,
String source,
Map<String, String> body
) {
timestamp = System.currentTimeMillis();
type = telemetryType;
this.level = level;
this.body = body;
}

private TelemetryEvent(
Long timestamp,
TelemetryType telemetryType,
Level level,
Map<String, String> body
) {
this.timestamp = timestamp;
type = telemetryType;
this.level = level;
this.body = body;
}

/**
* Creates a Log TelemetryEvent ({@link TelemetryType#LOG}).
*
* @param level the TelemetryEvent severity (e.g. {@link Level#DEBUG}).
* @param message the message sent for this event (e.g. "hello world").
*/
public static TelemetryEvent log(Level level, final String message) {
Map<String, String> body = new HashMap<String, String>() {
private static final long serialVersionUID = 3746979871039874692L;
{
put(LOG_KEY_MESSAGE, message);
}
};
return new TelemetryEvent(TelemetryType.LOG, level, body);
}
) {
type = telemetryType;
this.timestamp = timestamp;
this.level = level;
this.source = source;
this.body = body;
}

/**
* Creates a Manual TelemetryEvent ({@link TelemetryType#MANUAL}) .
*
* @param level the TelemetryEvent severity (e.g. {@link Level#DEBUG}).
* @param message the message sent for this event (e.g. "hello world").
*/
public static TelemetryEvent manual(Level level, final String message) {
Map<String, String> body = new HashMap<String, String>() {
private static final long serialVersionUID = 3746979871039874692L;
{
put(LOG_KEY_MESSAGE, message);
}
};
return new TelemetryEvent(TelemetryType.MANUAL, level, body);
}
@Override
public Map<String, Object> asJson() {
Map<String, Object> values = new HashMap<>();
values.put("type", type.asJson());
values.put("level", level.asJson());
values.put("source", source);
values.put("timestamp_ms", timestamp);
values.put("body", body);
return values;
}

/**
* Creates a Navigation TelemetryEvent ({@link TelemetryType#NAVIGATION}) .
*
* @param level the TelemetryEvent severity (e.g. {@link Level#DEBUG}).
* @param from the starting point (e.g. "SettingView").
* @param to the destination point (e.g. "HomeView").
*/
public static TelemetryEvent navigation(Level level, final String from, final String to) {
Map<String, String> body = new HashMap<String, String>() {
private static final long serialVersionUID = 3746979871039874692L;
{
put(NAVIGATION_KEY_FROM, from);
put(NAVIGATION_KEY_TO, to);
}
};
return new TelemetryEvent(TelemetryType.NAVIGATION, level, body);
@Override
public TelemetryEvent truncateStrings(int maxLength) {
Map<String, String> truncatedMap = new HashMap<>();
for (Map.Entry<String, String> entry : body.entrySet()) {
String truncatedValue = TruncationHelper.truncateString(entry.getValue(), maxLength);
truncatedMap.put(entry.getKey(), truncatedValue);
}
return new TelemetryEvent(
this.type,
this.level,
this.timestamp,
this.source,
truncatedMap
);
}

/**
* Creates a Network TelemetryEvent ({@link TelemetryType#NETWORK}).
*
* @param level the TelemetryEvent severity (e.g. {@link Level#DEBUG}).
* @param method the verb used (e.g. "POST").
* @param url the api url (e.g. "<a href="http://rollbar.com/test/api">http://rollbar.com/test/api</a>").
* @param statusCode the response status code (e.g. "404").
*/
public static TelemetryEvent network(Level level, final String method, final String url, final String statusCode) {
Map<String, String> body = new HashMap<String, String>() {
private static final long serialVersionUID = 3746979871039874692L;
{
put(NETWORK_KEY_METHOD, method);
put(NETWORK_KEY_URL, url);
put(NETWORK_KEY_STATUS_CODE, statusCode);
}
};
return new TelemetryEvent(TelemetryType.NETWORK, level, body);
}
@Override
public int hashCode() {
return Objects.hash(type, level, body);
}

@Override
public Map<String, Object> asJson() {
Map<String, Object> values = new HashMap<>();
values.put("type", type.asJson());
values.put("level", level.asJson());
values.put("source", SOURCE);
values.put("timestamp_ms", timestamp);
values.put("body", body);
return values;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TelemetryEvent that = (TelemetryEvent) o;
return Objects.equals(type, that.type) && Objects.equals(level, that.level) && Objects.equals(timestamp, that.timestamp) && Objects.equals(body, that.body);
}

@Override
public TelemetryEvent truncateStrings(int maxLength) {
Map<String, String> truncatedMap = new HashMap<>();
for (Map.Entry<String, String> entry : body.entrySet()) {
String truncatedValue = TruncationHelper.truncateString(entry.getValue(), maxLength);
truncatedMap.put(entry.getKey(), truncatedValue);
}
return new TelemetryEvent(
this.timestamp,
this.type,
this.level,
truncatedMap
);
}

@Override
public int hashCode() {
return Objects.hash(type, level, body);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TelemetryEvent that = (TelemetryEvent) o;
return Objects.equals(type, that.type) && Objects.equals(level, that.level) && Objects.equals(timestamp, that.timestamp) && Objects.equals(body, that.body);
}

@Override
public String toString() {
return "TelemetryEvent{" +
"type='" + type.asJson() + '\'' +
", level='" + level.asJson() + '\'' +
", source='" + SOURCE + '\'' +
", timestamp_ms=" + timestamp +
", body=" + body +
'}';
}
@Override
public String toString() {
return "TelemetryEvent{" +
"type='" + type.asJson() + '\'' +
", level='" + level.asJson() + '\'' +
", source='" + source + '\'' +
", timestamp_ms=" + timestamp +
", body=" + body +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.rollbar.api.json.JsonSerializable;

/**
* Represents the different types of {@link TelemetryEvent} available.
*/
public enum TelemetryType implements JsonSerializable {
LOG("log"),
MANUAL("manual"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.rollbar.api.payload.data;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.junit.Test;

import java.util.HashMap;

public class TelemetryEventTest {
private static final Level LEVEL = Level.DEBUG;
private static final String SOURCE = "Any message";
private static final long TIMESTAMP = 10L;

@Test
public void shouldBeEqual() {
TelemetryEvent telemetryEvent1 = logEvent();
TelemetryEvent telemetryEvent2 = logEvent();

assertThat(telemetryEvent1, is(telemetryEvent2));
}

private TelemetryEvent logEvent() {
return new TelemetryEvent(TelemetryType.LOG, LEVEL, TIMESTAMP, SOURCE, new HashMap<>());
}
}
Loading

0 comments on commit 935c933

Please sign in to comment.