use of io.netty.handler.codec.http.LastHttpContent in project riposte by Nike-Inc.
the class DTraceStartHandler method doChannelRead.
@Override
public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) {
// We only do the processing for HttpRequest which is the first message in a request chain.
if (shouldHandleDoChannelReadMessage(msg)) {
try {
startTrace((HttpRequest) msg, ctx);
} catch (Throwable t) {
logger.error("An unexpected error occurred while starting the distributed tracing overall request span. This " + "exception will be swallowed to avoid breaking the Netty pipeline, but it should be " + "investigated as it shouldn't ever happen.", t);
}
}
if (msg instanceof LastHttpContent) {
HttpProcessingState httpProcessingState = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
// Add the "we received the last bytes of the request on the wire" annotation to the span if possible
// and desired.
Span requestSpan = handlerUtils.getOverallRequestSpan(httpProcessingState);
if (requestSpan != null && spanNamingAndTaggingStrategy.shouldAddWireReceiveFinishAnnotation()) {
requestSpan.addTimestampedAnnotationForCurrentTime(spanNamingAndTaggingStrategy.wireReceiveFinishAnnotationName());
}
}
return PipelineContinuationBehavior.CONTINUE;
}
use of io.netty.handler.codec.http.LastHttpContent in project riposte by Nike-Inc.
the class RequestContentDeserializerHandler method doChannelRead.
@Override
public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof LastHttpContent) {
HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
Endpoint<?> endpoint = state.getEndpointForExecution();
RequestInfo reqInfo = state.getRequestInfo();
// Don't bother trying to deserialize until we have an endpoint and the request content has fully arrived
if (endpoint != null && reqInfo.isCompleteRequestWithAllChunks()) {
// Setup the content deserializer if desired
TypeReference<?> contentTypeRef = endpoint.requestContentType();
if (contentTypeRef != null) {
// A non-null TypeReference is available, so deserialization is possible. Retrieve the appropriate
// deserializer and setup the RequestInfo so that it can lazily deserialize when requested.
ObjectMapper deserializer = endpoint.customRequestContentDeserializer(reqInfo);
if (deserializer == null)
deserializer = defaultRequestContentDeserializer;
// noinspection unchecked
reqInfo.setupContentDeserializer(deserializer, contentTypeRef);
}
}
}
return PipelineContinuationBehavior.CONTINUE;
}
use of io.netty.handler.codec.http.LastHttpContent in project riposte by Nike-Inc.
the class AccessLogStartHandler method doChannelRead.
@Override
public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpRequest) {
HttpProcessingState httpProcessingState = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
if (httpProcessingState != null) {
httpProcessingState.setRequestStartTime(Instant.now());
httpProcessingState.setRequestStartTimeNanos(System.nanoTime());
} else {
runnableWithTracingAndMdc(() -> logger.warn("HttpProcessingState is null reading HttpRequest. This shouldn't happen."), ctx).run();
}
}
if (msg instanceof LastHttpContent) {
HttpProcessingState httpProcessingState = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
if (httpProcessingState != null) {
// Capture the current nano time as the time when the last chunk of the payload was received.
long lastChunkArrivedTimeNanos = System.nanoTime();
httpProcessingState.setRequestLastChunkArrivedTimeNanos(lastChunkArrivedTimeNanos);
// Calculate and set a request attribute for the payload transfer time from the caller.
// Note that this might be both a HttpRequest and LastHttpContent (e.g. in the case of
// a FullHttpRequest). If it's an HttpRequest then the RequestInfo might not have been
// set on the HttpProcessingState yet, so we'll call RiposteHandlerInternalUtil to create
// a RequestInfo and set it on the HttpProcessingState if necessary.
RequestInfo<?> requestInfo = (msg instanceof HttpRequest) ? handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary((HttpRequest) msg, httpProcessingState) : httpProcessingState.getRequestInfo();
if (requestInfo != null) {
Long reqStartTimeNanos = httpProcessingState.getRequestStartTimeNanos();
if (reqStartTimeNanos != null) {
requestInfo.addRequestAttribute(REQUEST_PAYLOAD_TRANSFER_TIME_NANOS_REQUEST_ATTR_KEY, (lastChunkArrivedTimeNanos - reqStartTimeNanos));
} else {
runnableWithTracingAndMdc(() -> logger.warn("HttpProcessingState.getRequestStartTimeNanos() is null. " + "This shouldn't happen."), ctx).run();
}
} else {
runnableWithTracingAndMdc(() -> logger.warn("RequestInfo is null. This shouldn't happen."), ctx).run();
}
} else {
runnableWithTracingAndMdc(() -> logger.warn("HttpProcessingState is null reading LastHttpContent. This shouldn't happen."), ctx).run();
}
}
return PipelineContinuationBehavior.CONTINUE;
}
use of io.netty.handler.codec.http.LastHttpContent in project riposte by Nike-Inc.
the class RequestInfoImplTest method addContentChunk_does_not_throw_IllegalStateException_if_requestInfo_trailingHeaders_is_already_populated_when_last_chunk_arrives_if_same_instance.
@Test
public void addContentChunk_does_not_throw_IllegalStateException_if_requestInfo_trailingHeaders_is_already_populated_when_last_chunk_arrives_if_same_instance() {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
requestInfo.isCompleteRequestWithAllChunks = false;
LastHttpContent lastChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(UUID.randomUUID().toString(), CharsetUtil.UTF_8));
lastChunk.trailingHeaders().add("somekey", "someval");
requestInfo.trailingHeaders = lastChunk.trailingHeaders();
// when
requestInfo.addContentChunk(lastChunk);
// then
assertThat(requestInfo.trailingHeaders, is(lastChunk.trailingHeaders()));
}
use of io.netty.handler.codec.http.LastHttpContent in project riposte by Nike-Inc.
the class RequestInfoImplTest method addContentChunk_throws_IllegalStateException_if_requestInfo_trailingHeaders_is_already_populated_when_last_chunk_arrives.
@Test(expected = IllegalStateException.class)
public void addContentChunk_throws_IllegalStateException_if_requestInfo_trailingHeaders_is_already_populated_when_last_chunk_arrives() {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
requestInfo.isCompleteRequestWithAllChunks = false;
LastHttpContent lastChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(UUID.randomUUID().toString(), CharsetUtil.UTF_8));
requestInfo.trailingHeaders.add("somekey", "someval");
// expect
requestInfo.addContentChunk(lastChunk);
fail("Expected an IllegalStateException, but no exception was thrown");
}
Aggregations