Skip to content
This repository was archived by the owner on Jul 26, 2023. It is now read-only.

Commit 2337f7f

Browse files
small restructuring of stats d (#24)
* small restructuring of stats d * BMP update max file size and dependant tests * Update readme * Promote snapshot
1 parent 3558f12 commit 2337f7f

File tree

14 files changed

+69
-80
lines changed

14 files changed

+69
-80
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ system properties will be used to specify the upstream proxy.
190190
- -proxyPortRange \<from\>-\<to\>
191191
- Range of ports reserved for proxies. Only applies if *port* parameter is not supplied in the POST request. Default values are \<port\>+1 to \<port\>+500+1.
192192
- -ttl \<seconds\>
193+
- -maxResponseSizeBytes max size of response in bytes by default 2097152 (2 MB)
193194
- Proxy will be automatically deleted after a specified time period. Off by default.
194195

195196
### Embedded Mode
@@ -395,7 +396,7 @@ When you build the latest code from source, you'll have access to the latest sna
395396
<dependency>
396397
<groupId>net.lightbody.bmp</groupId>
397398
<artifactId>browsermob-core</artifactId>
398-
<version>2.1.32-SNAPSHOT</version>
399+
<version>2.1.33-SNAPSHOT</version>
399400
<scope>test</scope>
400401
</dependency>
401402
```

browsermob-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>browsermob-proxy</artifactId>
88
<groupId>net.lightbody.bmp</groupId>
9-
<version>2.1.32-SNAPSHOT</version>
9+
<version>2.1.33-SNAPSHOT</version>
1010
</parent>
1111
<modelVersion>4.0.0</modelVersion>
1212

browsermob-core/src/main/java/net/lightbody/bmp/BrowserMobProxyServer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ public void addLastHttpFilterFactory(HttpFiltersSource filterFactory) {
893893
*/
894894
@Override
895895
public void addResponseFilter(ResponseFilter filter) {
896-
addLastHttpFilterFactory(new ResponseFilterAdapter.FilterSource(filter));
896+
addLastHttpFilterFactory(new ResponseFilterAdapter.FilterSource(filter, Integer.parseInt(System.getProperty("maxResponseSizeBytes", String.valueOf(ResponseFilterAdapter.FilterSource.DEFAULT_MAXIMUM_RESPONSE_BUFFER_SIZE)))));
897897
}
898898

899899
/**
@@ -1059,22 +1059,22 @@ public HttpFilters filterRequest(HttpRequest originalRequest) {
10591059

10601060
addHttpFilterFactory(new HttpFiltersSourceAdapter() {
10611061
@Override
1062-
public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext ctx) {
1063-
return new StatsDMetricsFilter(originalRequest, ctx);
1062+
public HttpFilters filterRequest(HttpRequest originalRequest) {
1063+
return new LatencyFilter(originalRequest, latencyMs);
10641064
}
10651065
});
10661066

10671067
addHttpFilterFactory(new HttpFiltersSourceAdapter() {
10681068
@Override
1069-
public HttpFilters filterRequest(HttpRequest originalRequest) {
1070-
return new LatencyFilter(originalRequest, latencyMs);
1069+
public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext ctx) {
1070+
return new UnregisterRequestFilter(originalRequest, ctx, activityMonitor);
10711071
}
10721072
});
10731073

10741074
addHttpFilterFactory(new HttpFiltersSourceAdapter() {
10751075
@Override
10761076
public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext ctx) {
1077-
return new UnregisterRequestFilter(originalRequest, ctx, activityMonitor);
1077+
return new StatsDMetricsFilter(originalRequest, ctx);
10781078
}
10791079
});
10801080
}

browsermob-core/src/main/java/net/lightbody/bmp/filters/HarCaptureFilter.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
public class HarCaptureFilter extends HttpsAwareFiltersAdapter {
3636
private static final Logger log = LoggerFactory.getLogger(HarCaptureFilter.class);
37-
private static final ThreadLocal<StatsDClient> statsDClient = new InheritableThreadLocal<>();
3837
private static final InheritableThreadLocal<HarRequest> isAlreadyLoggedIn = new InheritableThreadLocal<>();
3938

4039
/**
@@ -163,22 +162,6 @@ public HarCaptureFilter(HttpRequest originalRequest, ChannelHandlerContext ctx,
163162
this.harEntry = new HarEntry(currentPageRef);
164163
}
165164

166-
@Override
167-
public HttpObject proxyToClientResponse(HttpObject httpObject) {
168-
if (httpObject instanceof HttpResponse) {
169-
if (harEntry.getResponse().getStatus() == 0) {
170-
HarCaptureFilter.logFailedRequestIfRequired(harEntry.getRequest(), harEntry.getResponse());
171-
StatsDClient client = createStatsDClient();
172-
client.increment(getProxyPrefix().concat(prepareMetric(harEntry.getRequest().getUrl()))
173-
.concat("." + harEntry.getResponse().getStatus()).concat(".request_timeout"));
174-
client.stop();
175-
176-
}
177-
}
178-
179-
return super.proxyToClientResponse(httpObject);
180-
}
181-
182165
@Override
183166
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
184167
// if a ServerResponseCaptureFilter is configured, delegate to it to collect the client request. if it is not

browsermob-core/src/main/java/net/lightbody/bmp/filters/ResponseFilterAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void setModifiedHttpRequest(HttpRequest modifiedHttpRequest) {
6262
* and sets a maximum response buffer size of 2 MiB.
6363
*/
6464
public static class FilterSource extends HttpFiltersSourceAdapter {
65-
private static final int DEFAULT_MAXIMUM_RESPONSE_BUFFER_SIZE = 2097152;
65+
public static final int DEFAULT_MAXIMUM_RESPONSE_BUFFER_SIZE = 2097152;
6666

6767
private final ResponseFilter filter;
6868
private final int maximumResponseBufferSizeInBytes;
@@ -100,7 +100,7 @@ public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerCont
100100

101101
@Override
102102
public int getMaximumResponseBufferSizeInBytes() {
103-
return maximumResponseBufferSizeInBytes;
103+
return Math.toIntExact(maximumResponseBufferSizeInBytes);
104104
}
105105
}
106106
}

browsermob-core/src/main/java/net/lightbody/bmp/filters/StatsDMetricsFilter.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.timgroup.statsd.NonBlockingStatsDClient;
44
import com.timgroup.statsd.StatsDClient;
55
import io.netty.channel.ChannelHandlerContext;
6-
import io.netty.handler.codec.http.FullHttpResponse;
76
import io.netty.handler.codec.http.HttpObject;
87
import io.netty.handler.codec.http.HttpRequest;
98
import io.netty.handler.codec.http.HttpResponse;
@@ -19,9 +18,9 @@ public StatsDMetricsFilter(HttpRequest originalRequest, ChannelHandlerContext ct
1918
}
2019

2120
@Override
22-
public HttpObject proxyToClientResponse(HttpObject httpObject) {
23-
if (FullHttpResponse.class.isAssignableFrom(httpObject.getClass())) {
24-
HttpResponse httpResponse = (FullHttpResponse) httpObject;
21+
public HttpObject serverToProxyResponse(HttpObject httpObject) {
22+
if (httpObject instanceof HttpResponse) {
23+
HttpResponse httpResponse = (HttpResponse) httpObject;
2524
prepareStatsDMetrics(httpResponse.status().code());
2625
}
2726
return super.serverToProxyResponse(httpObject);

browsermob-dist/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<artifactId>browsermob-proxy</artifactId>
66
<groupId>net.lightbody.bmp</groupId>
7-
<version>2.1.32-SNAPSHOT</version>
7+
<version>2.1.33-SNAPSHOT</version>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
1010

browsermob-legacy/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>browsermob-proxy</artifactId>
88
<groupId>net.lightbody.bmp</groupId>
9-
<version>2.1.32-SNAPSHOT</version>
9+
<version>2.1.33-SNAPSHOT</version>
1010
</parent>
1111
<modelVersion>4.0.0</modelVersion>
1212

browsermob-rest/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>browsermob-proxy</artifactId>
77
<groupId>net.lightbody.bmp</groupId>
8-
<version>2.1.32-SNAPSHOT</version>
8+
<version>2.1.33-SNAPSHOT</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

browsermob-rest/src/main/java/net/lightbody/bmp/proxy/ProxyManager.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class ProxyManager {
3535
private int lastPort;
3636
private final int minPort;
3737
private final int maxPort;
38+
private final int maxResponseSizeBytes;
3839
private final Provider<LegacyProxyServer> proxyServerProvider;
3940
// retain a reference to the Cache to allow the ProxyCleanupTask to .cleanUp(), since asMap() is just a view into the cache.
4041
// it would seem to make sense to pass the newly-built Cache directly to the ProxyCleanupTask and have it retain a WeakReference to it, and
@@ -93,24 +94,23 @@ public void run() {
9394
}
9495

9596
@Inject
96-
public ProxyManager(Provider<LegacyProxyServer> proxyServerProvider, @Named("minPort") Integer minPort, @Named("maxPort") Integer maxPort, final @Named("ttl") Integer ttl) {
97+
public ProxyManager(Provider<LegacyProxyServer> proxyServerProvider, @Named("minPort") Integer minPort, @Named("maxPort") Integer maxPort, final @Named("ttl") Integer ttl, final @Named("maxResponseSizeBytes") Integer maxResponseSizeBytes) {
9798
this.proxyServerProvider = proxyServerProvider;
9899
this.minPort = minPort;
99100
this.maxPort = maxPort;
100101
this.lastPort = maxPort;
102+
this.maxResponseSizeBytes = maxResponseSizeBytes;
101103
if (ttl > 0) {
102104
// proxies should be evicted after the specified ttl, so set up an evicting cache and a listener to stop the proxies when they're evicted
103-
RemovalListener<Integer, LegacyProxyServer> removalListener = new RemovalListener<Integer, LegacyProxyServer>() {
104-
public void onRemoval(RemovalNotification<Integer, LegacyProxyServer> removal) {
105-
try {
106-
LegacyProxyServer proxy = removal.getValue();
107-
if (proxy != null) {
108-
LOG.info("Expiring ProxyServer on port {} after {} seconds without activity", proxy.getPort(), ttl);
109-
proxy.stop();
110-
}
111-
} catch (Exception ex) {
112-
LOG.warn("Error while stopping an expired proxy on port " + removal.getKey(), ex);
105+
RemovalListener<Integer, LegacyProxyServer> removalListener = removal -> {
106+
try {
107+
LegacyProxyServer proxy = removal.getValue();
108+
if (proxy != null) {
109+
LOG.info("Expiring ProxyServer on port {} after {} seconds without activity", proxy.getPort(), ttl);
110+
proxy.stop();
113111
}
112+
} catch (Exception ex) {
113+
LOG.warn("Error while stopping an expired proxy on port " + removal.getKey(), ex);
114114
}
115115
};
116116

browsermob-rest/src/main/java/net/lightbody/bmp/proxy/guice/ConfigModule.java

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.google.inject.Binder;
55
import com.google.inject.Key;
66
import com.google.inject.Module;
7-
import com.google.inject.Provider;
87
import joptsimple.ArgumentAcceptingOptionSpec;
98
import joptsimple.OptionParser;
109
import joptsimple.OptionSet;
@@ -29,31 +28,37 @@ public void configure(Binder binder) {
2928
ArgumentAcceptingOptionSpec<Integer> portSpec =
3029
parser.accepts("port", "The port to listen on")
3130
.withOptionalArg().ofType(Integer.class).defaultsTo(8080);
32-
31+
3332
ArgumentAcceptingOptionSpec<String> addressSpec =
3433
parser.accepts("address", "The address to bind to")
35-
.withOptionalArg()
36-
.ofType(String.class)
37-
.defaultsTo("0.0.0.0");
38-
34+
.withOptionalArg()
35+
.ofType(String.class)
36+
.defaultsTo("0.0.0.0");
37+
3938
ArgumentAcceptingOptionSpec<Integer> proxyPortRange =
4039
parser.accepts("proxyPortRange", "The range of ports to use for proxies")
41-
.withOptionalArg()
42-
.ofType(Integer.class)
43-
.defaultsTo(8081, 8581)
44-
.withValuesSeparatedBy('-');
40+
.withOptionalArg()
41+
.ofType(Integer.class)
42+
.defaultsTo(8081, 8581)
43+
.withValuesSeparatedBy('-');
4544

4645
ArgumentAcceptingOptionSpec<Integer> ttlSpec =
4746
parser.accepts("ttl", "Time in seconds until an unused proxy is deleted")
48-
.withOptionalArg()
49-
.ofType(Integer.class)
50-
.defaultsTo(0);
47+
.withOptionalArg()
48+
.ofType(Integer.class)
49+
.defaultsTo(0);
50+
51+
ArgumentAcceptingOptionSpec<Integer> maxResponseSizeSpec =
52+
parser.accepts("maxResponseSizeBytes", "Max response size")
53+
.withOptionalArg()
54+
.ofType(Integer.class)
55+
.defaultsTo(2097152);
5156

5257
ArgumentAcceptingOptionSpec<Boolean> useLittleProxy =
5358
parser.accepts("use-littleproxy", "Use the littleproxy backend instead of the legacy Jetty 5-based implementation")
54-
.withOptionalArg()
55-
.ofType(Boolean.class)
56-
.defaultsTo(true);
59+
.withOptionalArg()
60+
.ofType(Boolean.class)
61+
.defaultsTo(true);
5762

5863
parser.acceptsAll(Arrays.asList("help", "?"), "This help text");
5964

@@ -78,44 +83,44 @@ public void configure(Binder binder) {
7883
System.out.println("Running BrowserMob Proxy using legacy implementation.");
7984
}
8085

81-
List<Integer> ports = options.valuesOf(proxyPortRange);
82-
if(ports.size() < 2){
86+
List<Integer> ports = options.valuesOf(proxyPortRange);
87+
if (ports.size() < 2) {
8388
throw new IllegalArgumentException();
8489
}
8590
Integer minPort;
86-
Integer maxPort;
87-
if(ports.get(1) > ports.get(0)){
91+
Integer maxPort;
92+
if (ports.get(1) > ports.get(0)) {
8893
minPort = ports.get(0);
8994
maxPort = ports.get(1);
90-
}else{
95+
} else {
9196
minPort = ports.get(1);
9297
maxPort = ports.get(0);
93-
}
98+
}
9499
Integer port = portSpec.value(options);
95-
if(port >= minPort && port <= maxPort){
100+
if (port >= minPort && port <= maxPort) {
96101
int num = maxPort - minPort;
97102
minPort = port + 1;
98103
maxPort = minPort + num;
99104
}
100105

106+
System.setProperty("maxResponseSizeBytes", String.valueOf(maxResponseSizeSpec.value(options)));
107+
101108
binder.bind(Key.get(Integer.class, new NamedImpl("port"))).toInstance(port);
102109
binder.bind(Key.get(String.class, new NamedImpl("address"))).toInstance(addressSpec.value(options));
103110
binder.bind(Key.get(Integer.class, new NamedImpl("minPort"))).toInstance(minPort);
104-
binder.bind(Key.get(Integer.class, new NamedImpl("maxPort"))).toInstance(maxPort);
111+
binder.bind(Key.get(Integer.class, new NamedImpl("maxPort"))).toInstance(maxPort);
105112
binder.bind(Key.get(Integer.class, new NamedImpl("ttl"))).toInstance(ttlSpec.value(options));
113+
binder.bind(Key.get(Integer.class, new NamedImpl("maxResponseSizeBytes"))).toInstance(maxResponseSizeSpec.value(options));
106114

107115
binder.bind(LegacyProxyServer.class).toProvider(LegacyProxyServerProvider.class);
108116

109117
// bind an ObjectMapper provider that uses the system time zone instead of UTC by default
110-
binder.bind(ObjectMapper.class).toProvider(new Provider<ObjectMapper>() {
111-
@Override
112-
public ObjectMapper get() {
113-
ObjectMapper objectMapper = new ObjectMapper();
118+
binder.bind(ObjectMapper.class).toProvider(() -> {
119+
ObjectMapper objectMapper = new ObjectMapper();
114120

115-
objectMapper.setTimeZone(TimeZone.getDefault());
121+
objectMapper.setTimeZone(TimeZone.getDefault());
116122

117-
return objectMapper;
118-
}
123+
return objectMapper;
119124
});
120125
}
121126
}

browsermob-rest/src/test/java/net/lightbody/bmp/proxy/ExpiringProxyTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.lightbody.bmp.proxy;
22

3+
import net.lightbody.bmp.filters.ResponseFilterAdapter;
34
import net.lightbody.bmp.proxy.guice.LegacyProxyServerProvider;
45
import org.junit.Test;
56

@@ -17,7 +18,7 @@ public void testExpiredProxyStops() throws InterruptedException {
1718
ProxyManager proxyManager = new ProxyManager(new LegacyProxyServerProvider(),
1819
minPort,
1920
minPort + 100,
20-
2);
21+
2, ResponseFilterAdapter.FilterSource.DEFAULT_MAXIMUM_RESPONSE_BUFFER_SIZE);
2122

2223
LegacyProxyServer proxy = proxyManager.create(Collections.<String, String>emptyMap());
2324
int port = proxy.getPort();
@@ -44,7 +45,7 @@ public void testZeroTtlProxyDoesNotExpire() throws InterruptedException {
4445
ProxyManager proxyManager = new ProxyManager(new LegacyProxyServerProvider(),
4546
minPort,
4647
minPort + 100,
47-
0);
48+
0, ResponseFilterAdapter.FilterSource.DEFAULT_MAXIMUM_RESPONSE_BUFFER_SIZE);
4849

4950
LegacyProxyServer proxy = proxyManager.create(Collections.<String, String>emptyMap());
5051
int port = proxy.getPort();

mitm/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<artifactId>browsermob-proxy</artifactId>
66
<groupId>net.lightbody.bmp</groupId>
7-
<version>2.1.32-SNAPSHOT</version>
7+
<version>2.1.33-SNAPSHOT</version>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
1010

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>net.lightbody.bmp</groupId>
55
<artifactId>browsermob-proxy</artifactId>
6-
<version>2.1.32-SNAPSHOT</version>
6+
<version>2.1.33-SNAPSHOT</version>
77
<modules>
88
<module>browsermob-core</module>
99
<module>browsermob-legacy</module>

0 commit comments

Comments
 (0)