Skip to content

Commit 162c253

Browse files
committed
refactor: set expected payload, first threads, with a group of trace_chains inside every thread
1 parent 62c038b commit 162c253

File tree

5 files changed

+84
-79
lines changed

5 files changed

+84
-79
lines changed

Diff for: rollbar-api/src/main/java/com/rollbar/api/payload/data/body/Body.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public class Body implements JsonSerializable, StringTruncatable<Body> {
1818

1919
private final List<TelemetryEvent> telemetryEvents;
2020

21-
private final List<Group> groups;
21+
private final List<RollbarThread> rollbarThreads;
2222

2323
private Body(Builder builder) {
2424
this.bodyContent = builder.bodyContent;
2525
this.telemetryEvents = builder.telemetryEvents;
26-
this.groups = builder.groups;
26+
this.rollbarThreads = builder.rollbarThreads;
2727
}
2828

2929
/**
@@ -47,8 +47,8 @@ public Object asJson() {
4747
values.put("telemetry", telemetryEvents);
4848
}
4949

50-
if (groups != null) {
51-
values.put("group", groups);
50+
if (rollbarThreads != null) {
51+
values.put("threads", rollbarThreads);
5252
}
5353

5454
return values;
@@ -60,7 +60,7 @@ public Body truncateStrings(int maxSize) {
6060
return new Body.Builder(this)
6161
.bodyContent(bodyContent.truncateStrings(maxSize))
6262
.telemetryEvents(TruncationHelper.truncate(telemetryEvents, maxSize))
63-
.groups(TruncationHelper.truncate(groups, maxSize))
63+
.rollbarThreads(TruncationHelper.truncate(rollbarThreads, maxSize))
6464
.build();
6565
} else {
6666
return this;
@@ -92,7 +92,7 @@ public String toString() {
9292
return "Body{"
9393
+ "bodyContent=" + bodyContent
9494
+ ", telemetry=" + telemetryEvents
95-
+ ", group=" + groups
95+
+ ", threads=" + rollbarThreads
9696
+ '}';
9797
}
9898

@@ -105,7 +105,7 @@ public static final class Builder {
105105

106106
private List<TelemetryEvent> telemetryEvents;
107107

108-
private List<Group> groups;
108+
private List<RollbarThread> rollbarThreads;
109109

110110
/**
111111
* Constructor.
@@ -122,7 +122,7 @@ public Builder() {
122122
public Builder(Body body) {
123123
bodyContent = body.bodyContent;
124124
telemetryEvents = body.telemetryEvents;
125-
groups = body.groups;
125+
rollbarThreads = body.rollbarThreads;
126126
}
127127

128128
/**
@@ -149,13 +149,13 @@ public Builder telemetryEvents(List<TelemetryEvent> telemetryEvents) {
149149
}
150150

151151
/**
152-
* Information from a group of threads.
152+
* Information from threads.
153153
*
154-
* @param groups a group with a list of threads;
154+
* @param rollbarThreads a list of threads;
155155
* @return the builder instance.
156156
*/
157-
public Builder groups(List<Group> groups) {
158-
this.groups = groups;
157+
public Builder rollbarThreads(List<RollbarThread> rollbarThreads) {
158+
this.rollbarThreads = rollbarThreads;
159159
return this;
160160
}
161161

Diff for: rollbar-api/src/main/java/com/rollbar/api/payload/data/body/Group.java

+12-15
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,41 @@
22

33
import com.rollbar.api.json.JsonSerializable;
44
import com.rollbar.api.truncation.StringTruncatable;
5-
import com.rollbar.api.truncation.TruncationHelper;
65

6+
import java.util.ArrayList;
77
import java.util.HashMap;
8-
import java.util.List;
98
import java.util.Objects;
109

1110
public class Group implements JsonSerializable, StringTruncatable<Group> {
12-
private final List<RollbarThread> rollbarThreads;
11+
private final BodyContent traceChain;
1312

14-
public Group(List<RollbarThread> threads) {
15-
rollbarThreads = threads;
13+
public Group(BodyContent traceChain) {
14+
this.traceChain = traceChain;
1615
}
1716

1817
@Override
1918
public Object asJson() {
2019
HashMap<String, Object> values = new HashMap<>();
21-
22-
if (rollbarThreads != null) {
23-
values.put("threads", rollbarThreads);
24-
}
25-
26-
return values;
20+
values.put("trace_chain", traceChain);
21+
ArrayList<HashMap<String, Object>> traceChains = new ArrayList<>();
22+
traceChains.add(values);
23+
return traceChains;
2724
}
2825

2926
@Override
3027
public Group truncateStrings(int maxLength) {
31-
return new Group(TruncationHelper.truncate(rollbarThreads, maxLength));
28+
return new Group(traceChain.truncateStrings(maxLength));
3229
}
3330

3431
@Override
3532
public boolean equals(Object o) {
36-
if (!(o instanceof Group)) return false;
33+
if (o == null || getClass() != o.getClass()) return false;
3734
Group group = (Group) o;
38-
return Objects.equals(rollbarThreads, group.rollbarThreads);
35+
return Objects.equals(traceChain, group.traceChain);
3936
}
4037

4138
@Override
4239
public int hashCode() {
43-
return Objects.hashCode(rollbarThreads);
40+
return Objects.hashCode(traceChain);
4441
}
4542
}

Diff for: rollbar-api/src/main/java/com/rollbar/api/payload/data/body/RollbarThread.java

+48-37
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,85 @@
22

33
import com.rollbar.api.json.JsonSerializable;
44
import com.rollbar.api.truncation.StringTruncatable;
5+
import com.rollbar.api.truncation.TruncationHelper;
56

67
import java.util.HashMap;
8+
import java.util.List;
79
import java.util.Map;
810
import java.util.Objects;
911

1012
public class RollbarThread implements JsonSerializable, StringTruncatable<RollbarThread> {
11-
private final Thread thread;
12-
private final BodyContent bodyContent;
13+
private final String name;
14+
private final String id;
15+
private final String priority;
16+
private final String state;
17+
private final Group group;
1318

14-
public RollbarThread(Thread thread, BodyContent bodyContent) {
15-
this.thread = thread;
16-
this.bodyContent = bodyContent;
19+
public RollbarThread(Thread thread, Group group) {
20+
name = thread.getName();
21+
id = String.valueOf(thread.getId());
22+
priority = String.valueOf(thread.getPriority());
23+
state = thread.getState().toString();
24+
this.group = group;
25+
}
26+
27+
private RollbarThread(
28+
String name,
29+
String id,
30+
String priority,
31+
String state,
32+
Group group
33+
) {
34+
this.name = name;
35+
this.id = id;
36+
this.priority = priority;
37+
this.state = state;
38+
this.group = group;
1739
}
1840

1941
@Override
2042
public Object asJson() {
2143
Map<String, Object> values = new HashMap<>();
22-
values.put("name", getThreadName());
23-
values.put("id", getThreadId());
24-
values.put("priority", getThreadPriority());
25-
values.put("state", getThreadState());
26-
if (bodyContent != null) {
27-
values.put(bodyContent.getKeyName(), bodyContent);
28-
}
44+
values.put("name", name);
45+
values.put("id", id);
46+
values.put("priority", priority);
47+
values.put("state", state);
48+
values.put("group", group);
2949
return values;
3050
}
3151

3252
@Override
3353
public RollbarThread truncateStrings(int maxLength) {
34-
return new RollbarThread(thread, bodyContent.truncateStrings(maxLength));
54+
return new RollbarThread(
55+
name,
56+
id,
57+
priority,
58+
state,
59+
group.truncateStrings(maxLength)
60+
);
3561
}
3662

3763
@Override
3864
public String toString() {
3965
return "RollbarThread{" +
40-
"name='" + getThreadName() + '\'' +
41-
", id='" + getThreadId() + '\'' +
42-
", priority='" + getThreadPriority() + '\'' +
43-
", state='" + getThreadState() + '\'' +
44-
", " + bodyContent.getKeyName() + "=" + bodyContent +
45-
'}';
66+
"name='" + name + '\'' +
67+
", id='" + id + '\'' +
68+
", priority='" + priority + '\'' +
69+
", state='" + state + '\'' +
70+
", group='" + group +
71+
'}';
4672
}
4773

48-
private String getThreadName() {
49-
return thread.getName();
50-
}
51-
52-
private String getThreadId() {
53-
return String.valueOf(thread.getId());
54-
}
55-
56-
private String getThreadPriority() {
57-
return String.valueOf(thread.getPriority());
58-
}
59-
60-
private String getThreadState() {
61-
return thread.getState().toString();
62-
}
6374

6475
@Override
6576
public boolean equals(Object o) {
66-
if (!(o instanceof RollbarThread)) return false;
77+
if (o == null || getClass() != o.getClass()) return false;
6778
RollbarThread that = (RollbarThread) o;
68-
return Objects.equals(thread, that.thread) && Objects.equals(bodyContent, that.bodyContent);
79+
return Objects.equals(name, that.name) && Objects.equals(id, that.id) && Objects.equals(priority, that.priority) && Objects.equals(state, that.state) && Objects.equals(group, that.group);
6980
}
7081

7182
@Override
7283
public int hashCode() {
73-
return Objects.hash(thread, bodyContent);
84+
return Objects.hash(name, id, priority, state, group);
7485
}
7586
}

Diff for: rollbar-java/src/main/java/com/rollbar/notifier/util/BodyFactory.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -71,33 +71,30 @@ private Body from(
7171
) {
7272
return builder
7373
.bodyContent(makeBodyContent(throwableWrapper, description))
74-
.groups(makeGroups(throwableWrapper, description))
74+
.rollbarThreads(makeRollbarThreads(throwableWrapper, description))
7575
.build();
7676
}
7777

78-
private List<Group> makeGroups(
78+
private List<RollbarThread> makeRollbarThreads(
7979
ThrowableWrapper throwableWrapper,
8080
String description
8181
) {
8282
if (throwableWrapper == null) {
8383
return null;
8484
}
85-
8685
Map<Thread, StackTraceElement[]> allStackTraces = throwableWrapper.getAllStackTraces();
8786
if (allStackTraces == null) {
8887
return null;
8988
}
9089

9190
ArrayList<RollbarThread> rollbarThreads = new ArrayList<>();
9291
rollbarThreads.add(makeInitialRollbarThread(throwableWrapper, description));
93-
ArrayList<Group> groups = new ArrayList<>();
94-
groups.add(new Group(addOtherThreads(rollbarThreads, allStackTraces)));
95-
return groups;
92+
return addOtherThreads(rollbarThreads, allStackTraces);
9693
}
9794

9895
private RollbarThread makeInitialRollbarThread(ThrowableWrapper throwableWrapper, String description) {
99-
BodyContent bodyContent = traceChain(throwableWrapper, description);
100-
return new RollbarThread(throwableWrapper.getThread(), bodyContent);
96+
TraceChain traceChain = traceChain(throwableWrapper, description);
97+
return new RollbarThread(throwableWrapper.getThread(), new Group(traceChain));
10198
}
10299

103100
private ArrayList<RollbarThread> addOtherThreads(
@@ -106,7 +103,7 @@ private ArrayList<RollbarThread> addOtherThreads(
106103
) {
107104
for (Map.Entry<Thread, StackTraceElement[]> entry : allStackTraces.entrySet()) {
108105
TraceChain traceChain = traceChain(entry.getValue());
109-
RollbarThread rollbarThread = new RollbarThread(entry.getKey(), traceChain);
106+
RollbarThread rollbarThread = new RollbarThread(entry.getKey(), new Group(traceChain));
110107
rollbarThreads.add(rollbarThread);
111108
}
112109
return rollbarThreads;

Diff for: rollbar-java/src/test/java/com/rollbar/notifier/util/BodyFactoryTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void shouldBuildBodyWithThreads() {
9696
assertThat(body.getContents(), is(instanceOf(Trace.class)));
9797
HashMap<String, Object> map = (HashMap<String, Object>) body.asJson();
9898
assertNull(map.get("telemetry"));
99-
assertNotNull(map.get("group"));
99+
assertNotNull(map.get("threads"));
100100
}
101101

102102
@Test
@@ -152,13 +152,13 @@ private TelemetryEvent getFirstTelemetryEvent(Body body) {
152152

153153
private Trace getFirstTraceFromGroup(Body body) {
154154
HashMap<String, Object> bodyJson = (HashMap<String, Object>) body.asJson();
155-
List<Group> groups = (List<Group>) bodyJson.get("group");
155+
List<RollbarThread> rollbarThreads = (List<RollbarThread>) bodyJson.get("threads");
156156

157-
HashMap<String, Object> groupJson = (HashMap<String, Object>) groups.get(0).asJson();
158-
List<RollbarThread> rollbarThreads = (List<RollbarThread>) groupJson.get("threads");
157+
HashMap<String, Object> groupJson = (HashMap<String, Object>) rollbarThreads.get(0).asJson();
158+
Group group = (Group) groupJson.get("group");
159159

160-
HashMap<String, Object> rollbarThreadJson = (HashMap<String, Object>) rollbarThreads.get(0).asJson();
161-
TraceChain traceChain = (TraceChain) rollbarThreadJson.get("trace_chain");
160+
List<HashMap<String, Object>> rollbarThreadJson = (List<HashMap<String, Object>>) group.asJson();
161+
TraceChain traceChain = (TraceChain) rollbarThreadJson.get(0).get("trace_chain");
162162
return traceChain.getTraces().get(0);
163163
}
164164

0 commit comments

Comments
 (0)