io.prometheus.client.exporter.MetricsServlet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simpleclient_servlet Show documentation
Show all versions of simpleclient_servlet Show documentation
HTTP servlet exporter for the simpleclient.
package io.prometheus.client.exporter;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.common.TextFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* The MetricsServlet class exists to provide a simple way of exposing the metrics values.
*
*/
public class MetricsServlet extends HttpServlet {
private CollectorRegistry registry;
/**
* Construct a MetricsServlet for the default registry.
*/
public MetricsServlet() {
this(CollectorRegistry.defaultRegistry);
}
/**
* Construct a MetricsServlet for the given registry.
* @param registry collector registry
*/
public MetricsServlet(CollectorRegistry registry) {
this.registry = registry;
}
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
resp.setStatus(HttpServletResponse.SC_OK);
String contentType = TextFormat.chooseContentType(req.getHeader("Accept"));
resp.setContentType(contentType);
Writer writer = new BufferedWriter(resp.getWriter());
try {
TextFormat.writeFormat(contentType, writer, registry.filteredMetricFamilySamples(parse(req)));
writer.flush();
} finally {
writer.close();
}
}
private Set parse(HttpServletRequest req) {
String[] includedParam = req.getParameterValues("name[]");
if (includedParam == null) {
return Collections.emptySet();
} else {
return new HashSet(Arrays.asList(includedParam));
}
}
@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}