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.Collector;
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.util.Collections;
import java.util.Enumeration;
import java.io.IOException;
import java.io.Writer;
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.
*/
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);
resp.setContentType(TextFormat.CONTENT_TYPE_004);
Writer writer = resp.getWriter();
TextFormat.write004(writer, registry.metricFamilySamples());
writer.flush();
writer.close();
}
@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy