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

Report active Spring profiles in envelope item context #4147

Merged
merged 19 commits into from
Feb 20, 2025
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
Expand Up @@ -5,6 +5,8 @@
### Features

- The `ignoredErrors` option is now configurable via the manifest property `io.sentry.traces.ignored-errors` ([#4178](https://github.com/getsentry/sentry-java/pull/4178))
- A list of active Spring profiles is attached to payloads sent to Sentry (errors, traces, etc.) and displayed in the UI when using our Spring or Spring Boot integrations ([#4147](https://github.com/getsentry/sentry-java/pull/4147))
- This consists of an empty list when only the default profile is active

### Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.sentry.spring.jakarta.SentryUserFilter;
import io.sentry.spring.jakarta.SentryUserProvider;
import io.sentry.spring.jakarta.SentryWebConfiguration;
import io.sentry.spring.jakarta.SpringProfilesEventProcessor;
import io.sentry.spring.jakarta.SpringSecuritySentryUserProvider;
import io.sentry.spring.jakarta.checkin.SentryCheckInAdviceConfiguration;
import io.sentry.spring.jakarta.checkin.SentryCheckInPointcutConfiguration;
Expand Down Expand Up @@ -68,6 +69,7 @@
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down Expand Up @@ -470,4 +472,14 @@ private static class SentryTracesSampleRateCondition {}
@SuppressWarnings("UnusedNestedClass")
private static class SentryTracesSamplerBeanCondition {}
}

@Configuration(proxyBeanMethods = false)
@Open
static class SpringProfilesEventProcessorConfiguration {
@Bean
public @NotNull SpringProfilesEventProcessor springProfilesEventProcessor(
final Environment environment) {
return new SpringProfilesEventProcessor(environment);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import io.sentry.spring.jakarta.HttpServletRequestSentryUserProvider
import io.sentry.spring.jakarta.SentryExceptionResolver
import io.sentry.spring.jakarta.SentryUserFilter
import io.sentry.spring.jakarta.SentryUserProvider
import io.sentry.spring.jakarta.SpringProfilesEventProcessor
import io.sentry.spring.jakarta.SpringSecuritySentryUserProvider
import io.sentry.spring.jakarta.tracing.SentryTracingFilter
import io.sentry.spring.jakarta.tracing.SpringServletTransactionNameProvider
Expand Down Expand Up @@ -925,6 +926,14 @@ class SentryAutoConfigurationTest {
}
}

@Test
fun `registers SpringProfilesEventProcessor on SentryOptions`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.run {
assertThat(it.getBean(SentryOptions::class.java).eventProcessors).anyMatch { processor -> processor.javaClass == SpringProfilesEventProcessor::class.java }
}
}

@Configuration(proxyBeanMethods = false)
open class CustomSchedulerFactoryBeanCustomizerConfiguration {
class MyJobListener : JobListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.sentry.spring.SentryUserFilter;
import io.sentry.spring.SentryUserProvider;
import io.sentry.spring.SentryWebConfiguration;
import io.sentry.spring.SpringProfilesEventProcessor;
import io.sentry.spring.SpringSecuritySentryUserProvider;
import io.sentry.spring.boot.graphql.SentryGraphqlAutoConfiguration;
import io.sentry.spring.checkin.SentryCheckInAdviceConfiguration;
Expand Down Expand Up @@ -67,6 +68,7 @@
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down Expand Up @@ -439,4 +441,14 @@ private static class SentryTracesSampleRateCondition {}
@SuppressWarnings("UnusedNestedClass")
private static class SentryTracesSamplerBeanCondition {}
}

@Configuration(proxyBeanMethods = false)
@Open
static class SpringProfilesEventProcessorConfiguration {
@Bean
public @NotNull SpringProfilesEventProcessor springProfilesEventProcessor(
final Environment environment) {
return new SpringProfilesEventProcessor(environment);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import io.sentry.spring.HttpServletRequestSentryUserProvider
import io.sentry.spring.SentryExceptionResolver
import io.sentry.spring.SentryUserFilter
import io.sentry.spring.SentryUserProvider
import io.sentry.spring.SpringProfilesEventProcessor
import io.sentry.spring.SpringSecuritySentryUserProvider
import io.sentry.spring.tracing.SentryTracingFilter
import io.sentry.spring.tracing.SpringServletTransactionNameProvider
Expand Down Expand Up @@ -865,6 +866,14 @@ class SentryAutoConfigurationTest {
}
}

@Test
fun `registers SpringProfilesEventProcessor on SentryOptions`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.run {
assertThat(it.getBean(SentryOptions::class.java).eventProcessors).anyMatch { processor -> processor.javaClass == SpringProfilesEventProcessor::class.java }
}
}

@Configuration(proxyBeanMethods = false)
open class CustomSchedulerFactoryBeanCustomizerConfiguration {
class MyJobListener : JobListener {
Expand Down
7 changes: 7 additions & 0 deletions sentry-spring-jakarta/api/sentry-spring-jakarta.api
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ public class io/sentry/spring/jakarta/SentryWebConfiguration {
public fun httpServletRequestSentryUserProvider (Lio/sentry/SentryOptions;)Lio/sentry/spring/jakarta/HttpServletRequestSentryUserProvider;
}

public final class io/sentry/spring/jakarta/SpringProfilesEventProcessor : io/sentry/EventProcessor {
public fun <init> (Lorg/springframework/core/env/Environment;)V
public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent;
public fun process (Lio/sentry/SentryReplayEvent;Lio/sentry/Hint;)Lio/sentry/SentryReplayEvent;
public fun process (Lio/sentry/protocol/SentryTransaction;Lio/sentry/Hint;)Lio/sentry/protocol/SentryTransaction;
}

public final class io/sentry/spring/jakarta/SpringSecuritySentryUserProvider : io/sentry/spring/jakarta/SentryUserProvider {
public fun <init> (Lio/sentry/SentryOptions;)V
public fun provideUser ()Lio/sentry/protocol/User;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.sentry.spring.jakarta;

import io.sentry.EventProcessor;
import io.sentry.Hint;
import io.sentry.SentryBaseEvent;
import io.sentry.SentryEvent;
import io.sentry.SentryReplayEvent;
import io.sentry.protocol.SentryTransaction;
import io.sentry.protocol.Spring;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.core.env.Environment;

/**
* Attaches the list of active Spring profiles (an empty list if only the default profile is active)
* to the {@link io.sentry.TraceContext} associated with the event.
*/
public final class SpringProfilesEventProcessor implements EventProcessor {
private final @NotNull Environment environment;

@Override
public @NotNull SentryEvent process(final @NotNull SentryEvent event, final @NotNull Hint hint) {
processInternal(event);
return event;
}

@Override
public @NotNull SentryTransaction process(
final @NotNull SentryTransaction transaction, final @NotNull Hint hint) {
processInternal(transaction);
return transaction;
}

@Override
public @NotNull SentryReplayEvent process(
final @NotNull SentryReplayEvent event, final @NotNull Hint hint) {
processInternal(event);
return event;
}

private void processInternal(final @NotNull SentryBaseEvent event) {
@Nullable String[] activeProfiles = environment.getActiveProfiles();
@NotNull Spring springContext = new Spring();
springContext.setActiveProfiles(activeProfiles);
event.getContexts().setSpring(springContext);
}

public SpringProfilesEventProcessor(final @NotNull Environment environment) {
this.environment = environment;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io.sentry.spring.jakarta

import io.sentry.ITransportFactory
import io.sentry.Sentry
import io.sentry.checkEvent
import io.sentry.protocol.Spring
import io.sentry.transport.ITransport
import org.assertj.core.api.Assertions.assertThat
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.springframework.boot.test.context.runner.ApplicationContextRunner
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.env.Environment
import kotlin.test.Test

class SpringProfilesEventProcessorTest {

private val contextRunner = ApplicationContextRunner()
.withUserConfiguration(AppConfiguration::class.java)
.withUserConfiguration(SpringProfilesEventProcessorConfiguration::class.java)
.withUserConfiguration(MockTransportConfiguration::class.java)

@Test
fun `when default Spring profile is active, sets active_profiles in Spring context to empty list on sent event`() {
contextRunner
.run {
Sentry.captureMessage("test")
val transport = it.getBean(ITransport::class.java)
verify(transport).send(
checkEvent { event ->
val expected = Spring()
expected.activeProfiles = listOf<String>().toTypedArray()
assertThat(event.contexts.spring).isEqualTo(expected)
},
anyOrNull()
)
}
}

@Test
fun `when non-default Spring profiles are active, sets active profiles in Spring context to list of profile names`() {
contextRunner
.withPropertyValues(
"spring.profiles.active=test1,test2"
)
.run {
Sentry.captureMessage("test")
val transport = it.getBean(ITransport::class.java)
verify(transport).send(
checkEvent { event ->
val expected = Spring()
expected.activeProfiles = listOf("test1", "test2").toTypedArray()
assertThat(event.contexts.spring).isEqualTo(expected)
},
anyOrNull()
)
}
}

@EnableSentry(dsn = "http://key@localhost/proj")
class AppConfiguration

@Configuration(proxyBeanMethods = false)
open class SpringProfilesEventProcessorConfiguration {
@Bean
open fun springProfilesEventProcessor(environment: Environment): SpringProfilesEventProcessor {
return SpringProfilesEventProcessor(environment)
}
}

@Configuration(proxyBeanMethods = false)
open class MockTransportConfiguration {

private val transport = mock<ITransport>()

@Bean
open fun mockTransportFactory(): ITransportFactory {
val factory = mock<ITransportFactory>()
whenever(factory.create(any(), any())).thenReturn(transport)
return factory
}

@Bean
open fun sentryTransport() = transport
}
}
7 changes: 7 additions & 0 deletions sentry-spring/api/sentry-spring.api
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ public class io/sentry/spring/SentryWebConfiguration {
public fun httpServletRequestSentryUserProvider (Lio/sentry/SentryOptions;)Lio/sentry/spring/HttpServletRequestSentryUserProvider;
}

public final class io/sentry/spring/SpringProfilesEventProcessor : io/sentry/EventProcessor {
public fun <init> (Lorg/springframework/core/env/Environment;)V
public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent;
public fun process (Lio/sentry/SentryReplayEvent;Lio/sentry/Hint;)Lio/sentry/SentryReplayEvent;
public fun process (Lio/sentry/protocol/SentryTransaction;Lio/sentry/Hint;)Lio/sentry/protocol/SentryTransaction;
}

public final class io/sentry/spring/SpringSecuritySentryUserProvider : io/sentry/spring/SentryUserProvider {
public fun <init> (Lio/sentry/SentryOptions;)V
public fun provideUser ()Lio/sentry/protocol/User;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.sentry.spring;

import io.sentry.EventProcessor;
import io.sentry.Hint;
import io.sentry.SentryBaseEvent;
import io.sentry.SentryEvent;
import io.sentry.SentryReplayEvent;
import io.sentry.protocol.SentryTransaction;
import io.sentry.protocol.Spring;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.core.env.Environment;

/**
* Attaches the list of active Spring profiles (an empty list if only the default profile is active)
* to the {@link io.sentry.TraceContext} associated with the event.
*/
public final class SpringProfilesEventProcessor implements EventProcessor {
private final @NotNull Environment environment;

@Override
public @NotNull SentryEvent process(final @NotNull SentryEvent event, final @NotNull Hint hint) {
processInternal(event);
return event;
}

@Override
public @NotNull SentryTransaction process(
final @NotNull SentryTransaction transaction, final @NotNull Hint hint) {
processInternal(transaction);
return transaction;
}

@Override
public @NotNull SentryReplayEvent process(
final @NotNull SentryReplayEvent event, final @NotNull Hint hint) {
processInternal(event);
return event;
}

private void processInternal(final @NotNull SentryBaseEvent event) {
@Nullable String[] activeProfiles = environment.getActiveProfiles();
@NotNull Spring springContext = new Spring();
springContext.setActiveProfiles(activeProfiles);
event.getContexts().setSpring(springContext);
}

public SpringProfilesEventProcessor(final @NotNull Environment environment) {
this.environment = environment;
}
}
Loading
Loading