getsentry / sentry-java

A Sentry SDK for Java, Android and other JVM languages.
https://docs.sentry.io/
MIT License
1.16k stars 435 forks source link

Spring application fails to start with message source when Sentry is added #3068

Open adinauer opened 1 year ago

adinauer commented 1 year ago
          I don't know if it's the same issue, but my Spring Boot application still fails to start with the most recent Gradle plugin.

Using Spring Boot 3.1.5 and Sentry Gradle Plugin 3.14.0

image

With a lot of preceding warnings much like posted above.

Originally posted by @khkramer in https://github.com/getsentry/sentry-java/issues/3019#issuecomment-1831647218

adinauer commented 1 year ago

Also see https://github.com/getsentry/sentry-java/issues/3019#issuecomment-1831840003

adinauer commented 1 year ago

@khkramer can you please try setting sentry.enable-aot-compatibility=true and check whether that makes a difference for you?

I tried using MessageSource in our sample and it worked. So I'm currently unable to reproduce the problem. Can you please give us more details on how you're using it and what your SpringConfig does?

khkramer commented 1 year ago

I just tried that property, it seems to resolve all issues. Thank you very much! Where is this documented though? I can't recall the setup docs mentioning this might be necessary with some projects.

Although the issue is resolved, in case it helps here is my Spring config related to the message source: I register a messageSource manually since I prefer my translations to be loaded from YAML Files.

package nl.quotec.food.config

import nl.quotec.food.app.infra.QuotecMessageSource
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean
import org.springframework.context.MessageSource
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.io.ClassPathResource
import org.springframework.web.servlet.LocaleResolver
import org.springframework.web.servlet.i18n.SessionLocaleResolver
import java.io.IOException
import java.util.*

val defaultLocale: Locale = Locale.forLanguageTag("nl-NL")

@Configuration
class I18nConfig {

    @Bean
    fun localeResolver(): LocaleResolver =
        SessionLocaleResolver().apply { setDefaultLocale(defaultLocale) }

    @Bean(name = ["messageProperties"])
    @Throws(IOException::class)
    fun i18nMessageProperties(): Properties {
        val bean = YamlPropertiesFactoryBean()
        bean.setResources(ClassPathResource("i18n/translations.yml"))
        return bean.getObject()!!
    }

    @Bean
    @Throws(IOException::class)
    fun messageSource(): MessageSource {
        val messages = i18nMessageProperties()
        return QuotecMessageSource(messages)
    }
}
adinauer commented 1 year ago

The flag is just meant as a workaround to unblock you until we figure out a fix. It's intended for things like GraalVM where the aspect config also causes issues. We haven't documented the flag yet - an oversight on our part.

Thanks for the code, we'll try to reproduce and report back.

lbloder commented 11 months ago

Hello @khkramer, I'm currently trying to reproduce your issue. So far to no avail. Your I18nConfig works for me without issues.

Looking at the error message. Could you please provide some information on your nl.quotec.food.app.infra.QuotecMessageSource and nl.quotec.food.config.SpringConfig classes? Also, do you use our @SentryCheckIn annotation, as this is also mentioned in the error?

Here's the setup I used to try and reproduce your issue:

In your I18nConfig I replaced the QuotecMessageSource with the following:

class SentryMessageSource(val props: Properties): AbstractMessageSource() {

    override fun resolveCode(code: String, locale: Locale): MessageFormat {
        return MessageFormat("hello")
    }
}

I also created a MessageSourceUser with message source as a constructor parameter:

class MessageSourceUser(val messageSource: AbstractMessageSource)

And a AppConfig configuration class that exposes the MessageSourceUser as a bean:

@Configuration
class Appconfig {
    @Bean
    fun messageSourceUser(source: AbstractMessageSource): MessageSourceUser {
        return MessageSourceUser(source)
    }
}
khkramer commented 11 months ago

I don't use any @SentryCheckIn annotations anywhere in my project, I was wondering why the error message made notice of it.

Here is our message source class but it's rather basic:

package nl.quotec.food.app.infra

import org.springframework.context.support.ReloadableResourceBundleMessageSource
import java.util.*
import nl.quotec.food.config.defaultLocale as DEFAULT_LOCALE

class QuotecMessageSource(
    messages: Properties,
) : ReloadableResourceBundleMessageSource() {
    init {
        commonMessages = messages
    }

    val messages = messages.toMap() as Map<String, String>

    fun getAllMessages(locale: Locale = DEFAULT_LOCALE): Map<String, String> {
        return messages
    }
}

val defaultLocale: Locale = Locale.forLanguageTag("nl-NL")

lbloder commented 11 months ago

Hello @khkramer, Thank you for the update. I tried again with your provided QuotecMessageSource but was, unfortunately, still unable to reproduce the problem.

We'll have a short discussion internally on how we proceed and will let you know.

adinauer commented 10 months ago

@khkramer can you please setup a minimum reproducible sample that allows us to see the problem and then fix it?