package org.atlasapi.system;

import com.datastax.driver.core.Session;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import static com.google.common.base.Preconditions.checkNotNull;

@Controller
public class HealthController {

    private final Gson gson;
    private final Session cassandra;

    public HealthController(Session cassandra) {
        this.cassandra = cassandra;
        this.gson = new GsonBuilder().create();
    }

    @RequestMapping("/system/threads")
    public void showThreads(HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        response.setStatus(200);
        ServletOutputStream out = response.getOutputStream();
        out.print("<html><body>");
        final Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
        for (Entry<Thread, StackTraceElement[]> entry : traces.entrySet()) {
            out.print(String.format("<font color='blue'>Stack trace for thread '%s'</font>:<br/>", entry.getKey().getName()));
            for (StackTraceElement e : entry.getValue()) {
                out.print(e.toString() + "<br/>");
            }
            out.print("<hr/>");
        }
        out.print("</body></html>");
        out.close();
    }

    @RequestMapping("/system/cassandra")
    public void showCassandraMetrics(HttpServletResponse response) throws IOException {
        gson.toJson(cassandra.getCluster().getMetrics().getRegistry(), response.getWriter());
        response.setStatus(200);
        response.flushBuffer();
    }
}
package org.atlasapi.system;

import com.datastax.driver.core.Session;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import static com.google.common.base.Preconditions.checkNotNull;

@Controller
public class HealthController {

    private final Gson gson;
    private final Session cassandra;

    public HealthController(Session cassandra) {
        this.cassandra = cassandra;
        this.gson = new GsonBuilder().create();
    }

    @RequestMapping("/system/threads")
    public void showThreads(HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        response.setStatus(200);
        ServletOutputStream out = response.getOutputStream();
        out.print("<html><body>");
        final Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
        for (Entry<Thread, StackTraceElement[]> entry : traces.entrySet()) {
            out.print(String.format("<font color='blue'>Stack trace for thread '%s'</font>:<br/>", entry.getKey().getName()));
            for (StackTraceElement e : entry.getValue()) {
                out.print(e.toString() + "<br/>");
            }
            out.print("<hr/>");
        }
        out.print("</body></html>");
        out.close();
    }

    @RequestMapping("/system/cassandra")
    public void showCassandraMetrics(HttpServletResponse response) throws IOException {
        JsonElement json = gson.toJsonTree(cassandra.getCluster().getMetrics().getRegistry().getMetrics());
        response.getWriter().write(gson.toJson(json));
        response.setStatus(200);
        response.flushBuffer();
    }
}
Correct serialisation of metrics registry