Search in sources :

Example 1 with MetricsService

use of fish.payara.microprofile.metrics.MetricsService 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.isMetricsEnabled()) {
        response.sendError(SC_FORBIDDEN, "MP Metrics is disabled");
        return;
    }
    if (!request.isSecure() && metricsService.isMetricsSecure()) {
        response.sendError(SC_FORBIDDEN, "MP Metrics security is enabled");
        return;
    }
    MetricsRequest metricsRequest = new MetricsRequest(request);
    try {
        if (metricsRequest.isRegistryRequested() && !REGISTRY_NAMES.contains(metricsRequest.getRegistryName())) {
            throw new NoSuchRegistryException(metricsRequest.getRegistryName());
        }
        MetricsWriter outputWriter = getOutputWriter(request, response);
        if (outputWriter != null) {
            setContentType(outputWriter, response);
            if (metricsRequest.isRegistryRequested() && metricsRequest.isMetricRequested()) {
                outputWriter.write(metricsRequest.getRegistryName(), metricsRequest.getMetricName());
            } else if (metricsRequest.isRegistryRequested()) {
                outputWriter.write(metricsRequest.getRegistryName());
            } else {
                outputWriter.write();
            }
        }
    } catch (NoSuchRegistryException ex) {
        response.sendError(SC_NOT_FOUND, String.format("[%s] registry not found", metricsRequest.getRegistryName()));
    } catch (NoSuchMetricException ex) {
        response.sendError(SC_NOT_FOUND, String.format("[%s] metric not found", metricsRequest.getMetricName()));
    }
}
Also used : MetricsWriter(fish.payara.microprofile.metrics.writer.MetricsWriter) NoSuchRegistryException(fish.payara.microprofile.metrics.exception.NoSuchRegistryException) NoSuchMetricException(fish.payara.microprofile.metrics.exception.NoSuchMetricException) MetricsService(fish.payara.microprofile.metrics.MetricsService)

Example 2 with MetricsService

use of fish.payara.microprofile.metrics.MetricsService in project Payara by payara.

the class MetricCDIExtension method registerCustomMetrics.

private void registerCustomMetrics(@Observes AfterDeploymentValidation adv, BeanManager manager) {
    MetricsService metricsService = Globals.getDefaultBaseServiceLocator().getService(MetricsService.class);
    MetricRegistry registry = metricsService.getOrAddRegistry(metricsService.getApplicationName());
    MetricsHelper helper = getReference(manager, MetricsHelper.class);
    for (Map.Entry<Producer<?>, AnnotatedMember<?>> entry : metrics.entrySet()) {
        AnnotatedMember<?> annotatedMember = entry.getValue();
        Producer<?> prod = entry.getKey();
        if (hasInjectionPoints(annotatedMember)) {
            continue;
        }
        Metadata metadata = helper.metadataOf(annotatedMember);
        registry.register(metadata.getName(), (Metric) prod.produce(manager.createCreationalContext(null)), metadata);
    }
    metrics.clear();
}
Also used : MetricProducer(fish.payara.microprofile.metrics.cdi.producer.MetricProducer) ProcessProducer(javax.enterprise.inject.spi.ProcessProducer) Producer(javax.enterprise.inject.spi.Producer) MetricRegistryProducer(fish.payara.microprofile.metrics.cdi.producer.MetricRegistryProducer) MetricsService(fish.payara.microprofile.metrics.MetricsService) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) MetricsHelper(fish.payara.microprofile.metrics.cdi.MetricsHelper) AnnotatedMember(javax.enterprise.inject.spi.AnnotatedMember) Metadata(org.eclipse.microprofile.metrics.Metadata) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with MetricsService

use of fish.payara.microprofile.metrics.MetricsService in project Payara by payara.

the class MetricsInterceptor method constructorInvocation.

@AroundConstruct
private Object constructorInvocation(InvocationContext context) throws Exception {
    Object target;
    MetricsService metricsService = Globals.getDefaultBaseServiceLocator().getService(MetricsService.class);
    if (metricsService.isMetricsEnabled()) {
        Class<?> beanClass = bean.getBeanClass();
        registerMetrics(beanClass, context.getConstructor(), context.getTarget());
        target = context.proceed();
        Class<?> type = beanClass;
        do {
            for (Method method : type.getDeclaredMethods()) {
                if (!method.isSynthetic() && !Modifier.isPrivate(method.getModifiers())) {
                    registerMetrics(beanClass, method, context.getTarget());
                }
            }
            type = type.getSuperclass();
        } while (!Object.class.equals(type));
    } else {
        target = context.proceed();
    }
    return target;
}
Also used : MetricsService(fish.payara.microprofile.metrics.MetricsService) Method(java.lang.reflect.Method) AroundConstruct(javax.interceptor.AroundConstruct)

Aggregations

MetricsService (fish.payara.microprofile.metrics.MetricsService)3 MetricsHelper (fish.payara.microprofile.metrics.cdi.MetricsHelper)1 MetricProducer (fish.payara.microprofile.metrics.cdi.producer.MetricProducer)1 MetricRegistryProducer (fish.payara.microprofile.metrics.cdi.producer.MetricRegistryProducer)1 NoSuchMetricException (fish.payara.microprofile.metrics.exception.NoSuchMetricException)1 NoSuchRegistryException (fish.payara.microprofile.metrics.exception.NoSuchRegistryException)1 MetricsWriter (fish.payara.microprofile.metrics.writer.MetricsWriter)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AnnotatedMember (javax.enterprise.inject.spi.AnnotatedMember)1 ProcessProducer (javax.enterprise.inject.spi.ProcessProducer)1 Producer (javax.enterprise.inject.spi.Producer)1 AroundConstruct (javax.interceptor.AroundConstruct)1 Metadata (org.eclipse.microprofile.metrics.Metadata)1 MetricRegistry (org.eclipse.microprofile.metrics.MetricRegistry)1