Skip to content

Commit b88ad18

Browse files
[Feat][SDK- 431] Add threads information to payload (#327)
* fix: typos * feat: add new class RollbarThread * feat: set thread information from RollbarUncaughtExceptionHandler * feat: add Exception Thread and other threads information * feat: add Threads to body * feat: add Threads to throwable wrapper in notifier * refactor: a Thread should only contain trace_chain, not trace * refactor: add a list of groups before threads in payload * refactor: simplify truncation in TelemetryEvents * refactor: add truncation to TelemetryEvents in body * refactor: add truncation helper method for a list with Truncatable objects * refactor: truncate Group in body * refactor: set expected payload, first threads, with a group of trace_chains inside every thread * refactor: remove locals field in a Trace from a non exception thread * chore: fix indentation * chore: remove * from import * chore: add Java doc and fix indentations * chore: add Java doc and fix indentations * chore: fix lint * chore: fix lint * chore: add revapi info * feat: add threads to a catch exception log * chore: fix lint, line max size exceeded * test: now we capture threads info using the current thread * refactor(FramesStrategy): set truncateTraceChain with default visibility to use it in RollbarThreadStrategy * refactor(Group): set property as type TraceChain and add getter * feat(RollbarThreadStrategy): add truncation strategy for RollbarThread * fix: lint * fix: lint * fix: lint * fix(RollbarThreadStrategy): lint * feat: Add strategy for Telemetry events * feat: Add is_main field * refactor: replace Thread by RollbarThread in ThrowableWrapper * CI: bump ubuntu image to 22.04 Ref. actions/runner-images#11101 * fix: lint * fix: update revapi.yml replacing getThread with getRollbarThread --------- Co-authored-by: chris <[email protected]>
1 parent e131761 commit b88ad18

24 files changed

+1100
-96
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ on:
1919
jobs:
2020
validation:
2121
name: Gradle wrapper validation
22-
runs-on: ubuntu-20.04
22+
runs-on: ubuntu-22.04
2323
steps:
2424
- uses: actions/checkout@v4
2525
- uses: gradle/actions/wrapper-validation@v3
2626

2727
build:
28-
runs-on: ubuntu-20.04
28+
runs-on: ubuntu-22.04
2929
name: Build with Java ${{ matrix.java }}
3030
needs: [ validation ]
3131
strategy:
@@ -71,7 +71,7 @@ jobs:
7171
**/build/reports/*
7272
7373
release:
74-
runs-on: ubuntu-20.04
74+
runs-on: ubuntu-22.04
7575
name: Release
7676
# It would be nice to run this as part of the build job, since it would be
7777
# faster and have less duplicated Yaml, it would not be possible to check

.palantir/revapi.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,15 @@ acceptedBreaks:
3333
- code: "java.method.addedToInterface"
3434
new: "method int com.rollbar.notifier.config.CommonConfig::maximumTelemetryData()"
3535
justification: "This is going to be added in a major version"
36+
"2.1.0":
37+
com.rollbar:rollbar-java:
38+
- code: "java.method.addedToInterface"
39+
new: "method java.util.Map<java.lang.Thread, java.lang.StackTraceElement[]> com.rollbar.notifier.wrapper.ThrowableWrapper::getAllStackTraces()"
40+
justification: "This is a binary compatible change, which could only break custom\
41+
\ implementations of our config interfaces, but those interfaces are not meant\
42+
\ to be implemented by users"
43+
- code: "java.method.addedToInterface"
44+
new: "method com.rollbar.api.payload.data.body.RollbarThread com.rollbar.notifier.wrapper.ThrowableWrapper::getRollbarThread()"
45+
justification: "This is a binary compatible change, which could only break custom\
46+
\ implementations of our config interfaces, but those interfaces are not meant\
47+
\ to be implemented by users"

rollbar-api/src/main/java/com/rollbar/api/payload/data/TelemetryEvent.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,12 @@ public Map<String, Object> asJson() {
5656

5757
@Override
5858
public TelemetryEvent truncateStrings(int maxLength) {
59-
Map<String, String> truncatedMap = new HashMap<>();
60-
for (Map.Entry<String, String> entry : body.entrySet()) {
61-
String truncatedValue = TruncationHelper.truncateString(entry.getValue(), maxLength);
62-
truncatedMap.put(entry.getKey(), truncatedValue);
63-
}
6459
return new TelemetryEvent(
6560
this.type,
6661
this.level,
6762
this.timestamp,
6863
this.source,
69-
truncatedMap
64+
TruncationHelper.truncateStringsInStringMap(body, maxLength)
7065
);
7166
}
7267

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

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.rollbar.api.json.JsonSerializable;
44
import com.rollbar.api.payload.data.TelemetryEvent;
55
import com.rollbar.api.truncation.StringTruncatable;
6+
import com.rollbar.api.truncation.TruncationHelper;
67

78
import java.util.HashMap;
89
import java.util.List;
@@ -19,19 +20,41 @@ public class Body implements JsonSerializable, StringTruncatable<Body> {
1920

2021
private final List<TelemetryEvent> telemetryEvents;
2122

23+
private final List<RollbarThread> rollbarThreads;
24+
2225
private Body(Builder builder) {
2326
this.bodyContent = builder.bodyContent;
2427
this.telemetryEvents = builder.telemetryEvents;
28+
this.rollbarThreads = builder.rollbarThreads;
2529
}
2630

2731
/**
2832
* Getter.
33+
*
2934
* @return the contents.
3035
*/
3136
public BodyContent getContents() {
3237
return bodyContent;
3338
}
3439

40+
/**
41+
* Getter.
42+
*
43+
* @return the rollbar threads.
44+
*/
45+
public List<RollbarThread> getRollbarThreads() {
46+
return rollbarThreads;
47+
}
48+
49+
/**
50+
* Getter.
51+
*
52+
* @return the list of Telemetry events.
53+
*/
54+
public List<TelemetryEvent> getTelemetryEvents() {
55+
return telemetryEvents;
56+
}
57+
3558
@Override
3659
public Object asJson() {
3760
HashMap<String, Object> values = new HashMap<>();
@@ -44,15 +67,21 @@ public Object asJson() {
4467
values.put("telemetry", telemetryEvents);
4568
}
4669

70+
if (rollbarThreads != null) {
71+
values.put("threads", rollbarThreads);
72+
}
73+
4774
return values;
4875
}
4976

5077
@Override
5178
public Body truncateStrings(int maxSize) {
5279
if (bodyContent != null) {
5380
return new Body.Builder(this)
54-
.bodyContent(bodyContent.truncateStrings(maxSize))
55-
.build();
81+
.bodyContent(bodyContent.truncateStrings(maxSize))
82+
.telemetryEvents(TruncationHelper.truncate(telemetryEvents, maxSize))
83+
.rollbarThreads(TruncationHelper.truncate(rollbarThreads, maxSize))
84+
.build();
5685
} else {
5786
return this;
5887
}
@@ -70,20 +99,22 @@ public boolean equals(Object o) {
7099

71100
Body body = (Body) o;
72101
return Objects.equals(bodyContent, body.bodyContent)
73-
&& Objects.equals(telemetryEvents, body.telemetryEvents);
102+
&& Objects.equals(telemetryEvents, body.telemetryEvents)
103+
&& Objects.equals(rollbarThreads, body.rollbarThreads);
74104
}
75105

76106
@Override
77107
public int hashCode() {
78-
return Objects.hash(bodyContent, telemetryEvents);
108+
return Objects.hash(bodyContent, telemetryEvents, rollbarThreads);
79109
}
80110

81111
@Override
82112
public String toString() {
83113
return "Body{"
84-
+ "bodyContent=" + bodyContent
85-
+ ", telemetry=" + telemetryEvents
86-
+ '}';
114+
+ "bodyContent=" + bodyContent
115+
+ ", telemetry=" + telemetryEvents
116+
+ ", threads=" + rollbarThreads
117+
+ '}';
87118
}
88119

89120
/**
@@ -95,6 +126,8 @@ public static final class Builder {
95126

96127
private List<TelemetryEvent> telemetryEvents;
97128

129+
private List<RollbarThread> rollbarThreads;
130+
98131
/**
99132
* Constructor.
100133
*/
@@ -108,8 +141,9 @@ public Builder() {
108141
* @param body the {@link Body body} to initialize a new builder instance.
109142
*/
110143
public Builder(Body body) {
111-
this.bodyContent = body.bodyContent;
112-
this.telemetryEvents = body.telemetryEvents;
144+
bodyContent = body.bodyContent;
145+
telemetryEvents = body.telemetryEvents;
146+
rollbarThreads = body.rollbarThreads;
113147
}
114148

115149
/**
@@ -135,6 +169,17 @@ public Builder telemetryEvents(List<TelemetryEvent> telemetryEvents) {
135169
return this;
136170
}
137171

172+
/**
173+
* Information from threads.
174+
*
175+
* @param rollbarThreads a list of threads;
176+
* @return the builder instance.
177+
*/
178+
public Builder rollbarThreads(List<RollbarThread> rollbarThreads) {
179+
this.rollbarThreads = rollbarThreads;
180+
return this;
181+
}
182+
138183
/**
139184
* Builds the {@link Body body}.
140185
*
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.rollbar.api.payload.data.body;
2+
3+
import com.rollbar.api.json.JsonSerializable;
4+
import com.rollbar.api.truncation.StringTruncatable;
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.Objects;
9+
10+
/**
11+
* This represent a Group of trace chains in a Thread.
12+
* In Java we only send 1 TraceChain per Group, in other languages like Python (ExceptionGroup) or
13+
* JS (AggregatorError), it may be more TraceChains per group.
14+
*/
15+
public class Group implements JsonSerializable, StringTruncatable<Group> {
16+
private final TraceChain traceChain;
17+
18+
public Group(TraceChain traceChain) {
19+
this.traceChain = traceChain;
20+
}
21+
22+
/**
23+
* Getter.
24+
*
25+
* @return the trace chain.
26+
*/
27+
public TraceChain getTraceChain() {
28+
return traceChain;
29+
}
30+
31+
@Override
32+
public Object asJson() {
33+
HashMap<String, Object> values = new HashMap<>();
34+
values.put("trace_chain", traceChain);
35+
ArrayList<HashMap<String, Object>> traceChains = new ArrayList<>();
36+
traceChains.add(values);
37+
return traceChains;
38+
}
39+
40+
@Override
41+
public Group truncateStrings(int maxLength) {
42+
return new Group(traceChain.truncateStrings(maxLength));
43+
}
44+
45+
@Override
46+
public boolean equals(Object o) {
47+
if (o == null || getClass() != o.getClass()) {
48+
return false;
49+
}
50+
Group group = (Group) o;
51+
return Objects.equals(traceChain, group.traceChain);
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
return Objects.hashCode(traceChain);
57+
}
58+
59+
@Override
60+
public String toString() {
61+
return "Group{traceChain=" + traceChain + '}';
62+
}
63+
}

0 commit comments

Comments
 (0)