|
| 1 | +package com.vaadin.flow.server; |
| 2 | + |
| 3 | +import io.micrometer.common.lang.Nullable; |
| 4 | +import io.micrometer.jakarta.instrument.binder.http.DefaultHttpJakartaServerServletRequestObservationConvention; |
| 5 | +import io.micrometer.jakarta.instrument.binder.http.HttpJakartaServerServletRequestObservationContext; |
| 6 | +import io.micrometer.jakarta.instrument.binder.http.HttpJakartaServerServletRequestObservationConvention; |
| 7 | +import io.micrometer.jakarta.instrument.binder.http.JakartaHttpObservationDocumentation; |
| 8 | +import io.micrometer.observation.Observation; |
| 9 | +import io.micrometer.observation.ObservationRegistry; |
| 10 | +import jakarta.servlet.http.HttpServletResponse; |
| 11 | + |
| 12 | +/** |
| 13 | + * Micrometer Observation {@link VaadinFilter} that will start |
| 14 | + * observations around processing of a request. |
| 15 | + * |
| 16 | + * @author Marcin Grzejszczak |
| 17 | + * @since 24.2 |
| 18 | + */ |
| 19 | +public class ObservedVaadinFilter implements VaadinFilter { |
| 20 | + |
| 21 | + private static final String SCOPE_ATTRIBUTE = ObservedVaadinFilter.class.getName() + ".scope"; |
| 22 | + |
| 23 | + private final ObservationRegistry observationRegistry; |
| 24 | + |
| 25 | + private final HttpJakartaServerServletRequestObservationConvention convention; |
| 26 | + |
| 27 | + public ObservedVaadinFilter(ObservationRegistry observationRegistry, |
| 28 | + @Nullable HttpJakartaServerServletRequestObservationConvention convention) { |
| 29 | + this.observationRegistry = observationRegistry; |
| 30 | + this.convention = convention; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void requestStart(VaadinRequest request, VaadinResponse response) { |
| 35 | + if (request instanceof VaadinServletRequest servletRequest && response instanceof VaadinServletResponse servletResponse) { |
| 36 | + HttpJakartaServerServletRequestObservationContext context = new HttpJakartaServerServletRequestObservationContext(servletRequest, servletResponse); |
| 37 | + Observation observation = JakartaHttpObservationDocumentation.JAKARTA_SERVLET_SERVER_OBSERVATION.start(this.convention, DefaultHttpJakartaServerServletRequestObservationConvention.INSTANCE, () -> context, observationRegistry); |
| 38 | + request.setAttribute(SCOPE_ATTRIBUTE, observation.openScope()); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void handleException(VaadinRequest request, VaadinResponse response, VaadinSession vaadinSession, Exception t) { |
| 44 | + Observation.Scope scope = (Observation.Scope) request.getAttribute(SCOPE_ATTRIBUTE); |
| 45 | + if (scope == null) { |
| 46 | + return; |
| 47 | + } |
| 48 | + scope.getCurrentObservation().error(t); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void requestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session) { |
| 53 | + Observation.Scope scope = (Observation.Scope) request.getAttribute(SCOPE_ATTRIBUTE); |
| 54 | + if (scope == null) { |
| 55 | + return; |
| 56 | + } |
| 57 | + scope.close(); |
| 58 | + Observation observation = scope.getCurrentObservation(); |
| 59 | + if (!observation.isNoop() && response instanceof HttpServletResponse httpServletResponse) { |
| 60 | + HttpJakartaServerServletRequestObservationContext ctx = (HttpJakartaServerServletRequestObservationContext) observation.getContext(); |
| 61 | + ctx.setResponse(httpServletResponse); |
| 62 | + } |
| 63 | + observation.stop(); |
| 64 | + } |
| 65 | +} |
0 commit comments