-
-
Notifications
You must be signed in to change notification settings - Fork 465
Add request details to transactions created through OpenTelemetry
#4098
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
10 commits
Select commit
Hold shift + click to select a range
8ef3825
Attach request object to event for OTel
adinauer d3cab7c
fix test name
adinauer 7c51a68
Merge branch 'main' into feat/request-for-otel
adinauer ecd6040
rename test class
adinauer 6e0b24d
changelog
adinauer 3f10e3c
Merge branch 'main' into feat/request-for-otel
adinauer 28cb76a
Merge branch 'main' into feat/request-for-otel
adinauer 28a7e56
do not override existing url on request even with full url
adinauer 7b51dbd
Merge branch 'main' into feat/request-for-otel
adinauer db933d5
Merge branch 'main' into feat/request-for-otel
adinauer 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
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
5 changes: 5 additions & 0 deletions
5
sentry-opentelemetry/sentry-opentelemetry-core/api/sentry-opentelemetry-core.api
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
90 changes: 90 additions & 0 deletions
90
...elemetry-core/src/main/java/io/sentry/opentelemetry/OpenTelemetryAttributesExtractor.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,90 @@ | ||
| package io.sentry.opentelemetry; | ||
|
|
||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.sdk.trace.data.SpanData; | ||
| import io.opentelemetry.semconv.HttpAttributes; | ||
| import io.opentelemetry.semconv.ServerAttributes; | ||
| import io.opentelemetry.semconv.UrlAttributes; | ||
| import io.sentry.IScope; | ||
| import io.sentry.ISpan; | ||
| import io.sentry.protocol.Request; | ||
| import io.sentry.util.UrlUtils; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| @ApiStatus.Internal | ||
| public final class OpenTelemetryAttributesExtractor { | ||
|
|
||
| public void extract( | ||
| final @NotNull SpanData otelSpan, | ||
| final @NotNull ISpan sentrySpan, | ||
| final @NotNull IScope scope) { | ||
| final @NotNull Attributes attributes = otelSpan.getAttributes(); | ||
| addRequestAttributesToScope(attributes, scope); | ||
| } | ||
|
|
||
| private void addRequestAttributesToScope(Attributes attributes, IScope scope) { | ||
| if (scope.getRequest() == null) { | ||
| scope.setRequest(new Request()); | ||
| } | ||
| final @Nullable Request request = scope.getRequest(); | ||
| if (request != null) { | ||
| final @Nullable String requestMethod = attributes.get(HttpAttributes.HTTP_REQUEST_METHOD); | ||
| if (requestMethod != null) { | ||
| request.setMethod(requestMethod); | ||
| } | ||
|
|
||
| if (request.getUrl() == null) { | ||
| final @Nullable String urlFull = attributes.get(UrlAttributes.URL_FULL); | ||
| if (urlFull != null) { | ||
| final @NotNull UrlUtils.UrlDetails urlDetails = UrlUtils.parse(urlFull); | ||
| urlDetails.applyToRequest(request); | ||
| } | ||
| } | ||
|
|
||
| if (request.getUrl() == null) { | ||
| final String urlString = buildUrlString(attributes); | ||
| if (!urlString.isEmpty()) { | ||
| request.setUrl(urlString); | ||
| } | ||
| } | ||
|
|
||
| if (request.getQueryString() == null) { | ||
| final @Nullable String query = attributes.get(UrlAttributes.URL_QUERY); | ||
| if (query != null) { | ||
| request.setQueryString(query); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private @NotNull String buildUrlString(final @NotNull Attributes attributes) { | ||
| final @Nullable String scheme = attributes.get(UrlAttributes.URL_SCHEME); | ||
| final @Nullable String serverAddress = attributes.get(ServerAttributes.SERVER_ADDRESS); | ||
| final @Nullable Long serverPort = attributes.get(ServerAttributes.SERVER_PORT); | ||
| final @Nullable String path = attributes.get(UrlAttributes.URL_PATH); | ||
|
|
||
| if (scheme == null || serverAddress == null) { | ||
| return ""; | ||
| } | ||
|
|
||
| final @NotNull StringBuilder urlBuilder = new StringBuilder(); | ||
| urlBuilder.append(scheme); | ||
| urlBuilder.append("://"); | ||
|
|
||
| if (serverAddress != null) { | ||
| urlBuilder.append(serverAddress); | ||
| if (serverPort != null) { | ||
| urlBuilder.append(":"); | ||
| urlBuilder.append(serverPort); | ||
| } | ||
| } | ||
|
|
||
| if (path != null) { | ||
| urlBuilder.append(path); | ||
| } | ||
|
|
||
| return urlBuilder.toString(); | ||
| } | ||
| } | ||
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
201 changes: 201 additions & 0 deletions
201
...lemetry/sentry-opentelemetry-core/src/test/kotlin/OpenTelemetryAttributesExtractorTest.kt
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,201 @@ | ||
| package io.sentry.opentelemetry | ||
|
|
||
| import io.opentelemetry.api.common.AttributeKey | ||
| import io.opentelemetry.sdk.internal.AttributesMap | ||
| import io.opentelemetry.sdk.trace.data.SpanData | ||
| import io.opentelemetry.semconv.ServerAttributes | ||
| import io.opentelemetry.semconv.UrlAttributes | ||
| import io.sentry.ISpan | ||
| import io.sentry.Scope | ||
| import io.sentry.SentryOptions | ||
| import io.sentry.protocol.Request | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.whenever | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertNotNull | ||
| import kotlin.test.assertNull | ||
|
|
||
| class OpenTelemetryAttributesExtractorTest { | ||
|
|
||
| private class Fixture { | ||
| val spanData = mock<SpanData>() | ||
| val attributes = AttributesMap.create(100, 100) | ||
| val sentrySpan = mock<ISpan>() | ||
| val options = SentryOptions.empty() | ||
| val scope = Scope(options) | ||
|
|
||
| init { | ||
| whenever(spanData.attributes).thenReturn(attributes) | ||
| } | ||
| } | ||
|
|
||
| private val fixture = Fixture() | ||
|
|
||
| @Test | ||
| fun `sets URL based on OTel attributes`() { | ||
| givenAttributes( | ||
| mapOf( | ||
| UrlAttributes.URL_SCHEME to "https", | ||
| UrlAttributes.URL_PATH to "/path/to/123", | ||
| UrlAttributes.URL_QUERY to "q=123456&b=X", | ||
| ServerAttributes.SERVER_ADDRESS to "io.sentry", | ||
| ServerAttributes.SERVER_PORT to 8081L | ||
| ) | ||
| ) | ||
|
|
||
| whenExtractingAttributes() | ||
|
|
||
| thenRequestIsSet() | ||
| thenUrlIsSetTo("https://io.sentry:8081/path/to/123") | ||
| thenQueryIsSetTo("q=123456&b=X") | ||
| } | ||
|
|
||
| @Test | ||
| fun `when there is an existing request on scope it is filled with more details`() { | ||
| fixture.scope.request = Request().also { it.bodySize = 123L } | ||
| givenAttributes( | ||
| mapOf( | ||
| UrlAttributes.URL_SCHEME to "https", | ||
| UrlAttributes.URL_PATH to "/path/to/123", | ||
| UrlAttributes.URL_QUERY to "q=123456&b=X", | ||
| ServerAttributes.SERVER_ADDRESS to "io.sentry", | ||
| ServerAttributes.SERVER_PORT to 8081L | ||
| ) | ||
| ) | ||
|
|
||
| whenExtractingAttributes() | ||
|
|
||
| thenRequestIsSet() | ||
| thenUrlIsSetTo("https://io.sentry:8081/path/to/123") | ||
| thenQueryIsSetTo("q=123456&b=X") | ||
| assertEquals(123L, fixture.scope.request!!.bodySize) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when there is an existing request with url on scope it is kept`() { | ||
| fixture.scope.request = Request().also { | ||
| it.url = "http://docs.sentry.io:3000/platform" | ||
| it.queryString = "s=abc" | ||
| } | ||
| givenAttributes( | ||
| mapOf( | ||
| UrlAttributes.URL_SCHEME to "https", | ||
| UrlAttributes.URL_PATH to "/path/to/123", | ||
| UrlAttributes.URL_QUERY to "q=123456&b=X", | ||
| ServerAttributes.SERVER_ADDRESS to "io.sentry", | ||
| ServerAttributes.SERVER_PORT to 8081L | ||
| ) | ||
| ) | ||
|
|
||
| whenExtractingAttributes() | ||
|
|
||
| thenRequestIsSet() | ||
| thenUrlIsSetTo("http://docs.sentry.io:3000/platform") | ||
| thenQueryIsSetTo("s=abc") | ||
| } | ||
|
|
||
| @Test | ||
| fun `when there is an existing request with url on scope it is kept with URL_FULL`() { | ||
| fixture.scope.request = Request().also { | ||
| it.url = "http://docs.sentry.io:3000/platform" | ||
| it.queryString = "s=abc" | ||
| } | ||
| givenAttributes( | ||
| mapOf( | ||
| UrlAttributes.URL_FULL to "https://io.sentry:8081/path/to/123?q=123456&b=X" | ||
| ) | ||
| ) | ||
|
|
||
| whenExtractingAttributes() | ||
|
|
||
| thenRequestIsSet() | ||
| thenUrlIsSetTo("http://docs.sentry.io:3000/platform") | ||
| thenQueryIsSetTo("s=abc") | ||
| } | ||
|
|
||
| @Test | ||
| fun `sets URL based on OTel attributes without port`() { | ||
| givenAttributes( | ||
| mapOf( | ||
| UrlAttributes.URL_SCHEME to "https", | ||
| UrlAttributes.URL_PATH to "/path/to/123", | ||
| ServerAttributes.SERVER_ADDRESS to "io.sentry" | ||
| ) | ||
| ) | ||
|
|
||
| whenExtractingAttributes() | ||
|
|
||
| thenRequestIsSet() | ||
| thenUrlIsSetTo("https://io.sentry/path/to/123") | ||
| } | ||
|
|
||
| @Test | ||
| fun `sets URL based on OTel attributes without path`() { | ||
| givenAttributes( | ||
| mapOf( | ||
| UrlAttributes.URL_SCHEME to "https", | ||
| ServerAttributes.SERVER_ADDRESS to "io.sentry" | ||
| ) | ||
| ) | ||
|
|
||
| whenExtractingAttributes() | ||
|
|
||
| thenRequestIsSet() | ||
| thenUrlIsSetTo("https://io.sentry") | ||
| } | ||
|
|
||
| @Test | ||
| fun `does not set URL if server address is missing`() { | ||
| givenAttributes( | ||
| mapOf( | ||
| UrlAttributes.URL_SCHEME to "https" | ||
| ) | ||
| ) | ||
|
|
||
| whenExtractingAttributes() | ||
|
|
||
| thenRequestIsSet() | ||
| thenUrlIsNotSet() | ||
| } | ||
|
|
||
| @Test | ||
| fun `does not set URL if scheme is missing`() { | ||
| givenAttributes( | ||
| mapOf( | ||
| ServerAttributes.SERVER_ADDRESS to "io.sentry" | ||
| ) | ||
| ) | ||
|
|
||
| whenExtractingAttributes() | ||
|
|
||
| thenRequestIsSet() | ||
| thenUrlIsNotSet() | ||
| } | ||
|
|
||
| private fun givenAttributes(map: Map<AttributeKey<out Any>, Any>) { | ||
| map.forEach { k, v -> | ||
| fixture.attributes.put(k, v) | ||
| } | ||
| } | ||
|
|
||
| private fun whenExtractingAttributes() { | ||
| OpenTelemetryAttributesExtractor().extract(fixture.spanData, fixture.sentrySpan, fixture.scope) | ||
| } | ||
|
|
||
| private fun thenRequestIsSet() { | ||
| assertNotNull(fixture.scope.request) | ||
| } | ||
|
|
||
| private fun thenUrlIsSetTo(expected: String) { | ||
| assertEquals(expected, fixture.scope.request!!.url) | ||
| } | ||
|
|
||
| private fun thenUrlIsNotSet() { | ||
| assertNull(fixture.scope.request!!.url) | ||
| } | ||
|
|
||
| private fun thenQueryIsSetTo(expected: String) { | ||
| assertEquals(expected, fixture.scope.request!!.queryString) | ||
| } | ||
| } |
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.