use of fish.payara.microprofile.metrics.writer.MetricsWriter in project Payara by payara.
the class MetricsResource method getOutputWriter.
private MetricsWriter getOutputWriter(HttpServletRequest request, HttpServletResponse response) throws IOException {
MetricsWriter outputWriter = null;
String method = request.getMethod();
Writer writer = response.getWriter();
String accept = request.getHeader(ACCEPT);
if (accept == null) {
accept = TEXT_PLAIN;
}
if (GET.equals(method)) {
if (accept.contains(APPLICATION_JSON)) {
outputWriter = new JsonMetricWriter(writer);
} else if (accept.contains(TEXT_PLAIN)) {
outputWriter = new PrometheusWriter(writer);
} else {
outputWriter = new PrometheusWriter(writer);
}
} else if (OPTIONS.equals(method)) {
if (accept.contains(APPLICATION_JSON)) {
outputWriter = new JsonMetadataWriter(writer);
} else {
response.sendError(SC_NOT_ACCEPTABLE, String.format("[%s] not acceptable", accept));
}
} else {
response.sendError(SC_METHOD_NOT_ALLOWED, String.format("HTTP method [%s] not allowed", method));
}
return outputWriter;
}
use of fish.payara.microprofile.metrics.writer.MetricsWriter in project Payara by payara.
the class MetricsResource method processRequest.
/**
* Processes requests for both HTTP <code>GET</code> and <code>OPTIONS</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
MetricsService metricsService = Globals.getDefaultBaseServiceLocator().getService(MetricsService.class);
if (!metricsService.isEnabled()) {
response.sendError(SC_FORBIDDEN, "MicroProfile Metrics Service is disabled");
return;
}
metricsService.refresh();
String pathInfo = request.getPathInfo() != null ? request.getPathInfo().substring(1) : EMPTY_STRING;
String[] pathInfos = pathInfo.split("/");
String registryName = pathInfos.length > 0 ? pathInfos[0] : null;
String metricName = pathInfos.length > 1 ? pathInfos[1] : null;
try {
String contentType = getContentType(request, response);
if (contentType != null) {
response.setContentType(contentType);
response.setCharacterEncoding(UTF_8.name());
MetricsWriter outputWriter = getOutputWriter(request, response, metricsService, contentType);
if (outputWriter != null) {
if (registryName != null && !registryName.isEmpty()) {
Type scope;
try {
scope = Type.valueOf(registryName.toUpperCase());
} catch (RuntimeException ex) {
throw new NoSuchRegistryException(registryName);
}
if (metricName != null && !metricName.isEmpty()) {
outputWriter.write(scope, metricName);
} else {
outputWriter.write(scope);
}
} else {
outputWriter.write();
}
}
}
} catch (NoSuchRegistryException ex) {
response.sendError(SC_NOT_FOUND, String.format("[%s] registry not found", registryName));
} catch (NoSuchMetricException ex) {
response.sendError(SC_NOT_FOUND, String.format("[%s] metric not found", metricName));
}
}
use of fish.payara.microprofile.metrics.writer.MetricsWriter in project Payara by payara.
the class MetricsResource method getOutputWriter.
@SuppressWarnings("resource")
private static MetricsWriter getOutputWriter(HttpServletRequest request, HttpServletResponse response, MetricsService service, String contentType) throws IOException {
Writer writer = response.getWriter();
String method = request.getMethod();
if (GET.equalsIgnoreCase(method)) {
if (APPLICATION_JSON.equals(contentType)) {
return new MetricsWriterImpl(new JsonExporter(writer, Mode.GET, true), service.getContextNames(), service::getContext, getGlobalTags());
}
if (TEXT_PLAIN.equals(contentType)) {
return new MetricsWriterImpl(new OpenMetricsExporter(writer), service.getContextNames(), service::getContext, getGlobalTags());
}
}
if (OPTIONS.equalsIgnoreCase(method)) {
if (APPLICATION_JSON.equals(contentType)) {
return new MetricsWriterImpl(new JsonExporter(writer, Mode.OPTIONS, true), service.getContextNames(), service::getContext, getGlobalTags());
}
}
return null;
}
Aggregations