-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Emit DB exceptions as logs under the opt-in preview #18889
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ab9aecc
Add DB exception event extractor helper
trask d6fbc1f
Add unit and integration tests for DB exception event extractor
trask 50dc81e
Apply suggestion from @trask
trask 031e788
simplify
trask 48a969b
Inline JDBC exception log assertion
trask 2c442bd
Fix JDBC exception log test filtering
trask 9d22c5d
Assert JDBC exception log records
trask f16611e
Address review comment: clean up failed JDBC test connection
trask c0fe32d
Relax DB 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
...lemetry/instrumentation/api/incubator/semconv/db/internal/DbExceptionEventExtractors.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.db.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 DbExceptionEventExtractors { | ||
|
|
||
| /** | ||
| * Configures the database client 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 setDbClientExceptionEventExtractor( | ||
| InstrumenterBuilder<REQUEST, ?> builder) { | ||
| Experimental.setExceptionEventExtractor( | ||
| builder, | ||
| (logRecordBuilder, context, request) -> { | ||
| logRecordBuilder.setEventName("db.client.operation.exception"); | ||
| logRecordBuilder.setSeverity(Severity.WARN); | ||
| }); | ||
| } | ||
|
|
||
| private DbExceptionEventExtractors() {} | ||
| } | ||
57 changes: 57 additions & 0 deletions
57
...try/instrumentation/api/incubator/semconv/db/internal/DbExceptionEventExtractorsTest.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.db.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 DbExceptionEventExtractorsTest { | ||
|
|
||
| @RegisterExtension | ||
| static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create(); | ||
|
|
||
| @Test | ||
| void dbClientExceptionLog() { | ||
| InstrumenterBuilder<String, String> builder = | ||
| Instrumenter.builder(otelTesting.getOpenTelemetry(), "test", unused -> "span"); | ||
| DbExceptionEventExtractors.setDbClientExceptionEventExtractor(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("db.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
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.