-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- adds a VaadinFilter mechanism to mimic an around-aspect - adds a Micrometer Observation implementation - configures VaadinServlet to automatically update the VaadinService with filters - configures Spring to automatically update the VaadinServlet with filters
- Loading branch information
1 parent
25c6f2e
commit d80ecec
Showing
9 changed files
with
240 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
flow-server/src/main/java/com/vaadin/flow/server/ObservedVaadinFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.vaadin.flow.server; | ||
|
||
import io.micrometer.common.lang.Nullable; | ||
import io.micrometer.jakarta.instrument.binder.http.DefaultHttpJakartaServerServletRequestObservationConvention; | ||
import io.micrometer.jakarta.instrument.binder.http.HttpJakartaServerServletRequestObservationContext; | ||
import io.micrometer.jakarta.instrument.binder.http.HttpJakartaServerServletRequestObservationConvention; | ||
import io.micrometer.jakarta.instrument.binder.http.JakartaHttpObservationDocumentation; | ||
import io.micrometer.observation.Observation; | ||
import io.micrometer.observation.ObservationRegistry; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* Micrometer Observation {@link VaadinFilter} that will start | ||
* observations around processing of a request. | ||
* | ||
* @author Marcin Grzejszczak | ||
* @since 24.2 | ||
*/ | ||
public class ObservedVaadinFilter implements VaadinFilter { | ||
|
||
private static final String SCOPE_ATTRIBUTE = ObservedVaadinFilter.class.getName() + ".scope"; | ||
|
||
private final ObservationRegistry observationRegistry; | ||
|
||
private final HttpJakartaServerServletRequestObservationConvention convention; | ||
|
||
public ObservedVaadinFilter(ObservationRegistry observationRegistry, | ||
@Nullable HttpJakartaServerServletRequestObservationConvention convention) { | ||
this.observationRegistry = observationRegistry; | ||
this.convention = convention; | ||
} | ||
|
||
@Override | ||
public void requestStart(VaadinRequest request, VaadinResponse response) { | ||
if (request instanceof VaadinServletRequest servletRequest && response instanceof VaadinServletResponse servletResponse) { | ||
HttpJakartaServerServletRequestObservationContext context = new HttpJakartaServerServletRequestObservationContext(servletRequest, servletResponse); | ||
Observation observation = JakartaHttpObservationDocumentation.JAKARTA_SERVLET_SERVER_OBSERVATION.start(this.convention, DefaultHttpJakartaServerServletRequestObservationConvention.INSTANCE, () -> context, observationRegistry); | ||
request.setAttribute(SCOPE_ATTRIBUTE, observation.openScope()); | ||
} | ||
} | ||
|
||
@Override | ||
public void handleException(VaadinRequest request, VaadinResponse response, VaadinSession vaadinSession, Exception t) { | ||
Observation.Scope scope = (Observation.Scope) request.getAttribute(SCOPE_ATTRIBUTE); | ||
if (scope == null) { | ||
return; | ||
} | ||
scope.getCurrentObservation().error(t); | ||
} | ||
|
||
@Override | ||
public void requestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session) { | ||
Observation.Scope scope = (Observation.Scope) request.getAttribute(SCOPE_ATTRIBUTE); | ||
if (scope == null) { | ||
return; | ||
} | ||
scope.close(); | ||
Observation observation = scope.getCurrentObservation(); | ||
if (!observation.isNoop() && response instanceof HttpServletResponse httpServletResponse) { | ||
HttpJakartaServerServletRequestObservationContext ctx = (HttpJakartaServerServletRequestObservationContext) observation.getContext(); | ||
ctx.setResponse(httpServletResponse); | ||
} | ||
observation.stop(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
flow-server/src/main/java/com/vaadin/flow/server/VaadinFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2000-2023 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.vaadin.flow.server; | ||
|
||
/** | ||
* Used to provide an around-like aspect option around request processing. | ||
* | ||
* @author Marcin Grzejszczak | ||
* @since 24.2 | ||
*/ | ||
public interface VaadinFilter { | ||
|
||
/** | ||
* Called when request is about to be processed. | ||
* @param request request | ||
* @param response response | ||
*/ | ||
void requestStart(VaadinRequest request, VaadinResponse response); | ||
|
||
/** | ||
* Called when an exception occurred | ||
* @param request request | ||
* @param response response | ||
* @param vaadinSession session | ||
* @param t exception | ||
*/ | ||
void handleException(VaadinRequest request, | ||
VaadinResponse response, VaadinSession vaadinSession, Exception t); | ||
|
||
/** | ||
* Called in the finally block of processing a request. Will be called | ||
* regardless of whether there was an exception or not. | ||
* @param request request | ||
* @param response response | ||
* @param session session | ||
*/ | ||
void requestEnd(VaadinRequest request, VaadinResponse response, | ||
VaadinSession session); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.