Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support hot set trace through /exception/trace API #1727

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@
import java.util.Map;
import java.util.NoSuchElementException;

import javax.annotation.security.RolesAllowed;
import javax.inject.Singleton;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
Expand All @@ -38,10 +45,13 @@
import org.glassfish.hk2.api.MultiException;

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.api.API;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.config.ServerOptions;
import com.baidu.hugegraph.exception.HugeGremlinException;
import com.baidu.hugegraph.exception.NotFoundException;
import com.codahale.metrics.annotation.Timed;
import com.google.common.collect.ImmutableMap;

public class ExceptionFilter {

Expand All @@ -52,12 +62,17 @@ public class ExceptionFilter {
private static final int INTERNAL_SERVER_ERROR =
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();

public static class TracedExceptionMapper {
public static class TracedExceptionMapper extends API {

private static boolean forcedTrace = false;

@Context
private javax.inject.Provider<HugeConfig> configProvider;

protected boolean trace() {
if (forcedTrace) {
return true;
}
HugeConfig config = this.configProvider.get();
if (config == null) {
return false;
Expand All @@ -66,54 +81,81 @@ protected boolean trace() {
}
}

@Path("exception/trace")
@Singleton
public static class TracedExceptionAPI extends API {

@GET
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin"})
public Object get() {
return ImmutableMap.of("trace", TracedExceptionMapper.forcedTrace);
}

@PUT
@Timed
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin"})
public Object trace(boolean trace) {
TracedExceptionMapper.forcedTrace = trace;
return ImmutableMap.of("trace", TracedExceptionMapper.forcedTrace);
}
}

@Provider
public static class HugeExceptionMapper
extends TracedExceptionMapper
implements ExceptionMapper<HugeException> {

@Override
public Response toResponse(HugeException exception) {
return Response.status(BAD_REQUEST_ERROR)
.type(MediaType.APPLICATION_JSON)
.entity(formatException(exception))
.entity(formatException(exception, this.trace()))
.build();
}
}

@Provider
public static class IllegalArgumentExceptionMapper
extends TracedExceptionMapper
implements ExceptionMapper<IllegalArgumentException> {

@Override
public Response toResponse(IllegalArgumentException exception) {
return Response.status(BAD_REQUEST_ERROR)
.type(MediaType.APPLICATION_JSON)
.entity(formatException(exception))
.entity(formatException(exception, this.trace()))
.build();
}
}

@Provider
public static class NotFoundExceptionMapper
extends TracedExceptionMapper
implements ExceptionMapper<NotFoundException> {

@Override
public Response toResponse(NotFoundException exception) {
return Response.status(NOT_FOUND_ERROR)
.type(MediaType.APPLICATION_JSON)
.entity(formatException(exception))
.entity(formatException(exception, this.trace()))
.build();
}
}

@Provider
public static class NoSuchElementExceptionMapper
extends TracedExceptionMapper
implements ExceptionMapper<NoSuchElementException> {

@Override
public Response toResponse(NoSuchElementException exception) {
return Response.status(NOT_FOUND_ERROR)
.type(MediaType.APPLICATION_JSON)
.entity(formatException(exception))
.entity(formatException(exception, this.trace()))
.build();
}
}
Expand Down Expand Up @@ -189,10 +231,6 @@ public Response toResponse(Throwable exception) {
}
}

public static String formatException(Throwable exception) {
return formatException(exception, false);
}

public static String formatException(Throwable exception, boolean trace) {
String clazz = exception.getClass().toString();
String message = exception.getMessage() != null ?
Expand Down