Search in sources :

Example 1 with MetricsWriter

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;
}
Also used : MetricsWriter(fish.payara.microprofile.metrics.writer.MetricsWriter) PrometheusWriter(fish.payara.microprofile.metrics.writer.PrometheusWriter) JsonMetadataWriter(fish.payara.microprofile.metrics.writer.JsonMetadataWriter) JsonMetricWriter(fish.payara.microprofile.metrics.writer.JsonMetricWriter) MetricsWriter(fish.payara.microprofile.metrics.writer.MetricsWriter) JsonMetadataWriter(fish.payara.microprofile.metrics.writer.JsonMetadataWriter) PrometheusWriter(fish.payara.microprofile.metrics.writer.PrometheusWriter) JsonMetricWriter(fish.payara.microprofile.metrics.writer.JsonMetricWriter) Writer(java.io.Writer)

Example 2 with MetricsWriter

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));
    }
}
Also used : MetricsWriter(fish.payara.microprofile.metrics.writer.MetricsWriter) MediaType(javax.ws.rs.core.MediaType) Type(org.eclipse.microprofile.metrics.MetricRegistry.Type) NoSuchRegistryException(fish.payara.microprofile.metrics.exception.NoSuchRegistryException) NoSuchMetricException(fish.payara.microprofile.metrics.exception.NoSuchMetricException) MetricsService(fish.payara.microprofile.metrics.MetricsService)

Example 3 with MetricsWriter

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;
}
Also used : MetricsWriterImpl(fish.payara.microprofile.metrics.writer.MetricsWriterImpl) OpenMetricsExporter(fish.payara.microprofile.metrics.writer.OpenMetricsExporter) JsonExporter(fish.payara.microprofile.metrics.writer.JsonExporter) MetricsWriter(fish.payara.microprofile.metrics.writer.MetricsWriter) Writer(java.io.Writer)

Aggregations

MetricsWriter (fish.payara.microprofile.metrics.writer.MetricsWriter)3 Writer (java.io.Writer)2 MetricsService (fish.payara.microprofile.metrics.MetricsService)1 NoSuchMetricException (fish.payara.microprofile.metrics.exception.NoSuchMetricException)1 NoSuchRegistryException (fish.payara.microprofile.metrics.exception.NoSuchRegistryException)1 JsonExporter (fish.payara.microprofile.metrics.writer.JsonExporter)1 JsonMetadataWriter (fish.payara.microprofile.metrics.writer.JsonMetadataWriter)1 JsonMetricWriter (fish.payara.microprofile.metrics.writer.JsonMetricWriter)1 MetricsWriterImpl (fish.payara.microprofile.metrics.writer.MetricsWriterImpl)1 OpenMetricsExporter (fish.payara.microprofile.metrics.writer.OpenMetricsExporter)1 PrometheusWriter (fish.payara.microprofile.metrics.writer.PrometheusWriter)1 MediaType (javax.ws.rs.core.MediaType)1 Type (org.eclipse.microprofile.metrics.MetricRegistry.Type)1