Skip to content

Add conditinal support for logging errors in observation Span #1441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2024 - 2024 the original author or authors.
*
* 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
*
* 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.springframework.ai.model.observation;

import java.util.List;
import java.util.function.Consumer;

import org.springframework.util.Assert;

import io.micrometer.observation.Observation;
import io.micrometer.observation.Observation.Context;
import io.micrometer.observation.ObservationHandler;
import io.micrometer.tracing.Tracer;
import io.micrometer.tracing.handler.TracingObservationHandler.TracingContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author Christian Tzolov
* @since 1.0.0
*/
@SuppressWarnings({ "rawtypes", "null" })
public class ErrorLoggingObservationHandler implements ObservationHandler {

private static final Logger logger = LoggerFactory.getLogger(ErrorLoggingObservationHandler.class);

private final Tracer tracer;

private final List<Class<? extends Observation.Context>> supportedContextTypes;

private final Consumer<Context> errorConsumer;

public ErrorLoggingObservationHandler(Tracer tracer,
List<Class<? extends Observation.Context>> supportedContextTypes) {
this(tracer, supportedContextTypes, context -> logger.error("Traced Error: ", context.getError()));
}

public ErrorLoggingObservationHandler(Tracer tracer,
List<Class<? extends Observation.Context>> supportedContextTypes, Consumer<Context> errorConsumer) {

Assert.notNull(tracer, "Tracer must not be null");
Assert.notNull(supportedContextTypes, "SupportedContextTypes must not be null");
Assert.notNull(errorConsumer, "ErrorConsumer must not be null");

this.tracer = tracer;
this.supportedContextTypes = supportedContextTypes;
this.errorConsumer = errorConsumer;
}

@Override
public boolean supportsContext(Context context) {
return (context == null) ? false : this.supportedContextTypes.stream().anyMatch(clz -> clz.isInstance(context));
}

@Override
public void onError(Context context) {
if (context != null) {
TracingContext tracingContext = context.get(TracingContext.class);
if (tracingContext != null) {
try (var val = this.tracer.withSpan(tracingContext.getSpan())) {
this.errorConsumer.accept(context);
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* Context used to store metadata for vector store operations.
*
* @author Christian Tzolo
* @author Christian Tzolov
* @author Thomas Vitale
* @since 1.0.0
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package org.springframework.ai.observation.tracing;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Test;

import io.micrometer.tracing.Span;
import io.micrometer.tracing.TraceContext;
import io.micrometer.tracing.handler.TracingObservationHandler;
import io.micrometer.tracing.otel.bridge.OtelCurrentTraceContext;
import io.micrometer.tracing.otel.bridge.OtelTracer;
import io.opentelemetry.api.OpenTelemetry;
import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.observation.ChatModelObservationContentProcessor;

import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

/**
* Unit tests for {@link TracingHelper}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@
*/
package org.springframework.ai.autoconfigure.chat.observation;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.tracing.otel.bridge.OtelTracer;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.advisor.observation.AdvisorObservationContext;
import org.springframework.ai.chat.client.observation.ChatClientObservationContext;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.observation.ChatModelCompletionObservationFilter;
import org.springframework.ai.chat.observation.ChatModelCompletionObservationHandler;
import org.springframework.ai.chat.observation.ChatModelMeterObservationHandler;
import org.springframework.ai.chat.observation.ChatModelObservationContext;
import org.springframework.ai.chat.observation.ChatModelPromptContentObservationFilter;
import org.springframework.ai.chat.observation.ChatModelPromptContentObservationHandler;
import org.springframework.ai.embedding.observation.EmbeddingModelObservationContext;
import org.springframework.ai.image.observation.ImageModelObservationContext;
import org.springframework.ai.model.observation.ErrorLoggingObservationHandler;
import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
Expand All @@ -36,6 +43,10 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.tracing.Tracer;
import io.micrometer.tracing.otel.bridge.OtelTracer;

/**
* Auto-configuration for Spring AI chat model observations.
*
Expand Down Expand Up @@ -113,6 +124,17 @@ ChatModelCompletionObservationFilter chatModelCompletionObservationFilter() {

}

@Bean
@ConditionalOnBean(Tracer.class)
@ConditionalOnProperty(prefix = ChatObservationProperties.CONFIG_PREFIX, name = "include-error-logging",
havingValue = "true")
public ErrorLoggingObservationHandler errorLoggingObservationHandler(Tracer tracer) {
return new ErrorLoggingObservationHandler(tracer,
List.of(EmbeddingModelObservationContext.class, ImageModelObservationContext.class,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it ok for the property pring.ai.chat.observations.include-error-logging to apply to all these context's as compared to the chatmodel one, e.g. embedding?

ChatModelObservationContext.class, ChatClientObservationContext.class,
AdvisorObservationContext.class, VectorStoreObservationContext.class));
}

private static void logPromptContentWarning() {
logger.warn(
"You have enabled the inclusion of the prompt content in the observations, with the risk of exposing sensitive or private information. Please, be careful!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public class ChatObservationProperties {
*/
private boolean includePrompt = false;

/**
* Whether to include error logging in the observations.
*/
private boolean includeErrorLogging = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll add this to observability/index.adoc


public boolean isIncludeCompletion() {
return includeCompletion;
}
Expand All @@ -54,4 +59,12 @@ public void setIncludePrompt(boolean includePrompt) {
this.includePrompt = includePrompt;
}

public boolean isIncludeErrorLogging() {
return this.includeErrorLogging;
}

public void setIncludeErrorLogging(boolean includeErrorLogging) {
this.includeErrorLogging = includeErrorLogging;
}

}