Skip to content

Commit

Permalink
refactor: set source as an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbuon committed Aug 16, 2024
1 parent e9645bc commit 2fcb323
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 24 deletions.
19 changes: 19 additions & 0 deletions rollbar-api/src/main/java/com/rollbar/api/payload/data/Source.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.rollbar.api.payload.data;

import com.rollbar.api.json.JsonSerializable;

public enum Source implements JsonSerializable {
CLIENT("client"),
SERVER("server");

private final String jsonName;

Source(String jsonName) {
this.jsonName = jsonName;
}

@Override
public Object asJson() {
return jsonName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public class TelemetryEvent implements JsonSerializable, StringTruncatable<Telem
private final Level level;
private final Long timestamp;
private final Map<String, String> body;
private final String source;
private final Source source;
private static final long serialVersionUID = 2843361810242481727L;

public TelemetryEvent(
TelemetryType telemetryType,
Level level,
Long timestamp,
String source,
Source source,
Map<String, String> body
) {
type = telemetryType;
Expand All @@ -39,7 +39,7 @@ 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("source", source.asJson());
values.put("timestamp_ms", timestamp);
values.put("body", body);
return values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.rollbar.api.payload.Payload;
import com.rollbar.api.payload.data.Data;
import com.rollbar.api.payload.data.Level;
import com.rollbar.api.payload.data.Source;
import com.rollbar.api.payload.data.TelemetryEvent;
import com.rollbar.api.payload.data.TelemetryType;
import com.rollbar.api.payload.data.body.Body;
Expand Down Expand Up @@ -340,12 +341,12 @@ private Body makeBody(ThrowableWrapper error, String description) {
return bodyFactory.from(error, description, telemetryEvents);
}

private String getSource() {
private Source getSource() {
String platform = config.platform();
if ("android".equals(platform)) {
return "client";
return Source.CLIENT;
} else {
return "server";
return Source.SERVER;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rollbar.notifier.telemetry;

import com.rollbar.api.payload.data.Level;
import com.rollbar.api.payload.data.Source;
import com.rollbar.api.payload.data.TelemetryEvent;
import com.rollbar.api.payload.data.TelemetryType;
import com.rollbar.notifier.provider.Provider;
Expand Down Expand Up @@ -43,7 +44,7 @@ public List<TelemetryEvent> dump() {
* @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, String source, String message) {
public void recordLogEventFor(Level level, Source source, String message) {
Map<String, String> body = new HashMap<>();
body.put(LOG_KEY_MESSAGE, message);
addEvent(new TelemetryEvent(TelemetryType.LOG, level, timestampProvider.provide(), source, body));
Expand All @@ -55,7 +56,7 @@ public void recordLogEventFor(Level level, String source, String message) {
* @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, String source, String message) {
public void recordManualEventFor(Level level, Source source, String message) {
Map<String, String> body = new HashMap<>();
body.put(LOG_KEY_MESSAGE, message);
addEvent(new TelemetryEvent(TelemetryType.MANUAL, level, timestampProvider.provide(), source, body));
Expand All @@ -68,7 +69,7 @@ public void recordManualEventFor(Level level, String source, String message) {
* @param from the starting point (e.g. "SettingView").
* @param to the destination point (e.g. "HomeView").
*/
public void recordNavigationEventFor(Level level, String source, String from, String to) {
public void recordNavigationEventFor(Level level, Source source, String from, String to) {
Map<String, String> body = new HashMap<>();
body.put(NAVIGATION_KEY_FROM, from);
body.put(NAVIGATION_KEY_TO, to);
Expand All @@ -83,7 +84,7 @@ public void recordNavigationEventFor(Level level, String source, String from, St
* @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, String source, String method, String url, String statusCode) {
public void recordNetworkEventFor(Level level, Source source, String method, String url, String statusCode) {
Map<String, String> body = new HashMap<>();
body.put(NETWORK_KEY_METHOD, method);
body.put(NETWORK_KEY_URL, url);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.rollbar.notifier.telemetry;

import com.rollbar.api.payload.data.Level;
import com.rollbar.api.payload.data.Source;
import com.rollbar.api.payload.data.TelemetryEvent;

import java.util.List;

public interface TelemetryEventTracker {
List<TelemetryEvent> dump();
void recordLogEventFor(Level level, String source, String message);
void recordManualEventFor(Level level, String source, String message);
void recordNavigationEventFor(Level level, String source, String from, String to);
void recordNetworkEventFor(Level level, String source, String method, String url, String statusCode);
void recordLogEventFor(Level level, Source source, String message);
void recordManualEventFor(Level level, Source source, String message);
void recordNavigationEventFor(Level level, Source source, String from, String to);
void recordNetworkEventFor(Level level, Source source, String method, String url, String statusCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.rollbar.api.payload.Payload;
import com.rollbar.api.payload.data.Level;
import com.rollbar.api.payload.data.Source;
import com.rollbar.notifier.config.Config;
import com.rollbar.notifier.telemetry.TelemetryEventTracker;
import com.rollbar.notifier.util.BodyFactory;
Expand Down Expand Up @@ -35,7 +36,7 @@ public void shouldRecordALogEventWithServerSourceWhenThePlatformIsNotAndroid() {

sut.recordLogEventFor(level, message);

verify(telemetryEventTracker).recordLogEventFor(level, "server", message);
verify(telemetryEventTracker).recordLogEventFor(level, Source.SERVER, message);
}

@Test
Expand All @@ -45,7 +46,7 @@ public void shouldRecordALogEventWithClientSourceWhenThePlatformIsAndroid() {

sut.recordLogEventFor(level, message);

verify(telemetryEventTracker).recordLogEventFor(level, "client", message);
verify(telemetryEventTracker).recordLogEventFor(level, Source.CLIENT, message);
}

@Test
Expand All @@ -55,7 +56,7 @@ public void shouldRecordAManualEventWithServerSourceWhenThePlatformIsNotAndroid(

sut.recordManualEventFor(level, message);

verify(telemetryEventTracker).recordManualEventFor(level, "server", message);
verify(telemetryEventTracker).recordManualEventFor(level, Source.SERVER, message);
}

@Test
Expand All @@ -65,7 +66,7 @@ public void shouldRecordAManualEventWithClientSourceWhenThePlatformIsAndroid() {

sut.recordManualEventFor(level, message);

verify(telemetryEventTracker).recordManualEventFor(level, "client", message);
verify(telemetryEventTracker).recordManualEventFor(level, Source.CLIENT, message);
}

@Test
Expand All @@ -77,7 +78,7 @@ public void shouldRecordANetworkEventWithServerSourceWhenThePlatformIsNotAndroid

sut.recordNetworkEventFor(level, method, url, statusCode);

verify(telemetryEventTracker).recordNetworkEventFor(level, "server", method, url, statusCode);
verify(telemetryEventTracker).recordNetworkEventFor(level, Source.SERVER, method, url, statusCode);
}

@Test
Expand All @@ -89,7 +90,7 @@ public void shouldRecordANetworkEventWithClientSourceWhenThePlatformIsAndroid()

sut.recordNetworkEventFor(level, method, url, statusCode);

verify(telemetryEventTracker).recordNetworkEventFor(level, "client", method, url, statusCode);
verify(telemetryEventTracker).recordNetworkEventFor(level, Source.CLIENT, method, url, statusCode);
}

@Test
Expand All @@ -100,7 +101,7 @@ public void shouldRecordANavigationEventWithServerSourceWhenThePlatformIsNotAndr

sut.recordNavigationEventFor(level, from, to);

verify(telemetryEventTracker).recordNavigationEventFor(level, "server", from, to);
verify(telemetryEventTracker).recordNavigationEventFor(level, Source.SERVER, from, to);
}

@Test
Expand All @@ -111,7 +112,7 @@ public void shouldRecordANavigationEventWithClientSourceWhenThePlatformIsAndroid

sut.recordNavigationEventFor(level, from, to);

verify(telemetryEventTracker).recordNavigationEventFor(level, "client", from, to);
verify(telemetryEventTracker).recordNavigationEventFor(level, Source.CLIENT, from, to);
}

private Config getConfigWith(String platform) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.Assert.*;

import com.rollbar.api.payload.data.Level;
import com.rollbar.api.payload.data.Source;
import com.rollbar.api.payload.data.TelemetryEvent;
import com.rollbar.api.payload.data.TelemetryType;
import com.rollbar.notifier.provider.timestamp.TimestampProvider;
Expand All @@ -16,7 +17,7 @@

public class RollbarTelemetryEventTrackerTest {

private static final String SOURCE = "Any source";
private static final Source SOURCE = Source.SERVER;
private static final String MESSAGE = "Any message";
private static final String FROM = "Any origin";
private static final String TO = "Any destination";
Expand Down Expand Up @@ -138,7 +139,7 @@ private Map<String, Object> getExpectedJsonForANetworkTelemetryEvent() {
private Map<String, Object> commonFields() {
Map<String, Object> map = new HashMap<>();
map.put("level", LEVEL.asJson());
map.put("source", SOURCE);
map.put("source", SOURCE.asJson());
map.put("timestamp_ms", TIMESTAMP);
return map;
}
Expand Down

0 comments on commit 2fcb323

Please sign in to comment.