Skip to content

feat(observability): Refactor content observation mechanism to support both logging and tracing #3612

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Expand Up @@ -26,10 +26,7 @@
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.ChatModelCompletionObservationHandler;
import org.springframework.ai.chat.observation.ChatModelMeterObservationHandler;
import org.springframework.ai.chat.observation.ChatModelObservationContext;
import org.springframework.ai.chat.observation.ChatModelPromptContentObservationHandler;
import org.springframework.ai.chat.observation.*;
import org.springframework.ai.embedding.observation.EmbeddingModelObservationContext;
import org.springframework.ai.image.observation.ImageModelObservationContext;
import org.springframework.ai.model.observation.ErrorLoggingObservationHandler;
Expand Down Expand Up @@ -70,6 +67,16 @@ private static void logCompletionWarning() {
"You have enabled logging out the completion content with the risk of exposing sensitive or private information. Please, be careful!");
}

private static void tracePromptContentWarning() {
logger.warn(
"You have enabled tracing out the prompt content with the risk of exposing sensitive or private information. Please, be careful!");
}

private static void traceCompletionWarning() {
logger.warn(
"You have enabled tracing out the completion content with the risk of exposing sensitive or private information. Please, be careful!");
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(MeterRegistry.class)
Expand Down Expand Up @@ -104,6 +111,30 @@ TracingAwareLoggingObservationHandler<ChatModelObservationContext> chatModelComp
return new TracingAwareLoggingObservationHandler<>(new ChatModelCompletionObservationHandler(), tracer);
}

@Bean
@ConditionalOnMissingBean(value = ChatModelPromptContentObservationTraceHandler.class,
name = "chatModelPromptContentObservationTraceHandler")
@ConditionalOnProperty(prefix = ChatObservationProperties.CONFIG_PREFIX, name = "trace-prompt",
havingValue = "true")
TracingAwareLoggingObservationHandler<ChatModelObservationContext> chatModelPromptContentObservationTraceHandler(
Copy link
Member

Choose a reason for hiding this comment

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

Why are these wrapped into TracingAwareLoggingObservationHandler?
Please check what it does, it is used to make the tracing data available for log entries (log correlation). If I understand correctly, this is not needed in this use-case.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, the wrapping of TracingAwareLoggingObservationHandler should be removed.

ChatObservationProperties properties, Tracer tracer) {
tracePromptContentWarning();
return new TracingAwareLoggingObservationHandler<>(new ChatModelPromptContentObservationTraceHandler(
properties.getContentFormatter(), properties.getTracePromptSize()), tracer);
}

@Bean
@ConditionalOnMissingBean(value = ChatModelCompletionObservationTraceHandler.class,
name = "chatModelCompletionObservationTraceHandler")
@ConditionalOnProperty(prefix = ChatObservationProperties.CONFIG_PREFIX, name = "trace-completion",
havingValue = "true")
TracingAwareLoggingObservationHandler<ChatModelObservationContext> chatModelCompletionObservationTraceHandler(
ChatObservationProperties properties, Tracer tracer) {
traceCompletionWarning();
return new TracingAwareLoggingObservationHandler<>(
new ChatModelCompletionObservationTraceHandler(properties.getContentFormatter()), tracer);
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = ChatObservationProperties.CONFIG_PREFIX, name = "include-error-logging",
Expand Down Expand Up @@ -139,6 +170,27 @@ ChatModelCompletionObservationHandler chatModelCompletionObservationHandler() {
return new ChatModelCompletionObservationHandler();
}

@Bean
@ConditionalOnMissingBean()
@ConditionalOnProperty(prefix = ChatObservationProperties.CONFIG_PREFIX, name = "trace-prompt",
havingValue = "true")
ChatModelPromptContentObservationTraceHandler chatModelPromptContentObservationTraceHandler(
ChatObservationProperties properties) {
tracePromptContentWarning();
return new ChatModelPromptContentObservationTraceHandler(properties.getContentFormatter(),
properties.getTracePromptSize());
}

@Bean
@ConditionalOnMissingBean()
@ConditionalOnProperty(prefix = ChatObservationProperties.CONFIG_PREFIX, name = "trace-completion",
havingValue = "true")
ChatModelCompletionObservationTraceHandler chatModelCompletionObservationTraceHandler(
ChatObservationProperties properties) {
traceCompletionWarning();
return new ChatModelCompletionObservationTraceHandler(properties.getContentFormatter());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.ai.model.chat.observation.autoconfigure;

import org.springframework.ai.chat.observation.trace.AiObservationContentFormatterName;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
Expand All @@ -40,6 +41,26 @@ public class ChatObservationProperties {
*/
private boolean logPrompt = false;

/**
* Whether to trace the completion content in the observations.
*/
private boolean traceCompletion = false;

/**
* Whether to trace the prompt content in the observations.
*/
private boolean tracePrompt = false;

/**
* prompt size in trace, smaller than 1 is unlimit
*/
private int tracePromptSize = 10;

/**
* prompt and completion formatter
*/
private AiObservationContentFormatterName contentFormatter = AiObservationContentFormatterName.TEXT;

/**
* Whether to include error logging in the observations.
*/
Expand All @@ -61,6 +82,38 @@ public void setLogPrompt(boolean logPrompt) {
this.logPrompt = logPrompt;
}

public boolean isTraceCompletion() {
return traceCompletion;
}

public void setTraceCompletion(boolean traceCompletion) {
this.traceCompletion = traceCompletion;
}

public boolean isTracePrompt() {
return tracePrompt;
}

public void setTracePrompt(boolean tracePrompt) {
this.tracePrompt = tracePrompt;
}

public int getTracePromptSize() {
return tracePromptSize;
}

public void setTracePromptSize(int tracePromptSize) {
this.tracePromptSize = tracePromptSize;
}

public AiObservationContentFormatterName getContentFormatter() {
return contentFormatter;
}

public void setContentFormatter(AiObservationContentFormatterName contentFormatter) {
this.contentFormatter = contentFormatter;
}

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