From 93eadd4f1d276b7aa01fe15cd0c708747320810a Mon Sep 17 00:00:00 2001 From: Edmundo Alvarez Date: Mon, 18 Jan 2016 16:13:43 +0100 Subject: [PATCH] Return thread dump in a JSON document That is more consistent, and plays nicer with other services consuming it. --- .../responses/SystemThreadDumpResponse.java | 35 +++++++++++++++++++ .../rest/resources/system/SystemResource.java | 13 +++---- 2 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 graylog2-server/src/main/java/org/graylog2/rest/models/system/responses/SystemThreadDumpResponse.java diff --git a/graylog2-server/src/main/java/org/graylog2/rest/models/system/responses/SystemThreadDumpResponse.java b/graylog2-server/src/main/java/org/graylog2/rest/models/system/responses/SystemThreadDumpResponse.java new file mode 100644 index 000000000000..6cc9d5170310 --- /dev/null +++ b/graylog2-server/src/main/java/org/graylog2/rest/models/system/responses/SystemThreadDumpResponse.java @@ -0,0 +1,35 @@ +/** + * This file is part of Graylog. + * + * Graylog is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Graylog is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Graylog. If not, see . + */ + +package org.graylog2.rest.models.system.responses; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.auto.value.AutoValue; + +@AutoValue +@JsonAutoDetect +public abstract class SystemThreadDumpResponse { + @JsonProperty("threaddump") + public abstract String threadDump(); + + @JsonCreator + public static SystemThreadDumpResponse create(@JsonProperty("threaddump") String threadDump) { + return new AutoValue_SystemThreadDumpResponse(threadDump); + } +} diff --git a/graylog2-server/src/main/java/org/graylog2/shared/rest/resources/system/SystemResource.java b/graylog2-server/src/main/java/org/graylog2/shared/rest/resources/system/SystemResource.java index c17e02f55131..ad66fc41a727 100644 --- a/graylog2-server/src/main/java/org/graylog2/shared/rest/resources/system/SystemResource.java +++ b/graylog2-server/src/main/java/org/graylog2/shared/rest/resources/system/SystemResource.java @@ -30,6 +30,7 @@ import org.graylog2.plugin.cluster.ClusterId; import org.graylog2.rest.models.system.responses.SystemJVMResponse; import org.graylog2.rest.models.system.responses.SystemOverviewResponse; +import org.graylog2.rest.models.system.responses.SystemThreadDumpResponse; import org.graylog2.shared.ServerVersion; import org.graylog2.shared.rest.resources.RestResource; import org.graylog2.shared.security.RestPermissions; @@ -38,18 +39,17 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; import java.io.ByteArrayOutputStream; import java.lang.management.ManagementFactory; import java.nio.charset.StandardCharsets; import java.util.Locale; import java.util.Map; -import static javax.ws.rs.core.MediaType.APPLICATION_JSON; -import static javax.ws.rs.core.MediaType.TEXT_PLAIN; - @RequiresAuthentication @Api(value = "System", description = "System information of this node.") @Path("/system") +@Produces(MediaType.APPLICATION_JSON) public class SystemResource extends RestResource { private final ServerStatus serverStatus; private final ClusterId clusterId; @@ -63,7 +63,6 @@ public SystemResource(ServerStatus serverStatus, ClusterConfigService clusterCon @GET @Timed @ApiOperation(value = "Get system overview") - @Produces(APPLICATION_JSON) public SystemOverviewResponse system() { checkPermission(RestPermissions.SYSTEM_READ, serverStatus.getNodeId().toString()); @@ -86,7 +85,6 @@ public SystemOverviewResponse system() { @ApiOperation(value = "Get JVM information") @Path("/jvm") @Timed - @Produces(APPLICATION_JSON) public SystemJVMResponse jvm() { checkPermission(RestPermissions.JVMSTATS_READ, serverStatus.getNodeId().toString()); @@ -105,8 +103,7 @@ public SystemJVMResponse jvm() { @Timed @ApiOperation(value = "Get a thread dump") @Path("/threaddump") - @Produces(TEXT_PLAIN) - public String threaddump() { + public SystemThreadDumpResponse threaddump() { checkPermission(RestPermissions.THREADS_DUMP, serverStatus.getNodeId().toString()); // The ThreadDump is built by internal codahale.metrics servlet library we are abusing. @@ -114,7 +111,7 @@ public String threaddump() { final ByteArrayOutputStream output = new ByteArrayOutputStream(); threadDump.dump(output); - return new String(output.toByteArray(), StandardCharsets.UTF_8); + return SystemThreadDumpResponse.create(new String(output.toByteArray(), StandardCharsets.UTF_8)); } private Map bytesToValueMap(long bytes) {