package org.glassfish.jersey.server.internal.monitoring;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.ws.rs.ProcessingException;
import javax.annotation.Priority;
import javax.inject.Inject;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.server.internal.LocalizationMessages;
import org.glassfish.jersey.server.model.ResourceMethod;
import org.glassfish.jersey.server.monitoring.ApplicationEvent;
import org.glassfish.jersey.server.monitoring.ApplicationEventListener;
import org.glassfish.jersey.server.monitoring.DestroyListener;
import org.glassfish.jersey.server.monitoring.RequestEvent;
import org.glassfish.jersey.server.monitoring.RequestEventListener;
import org.glassfish.jersey.uri.UriTemplate;
@Priority(ApplicationInfoListener.PRIORITY + 100)
public final class MonitoringEventListener implements ApplicationEventListener {
private static final Logger LOGGER = Logger.getLogger(MonitoringEventListener.class.getName());
private static final int EVENT_QUEUE_SIZE = 500_000;
@Inject
private InjectionManager injectionManager;
private final Queue<RequestStats> requestQueuedItems = new ArrayBlockingQueue<>(EVENT_QUEUE_SIZE);
private final Queue<Integer> responseStatuses = new ArrayBlockingQueue<>(EVENT_QUEUE_SIZE);
private final Queue<RequestEvent> exceptionMapperEvents = new ArrayBlockingQueue<>(EVENT_QUEUE_SIZE);
private volatile MonitoringStatisticsProcessor monitoringStatisticsProcessor;
static class TimeStats {
private final long duration;
private final long startTime;
private TimeStats(final long startTime, final long requestDuration) {
this.duration = requestDuration;
this.startTime = startTime;
}
long getDuration() {
return duration;
}
long getStartTime() {
return startTime;
}
}
static class MethodStats extends TimeStats {
private final ResourceMethod method;
private MethodStats(final ResourceMethod method, final long startTime, final long requestDuration) {
super(startTime, requestDuration);
this.method = method;
}
ResourceMethod getMethod() {
return method;
}
}
static class RequestStats {
private final TimeStats requestStats;
private final MethodStats methodStats;
private final String requestUri;
private RequestStats(final TimeStats requestStats, final MethodStats methodStats, final String requestUri) {
this.requestStats = requestStats;
this.methodStats = methodStats;
this.requestUri = requestUri;
}
TimeStats getRequestStats() {
return requestStats;
}
MethodStats getMethodStats() {
return methodStats;
}
String getRequestUri() {
return requestUri;
}
}
@Override
public ReqEventListener onRequest(final RequestEvent requestEvent) {
switch (requestEvent.getType()) {
case START:
return new ReqEventListener();
}
return null;
}
@Override
public void onEvent(final ApplicationEvent event) {
final ApplicationEvent.Type type = event.getType();
switch (type) {
case INITIALIZATION_START:
break;
case RELOAD_FINISHED:
case INITIALIZATION_FINISHED:
this.monitoringStatisticsProcessor = new MonitoringStatisticsProcessor(injectionManager, this);
this.monitoringStatisticsProcessor.startMonitoringWorker();
break;
case DESTROY_FINISHED:
if (monitoringStatisticsProcessor != null) {
try {
monitoringStatisticsProcessor.shutDown();
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new ProcessingException(LocalizationMessages.ERROR_MONITORING_SHUTDOWN_INTERRUPTED(), e);
}
}
final List<DestroyListener> listeners =
injectionManager.getAllInstances(DestroyListener.class);
for (final DestroyListener listener : listeners) {
try {
listener.onDestroy();
} catch (final Exception e) {
LOGGER.log(Level.WARNING,
LocalizationMessages.ERROR_MONITORING_STATISTICS_LISTENER_DESTROY(listener.getClass()), e);
}
}
break;
}
}
private class ReqEventListener implements RequestEventListener {
private final long requestTimeStart;
private volatile long methodTimeStart;
private volatile MethodStats methodStats;
public ReqEventListener() {
this.requestTimeStart = System.currentTimeMillis();
}
@Override
public void onEvent(final RequestEvent event) {
final long now = System.currentTimeMillis();
switch (event.getType()) {
case RESOURCE_METHOD_START:
this.methodTimeStart = now;
break;
case RESOURCE_METHOD_FINISHED:
final ResourceMethod method = event.getUriInfo().getMatchedResourceMethod();
methodStats = new MethodStats(method, methodTimeStart, now - methodTimeStart);
break;
case EXCEPTION_MAPPING_FINISHED:
if (!exceptionMapperEvents.offer(event)) {
LOGGER.warning(LocalizationMessages.ERROR_MONITORING_QUEUE_MAPPER());
}
break;
case FINISHED:
if (event.isResponseWritten()) {
if (!responseStatuses.offer(event.getContainerResponse().getStatus())) {
LOGGER.warning(LocalizationMessages.ERROR_MONITORING_QUEUE_RESPONSE());
}
}
final StringBuilder sb = new StringBuilder();
final List<UriTemplate> orderedTemplates =
event.getUriInfo().getMatchedTemplates()
.stream()
.collect(Collectors.collectingAndThen(Collectors.toList(), uriTemplates -> {
Collections.reverse(uriTemplates);
return uriTemplates;
}));
for (final UriTemplate uriTemplate : orderedTemplates) {
sb.append(uriTemplate.getTemplate());
if (!uriTemplate.endsWithSlash()) {
sb.append("/");
}
sb.setLength(sb.length() - 1);
}
if (!requestQueuedItems.offer(new RequestStats(new TimeStats(requestTimeStart, now - requestTimeStart),
methodStats, sb.toString()))) {
LOGGER.warning(LocalizationMessages.ERROR_MONITORING_QUEUE_REQUEST());
}
}
}
}
Queue<RequestEvent> getExceptionMapperEvents() {
return exceptionMapperEvents;
}
Queue<RequestStats> getRequestQueuedItems() {
return requestQueuedItems;
}
Queue<Integer> getResponseStatuses() {
return responseStatuses;
}
}