-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Emit messaging exceptions as logs under the opt-in preview #18891
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
laurit
merged 5 commits into
open-telemetry:main
from
trask:trask/messaging-exception-events
Jun 9, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9094090
Add messaging exception event extractor helpers
trask 006ce3b
Add unit and integration tests for messaging exception event extractors
trask e5b30b8
Improve Kafka receive exception log test
trask 67d5bbb
Simplify Kafka exception log assertion
trask 07f3839
Relax messaging helper builder types
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
79 changes: 79 additions & 0 deletions
79
...mentation/api/incubator/semconv/messaging/internal/MessagingExceptionEventExtractors.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,79 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.api.incubator.semconv.messaging.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 MessagingExceptionEventExtractors { | ||
|
|
||
| /** | ||
| * Configures the messaging create 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 setMessagingCreateExceptionEventExtractor( | ||
| InstrumenterBuilder<REQUEST, ?> builder) { | ||
| setExceptionEventExtractor(builder, "messaging.create.exception", Severity.WARN); | ||
| } | ||
|
|
||
| /** | ||
| * Configures the messaging send 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 setMessagingSendExceptionEventExtractor( | ||
| InstrumenterBuilder<REQUEST, ?> builder) { | ||
| setExceptionEventExtractor(builder, "messaging.send.exception", Severity.WARN); | ||
| } | ||
|
|
||
| /** | ||
| * Configures the messaging receive 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 setMessagingReceiveExceptionEventExtractor( | ||
| InstrumenterBuilder<REQUEST, ?> builder) { | ||
| setExceptionEventExtractor(builder, "messaging.receive.exception", Severity.WARN); | ||
| } | ||
|
|
||
| /** | ||
| * Configures the messaging settle 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 setMessagingSettleExceptionEventExtractor( | ||
| InstrumenterBuilder<REQUEST, ?> builder) { | ||
| setExceptionEventExtractor(builder, "messaging.settle.exception", Severity.WARN); | ||
| } | ||
|
|
||
| /** | ||
| * Configures the messaging process 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 setMessagingProcessExceptionEventExtractor( | ||
| InstrumenterBuilder<REQUEST, ?> builder) { | ||
| setExceptionEventExtractor(builder, "messaging.process.exception", Severity.ERROR); | ||
| } | ||
|
|
||
| private static <REQUEST> void setExceptionEventExtractor( | ||
| InstrumenterBuilder<REQUEST, ?> builder, String eventName, Severity severity) { | ||
| Experimental.setExceptionEventExtractor( | ||
| builder, | ||
| (logRecordBuilder, context, request) -> { | ||
| logRecordBuilder.setEventName(eventName); | ||
| logRecordBuilder.setSeverity(severity); | ||
| }); | ||
| } | ||
|
|
||
| private MessagingExceptionEventExtractors() {} | ||
| } |
100 changes: 100 additions & 0 deletions
100
...ation/api/incubator/semconv/messaging/internal/MessagingExceptionEventExtractorsTest.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,100 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.api.incubator.semconv.messaging.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 java.util.function.Consumer; | ||
| import org.assertj.core.api.AbstractAssert; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| class MessagingExceptionEventExtractorsTest { | ||
|
|
||
| @RegisterExtension | ||
| static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create(); | ||
|
|
||
| @Test | ||
| void messagingCreateExceptionLog() { | ||
| assertExceptionLog( | ||
| MessagingExceptionEventExtractors::setMessagingCreateExceptionEventExtractor, | ||
| "messaging.create.exception", | ||
| Severity.WARN); | ||
| } | ||
|
|
||
| @Test | ||
| void messagingSendExceptionLog() { | ||
| assertExceptionLog( | ||
| MessagingExceptionEventExtractors::setMessagingSendExceptionEventExtractor, | ||
| "messaging.send.exception", | ||
| Severity.WARN); | ||
| } | ||
|
|
||
| @Test | ||
| void messagingReceiveExceptionLog() { | ||
| assertExceptionLog( | ||
| MessagingExceptionEventExtractors::setMessagingReceiveExceptionEventExtractor, | ||
| "messaging.receive.exception", | ||
| Severity.WARN); | ||
| } | ||
|
|
||
| @Test | ||
| void messagingSettleExceptionLog() { | ||
| assertExceptionLog( | ||
| MessagingExceptionEventExtractors::setMessagingSettleExceptionEventExtractor, | ||
| "messaging.settle.exception", | ||
| Severity.WARN); | ||
| } | ||
|
|
||
| @Test | ||
| void messagingProcessExceptionLog() { | ||
| assertExceptionLog( | ||
| MessagingExceptionEventExtractors::setMessagingProcessExceptionEventExtractor, | ||
| "messaging.process.exception", | ||
| Severity.ERROR); | ||
| } | ||
|
|
||
| private static void assertExceptionLog( | ||
| Consumer<InstrumenterBuilder<String, String>> configure, | ||
| String expectedEventName, | ||
| Severity expectedSeverity) { | ||
| InstrumenterBuilder<String, String> builder = | ||
| Instrumenter.builder(otelTesting.getOpenTelemetry(), "test", unused -> "span"); | ||
| configure.accept(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(expectedSeverity) | ||
| .hasEventName(expectedEventName) | ||
| .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
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.