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);
}
}
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);
}
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());
}
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;
}
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;
}
Aggregations