Skip to content

Commit f8a6d12

Browse files
authored
Strict entity for request body (#301)
1 parent f7cfc1f commit f8a6d12

File tree

2 files changed

+53
-47
lines changed

2 files changed

+53
-47
lines changed

src/main/java/com/arpnetworking/http/Routes.java

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -127,53 +127,59 @@ public CompletionStage<HttpResponse> apply(final HttpRequest request) {
127127
.addData("url", request.getUri())
128128
.addData("headers", request.getHeaders())
129129
.log();
130-
return process(request).whenComplete(
131-
(response, failure) -> {
132-
requestTimer.stop();
130+
return request.entity().toStrict(30000, _actorSystem)
131+
.thenApply(request::withEntity)
132+
.thenCompose(strictRequest -> {
133+
final CompletionStage<HttpResponse> process = this.process(strictRequest);
134+
process.whenComplete(
135+
(response, failure) -> {
136+
requestTimer.stop();
133137

134-
final int responseStatus;
135-
if (response != null) {
136-
responseStatus = response.status().intValue();
137-
} else {
138-
// TODO(ville): Figure out how to intercept post-exception mapping.
139-
responseStatus = 599;
140-
}
138+
final int responseStatus;
139+
if (response != null) {
140+
responseStatus = response.status().intValue();
141+
} else {
142+
// TODO(ville): Figure out how to intercept post-exception mapping.
143+
responseStatus = 599;
144+
}
141145

142-
_metrics.recordTimer(
143-
createMetricName(request, responseStatus, REQUEST_METRIC),
144-
requestTimer.elapsed(TimeUnit.NANOSECONDS),
145-
Optional.of(TimeUnit.NANOSECONDS));
146-
_metrics.recordGauge(
147-
createMetricName(request, responseStatus, BODY_SIZE_METRIC),
148-
request.entity().getContentLengthOption().orElse(0L));
149-
final int responseStatusClass = responseStatus / 100;
150-
for (final int i : STATUS_CLASSES) {
151-
_metrics.recordCounter(
152-
createMetricName(request, responseStatus, String.format("%s/%dxx", STATUS_METRIC, i)),
153-
responseStatusClass == i ? 1 : 0);
154-
}
146+
_metrics.recordTimer(
147+
createMetricName(strictRequest, responseStatus, REQUEST_METRIC),
148+
requestTimer.elapsed(TimeUnit.NANOSECONDS),
149+
Optional.of(TimeUnit.NANOSECONDS));
150+
_metrics.recordGauge(
151+
createMetricName(strictRequest, responseStatus, BODY_SIZE_METRIC),
152+
strictRequest.entity().getContentLengthOption().orElse(0));
153+
final int responseStatusClass = responseStatus / 100;
154+
for (final int i : STATUS_CLASSES) {
155+
_metrics.recordCounter(
156+
createMetricName(strictRequest, responseStatus, String.format("%s/%dxx", STATUS_METRIC, i)),
157+
responseStatusClass == i ? 1 : 0);
158+
}
155159

156-
final LogBuilder log;
157-
if (failure != null || responseStatusClass == 5) {
158-
log = LOGGER.info().setEvent("http.in.failure");
159-
if (failure != null) {
160-
log.setThrowable(failure);
161-
}
162-
if (!LOGGER.isTraceEnabled() && LOGGER.isInfoEnabled()) {
163-
log.addData("method", request.method().toString())
164-
.addData("url", request.getUri().toString())
165-
.addData(
166-
"headers",
167-
StreamSupport.stream(request.getHeaders().spliterator(), false)
168-
.map(h -> h.name() + "=" + h.value())
169-
.collect(Collectors.toList()));
170-
}
171-
} else {
172-
log = LOGGER.trace().setEvent("http.in.complete");
173-
}
174-
log.addContext("requestId", requestId)
175-
.addData("status", responseStatus)
176-
.log();
160+
final LogBuilder log;
161+
if (failure != null || responseStatusClass == 5) {
162+
log = LOGGER.info().setEvent("http.in.failure");
163+
if (failure != null) {
164+
log.setThrowable(failure);
165+
}
166+
if (!LOGGER.isTraceEnabled() && LOGGER.isInfoEnabled()) {
167+
log.addData("method", strictRequest.method().toString())
168+
.addData("url", strictRequest.getUri().toString())
169+
.addData(
170+
"headers",
171+
StreamSupport.stream(strictRequest.getHeaders().spliterator(), false)
172+
.map(h -> h.name() + "=" + h.value())
173+
.collect(Collectors.toList()));
174+
}
175+
} else {
176+
log = LOGGER.trace().setEvent("http.in.complete");
177+
}
178+
log.addContext("requestId", requestId)
179+
.addData("status", responseStatus)
180+
.log();
181+
});
182+
return process;
177183
});
178184
}
179185

src/main/java/com/arpnetworking/metrics/mad/PeriodWorker.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ private void scheduleRotation(final ZonedDateTime now) {
141141
_nextScheduledRotationTime = getRotateAt();
142142

143143
if (_nextScheduledRotationTime.isPresent()) {
144-
// If we we don't need to wait then just set the scheduled delay to 0.
144+
// If we don't need to wait then just set the scheduled delay to 0.
145145
// If we need to wait a really small amount of time, set the delay to a minimum to avoid sleep thrashing.
146-
// Otherwise schedule the next rotation at the predicted time.
147-
// Finally, if we wake-up and there's nothing to rotate we'll just re-apply these rules.
146+
// Otherwise, schedule the next rotation at the predicted time.
147+
// Finally, if we wake up and there's nothing to rotate we'll just re-apply these rules.
148148

149149
Duration timeToRotate = Duration.between(now, _nextScheduledRotationTime.get());
150150
if (timeToRotate.isNegative()) {

0 commit comments

Comments
 (0)