-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Emit GenAI exceptions as logs under the opt-in preview #18893
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
Merged
trask
merged 11 commits into
open-telemetry:main
from
trask:trask/genai-exception-events
Jun 9, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
eff5722
Add GenAI exception event extractor helper
trask e8e8c77
Add unit and integration tests for GenAI exception event extractor
trask 2c3d5db
Mirror test system properties on OpenAI testExceptionSignalLogs tasks
trask 93bd015
simplify
trask 3d0c76d
Address review comment on GenAI exception spans
trask e70f7d3
Simplify OpenAI exception log assertions
trask faa79ea
Address GenAI exception log preview tests
trask 53edc2c
Potential fix for pull request finding
trask 3f389c6
Apply suggestion from @trask
trask 1494fdd
refactor
trask e23f93a
Relax GenAI helper builder type
trask 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
34 changes: 34 additions & 0 deletions
34
...y/instrumentation/api/incubator/semconv/genai/internal/GenAiExceptionEventExtractors.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,34 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.api.incubator.semconv.genai.internal; | ||
|
|
||
| import io.opentelemetry.api.logs.Severity; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; | ||
| import io.opentelemetry.instrumentation.api.internal.Experimental; | ||
|
|
||
| /** | ||
| * This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
| * any time. | ||
| */ | ||
| public final class GenAiExceptionEventExtractors { | ||
|
|
||
| /** | ||
| * Configures the GenAI client operation exception event name and severity. Only takes effect when | ||
| * emitting exceptions as logs is enabled via the {@code otel.semconv.exception.signal.preview} | ||
| * flag. | ||
| */ | ||
| public static <REQUEST> void setGenAiClientExceptionEventExtractor( | ||
| InstrumenterBuilder<REQUEST, ?> builder) { | ||
| Experimental.setExceptionEventExtractor( | ||
| builder, | ||
| (logRecordBuilder, context, request) -> { | ||
| logRecordBuilder.setEventName("gen_ai.client.operation.exception"); | ||
| logRecordBuilder.setSeverity(Severity.WARN); | ||
| }); | ||
| } | ||
|
|
||
| private GenAiExceptionEventExtractors() {} | ||
| } |
57 changes: 57 additions & 0 deletions
57
...strumentation/api/incubator/semconv/genai/internal/GenAiExceptionEventExtractorsTest.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,57 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.api.incubator.semconv.genai.internal; | ||
|
|
||
| import static io.opentelemetry.instrumentation.api.internal.SemconvExceptionSignal.emitExceptionAsLogs; | ||
| import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; | ||
| import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; | ||
| import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; | ||
| import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_MESSAGE; | ||
| import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_STACKTRACE; | ||
| import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_TYPE; | ||
|
|
||
| import io.opentelemetry.api.logs.Severity; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; | ||
| import io.opentelemetry.sdk.logs.data.LogRecordData; | ||
| import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension; | ||
| import java.util.List; | ||
| import org.assertj.core.api.AbstractAssert; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| class GenAiExceptionEventExtractorsTest { | ||
|
|
||
| @RegisterExtension | ||
| static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create(); | ||
|
|
||
| @Test | ||
| void genAiClientExceptionLog() { | ||
| InstrumenterBuilder<String, String> builder = | ||
| Instrumenter.builder(otelTesting.getOpenTelemetry(), "test", unused -> "span"); | ||
| GenAiExceptionEventExtractors.setGenAiClientExceptionEventExtractor(builder); | ||
| Instrumenter<String, String> instrumenter = builder.buildInstrumenter(); | ||
|
|
||
| Context context = instrumenter.start(Context.root(), "request"); | ||
| IllegalStateException error = new IllegalStateException("test"); | ||
| instrumenter.end(context, "request", "response", error); | ||
|
|
||
| List<LogRecordData> logs = otelTesting.getLogRecords(); | ||
| if (emitExceptionAsLogs()) { | ||
| assertThat(logs).hasSize(1); | ||
| assertThat(logs.get(0)) | ||
| .hasSeverity(Severity.WARN) | ||
| .hasEventName("gen_ai.client.operation.exception") | ||
| .hasAttributesSatisfyingExactly( | ||
| equalTo(EXCEPTION_TYPE, "java.lang.IllegalStateException"), | ||
| equalTo(EXCEPTION_MESSAGE, "test"), | ||
| satisfies(EXCEPTION_STACKTRACE, AbstractAssert::isNotNull)); | ||
| } else { | ||
| assertThat(logs).isEmpty(); | ||
| } | ||
| } | ||
| } | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.