
org.glowroot.ui.TraceDetailHttpService Maven / Gradle / Ivy
/*
* Copyright 2011-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.glowroot.ui;
import java.util.List;
import javax.annotation.Nullable;
import org.glowroot.agent.shaded.netty.channel.ChannelFuture;
import org.glowroot.agent.shaded.netty.channel.ChannelHandlerContext;
import org.glowroot.agent.shaded.netty.handler.codec.http.DefaultFullHttpResponse;
import org.glowroot.agent.shaded.netty.handler.codec.http.DefaultHttpResponse;
import org.glowroot.agent.shaded.netty.handler.codec.http.FullHttpResponse;
import org.glowroot.agent.shaded.netty.handler.codec.http.HttpHeaderNames;
import org.glowroot.agent.shaded.netty.handler.codec.http.HttpHeaderValues;
import org.glowroot.agent.shaded.netty.handler.codec.http.HttpRequest;
import org.glowroot.agent.shaded.netty.handler.codec.http.HttpResponse;
import org.glowroot.agent.shaded.netty.handler.codec.http.HttpUtil;
import org.glowroot.agent.shaded.netty.handler.codec.http.QueryStringDecoder;
import org.glowroot.agent.shaded.slf4j.Logger;
import org.glowroot.agent.shaded.slf4j.LoggerFactory;
import static org.glowroot.agent.shaded.google.common.base.Preconditions.checkNotNull;
import static org.glowroot.agent.shaded.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
import static org.glowroot.agent.shaded.netty.handler.codec.http.HttpResponseStatus.OK;
import static org.glowroot.agent.shaded.netty.handler.codec.http.HttpVersion.HTTP_1_1;
class TraceDetailHttpService implements HttpService {
private static final Logger logger = LoggerFactory.getLogger(TraceDetailHttpService.class);
private final TraceCommonService traceCommonService;
TraceDetailHttpService(TraceCommonService traceCommonService) {
this.traceCommonService = traceCommonService;
}
@Override
public @Nullable FullHttpResponse handleRequest(ChannelHandlerContext ctx, HttpRequest request)
throws Exception {
QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
String path = decoder.path();
String traceComponent = path.substring(path.lastIndexOf('/') + 1);
List serverIds = decoder.parameters().get("server-id");
checkNotNull(serverIds, "Missing server id in query string: %s", request.uri());
String serverId = serverIds.get(0);
List traceIds = decoder.parameters().get("trace-id");
checkNotNull(traceIds, "Missing trace id in query string: %s", request.uri());
String traceId = traceIds.get(0);
logger.debug("handleRequest(): traceComponent={}, serverId={}, traceId={}", traceComponent,
serverId, traceId);
ChunkSource detail = getDetailChunkSource(traceComponent, serverId, traceId);
if (detail == null) {
return new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND);
}
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=UTF-8");
boolean keepAlive = HttpUtil.isKeepAlive(request);
if (keepAlive && !request.protocolVersion().isKeepAliveDefault()) {
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
HttpServices.preventCaching(response);
ctx.write(response);
// TODO no more point in chunking here
ChannelFuture future = ctx.write(ChunkedInputs.from(detail));
HttpServices.addErrorListener(future);
if (!keepAlive) {
HttpServices.addCloseListener(future);
}
// return null to indicate streaming
return null;
}
private @Nullable ChunkSource getDetailChunkSource(String traceComponent, String serverName,
String traceId) throws Exception {
if (traceComponent.equals("entries")) {
String entriesJson = traceCommonService.getEntriesJson(serverName, traceId);
if (entriesJson == null) {
// this includes trace was found but the trace had no trace entries
// caller should check trace.entry_count
return null;
}
return ChunkSource.wrap(entriesJson);
}
if (traceComponent.equals("profile")) {
String profileJson = traceCommonService.getProfileTreeJson(serverName, traceId);
if (profileJson == null) {
return null;
}
return ChunkSource.wrap(profileJson);
}
throw new IllegalStateException("Unexpected trace component: " + traceComponent);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy