codeconsole commented on code in PR #15718:
URL: https://github.com/apache/grails-core/pull/15718#discussion_r3416592744


##########
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:
   The default-convention path is not actually exercised the way this assumes. 
Every instrumentation site uses the 4-arg 
`ObservationDocumentation.observation(custom, default, ctx, registry)` overload 
and passes an explicitly-named 
`DefaultGroovyPageObservationConvention("gsp.view" | "gsp.template" | 
"gsp.layout")` as the default. That overload only calls 
`getDefaultConvention()` for a null-check and an `isAssignableFrom` type check 
— it never instantiates the class reflectively, and the observation name comes 
from the passed instance, not from `getDefaultConvention()`. So neither the 
no-arg-constructor concern nor the per-constant-name concern applies here. A 
no-arg constructor was nevertheless added defensively (commit e911790) so the 
simpler `observation(registry, supplier)` overload would also work if anyone 
uses it.



##########
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:
   Good catch — this was a real gap at the time. Fixed: 
`EmbeddedGrailsLayoutView` now has `setObservationConvention(...)`, and 
`GrailsLayoutViewResolver` wires it through 
(`layoutView.setObservationConvention(this.observationConvention)`). See commit 
2f77a8e.



##########
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:
   A custom convention is picked up without an explicit setter call: the sites 
pass `customConvention = null`, so a `GroovyPageObservationConvention` 
registered on the `ObservationRegistry` 
(`observationConfig().observationConvention(...)`) is resolved automatically 
via `supportsContext` — the idiomatic Micrometer mechanism. The per-resolver 
setter is just an additional escape hatch for programmatic config, not the only 
path.



##########
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:
   Fixed — the `containsKey` heuristic is gone. Hit/miss is now derived from an 
actual "had to build the entry" signal: the `CacheEntry` updater flag in 
`GroovyPagesTemplateRenderer` (`record(!built[0])`) and `entry == null` in 
`GroovyPageViewResolver`, so a reloadable/expired entry that triggers a rebuild 
is correctly counted as a miss. The engine compile path is no longer counted 
via `containsKey` at all — `gsp.compile` only fires inside `buildPageMetaInfo`, 
i.e. on a genuine compile. See commits 36f657c and 36a8101.



##########
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:
   Agreed — the description was stale. Updated it to disclose the new 
`io.micrometer:micrometer-core` dependency (`implementation` in 
grails-gsp/core, `api` in grails-web-gsp — the `MeterRegistry`/`Counter` types 
and `setMeterRegistry` ABI come from core, not observation) and to reflect that 
`gsp.template`/`gsp.layout`/`gsp.compile` and the `gsp.cache` counters are part 
of this PR rather than follow-ups.



-- 
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]

Reply via email to