-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
tzolov
wants to merge
1
commit into
spring-projects:main
from
tzolov:error-logging-observation-handler
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...rc/main/java/org/springframework/ai/model/observation/ErrorLoggingObservationHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 6 additions & 7 deletions
13
...-ai-core/src/test/java/org/springframework/ai/observation/tracing/TracingHelperTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,11 @@ public class ChatObservationProperties { | |
*/ | ||
private boolean includePrompt = false; | ||
|
||
/** | ||
* Whether to include error logging in the observations. | ||
*/ | ||
private boolean includeErrorLogging = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'll add this to |
||
|
||
public boolean isIncludeCompletion() { | ||
return includeCompletion; | ||
} | ||
|
@@ -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; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?