Skip to content

Commit

Permalink
Add option to truncate payloads before sending them to Rollbar. (#279)
Browse files Browse the repository at this point in the history
* Add option to truncate payloads before sending them to Rollbar.

The implementation is similar to the one found rollbar-gem and Rollbar.NET.
Truncation is disabled by default to maintain backwards compatibility.

* Add option to truncate payloads before sending them to Rollbar. Code review fixes

* Add option to truncate payloads before sending them to Rollbar. Remove unused import
  • Loading branch information
diegov authored Nov 3, 2021
1 parent 8c7ba60 commit 1795254
Show file tree
Hide file tree
Showing 40 changed files with 2,202 additions and 20 deletions.
12 changes: 12 additions & 0 deletions .palantir/revapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ acceptedBreaks:
old: "field com.rollbar.notifier.config.ConfigBuilder.defaultThrowableLevel"
justification: "Removing protected fields, deriving from ConfigBuilder should\
\ be done by other SDK classes only"
"1.7.9":
com.rollbar:rollbar-java:
- code: "java.method.addedToInterface"
new: "method boolean com.rollbar.notifier.config.CommonConfig::truncateLargePayloads()"
justification: "This is a binary compatible change, which could only break custom\
\ implementations of our config interfaces, but those interfaces are not meant\
\ to be implemented by users"
- code: "java.method.addedToInterface"
new: "method com.rollbar.notifier.sender.json.JsonSerializer com.rollbar.notifier.config.CommonConfig::jsonSerializer()"
justification: "This is a binary compatible change, which could only break custom\
\ implementations of our config interfaces, but those interfaces are not meant\
\ to be implemented by users"
16 changes: 15 additions & 1 deletion rollbar-api/src/main/java/com/rollbar/api/payload/Payload.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.rollbar.api.payload;

import static com.rollbar.api.truncation.TruncationHelper.truncateStringsInObject;

import com.rollbar.api.json.JsonSerializable;
import com.rollbar.api.payload.data.Data;
import com.rollbar.api.truncation.StringTruncatable;
import java.util.HashMap;
import java.util.Map;

/**
* Represents the payload to send to Rollbar. A successfully constructed Payload matches Rollbar's
* spec, and should be successful when serialized and POSTed to the correct endpoint.
*/
public class Payload implements JsonSerializable {
public class Payload implements JsonSerializable, StringTruncatable<Payload> {

private static final long serialVersionUID = 3054041443735286159L;

Expand Down Expand Up @@ -102,6 +105,17 @@ public Map<String, Object> asJson() {
return values;
}

@Override
public Payload truncateStrings(int maxLength) {
if (this.data == null) {
return this;
}

return new Payload.Builder(this)
.data(truncateStringsInObject(data, maxLength))
.build();
}

public int getSendAttemptCount() {
return sendAttemptCount;
}
Expand Down
24 changes: 21 additions & 3 deletions rollbar-api/src/main/java/com/rollbar/api/payload/data/Client.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package com.rollbar.api.payload.data;

import static com.rollbar.api.truncation.TruncationHelper.truncateStringsInMap;
import static com.rollbar.api.truncation.TruncationHelper.truncateStringsInNestedMap;
import static java.util.Collections.unmodifiableMap;

import com.rollbar.api.json.JsonSerializable;
import com.rollbar.api.truncation.StringTruncatable;

import java.util.HashMap;
import java.util.Map;

/**
* Represents the client info to send to Rollbar.
*/
public class Client implements JsonSerializable {
public class Client implements JsonSerializable, StringTruncatable<Client> {

private static final long serialVersionUID = 1975664872679919021L;

private final Map<String, Map<String, Object>> data;
private final Map<String, Object> topLevelData;

private Client(Builder builder) {
this.data = unmodifiableMap(new HashMap<>(builder.data));
this.topLevelData = unmodifiableMap(new HashMap<>(builder.topLevelData));
this(builder.data, builder.topLevelData);
}

private Client(Map<String, Map<String, Object>> data, Map<String, Object> topLevelData) {
this.data = unmodifiableMap(new HashMap<>(data));
this.topLevelData = unmodifiableMap(new HashMap<>(topLevelData));
}

/**
Expand All @@ -45,6 +53,16 @@ public Map<String, Object> asJson() {
return values;
}


@Override
public Client truncateStrings(int maxLength) {
// The Client builder class is different from the rest, it doesn't map to each of the built
// object's properties, so we call the constructor directly.
Map<String, Map<String, Object>> truncatedData = truncateStringsInNestedMap(data, maxLength);
Map<String, Object> truncatedTopLevelData = truncateStringsInMap(topLevelData, maxLength);
return new Client(truncatedData, truncatedTopLevelData);
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
30 changes: 29 additions & 1 deletion rollbar-api/src/main/java/com/rollbar/api/payload/data/Data.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.rollbar.api.payload.data;

import static com.rollbar.api.truncation.TruncationHelper.truncateString;
import static com.rollbar.api.truncation.TruncationHelper.truncateStringsInMap;
import static com.rollbar.api.truncation.TruncationHelper.truncateStringsInObject;

import com.rollbar.api.json.JsonSerializable;
import com.rollbar.api.payload.data.body.Body;
import com.rollbar.api.truncation.StringTruncatable;

import java.util.HashMap;
import java.util.Map;

/**
* Represents the actual data being posted to Rollbar.
*/
public class Data implements JsonSerializable {
public class Data implements JsonSerializable, StringTruncatable<Data> {

private static final long serialVersionUID = 4996853277611613397L;

Expand Down Expand Up @@ -306,6 +312,28 @@ public Map<String, Object> asJson() {
return values;
}

@Override
public Data truncateStrings(int maxLength) {
return new Builder(this)
.environment(truncateString(environment, maxLength))
.body(truncateStringsInObject(body, maxLength))
.codeVersion(truncateString(codeVersion, maxLength))
.platform(truncateString(platform, maxLength))
.language(truncateString(language, maxLength))
.framework(truncateString(framework, maxLength))
.context(truncateString(context, maxLength))
.request(truncateStringsInObject(request, maxLength))
.person(truncateStringsInObject(person, maxLength))
.server(truncateStringsInObject(server, maxLength))
.client(truncateStringsInObject(client, maxLength))
.custom(truncateStringsInMap(custom, maxLength))
.fingerprint(truncateString(fingerprint, maxLength))
.title(truncateString(title, maxLength))
.uuid(truncateString(uuid, maxLength))
.notifier(truncateStringsInObject(notifier, maxLength))
.build();
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.rollbar.api.payload.data;

import static com.rollbar.api.truncation.TruncationHelper.truncateString;

import com.rollbar.api.json.JsonSerializable;
import com.rollbar.api.truncation.StringTruncatable;

import java.util.HashMap;
import java.util.Map;

/**
* Information about this notifier, or one based off of this.
*/
public class Notifier implements JsonSerializable {
public class Notifier implements JsonSerializable, StringTruncatable<Notifier> {

private static final long serialVersionUID = -2605608164795462842L;

Expand Down Expand Up @@ -82,6 +86,19 @@ public Map<String, Object> asJson() {
return values;
}


@Override
public Notifier truncateStrings(int maxLength) {
if (name == null && version == null) {
return this;
}

return new Notifier.Builder(this)
.name(truncateString(name, maxLength))
.version(truncateString(version, maxLength))
.build();
}

/**
* Builder class for {@link Notifier notifier}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.rollbar.api.payload.data;

import static com.rollbar.api.truncation.TruncationHelper.truncateString;
import static com.rollbar.api.truncation.TruncationHelper.truncateStringsInMap;
import static java.util.Collections.unmodifiableMap;

import com.rollbar.api.json.JsonSerializable;
import com.rollbar.api.truncation.StringTruncatable;

import java.util.HashMap;
import java.util.Map;

/**
* Represents the user affected by an error.
*/
public class Person implements JsonSerializable {
public class Person implements JsonSerializable, StringTruncatable<Person> {

private static final long serialVersionUID = -1589474813294741393L;

Expand Down Expand Up @@ -81,6 +85,16 @@ public Map<String, Object> asJson() {
return values;
}

@Override
public Person truncateStrings(int maxLength) {
return new Builder(this)
.metadata(truncateStringsInMap(metadata, maxLength))
.id(truncateString(id, maxLength))
.username(truncateString(username, maxLength))
.email(truncateString(email, maxLength))
.build();
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.rollbar.api.payload.data;

import static com.rollbar.api.truncation.TruncationHelper.truncateString;
import static com.rollbar.api.truncation.TruncationHelper.truncateStringsInMap;
import static com.rollbar.api.truncation.TruncationHelper.truncateStringsInStringListMap;
import static com.rollbar.api.truncation.TruncationHelper.truncateStringsInStringMap;
import static java.util.Collections.unmodifiableMap;

import com.rollbar.api.json.JsonSerializable;
import com.rollbar.api.truncation.StringTruncatable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Represents the HTTP request that triggered the error.
*/
public class Request implements JsonSerializable {
public class Request implements JsonSerializable, StringTruncatable<Request> {

private static final long serialVersionUID = 3552594955980476301L;

Expand Down Expand Up @@ -166,6 +172,22 @@ public Map<String, Object> asJson() {
return values;
}

@Override
public Request truncateStrings(int maxLength) {
return new Builder(this)
.metadata(truncateStringsInMap(metadata, maxLength))
.url(truncateString(url, maxLength))
.method(truncateString(method, maxLength))
.headers(truncateStringsInStringMap(headers, maxLength))
.params(truncateStringsInStringMap(params, maxLength))
.get(truncateStringsInStringListMap(get, maxLength))
.queryString(truncateString(queryString, maxLength))
.post(truncateStringsInMap(post, maxLength))
.body(truncateString(body, maxLength))
.userIp(truncateString(userIp, maxLength))
.build();
}

private Map<String, Object> processGetParams(Map<String, List<String>> map) {
Map<String, Object> result = new HashMap<>();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.rollbar.api.payload.data;

import static com.rollbar.api.truncation.TruncationHelper.truncateString;
import static com.rollbar.api.truncation.TruncationHelper.truncateStringsInMap;
import static java.util.Collections.max;
import static java.util.Collections.unmodifiableMap;

import com.rollbar.api.json.JsonSerializable;
import com.rollbar.api.truncation.StringTruncatable;

import java.util.HashMap;
import java.util.Map;

/**
* Represents the server object sent to Rollbar.
*/
public class Server implements JsonSerializable {
public class Server implements JsonSerializable, StringTruncatable<Server> {

private static final long serialVersionUID = -9135498029274932688L;

Expand Down Expand Up @@ -95,6 +100,18 @@ public Map<String, Object> asJson() {
return values;
}


@Override
public Server truncateStrings(int maxLength) {
return new Builder(this)
.metadata(truncateStringsInMap(metadata, maxLength))
.host(truncateString(host, maxLength))
.root(truncateString(root, maxLength))
.branch(truncateString(branch, maxLength))
.codeVersion(truncateString(codeVersion, maxLength))
.build();
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.rollbar.api.payload.data.body;

import com.rollbar.api.json.JsonSerializable;
import com.rollbar.api.truncation.StringTruncatable;

import java.util.HashMap;

/**
* A container for the actual error(s), message, or crash report that caused this error.
*/
public class Body implements JsonSerializable {
public class Body implements JsonSerializable, StringTruncatable<Body> {

private static final long serialVersionUID = -2783273957046705016L;

Expand Down Expand Up @@ -35,6 +37,17 @@ public Object asJson() {
return values;
}

@Override
public Body truncateStrings(int maxSize) {
if (bodyContent != null) {
return new Body.Builder(this)
.bodyContent(bodyContent.truncateStrings(maxSize))
.build();
} else {
return this;
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.rollbar.api.payload.data.body;

import com.rollbar.api.truncation.StringTruncatable;

/**
* A marker interface for the contents of the rollbar body.
*/
public interface BodyContent {
public interface BodyContent extends StringTruncatable<BodyContent> {

/**
* The key name of the body content.
Expand Down
Loading

0 comments on commit 1795254

Please sign in to comment.