Search in sources :

Example 1 with MetricRegistry

use of org.eclipse.microprofile.metrics.MetricRegistry in project Payara by payara.

the class MetricsService method getMetricsAsMap.

public Map<String, Metric> getMetricsAsMap(String registryName, String metricName) throws NoSuchRegistryException, NoSuchMetricException {
    MetricRegistry registry = getRegistry(registryName);
    Map<String, Metric> metricMap = registry.getMetrics();
    if (metricMap.containsKey(metricName)) {
        return singletonMap(metricName, metricMap.get(metricName));
    } else {
        throw new NoSuchMetricException(metricName);
    }
}
Also used : NoSuchMetricException(fish.payara.microprofile.metrics.exception.NoSuchMetricException) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) Metric(org.eclipse.microprofile.metrics.Metric)

Example 2 with MetricRegistry

use of org.eclipse.microprofile.metrics.MetricRegistry in project wildfly-swarm by wildfly-swarm.

the class MetricRegistryFactory method get.

public static MetricRegistry get(MetricRegistry.Type type) {
    synchronized (registries) {
        if (registries.get(type) == null) {
            MetricRegistry result = new MetricsRegistryImpl();
            registries.put(type, result);
        }
    }
    return registries.get(type);
}
Also used : MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry)

Example 3 with MetricRegistry

use of org.eclipse.microprofile.metrics.MetricRegistry in project wildfly-swarm by wildfly-swarm.

the class MetricsHttpHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    String requestPath = exchange.getRequestPath();
    if (dispatched.get() != null && dispatched.get().getCount() == 1) {
        next.handleRequest(exchange);
        dispatched.set(null);
        return;
    }
    if (!requestPath.startsWith("/metrics")) {
        next.handleRequest(exchange);
        return;
    }
    // request is for us, so let's handle it
    Exporter exporter = obtainExporter(exchange);
    if (exporter == null) {
        exchange.setStatusCode(406);
        exchange.setReasonPhrase("No exporter found for method " + exchange.getRequestMethod() + " and media type");
        return;
    }
    String scopePath = requestPath.substring(8);
    if (scopePath.startsWith("/")) {
        scopePath = scopePath.substring(1);
    }
    if (scopePath.endsWith("/")) {
        scopePath = scopePath.substring(0, scopePath.length() - 1);
    }
    StringBuilder sb;
    if (scopePath.isEmpty()) {
        // All metrics
        sb = exporter.exportAllScopes();
    } else if (scopePath.contains("/")) {
        // One metric in a scope
        String attribute = scopePath.substring(scopePath.indexOf('/') + 1);
        MetricRegistry.Type scope = getScopeFromPath(exchange, scopePath.substring(0, scopePath.indexOf('/')));
        if (scope == null) {
            exchange.setStatusCode(404);
            exchange.setReasonPhrase("Scope " + scopePath + " not found");
            return;
        }
        MetricRegistry registry = MetricRegistryFactory.get(scope);
        Map<String, Metric> metricValuesMap = registry.getMetrics();
        if (metricValuesMap.containsKey(attribute)) {
            sb = exporter.exportOneMetric(scope, attribute);
        } else {
            exchange.setStatusCode(404);
            exchange.setReasonPhrase("Metric " + scopePath + " not found");
            return;
        }
    } else {
        // A single scope
        MetricRegistry.Type scope = getScopeFromPath(exchange, scopePath);
        if (scope == null) {
            exchange.setStatusCode(404);
            exchange.setReasonPhrase("Scope " + scopePath + " not found");
            return;
        }
        MetricRegistry reg = MetricRegistryFactory.get(scope);
        if (reg.getMetadata().size() == 0) {
            exchange.setStatusCode(204);
            exchange.setReasonPhrase("No data in scope " + scopePath);
        }
        sb = exporter.exportOneScope(scope);
    }
    exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, exporter.getContentType());
    provideCorsHeaders(exchange);
    exchange.getResponseHeaders().put(new HttpString("Access-Control-Max-Age"), "1209600");
    exchange.getResponseSender().send(sb.toString());
}
Also used : MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) HttpString(io.undertow.util.HttpString) JsonExporter(org.wildfly.swarm.microprofile.metrics.runtime.exporters.JsonExporter) JsonMetadataExporter(org.wildfly.swarm.microprofile.metrics.runtime.exporters.JsonMetadataExporter) Exporter(org.wildfly.swarm.microprofile.metrics.runtime.exporters.Exporter) PrometheusExporter(org.wildfly.swarm.microprofile.metrics.runtime.exporters.PrometheusExporter) Map(java.util.Map) HttpString(io.undertow.util.HttpString)

