Skip to content
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

Pass request to CustomSamplingContext in Spring integration. #1172

Merged
merged 2 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# vNext

* Enhancement: Pass request to CustomSamplingContext in Spring integration (#1172)

# 4.0.0-alpha.3

* Feat: Add maxAttachmentSize to SentryOptions (#1138)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.sentry.spring.tracing;

import com.jakewharton.nopen.annotation.Open;
import io.sentry.CustomSamplingContext;
import io.sentry.IHub;
import io.sentry.ITransaction;
import io.sentry.SentryLevel;
Expand Down Expand Up @@ -60,9 +61,7 @@ protected void doFilterInternal(
final String sentryTraceHeader = httpRequest.getHeader(SentryTraceHeader.SENTRY_TRACE_HEADER);

// at this stage we are not able to get real transaction name
final ITransaction transaction =
startTransaction(
httpRequest.getMethod() + " " + httpRequest.getRequestURI(), sentryTraceHeader);
final ITransaction transaction = startTransaction(httpRequest, sentryTraceHeader);
try {
filterChain.doFilter(httpRequest, httpResponse);
} finally {
Expand All @@ -81,12 +80,18 @@ protected void doFilterInternal(
}

private ITransaction startTransaction(
final @NotNull String name, final @Nullable String sentryTraceHeader) {
final @NotNull HttpServletRequest request, final @Nullable String sentryTraceHeader) {

final String name = request.getMethod() + " " + request.getRequestURI();

final CustomSamplingContext customSamplingContext = new CustomSamplingContext();
customSamplingContext.put("request", request);

if (sentryTraceHeader != null) {
try {
final TransactionContext contexts =
TransactionContext.fromSentryTrace(name, new SentryTraceHeader(sentryTraceHeader));
final ITransaction transaction = hub.startTransaction(contexts);
final ITransaction transaction = hub.startTransaction(contexts, customSamplingContext);
hub.configureScope(scope -> scope.setTransaction(transaction));
return transaction;
} catch (InvalidSentryTraceHeaderException e) {
Expand All @@ -95,7 +100,7 @@ private ITransaction startTransaction(
.log(SentryLevel.DEBUG, "Failed to parse Sentry trace header: %s", e.getMessage());
}
}
final ITransaction transaction = hub.startTransaction(name);
final ITransaction transaction = hub.startTransaction(name, customSamplingContext);
hub.configureScope(scope -> scope.setTransaction(transaction));
return transaction;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import io.sentry.TransactionContext
import io.sentry.protocol.SentryId
import io.sentry.spring.SentryRequestResolver
import javax.servlet.FilterChain
import javax.servlet.http.HttpServletRequest
import kotlin.test.Test
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import org.assertj.core.api.Assertions.assertThat
import org.springframework.mock.web.MockHttpServletRequest
import org.springframework.mock.web.MockHttpServletResponse
Expand All @@ -36,10 +39,10 @@ class SentryTracingFilterTest {
request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, "/product/{id}")
if (sentryTraceHeader != null) {
request.addHeader("sentry-trace", sentryTraceHeader)
whenever(hub.startTransaction(any<TransactionContext>())).thenAnswer { SentryTransaction((it.arguments[0] as TransactionContext).name, it.arguments[0] as SpanContext, hub) }
whenever(hub.startTransaction(any<TransactionContext>(), any())).thenAnswer { SentryTransaction((it.arguments[0] as TransactionContext).name, it.arguments[0] as SpanContext, hub) }
}
response.status = 200
whenever(hub.startTransaction(any<String>())).thenAnswer { SentryTransaction(it.arguments[0] as String, SpanContext(), hub) }
whenever(hub.startTransaction(any<String>(), any())).thenAnswer { SentryTransaction(it.arguments[0] as String, SpanContext(), hub) }
return SentryTracingFilter(hub, SentryOptions(), requestResolver)
}
}
Expand All @@ -52,6 +55,10 @@ class SentryTracingFilterTest {

filter.doFilter(fixture.request, fixture.response, fixture.chain)

verify(fixture.hub).startTransaction(eq("POST /product/12"), check {
assertNotNull(it["request"])
assertTrue(it["request"] is HttpServletRequest)
})
verify(fixture.chain).doFilter(fixture.request, fixture.response)
verify(fixture.hub).captureTransaction(check {
assertThat(it.transaction).isEqualTo("POST /product/{id}")
Expand Down