sentry-java icon indicating copy to clipboard operation
sentry-java copied to clipboard

Automatic performance transactions for Webflux

Open marandaneto opened this issue 5 years ago • 12 comments

Similar to SentryTracingFilter but for Webflux

marandaneto avatar Nov 16 '21 09:11 marandaneto

I've tried something similar as what is in SentryTracingFilter, but I think it needs some specifics to bind it to the reactor context.

Click to expand code
package oopen.timemachine.config

import io.sentry.CustomSamplingContext
import io.sentry.HubAdapter
import io.sentry.ITransaction
import io.sentry.SentryLevel
import io.sentry.SentryTraceHeader
import io.sentry.SpanStatus
import io.sentry.TransactionContext
import io.sentry.exception.InvalidSentryTraceHeaderException
import org.springframework.http.server.reactive.ServerHttpRequest
import org.springframework.stereotype.Component
import org.springframework.web.reactive.HandlerMapping
import org.springframework.web.server.ServerWebExchange
import org.springframework.web.server.WebFilter
import org.springframework.web.server.WebFilterChain
import org.springframework.web.util.pattern.PathPattern
import reactor.core.publisher.Mono

@Component
class SentryTransactionWebFilter(private val hub: HubAdapter = HubAdapter.getInstance()) : WebFilter {
    companion object {
        private const val TRANSACTION_OP = "http.server"
    }

    override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
        val sentryTraceHeader = exchange.request.headers.getFirst(SentryTraceHeader.SENTRY_TRACE_HEADER)

        // at this stage we are not able to get real transaction name
        val transaction = startTransaction(exchange.request, sentryTraceHeader);

        return chain.filter(exchange).doFinally {
            // after all filters run, templated path pattern is available in request attribute
            val transactionName = provideTransactionName(exchange);
            // if transaction name is not resolved, the request has not been processed by a controller
            // and we should not report it to Sentry
            if (transactionName != null) {
                transaction.name = transactionName;
                transaction.operation = TRANSACTION_OP;
                // if exception has been thrown, transaction status is already set to INTERNAL_ERROR, and
                // httpResponse.getStatus() returns 200.
                if (transaction.status == null) {
                    transaction.status = SpanStatus.fromHttpStatusCode(exchange.response.rawStatusCode ?: 0);
                }
                transaction.finish();
            }
        }.doOnError {
            transaction.status = SpanStatus.INTERNAL_ERROR
            transaction.throwable = it
        }
    }

    private fun startTransaction(request: ServerHttpRequest, sentryTraceHeader: String?): ITransaction {
        val name = "${request.methodValue} ${request.uri}"
        val customSamplingContext = CustomSamplingContext()
        customSamplingContext["request"] = request
        if (sentryTraceHeader != null) {
            try {
                val contexts = TransactionContext.fromSentryTrace(
                    name, TRANSACTION_OP, SentryTraceHeader(sentryTraceHeader)
                )
                return hub.startTransaction(contexts, customSamplingContext, true)
            } catch (e: InvalidSentryTraceHeaderException) {
                hub.options.logger
                    .log(SentryLevel.DEBUG, e, "Failed to parse Sentry trace header: %s", e.message)
            }
        }
        return hub.startTransaction(name, "http.server", customSamplingContext, true)
    }

    // Could be `io.sentry.spring.webflux.TransactionNameProvider`, but that class is private.
    private fun provideTransactionName(
        serverWebExchange: ServerWebExchange
    ): String? {
        val pattern = serverWebExchange.getAttribute<PathPattern>(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)
        return if (pattern != null) {
            serverWebExchange.request.methodValue + " " + pattern.patternString
        } else {
            null
        }
    }
}

koenpunt avatar Nov 16 '21 10:11 koenpunt

@koenpunt if you feel submitting a PR, you can make TransactionNameProvider public with an @ApiStatus.Internal annotation, so it can be used by the SDK.

marandaneto avatar Nov 16 '21 10:11 marandaneto

@maciejwalkowiak can likely guide you as well, since he has made the WebFlux integration.

marandaneto avatar Nov 16 '21 10:11 marandaneto

First I need to figure out how to fix Webflux integration for error handlers. Either we stay with Reactor hook that re-binds Hub to current thread, or we move to using Reactor context.

maciejwalkowiak avatar Nov 16 '21 20:11 maciejwalkowiak

Any pointers for how to get the project to build in IntelliJ? I keep getting this error on gradle sync;

This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 2020.3.1 or newer.

koenpunt avatar Nov 16 '21 21:11 koenpunt

Any pointers for how to get the project to build in IntelliJ? I keep getting this error on gradle sync;

This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 2020.3.1 or newer.

that's because the latest Android Gradle Plugin isn't compatible with IDEA but Android Studio, you can comment the Android projects as a temporary workaround -> https://github.com/getsentry/sentry-java/blob/main/settings.gradle.kts#L9-L14 https://github.com/getsentry/sentry-java/blob/main/settings.gradle.kts#L28

marandaneto avatar Nov 17 '21 08:11 marandaneto

Hi, is there any update on this issue? WebFlux transaction support would be a great feature to have.

yhware avatar Feb 14 '22 04:02 yhware

Hi, is there any update on this issue? WebFlux transaction support would be a great feature to have.

Not yet, someone tried to do a Draft PR tho https://github.com/getsentry/sentry-java/pull/1809

marandaneto avatar Feb 14 '22 10:02 marandaneto

We want to wait for https://github.com/getsentry/sentry-java/pull/1819 before moving on with this.

adinauer avatar Apr 27 '22 12:04 adinauer

For anyone waiting for this. Please give our OpenTelemetry Integration a try.

adinauer avatar Feb 07 '23 07:02 adinauer

6.16.0 has been released, adding support for WebFlux Performance. If you decide to give it a try, please give us feedback 🙏

adinauer avatar Mar 22 '23 08:03 adinauer

By now we've removed sentry from the project and rely on newrelic. But good to know it's implemented.

koenpunt avatar Mar 22 '23 09:03 koenpunt