Skip to content

Commit 6a24db9

Browse files
committed
Add support for configuring the rollbar url
Added the url field in the `Config` interface to be used in the rollbar-logback module when wanting to set a custom url (e.g. region specific rollar-us, rollbar-eu). For RollbarAppender, added the possibility to customise the environment, language and to report the server hostname.
1 parent d21c57d commit 6a24db9

4 files changed

Lines changed: 94 additions & 12 deletions

File tree

rollbar-java/src/main/java/com/rollbar/notifier/config/Config.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public interface Config {
2626
*/
2727
String accessToken();
2828

29+
/**
30+
* Get the url.
31+
*
32+
* @return the Rollbar url.
33+
*/
34+
String url();
35+
2936
/**
3037
* Get the environment.
3138
*

rollbar-java/src/main/java/com/rollbar/notifier/config/ConfigBuilder.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class ConfigBuilder {
2727

2828
private String accessToken;
2929

30+
private String url;
31+
3032
private String environment;
3133

3234
private String codeVersion;
@@ -103,6 +105,7 @@ private ConfigBuilder(Config config) {
103105
this.sender = config.sender();
104106
this.handleUncaughtErrors = config.handleUncaughtErrors();
105107
this.enabled = config.isEnabled();
108+
this.url = config.url();
106109
}
107110

108111
/**
@@ -134,6 +137,17 @@ public ConfigBuilder accessToken(String accessToken) {
134137
return this;
135138
}
136139

140+
/**
141+
* The Rollbar url to use.
142+
*
143+
* @param url the Rollbar url endpoint.
144+
* @return the builder instance.
145+
*/
146+
public ConfigBuilder url(String url) {
147+
this.url = url;
148+
return this;
149+
}
150+
137151
/**
138152
* Represents the current environment (e.g.: production, debug, test).
139153
*
@@ -363,9 +377,10 @@ public Config build() {
363377
this.notifier = new NotifierProvider();
364378
}
365379
if (this.sender == null) {
366-
this.sender = new BufferedSender.Builder()
380+
this.sender =
381+
new BufferedSender.Builder()
367382
.sender(
368-
new SyncSender.Builder()
383+
new SyncSender.Builder(this.url)
369384
.accessToken(accessToken)
370385
.build()
371386
).build();
@@ -381,6 +396,8 @@ private static class ConfigImpl implements Config {
381396

382397
private final String accessToken;
383398

399+
private final String url;
400+
384401
private final String environment;
385402

386403
private final String codeVersion;
@@ -423,6 +440,7 @@ private static class ConfigImpl implements Config {
423440

424441
ConfigImpl(ConfigBuilder builder) {
425442
this.accessToken = builder.accessToken;
443+
this.url = builder.url;
426444
this.environment = builder.environment;
427445
this.codeVersion = builder.codeVersion;
428446
this.platform = builder.platform;
@@ -450,6 +468,11 @@ public String accessToken() {
450468
return accessToken;
451469
}
452470

471+
@Override
472+
public String url() {
473+
return url;
474+
}
475+
453476
@Override
454477
public String environment() {
455478
return environment;

rollbar-logback/src/main/java/com/rollbar/logback/RollbarAppender.java

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import ch.qos.logback.core.AppenderBase;
99
import com.rollbar.api.payload.data.Level;
1010
import com.rollbar.notifier.Rollbar;
11+
import com.rollbar.notifier.config.Config;
1112
import com.rollbar.notifier.wrapper.RollbarThrowableWrapper;
1213
import com.rollbar.notifier.wrapper.ThrowableWrapper;
1314
import java.util.HashMap;
@@ -32,19 +33,31 @@ public class RollbarAppender extends AppenderBase<ILoggingEvent> {
3233

3334
private String accessToken;
3435

35-
/**
36-
* Constructor for testing purposes.
37-
*
38-
* @param rollbar the rollbar notifier.
39-
*/
40-
protected RollbarAppender(Rollbar rollbar) {
41-
this.rollbar = rollbar;
42-
}
36+
private String url;
37+
38+
private String environment;
39+
40+
private String language;
41+
42+
// /**
43+
// * Constructor for testing purposes.
44+
// *
45+
// * @param rollbar the rollbar notifier.
46+
// */
47+
// protected RollbarAppender(Rollbar rollbar) {
48+
// this.rollbar = rollbar;
49+
// }
4350

4451
@Override
4552
public void start() {
46-
this.rollbar = new Rollbar(withAccessToken(this.accessToken).build());
47-
53+
Config config = withAccessToken(this.accessToken)
54+
.environment(this.environment)
55+
.url(this.url)
56+
.server(new ServerProvider())
57+
.language(this.language)
58+
.build();
59+
60+
this.rollbar = new Rollbar(config);
4861
super.start();
4962
}
5063

@@ -73,6 +86,18 @@ public void setAccessToken(String accessToken) {
7386
this.accessToken = accessToken;
7487
}
7588

89+
public void setUrl(String url) {
90+
this.url = url;
91+
}
92+
93+
public void setEnvironment(String environment) {
94+
this.environment = environment;
95+
}
96+
97+
public void setLanguage(String language) {
98+
this.language = language;
99+
}
100+
76101
private ThrowableWrapper buildRollbarThrowableWrapper(IThrowableProxy throwableProxy) {
77102
if (throwableProxy == null) {
78103
return null;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.rollbar.logback;
2+
3+
import com.rollbar.api.payload.data.Server;
4+
import com.rollbar.notifier.provider.Provider;
5+
6+
import java.net.InetAddress;
7+
import java.net.UnknownHostException;
8+
9+
/**
10+
* Server provider that populates the server's hostname
11+
*/
12+
public class ServerProvider implements Provider<Server> {
13+
14+
@Override
15+
public Server provide() {
16+
try {
17+
InetAddress host = InetAddress.getLocalHost();
18+
return new Server.Builder()
19+
.host(host.getHostName())
20+
.build();
21+
} catch (UnknownHostException e) {
22+
return new Server.Builder()
23+
.host("localhost")
24+
.build();
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)