Copilot commented on code in PR #15718: URL: https://github.com/apache/grails-core/pull/15718#discussion_r3363112056
########## grails-gsp/core/src/main/groovy/org/grails/gsp/observation/GroovyPageObservationDocumentation.java: ########## @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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 org.grails.gsp.observation; + +import io.micrometer.common.docs.KeyName; +import io.micrometer.observation.Observation; +import io.micrometer.observation.ObservationConvention; +import io.micrometer.observation.docs.ObservationDocumentation; + +/** + * Documented {@link io.micrometer.observation.Observation}s for Groovy Server Pages (GSP) rendering. + * + * <p>Each observation shares the same {@link LowCardinalityKeyNames key names} ({@code gsp.name}, + * {@code error}); the observation name ({@code gsp.view} / {@code gsp.template} / {@code gsp.layout}) + * distinguishes what was rendered.</p> + * + * @author Grails + * @since 8.0 + */ +public enum GroovyPageObservationDocumentation implements ObservationDocumentation { + + /** + * Rendering of a single GSP view. + */ + GSP_VIEW, + + /** + * Rendering of an included GSP template (e.g. {@code <g:render template="..."/>}). + */ + GSP_TEMPLATE, + + /** + * Decoration of rendered content by a GSP layout (SiteMesh). + */ + GSP_LAYOUT, + + /** + * Compilation of a GSP into its {@code GroovyPageMetaInfo} (happens on a template cache miss). + */ + GSP_COMPILE; + + @Override + public Class<? extends ObservationConvention<? extends Observation.Context>> getDefaultConvention() { + return DefaultGroovyPageObservationConvention.class; + } Review Comment: `getDefaultConvention()` returns `DefaultGroovyPageObservationConvention.class`, but that class has no no-arg constructor and also can’t encode a different observation name per enum constant. Anything that uses the `ObservationDocumentation` default-convention path (e.g. `GSP_VIEW.observation(registry)`) is likely to fail at runtime or produce incorrect names; consider providing per-constant default convention classes with public no-arg constructors (or overriding `getDefaultConvention()` per enum constant). ########## grails-gsp/grails-layout/src/main/groovy/org/apache/grails/web/layout/EmbeddedGrailsLayoutView.java: ########## @@ -48,6 +55,10 @@ public class EmbeddedGrailsLayoutView extends AbstractGrailsView { public static final String GSP_GRAILS_LAYOUT_PAGE = EmbeddedGrailsLayoutView.class.getName() + ".GSP_GRAILS_LAYOUT_PAGE"; + private static final GroovyPageObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultGroovyPageObservationConvention("gsp.layout"); + private ObservationRegistry observationRegistry = ObservationRegistry.NOOP; + private GroovyPageObservationConvention observationConvention; Review Comment: `observationConvention` is declared but there’s no setter and no code path that assigns it, so it will always be `null` (making the custom-convention branch effectively dead). Either add a setter (and wire it from the resolver) or remove the field and pass `null` explicitly to avoid implying configurability that isn’t actually supported. ########## grails-gsp/core/src/main/groovy/org/grails/gsp/GroovyPagesTemplateEngine.java: ########## @@ -298,6 +312,7 @@ public Template createTemplate(Resource resource, final boolean cacheable) { protected Template createTemplate(Resource resource, final String pageName, final boolean cacheable) throws IOException { GroovyPageMetaInfo meta; if (cacheable) { + recordCacheAccess(pageCache.containsKey(pageName)); meta = CacheEntry.getValue(pageCache, pageName, -1, null, new GroovyPagesTemplateEngineCallable(new GroovyPagesTemplateEngineCacheEntry(pageName)), true, resource); Review Comment: Cache hit/miss counters are currently based on `pageCache.containsKey(pageName)`, which will count an expired/reloadable entry as a hit even though it triggers recompilation (i.e. a real cache miss). This will skew both the `gsp.template.cache{result=hit|miss}` counters and any derived hit ratio. ########## grails-gsp/grails-web-gsp/src/main/groovy/org/grails/web/servlet/view/GroovyPageViewResolver.java: ########## @@ -232,6 +240,38 @@ private View createGroovyPageView(String gspView, ScriptSource scriptSource) { return gspSpringView; } + /** + * Resolves the {@link ObservationRegistry} to apply to GSP views: an explicitly configured one + * if set, otherwise the registry bean from the application context, falling back to + * {@link ObservationRegistry#NOOP} when none is available. + */ + private ObservationRegistry resolveObservationRegistry() { + ObservationRegistry registry = this.observationRegistry; + if (registry == null) { + ApplicationContext ctx = getApplicationContext(); + registry = (ctx != null) + ? ctx.getBeanProvider(ObservationRegistry.class).getIfAvailable(() -> ObservationRegistry.NOOP) + : ObservationRegistry.NOOP; + this.observationRegistry = registry; + } + return registry; + } + + /** + * Sets the {@link ObservationRegistry} used to instrument GSP view rendering. When left unset it + * is resolved from the application context (falling back to {@link ObservationRegistry#NOOP}). + */ + public void setObservationRegistry(ObservationRegistry observationRegistry) { + this.observationRegistry = observationRegistry; + } + + /** + * Sets a custom {@link GroovyPageObservationConvention} applied to GSP view observations. + */ + public void setObservationConvention(GroovyPageObservationConvention observationConvention) { + this.observationConvention = observationConvention; + } Review Comment: `GroovyPageObservationConvention` is only applied if someone explicitly sets the property on `GroovyPageViewResolver`. If the intent is “register a custom convention as a bean”, the resolver should actually autowire it (similar to `setTemplateEngine(...)`), otherwise a convention bean won’t be picked up automatically. ########## grails-gsp/core/build.gradle: ########## @@ -44,6 +44,8 @@ dependencies { api project(':grails-core') api project(':grails-taglib') api 'org.apache.groovy:groovy-templates' + // GSP rendering/compilation Micrometer instrumentation (gsp.view/template/layout/compile + cache counters) + implementation 'io.micrometer:micrometer-core' Review Comment: The PR description notes “no new runtime dependency” and lists template/layout/compile instrumentation as follow-ups, but this change adds an `implementation` dependency on `io.micrometer:micrometer-core` and introduces `gsp.template` / `gsp.layout` / `gsp.compile` + cache counters. Please align the PR description (or reduce the scope) so consumers understand the added dependency and additional instrumentation surface area. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
