com.qmetric.spark.metrics.MetricsRoute Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spark-metrics Show documentation
Show all versions of spark-metrics Show documentation
A library of Route decorators to add metrics to spark based applications and Routes to access the metrics
package com.qmetric.spark.metrics;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.json.MetricsModule;
import com.fasterxml.jackson.databind.ObjectMapper;
import spark.Request;
import spark.Response;
import spark.Route;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class MetricsRoute extends Route
{
public static final String PATH = "/metrics";
private final MetricRegistry metricRegistry;
private final ObjectMapper objectMapper;
public MetricsRoute(final MetricRegistry metricRegistry)
{
super(PATH);
this.metricRegistry = metricRegistry;
objectMapper = new ObjectMapper().registerModule(new MetricsModule(TimeUnit.SECONDS, TimeUnit.MILLISECONDS, true));
}
@Override public Object handle(final Request request, final Response response)
{
response.raw().setContentType("application/json");
response.raw().setHeader("Cache-Control", "must-revalidate,no-cache,no-store");
response.raw().setStatus(HttpServletResponse.SC_OK);
try (ServletOutputStream outputStream = response.raw().getOutputStream())
{
objectMapper.writer().withDefaultPrettyPrinter().writeValue(outputStream, metricRegistry);
}
catch (IOException e)
{
return e.getMessage();
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy