davsclaus commented on code in PR #25882:
URL: https://github.com/apache/camel/pull/25882#discussion_r3886476951


##########
components/camel-ai/camel-ai-observability/src/main/java/org/apache/camel/component/ai/observability/GenAiMicrometerObservationSupport.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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
+ *
+ *      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 org.apache.camel.component.ai.observability;
+
+import io.micrometer.observation.Observation;
+import io.micrometer.observation.ObservationRegistry;
+import org.apache.camel.CamelContext;
+import org.apache.camel.support.CamelContextHelper;
+import org.apache.camel.util.ObjectHelper;
+
+/**
+ * Micrometer Observation-backed instrumentation. Loaded reflectively only 
when {@link ObservationRegistry} is on the
+ * classpath.
+ */
+final class GenAiMicrometerObservationSupport implements 
GenAiMicrometerObservationBackend {
+
+    private final ObservationRegistry observationRegistry;
+
+    GenAiMicrometerObservationSupport(CamelContext camelContext) {
+        ObservationRegistry registry = 
CamelContextHelper.findSingleByType(camelContext, ObservationRegistry.class);
+        this.observationRegistry = isUsable(registry) ? registry : null;
+    }
+
+    @Override
+    public boolean isAvailable() {
+        return observationRegistry != null;
+    }
+
+    @Override
+    public Handle start(GenAiObservationContext context) {
+        if (observationRegistry == null) {
+            return null;
+        }
+        Observation observation = 
Observation.createNotStarted(GenAiMetrics.CLIENT_OPERATION, 
observationRegistry);
+        observation.contextualName(context.spanName());
+        observation.lowCardinalityKeyValue(GenAiAttributes.OPERATION_NAME, 
context.operationName().value());
+        observation.lowCardinalityKeyValue(GenAiAttributes.SYSTEM, 
nullToUnknown(context.system()));
+        observation.lowCardinalityKeyValue(GenAiAttributes.REQUEST_MODEL, 
nullToUnknown(context.requestModel()));
+        if (ObjectHelper.isNotEmpty(context.componentScheme())) {
+            
observation.lowCardinalityKeyValue(GenAiAttributes.CAMEL_COMPONENT, 
context.componentScheme());
+        }
+        observation.start();
+        if (observation.isNoop()) {
+            return null;
+        }
+        return new ObservationHandle(observation);
+    }

Review Comment:
   This starts the `Observation` but never opens an `Observation.Scope` 
(`observation.openScope()`), and `ObservationHandle.stop()` never closes one 
either.
   
   Without a scope, the observation is never registered as 
`ObservationRegistry.getCurrentObservation()` for the duration of the wrapped 
call — I verified this empirically: after `observation.start()` alone (no 
`openScope()`), `registry.getCurrentObservation()` returns `null`. Since the 
actual LLM/HTTP call happens between `start()`/`close()` in the producers (e.g. 
`LangChain4jChatProducer.sendChatMessage`), any nested Observation-based 
instrumentation from that call won't be parented under this GenAI observation.
   
   The existing `MicrometerObservationSpanAdapter.makeCurrent()` (in 
`camel-observation`) uses `observation.openScope()` for exactly this reason — 
worth following the same pattern here, e.g. have `Handle` hold the 
`Observation.Scope` opened at `start()` and close it in `stop()`.



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