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


##########
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:
   Thanks for the review. Addressed the Observation.Scope issue.
   
   The Micrometer GenAI Observation is now made current for the duration of the 
LLM operation:
   
   start → openScope → LLM operation → closeScope → stop
   
   ObservationHandle owns the scope and closes it before stopping the 
Observation, with cleanup protected by try/finally. If openScope() fails, the 
started Observation is stopped before the exception is propagated.
   
   Added tests covering:
   
   -  the GenAI Observation being current while the operation is active
   -  nested Observation parenting
   -  scope cleanup on the error path
   
   Validation:
   
   -  mvn test -Psourcecheck -pl components/camel-ai/camel-ai-observability
   -  31 tests passed, 0 failures/errors
   -  git diff --check clean
   
   The changes have been amended into the existing CAMEL-24561 commit and 
pushed to PR #25882.
   



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