Skip to content

Commit

Permalink
Return thread dump in a JSON document
Browse files Browse the repository at this point in the history
That is more consistent, and plays nicer with other services consuming
it.
  • Loading branch information
Edmundo Alvarez committed Jan 21, 2016
1 parent 6eb0023 commit 93eadd4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
@@ -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 <http://www.gnu.org/licenses/>.
*/

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);
}
}
Expand Up @@ -30,6 +30,7 @@
import org.graylog2.plugin.cluster.ClusterId; import org.graylog2.plugin.cluster.ClusterId;
import org.graylog2.rest.models.system.responses.SystemJVMResponse; import org.graylog2.rest.models.system.responses.SystemJVMResponse;
import org.graylog2.rest.models.system.responses.SystemOverviewResponse; 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.ServerVersion;
import org.graylog2.shared.rest.resources.RestResource; import org.graylog2.shared.rest.resources.RestResource;
import org.graylog2.shared.security.RestPermissions; import org.graylog2.shared.security.RestPermissions;
Expand All @@ -38,18 +39,17 @@
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;


import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;

@RequiresAuthentication @RequiresAuthentication
@Api(value = "System", description = "System information of this node.") @Api(value = "System", description = "System information of this node.")
@Path("/system") @Path("/system")
@Produces(MediaType.APPLICATION_JSON)
public class SystemResource extends RestResource { public class SystemResource extends RestResource {
private final ServerStatus serverStatus; private final ServerStatus serverStatus;
private final ClusterId clusterId; private final ClusterId clusterId;
Expand All @@ -63,7 +63,6 @@ public SystemResource(ServerStatus serverStatus, ClusterConfigService clusterCon
@GET @GET
@Timed @Timed
@ApiOperation(value = "Get system overview") @ApiOperation(value = "Get system overview")
@Produces(APPLICATION_JSON)
public SystemOverviewResponse system() { public SystemOverviewResponse system() {
checkPermission(RestPermissions.SYSTEM_READ, serverStatus.getNodeId().toString()); checkPermission(RestPermissions.SYSTEM_READ, serverStatus.getNodeId().toString());


Expand All @@ -86,7 +85,6 @@ public SystemOverviewResponse system() {
@ApiOperation(value = "Get JVM information") @ApiOperation(value = "Get JVM information")
@Path("/jvm") @Path("/jvm")
@Timed @Timed
@Produces(APPLICATION_JSON)
public SystemJVMResponse jvm() { public SystemJVMResponse jvm() {
checkPermission(RestPermissions.JVMSTATS_READ, serverStatus.getNodeId().toString()); checkPermission(RestPermissions.JVMSTATS_READ, serverStatus.getNodeId().toString());


Expand All @@ -105,16 +103,15 @@ public SystemJVMResponse jvm() {
@Timed @Timed
@ApiOperation(value = "Get a thread dump") @ApiOperation(value = "Get a thread dump")
@Path("/threaddump") @Path("/threaddump")
@Produces(TEXT_PLAIN) public SystemThreadDumpResponse threaddump() {
public String threaddump() {
checkPermission(RestPermissions.THREADS_DUMP, serverStatus.getNodeId().toString()); checkPermission(RestPermissions.THREADS_DUMP, serverStatus.getNodeId().toString());


// The ThreadDump is built by internal codahale.metrics servlet library we are abusing. // The ThreadDump is built by internal codahale.metrics servlet library we are abusing.
final ThreadDump threadDump = new ThreadDump(ManagementFactory.getThreadMXBean()); final ThreadDump threadDump = new ThreadDump(ManagementFactory.getThreadMXBean());
final ByteArrayOutputStream output = new ByteArrayOutputStream(); final ByteArrayOutputStream output = new ByteArrayOutputStream();


threadDump.dump(output); threadDump.dump(output);
return new String(output.toByteArray(), StandardCharsets.UTF_8); return SystemThreadDumpResponse.create(new String(output.toByteArray(), StandardCharsets.UTF_8));
} }


private Map<String, Long> bytesToValueMap(long bytes) { private Map<String, Long> bytesToValueMap(long bytes) {
Expand Down

0 comments on commit 93eadd4

Please sign in to comment.