Example 4 with MetricRegistry

use of org.eclipse.microprofile.metrics.MetricRegistry in project wildfly-swarm by wildfly-swarm.

the class JsonExporter method exportAllScopes.

@Override
public StringBuilder exportAllScopes() {
    StringBuilder sb = new StringBuilder();
    sb.append("{");
    MetricRegistry.Type[] values = MetricRegistry.Type.values();
    int totalNonEmptyScopes = Helper.countNonEmptyScopes();
    int scopes = 0;
    for (int i = 0; i < values.length; i++) {
        MetricRegistry.Type scope = values[i];
        MetricRegistry registry = MetricRegistryFactory.get(scope);
        if (registry.getNames().size() > 0) {
            sb.append('"').append(scope.getName().toLowerCase()).append('"').append(" :\n");
            getMetricsForAScope(sb, scope);
            sb.append(JsonExporter.LF);
            scopes++;
            if (scopes < totalNonEmptyScopes) {
                sb.append(',');
            }
        }
    }
    sb.append("}");
    return sb;
}
Also used : MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry)

Example 5 with MetricRegistry

use of org.eclipse.microprofile.metrics.MetricRegistry in project wildfly-swarm by wildfly-swarm.

the class JsonExporter method exportOneMetric.

@Override
public StringBuilder exportOneMetric(MetricRegistry.Type scope, String metricName) {
    MetricRegistry registry = MetricRegistryFactory.get(scope);
    Map<String, Metric> metricMap = registry.getMetrics();
    Map<String, Metadata> metadataMap = registry.getMetadata();
    Metric m = metricMap.get(metricName);
    Map<String, Metric> outMap = new HashMap<>(1);
    outMap.put(metricName, m);
    StringBuilder sb = new StringBuilder();
    sb.append("{");
    writeMetricsForMap(sb, outMap, metadataMap);
    sb.append("}");
    sb.append(JsonExporter.LF);
    return sb;
}
Also used : HashMap(java.util.HashMap) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) Metadata(org.eclipse.microprofile.metrics.Metadata) Metric(org.eclipse.microprofile.metrics.Metric)

Aggregations

MetricRegistry (org.eclipse.microprofile.metrics.MetricRegistry)18 Metadata (org.eclipse.microprofile.metrics.Metadata)7 HashMap (java.util.HashMap)5 Metric (org.eclipse.microprofile.metrics.Metric)5 Map (java.util.Map)3 MetricsService (fish.payara.microprofile.metrics.MetricsService)2 NoSuchMetricException (fish.payara.microprofile.metrics.exception.NoSuchMetricException)2 AnnotatedMember (javax.enterprise.inject.spi.AnnotatedMember)2 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 MetricRegistryImpl (fish.payara.microprofile.metrics.impl.MetricRegistryImpl)1 MBeanMetadata (fish.payara.microprofile.metrics.jmx.MBeanMetadata)1 MBeanMetadataHelper.registerMetadata (fish.payara.microprofile.metrics.jmx.MBeanMetadataHelper.registerMetadata)1 MonitoringDataCollector (fish.payara.monitoring.collect.MonitoringDataCollector)1 HttpString (io.undertow.util.HttpString)1 Bean (javax.enterprise.inject.spi.Bean)1 ProcessProducer (javax.enterprise.inject.spi.ProcessProducer)1 Producer (javax.enterprise.inject.spi.Producer)1 MetricID (org.eclipse.microprofile.metrics.MetricID)1