|
| 1 | +/* |
| 2 | + * Copyright 2022 VMware, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.micrometer.jetty11; |
| 17 | + |
| 18 | +import io.micrometer.core.instrument.*; |
| 19 | +import io.micrometer.core.instrument.binder.BaseUnits; |
| 20 | +import io.micrometer.core.instrument.binder.http.DefaultHttpJakartaServletRequestTagsProvider; |
| 21 | +import io.micrometer.core.instrument.binder.http.HttpJakartaServletRequestTagsProvider; |
| 22 | +import jakarta.servlet.AsyncEvent; |
| 23 | +import jakarta.servlet.AsyncListener; |
| 24 | +import jakarta.servlet.ServletException; |
| 25 | +import jakarta.servlet.http.HttpServletRequest; |
| 26 | +import jakarta.servlet.http.HttpServletResponse; |
| 27 | +import org.eclipse.jetty.http.HttpStatus; |
| 28 | +import org.eclipse.jetty.server.AsyncContextEvent; |
| 29 | +import org.eclipse.jetty.server.Handler; |
| 30 | +import org.eclipse.jetty.server.HttpChannelState; |
| 31 | +import org.eclipse.jetty.server.Request; |
| 32 | +import org.eclipse.jetty.server.handler.HandlerWrapper; |
| 33 | +import org.eclipse.jetty.util.component.Graceful; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.util.concurrent.CompletableFuture; |
| 37 | +import java.util.concurrent.atomic.AtomicInteger; |
| 38 | + |
| 39 | +/** |
| 40 | + * Adapted from Jetty's <a href= |
| 41 | + * "https://github.com/eclipse/jetty.project/blob/jetty-9.4.x/jetty-server/src/main/java/org/eclipse/jetty/server/handler/StatisticsHandler.java">StatisticsHandler</a>. |
| 42 | + * |
| 43 | + * @author Jon Schneider |
| 44 | + * @since 1.10.0 |
| 45 | + */ |
| 46 | +public class TimedHandler extends HandlerWrapper implements Graceful { |
| 47 | + |
| 48 | + private static final String SAMPLE_REQUEST_TIMER_ATTRIBUTE = "__micrometer_timer_sample"; |
| 49 | + |
| 50 | + private static final String SAMPLE_REQUEST_LONG_TASK_TIMER_ATTRIBUTE = "__micrometer_ltt_sample"; |
| 51 | + |
| 52 | + private final MeterRegistry registry; |
| 53 | + |
| 54 | + private final Iterable<Tag> tags; |
| 55 | + |
| 56 | + private final HttpJakartaServletRequestTagsProvider tagsProvider; |
| 57 | + |
| 58 | + private final Shutdown shutdown = new Shutdown(this) { |
| 59 | + @Override |
| 60 | + public boolean isShutdownDone() { |
| 61 | + return openRequests.activeTasks() == 0; |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + private final LongTaskTimer openRequests; |
| 66 | + |
| 67 | + private final Counter asyncDispatches; |
| 68 | + |
| 69 | + private final Counter asyncExpires; |
| 70 | + |
| 71 | + private final AtomicInteger asyncWaits = new AtomicInteger(); |
| 72 | + |
| 73 | + public TimedHandler(MeterRegistry registry, Iterable<Tag> tags) { |
| 74 | + this(registry, tags, new DefaultHttpJakartaServletRequestTagsProvider()); |
| 75 | + } |
| 76 | + |
| 77 | + public TimedHandler(MeterRegistry registry, Iterable<Tag> tags, |
| 78 | + HttpJakartaServletRequestTagsProvider tagsProvider) { |
| 79 | + this.registry = registry; |
| 80 | + this.tags = tags; |
| 81 | + this.tagsProvider = tagsProvider; |
| 82 | + |
| 83 | + this.openRequests = LongTaskTimer.builder("jetty.server.dispatches.open") |
| 84 | + .description("Jetty dispatches that are currently in progress").tags(tags).register(registry); |
| 85 | + |
| 86 | + this.asyncDispatches = Counter.builder("jetty.server.async.dispatches").description("Asynchronous dispatches") |
| 87 | + .tags(tags).register(registry); |
| 88 | + |
| 89 | + this.asyncExpires = Counter.builder("jetty.server.async.expires") |
| 90 | + .description("Asynchronous operations that timed out before completing").tags(tags).register(registry); |
| 91 | + |
| 92 | + Gauge.builder("jetty.server.async.waits", asyncWaits, AtomicInteger::doubleValue) |
| 93 | + .description("Pending asynchronous wait operations").baseUnit(BaseUnits.OPERATIONS).tags(tags) |
| 94 | + .register(registry); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void handle(String path, Request baseRequest, HttpServletRequest request, HttpServletResponse response) |
| 99 | + throws IOException, ServletException { |
| 100 | + Timer.Sample sample = Timer.start(registry); |
| 101 | + LongTaskTimer.Sample requestSample; |
| 102 | + |
| 103 | + HttpChannelState state = baseRequest.getHttpChannelState(); |
| 104 | + if (state.isInitial()) { |
| 105 | + requestSample = openRequests.start(); |
| 106 | + request.setAttribute(SAMPLE_REQUEST_TIMER_ATTRIBUTE, sample); |
| 107 | + request.setAttribute(SAMPLE_REQUEST_LONG_TASK_TIMER_ATTRIBUTE, requestSample); |
| 108 | + } |
| 109 | + else { |
| 110 | + asyncDispatches.increment(); |
| 111 | + request.setAttribute(SAMPLE_REQUEST_TIMER_ATTRIBUTE, sample); |
| 112 | + requestSample = (LongTaskTimer.Sample) request.getAttribute(SAMPLE_REQUEST_LONG_TASK_TIMER_ATTRIBUTE); |
| 113 | + } |
| 114 | + |
| 115 | + try { |
| 116 | + Handler handler = getHandler(); |
| 117 | + if (handler != null && !shutdown.isShutdown() && isStarted()) { |
| 118 | + handler.handle(path, baseRequest, request, response); |
| 119 | + } |
| 120 | + else { |
| 121 | + if (!baseRequest.isHandled()) { |
| 122 | + baseRequest.setHandled(true); |
| 123 | + } |
| 124 | + if (!baseRequest.getResponse().isCommitted()) { |
| 125 | + response.sendError(HttpStatus.SERVICE_UNAVAILABLE_503); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + finally { |
| 130 | + if (state.isSuspended()) { |
| 131 | + if (state.isInitial()) { |
| 132 | + state.addListener(onCompletion); |
| 133 | + asyncWaits.incrementAndGet(); |
| 134 | + } |
| 135 | + } |
| 136 | + else if (state.isInitial()) { |
| 137 | + sample.stop(Timer.builder("jetty.server.requests").description("HTTP requests to the Jetty server") |
| 138 | + .tags(tagsProvider.getTags(request, response)).tags(tags).register(registry)); |
| 139 | + |
| 140 | + requestSample.stop(); |
| 141 | + |
| 142 | + // If we have no more dispatches, should we signal shutdown? |
| 143 | + if (shutdown.isShutdown()) { |
| 144 | + shutdown.check(); |
| 145 | + } |
| 146 | + } |
| 147 | + // else onCompletion will handle it. |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private final AsyncListener onCompletion = new AsyncListener() { |
| 152 | + @Override |
| 153 | + public void onTimeout(AsyncEvent event) { |
| 154 | + asyncExpires.increment(); |
| 155 | + |
| 156 | + HttpChannelState state = ((AsyncContextEvent) event).getHttpChannelState(); |
| 157 | + Request request = state.getBaseRequest(); |
| 158 | + |
| 159 | + LongTaskTimer.Sample lttSample = (LongTaskTimer.Sample) request |
| 160 | + .getAttribute(SAMPLE_REQUEST_LONG_TASK_TIMER_ATTRIBUTE); |
| 161 | + lttSample.stop(); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public void onStartAsync(AsyncEvent event) { |
| 166 | + event.getAsyncContext().addListener(this); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void onError(AsyncEvent event) { |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void onComplete(AsyncEvent event) { |
| 175 | + HttpChannelState state = ((AsyncContextEvent) event).getHttpChannelState(); |
| 176 | + |
| 177 | + Request request = state.getBaseRequest(); |
| 178 | + Timer.Sample sample = (Timer.Sample) request.getAttribute(SAMPLE_REQUEST_TIMER_ATTRIBUTE); |
| 179 | + LongTaskTimer.Sample lttSample = (LongTaskTimer.Sample) request |
| 180 | + .getAttribute(SAMPLE_REQUEST_LONG_TASK_TIMER_ATTRIBUTE); |
| 181 | + |
| 182 | + if (sample != null) { |
| 183 | + sample.stop(Timer.builder("jetty.server.requests").description("HTTP requests to the Jetty server") |
| 184 | + .tags(tagsProvider.getTags(request, request.getResponse())).tags(tags).register(registry)); |
| 185 | + |
| 186 | + lttSample.stop(); |
| 187 | + } |
| 188 | + |
| 189 | + asyncWaits.decrementAndGet(); |
| 190 | + |
| 191 | + // If we have no more dispatches, should we signal shutdown? |
| 192 | + if (shutdown.isShutdown()) { |
| 193 | + shutdown.check(); |
| 194 | + } |
| 195 | + } |
| 196 | + }; |
| 197 | + |
| 198 | + @Override |
| 199 | + protected void doStart() throws Exception { |
| 200 | + shutdown.cancel(); |
| 201 | + super.doStart(); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + protected void doStop() throws Exception { |
| 206 | + shutdown.cancel(); |
| 207 | + super.doStop(); |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public CompletableFuture<Void> shutdown() { |
| 212 | + return shutdown.shutdown(); |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public boolean isShutdown() { |
| 217 | + return shutdown.isShutdown(); |
| 218 | + } |
| 219 | + |
| 220 | +} |
0 commit comments