|
| 1 | +package io.openaev.debug; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Value; |
| 4 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 5 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 6 | +import org.springframework.context.annotation.Bean; |
| 7 | +import org.springframework.context.annotation.Conditional; |
| 8 | +import org.springframework.context.annotation.Configuration; |
| 9 | +import org.springframework.context.annotation.Role; |
| 10 | + |
| 11 | +/** |
| 12 | + * Wires the debug-mode beans, gated by {@link DebugEnabledCondition}. When off (or refused in |
| 13 | + * production) the whole configuration backs off: no proxy, no extra per-request cost. |
| 14 | + */ |
| 15 | +@Configuration(proxyBeanMethods = false) |
| 16 | +@Conditional(DebugEnabledCondition.class) |
| 17 | +public class DebugConfiguration { |
| 18 | + |
| 19 | + @Bean |
| 20 | + public DebugRuntimeState debugRuntimeState() { |
| 21 | + return new DebugRuntimeState(); |
| 22 | + } |
| 23 | + |
| 24 | + @Bean |
| 25 | + @Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
| 26 | + public SensitiveDataMasker sensitiveDataMasker(DebugProperties properties) { |
| 27 | + return new SensitiveDataMasker(properties.getMasking()); |
| 28 | + } |
| 29 | + |
| 30 | + @Bean |
| 31 | + @Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
| 32 | + @ConditionalOnProperty( |
| 33 | + prefix = "openaev.debug.sql", |
| 34 | + name = "enabled", |
| 35 | + havingValue = "true", |
| 36 | + matchIfMissing = true) |
| 37 | + public MaskingSqlLoggingListener maskingSqlLoggingListener( |
| 38 | + SensitiveDataMasker masker, DebugRuntimeState runtimeState, DebugProperties properties) { |
| 39 | + return new MaskingSqlLoggingListener(masker, runtimeState, properties.getSql()); |
| 40 | + } |
| 41 | + |
| 42 | + /** {@code static} so the post-processor is created early enough to wrap the datasource. */ |
| 43 | + @Bean |
| 44 | + @ConditionalOnProperty( |
| 45 | + prefix = "openaev.debug.sql", |
| 46 | + name = "enabled", |
| 47 | + havingValue = "true", |
| 48 | + matchIfMissing = true) |
| 49 | + public static DataSourceProxyBeanPostProcessor dataSourceProxyBeanPostProcessor( |
| 50 | + MaskingSqlLoggingListener listener) { |
| 51 | + return new DataSourceProxyBeanPostProcessor(listener); |
| 52 | + } |
| 53 | + |
| 54 | + /** ORM summary / N+1 detection; rides on the SQL proxy, gated by {@code sql.enabled}. */ |
| 55 | + @Bean |
| 56 | + @ConditionalOnProperty( |
| 57 | + prefix = "openaev.debug.sql", |
| 58 | + name = "enabled", |
| 59 | + havingValue = "true", |
| 60 | + matchIfMissing = true) |
| 61 | + public OrmInsightFilter ormInsightFilter( |
| 62 | + SensitiveDataMasker masker, DebugRuntimeState runtimeState) { |
| 63 | + return new OrmInsightFilter(masker, runtimeState); |
| 64 | + } |
| 65 | + |
| 66 | + @Bean |
| 67 | + public JfrRecordingManager jfrRecordingManager(DebugProperties properties) { |
| 68 | + return new JfrRecordingManager(properties.getOutputDir(), properties.getJfr()); |
| 69 | + } |
| 70 | + |
| 71 | + /** Routes the verbose SQL log to a rotated file (off the console). Gated like SQL logging. */ |
| 72 | + @Bean |
| 73 | + @ConditionalOnProperty( |
| 74 | + prefix = "openaev.debug.sql", |
| 75 | + name = "enabled", |
| 76 | + havingValue = "true", |
| 77 | + matchIfMissing = true) |
| 78 | + public DebugSqlLogFileConfigurer debugSqlLogFileConfigurer(DebugProperties properties) { |
| 79 | + return new DebugSqlLogFileConfigurer(properties.getOutputDir()); |
| 80 | + } |
| 81 | + |
| 82 | + @Bean |
| 83 | + public DebugTenantSource debugTenantSource() { |
| 84 | + return new DebugTenantSource(); |
| 85 | + } |
| 86 | + |
| 87 | + @Bean |
| 88 | + public DebugTenantMdcInterceptor debugTenantMdcInterceptor(DebugTenantSource tenantSource) { |
| 89 | + return new DebugTenantMdcInterceptor(tenantSource); |
| 90 | + } |
| 91 | + |
| 92 | + @Bean |
| 93 | + public DebugWebMvcConfigurer debugWebMvcConfigurer( |
| 94 | + DebugTenantMdcInterceptor tenantMdcInterceptor) { |
| 95 | + return new DebugWebMvcConfigurer(tenantMdcInterceptor); |
| 96 | + } |
| 97 | + |
| 98 | + @Bean |
| 99 | + public DebugModeManager debugModeManager( |
| 100 | + DebugProperties properties, |
| 101 | + JfrRecordingManager jfrRecordingManager, |
| 102 | + DebugRuntimeState runtimeState, |
| 103 | + @Value("${pyroscope.agent.enabled:false}") boolean pyroscopeEnabled) { |
| 104 | + return new DebugModeManager(properties, jfrRecordingManager, runtimeState, pyroscopeEnabled); |
| 105 | + } |
| 106 | +} |
0 commit comments