/** * Updates the defined configuration in the holder with following tasks:<br> * 1. Creates the new {@link #agentConfiguration} for given environment and platform id<br> * 2. Resolves all {@link #instrumentationAppliers} for given environment<br> * 3. sets the passes environment to the holder. * <p> * If <code>null</code> is passed then everything saved in the holder will be reset to * <code>null</code> as well. * * @param environment * Environment to update the configuration and appliers with. * @param platformId * Agent id needed for resolving configuration. */ public void update(Environment environment, long platformId) { if (null != environment) { this.environment = environment; this.agentConfiguration = configurationCreator.environmentToConfiguration(environment, platformId); this.instrumentationAppliers = configurationResolver.getInstrumentationAppliers(environment); this.jmxMonitoringAppliers = configurationResolver.getJmxMonitoringAppliers(environment); } else { this.environment = null; // NOPMD this.agentConfiguration = null; // NOPMD this.instrumentationAppliers = null; // NOPMD this.jmxMonitoringAppliers = null; // NOPMD } }
for (IPlatformSensorConfig platformSensorConfig : environment.getPlatformSensorConfigs()) { if (platformSensorConfig.isActive()) { platformSensorTypeConfigs.add(getPlatformSensorTypeConfig(platformId, platformSensorConfig)); Collection<MethodSensorTypeConfig> methodSensorTypeConfigs = new ArrayList<>(); for (IMethodSensorConfig methodSensorConfig : environment.getMethodSensorConfigs()) { methodSensorTypeConfigs.add(getMethodSensorTypeConfig(platformId, methodSensorConfig)); agentConfiguration.setExceptionSensorTypeConfig(getExceptionSensorTypeConfig(platformId, exceptionSensorConfig)); agentConfiguration.setJmxSensorTypeConfig(getJmxSensorTypeConfig(platformId, jmxSensorConfig)); specialMethodSensorTypeConfigs.add(getMethodSensorTypeConfig(platformId, ClassLoadingDelegationSensorConfig.INSTANCE)); specialMethodSensorTypeConfigs.add(getMethodSensorTypeConfig(platformId, MBeanServerInterceptorSensorConfig.INSTANCE)); specialMethodSensorTypeConfigs.add(getMethodSensorTypeConfig(platformId, EUMInstrumentationSensorConfig.INSTANCE)); specialMethodSensorTypeConfigs.add(getMethodSensorTypeConfig(platformId, ExecutorIntercepterSensorConfig.INSTANCE)); specialMethodSensorTypeConfigs.add(getMethodSensorTypeConfig(platformId, CloseableHttpAsyncClientSensorConfig.INSTANCE));
@Test public void retransformationStrategy() throws Exception { RetransformationStrategy retransformationStrategy = RetransformationStrategy.ALWAYS; when(environment.getRetransformationStrategy()).thenReturn(retransformationStrategy); AgentConfig agentConfiguration = creator.environmentToConfiguration(environment, 0); assertThat(agentConfiguration.getRetransformationStrategy(), is(retransformationStrategy)); }
@Test public void disruptorStrategy() throws Exception { String className = "className"; Map<String, String> settings = Collections.singletonMap("key", "value"); IStrategyConfig config = mock(IStrategyConfig.class); when(config.getClassName()).thenReturn(className); when(config.getSettings()).thenReturn(settings); when(environment.getDisruptorStrategyConfig()).thenReturn(config); AgentConfig agentConfiguration = creator.environmentToConfiguration(environment, 0); StrategyConfig strategyConfig = agentConfiguration.getDisruptorStrategyConfig(); assertThat(strategyConfig.getClassName(), is(className)); assertThat(strategyConfig.getSettings(), is(settings)); }
@Test public void updateReset() { long platformId = 11; Environment environment = mock(Environment.class); AgentConfig configuration = mock(AgentConfig.class); IInstrumentationApplier applier = mock(IInstrumentationApplier.class); JmxMonitoringApplier jmxApplier = mock(JmxMonitoringApplier.class); when(configurationCreator.environmentToConfiguration(environment, platformId)).thenReturn(configuration); when(configurationResolver.getInstrumentationAppliers(environment)).thenReturn(Collections.singleton(applier)); when(configurationResolver.getJmxMonitoringAppliers(environment)).thenReturn(Collections.singleton(jmxApplier)); holder.update(environment, platformId); holder.update(null, platformId); assertThat(holder.isInitialized(), is(false)); // only one time verifications verify(configurationCreator).environmentToConfiguration(environment, platformId); verify(configurationResolver).getInstrumentationAppliers(environment); verify(configurationResolver).getJmxMonitoringAppliers(environment); verifyNoMoreInteractions(configurationCreator, configurationResolver); } }
@Test public void noSpecialSensors() throws Exception { long agentId = 13L; when(environment.isClassLoadingDelegation()).thenReturn(false); when(environment.getJmxSensorConfig()).thenReturn(null); AgentConfig agentConfiguration = creator.environmentToConfiguration(environment, agentId); Collection<MethodSensorTypeConfig> sensorTypeConfigs = agentConfiguration.getSpecialMethodSensorTypeConfigs(); assertThat(Iterators.get(sensorTypeConfigs.iterator(), 0).getClassName(), is(equalTo(eisc.getClassName()))); assertThat(Iterators.get(sensorTypeConfigs.iterator(), 1).getClassName(), is(equalTo(closeableHttpAsyncCLient.getClassName()))); }
@Test public void excludeRules() throws Exception { ExcludeRule er1 = new ExcludeRule("excludeRule1"); ExcludeRule er2 = new ExcludeRule("wildCard*"); when(configurationResolver.getAllExcludeRules(environment)).thenReturn(Arrays.asList(new ExcludeRule[] { er1, er2 })); AgentConfig agentConfiguration = creator.environmentToConfiguration(environment, 0); Collection<IMatchPattern> excludeClassesPatterns = agentConfiguration.getExcludeClassesPatterns(); assertThat(excludeClassesPatterns, hasSize(2)); assertThat(excludeClassesPatterns, hasItem(new EqualsMatchPattern(er1.getClassName()))); assertThat(excludeClassesPatterns, hasItem(new WildcardMatchPattern(er2.getClassName()))); }
@Test public void configurePlatformSensorNotActive() throws Exception { long agentId = 13L; IPlatformSensorConfig platformSensorConfig = mock(IPlatformSensorConfig.class); when(platformSensorConfig.isActive()).thenReturn(false); when(environment.getPlatformSensorConfigs()).thenReturn(Collections.singletonList(platformSensorConfig)); AgentConfig agentConfiguration = creator.environmentToConfiguration(environment, agentId); Collection<PlatformSensorTypeConfig> sensorTypeConfigs = agentConfiguration.getPlatformSensorTypeConfigs(); assertThat(sensorTypeConfigs, is(empty())); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(eisc.getClassName()), eq(eisc.getParameters())); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(closeableHttpAsyncCLient.getClassName()), eq(closeableHttpAsyncCLient.getParameters())); verifyNoMoreInteractions(registrationService); }
@Test public void configureJmxSensorNotActive() throws Exception { long agentId = 13L; JmxSensorConfig jmxSensorConfig = mock(JmxSensorConfig.class); when(jmxSensorConfig.isActive()).thenReturn(false); when(environment.getJmxSensorConfig()).thenReturn(jmxSensorConfig); AgentConfig agentConfiguration = creator.environmentToConfiguration(environment, agentId); JmxSensorTypeConfig sensorTypeConfig = agentConfiguration.getJmxSensorTypeConfig(); assertThat(sensorTypeConfig, is(nullValue())); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(eisc.getClassName()), eq(eisc.getParameters())); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(closeableHttpAsyncCLient.getClassName()), eq(closeableHttpAsyncCLient.getParameters())); verifyNoMoreInteractions(registrationService); }
@Test public void configureAgent() throws Exception { long agentId = 13L; AgentConfig agentConfiguration = creator.environmentToConfiguration(environment, agentId); assertThat(agentConfiguration.getPlatformId(), is(agentId)); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(eisc.getClassName()), eq(eisc.getParameters())); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(closeableHttpAsyncCLient.getClassName()), eq(closeableHttpAsyncCLient.getParameters())); verifyNoMoreInteractions(registrationService); }
@Test public void eumConfig() throws Exception { EndUserMonitoringConfig config = mock(EndUserMonitoringConfig.class); when(config.isEumEnabled()).thenReturn(true); String url = "/base/url"; when(config.getScriptBaseUrl()).thenReturn(url); String modules = "12a"; when(config.getActiveModules()).thenReturn(modules); when(config.isListenerInstrumentationAllowed()).thenReturn(false); when(config.isAgentMinificationEnabled()).thenReturn(false); when(environment.getEumConfig()).thenReturn(config); AgentConfig agentConfiguration = creator.environmentToConfiguration(environment, 0); AgentEndUserMonitoringConfig eumConfig = agentConfiguration.getEumConfig(); assertThat(eumConfig.isEnabled(), is(true)); assertThat(eumConfig.getActiveModules(), is(modules)); assertThat(eumConfig.getScriptBaseUrl(), is(url)); assertThat(eumConfig.isListenerInstrumentationAllowed(), is(false)); assertThat(eumConfig.isAgentMinificationEnabled(), is(false)); }
@Test public void mbeanServerInterceptorJmxNotActive() throws Exception { long agentId = 13L; JmxSensorConfig jmxSensorConfig = mock(JmxSensorConfig.class); when(jmxSensorConfig.isActive()).thenReturn(false); when(environment.getJmxSensorConfig()).thenReturn(jmxSensorConfig); AgentConfig agentConfiguration = creator.environmentToConfiguration(environment, agentId); Collection<MethodSensorTypeConfig> sensorTypeConfigs = agentConfiguration.getSpecialMethodSensorTypeConfigs(); assertThat(sensorTypeConfigs, hasSize(2)); assertThat(sensorTypeConfigs.iterator().next().getClassName(), is(equalTo(eisc.getClassName()))); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(eisc.getClassName()), eq(eisc.getParameters())); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(closeableHttpAsyncCLient.getClassName()), eq(closeableHttpAsyncCLient.getParameters())); verifyNoMoreInteractions(registrationService); } }
@Test public void update() { long platformId = 11; Environment environment = mock(Environment.class); AgentConfig configuration = mock(AgentConfig.class); IInstrumentationApplier applier = mock(IInstrumentationApplier.class); JmxMonitoringApplier jmxApplier = mock(JmxMonitoringApplier.class); when(configurationCreator.environmentToConfiguration(environment, platformId)).thenReturn(configuration); when(configurationResolver.getInstrumentationAppliers(environment)).thenReturn(Collections.singleton(applier)); when(configurationResolver.getJmxMonitoringAppliers(environment)).thenReturn(Collections.singleton(jmxApplier)); holder.update(environment, platformId); assertThat(holder.isInitialized(), is(true)); assertThat(holder.getEnvironment(), is(environment)); assertThat(holder.getAgentConfiguration(), is(configuration)); assertThat(holder.getInstrumentationAppliers(), hasSize(1)); assertThat(holder.getInstrumentationAppliers(), hasItem(applier)); assertThat(holder.getJmxMonitoringAppliers(), hasSize(1)); assertThat(holder.getJmxMonitoringAppliers(), hasItem(jmxApplier)); verify(configurationCreator).environmentToConfiguration(environment, platformId); verify(configurationResolver).getInstrumentationAppliers(environment); verify(configurationResolver).getJmxMonitoringAppliers(environment); verifyNoMoreInteractions(configurationCreator, configurationResolver); }
@SuppressWarnings("unchecked") @Test public void configureJmxSensor() throws Exception { long agentId = 13L; long sensorId = 17L; String className = "className"; Map<String, Object> parameters = Collections.<String, Object> singletonMap("key", "value"); JmxSensorConfig jmxSensorConfig = mock(JmxSensorConfig.class); when(jmxSensorConfig.getClassName()).thenReturn(className); when(jmxSensorConfig.getParameters()).thenReturn(parameters); when(jmxSensorConfig.isActive()).thenReturn(true); when(environment.getJmxSensorConfig()).thenReturn(jmxSensorConfig); when(registrationService.registerJmxSensorTypeIdent(agentId, className)).thenReturn(sensorId); AgentConfig agentConfiguration = creator.environmentToConfiguration(environment, agentId); JmxSensorTypeConfig sensorTypeConfig = agentConfiguration.getJmxSensorTypeConfig(); assertThat(sensorTypeConfig.getId(), is(sensorId)); assertThat(sensorTypeConfig.getClassName(), is(className)); assertThat(sensorTypeConfig.getParameters(), is(parameters)); verify(registrationService).registerJmxSensorTypeIdent(agentId, className); // needed because of the intercepting server sensor verify(registrationService, times(3)).registerMethodSensorTypeIdent(anyLong(), anyString(), anyMap()); verifyNoMoreInteractions(registrationService); }
@Test public void configureExceptionSensor() throws Exception { long agentId = 13L; long sensorId = 17L; String sensorName = "sensorName"; String className = "className"; Map<String, Object> parameters = Collections.<String, Object> singletonMap("key", "value"); IExceptionSensorConfig exceptionSensorConfig = mock(IExceptionSensorConfig.class); when(exceptionSensorConfig.getName()).thenReturn(sensorName); when(exceptionSensorConfig.getClassName()).thenReturn(className); when(exceptionSensorConfig.getParameters()).thenReturn(parameters); when(environment.getExceptionSensorConfig()).thenReturn(exceptionSensorConfig); when(registrationService.registerMethodSensorTypeIdent(agentId, className, parameters)).thenReturn(sensorId); AgentConfig agentConfiguration = creator.environmentToConfiguration(environment, agentId); ExceptionSensorTypeConfig sensorTypeConfig = agentConfiguration.getExceptionSensorTypeConfig(); assertThat(sensorTypeConfig.getId(), is(sensorId)); assertThat(sensorTypeConfig.getName(), is(sensorName)); assertThat(sensorTypeConfig.getClassName(), is(className)); assertThat(sensorTypeConfig.getParameters(), is(parameters)); assertThat(sensorTypeConfig.getPriority(), is(PriorityEnum.NORMAL)); // default priority verify(registrationService).registerMethodSensorTypeIdent(agentId, className, parameters); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(eisc.getClassName()), eq(eisc.getParameters())); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(closeableHttpAsyncCLient.getClassName()), eq(closeableHttpAsyncCLient.getParameters())); verifyNoMoreInteractions(registrationService); }
@Test public void configurePlatformSensor() throws Exception { long agentId = 13L; long sensorId = 17L; String className = "className"; Map<String, Object> parameters = Collections.<String, Object> singletonMap("key", "value"); IPlatformSensorConfig platformSensorConfig = mock(IPlatformSensorConfig.class); when(platformSensorConfig.getClassName()).thenReturn(className); when(platformSensorConfig.getParameters()).thenReturn(parameters); when(platformSensorConfig.isActive()).thenReturn(true); when(environment.getPlatformSensorConfigs()).thenReturn(Collections.singletonList(platformSensorConfig)); when(registrationService.registerPlatformSensorTypeIdent(agentId, className)).thenReturn(sensorId); AgentConfig agentConfiguration = creator.environmentToConfiguration(environment, agentId); Collection<PlatformSensorTypeConfig> sensorTypeConfigs = agentConfiguration.getPlatformSensorTypeConfigs(); assertThat(sensorTypeConfigs, hasSize(1)); PlatformSensorTypeConfig sensorTypeConfig = sensorTypeConfigs.iterator().next(); assertThat(sensorTypeConfig.getId(), is(sensorId)); assertThat(sensorTypeConfig.getClassName(), is(className)); assertThat(sensorTypeConfig.getParameters(), is(parameters)); verify(registrationService).registerPlatformSensorTypeIdent(agentId, className); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(eisc.getClassName()), eq(eisc.getParameters())); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(closeableHttpAsyncCLient.getClassName()), eq(closeableHttpAsyncCLient.getParameters())); verifyNoMoreInteractions(registrationService); }
@Test public void configureMethodSensor() throws Exception { long agentId = 13L; long sensorId = 17L; String sensorName = "sensorName"; String className = "className"; Map<String, Object> parameters = Collections.<String, Object> singletonMap("key", "value"); IMethodSensorConfig methodSensorConfig = mock(IMethodSensorConfig.class); when(methodSensorConfig.getName()).thenReturn(sensorName); when(methodSensorConfig.getClassName()).thenReturn(className); when(methodSensorConfig.getParameters()).thenReturn(parameters); when(methodSensorConfig.getPriority()).thenReturn(PriorityEnum.MAX); when(environment.getMethodSensorConfigs()).thenReturn(Collections.singletonList(methodSensorConfig)); when(registrationService.registerMethodSensorTypeIdent(agentId, className, parameters)).thenReturn(sensorId); AgentConfig agentConfiguration = creator.environmentToConfiguration(environment, agentId); Collection<MethodSensorTypeConfig> sensorTypeConfigs = agentConfiguration.getMethodSensorTypeConfigs(); assertThat(sensorTypeConfigs, hasSize(1)); MethodSensorTypeConfig sensorTypeConfig = sensorTypeConfigs.iterator().next(); assertThat(sensorTypeConfig.getId(), is(sensorId)); assertThat(sensorTypeConfig.getName(), is(sensorName)); assertThat(sensorTypeConfig.getClassName(), is(className)); assertThat(sensorTypeConfig.getParameters(), is(parameters)); assertThat(sensorTypeConfig.getPriority(), is(PriorityEnum.MAX)); verify(registrationService).registerMethodSensorTypeIdent(agentId, className, parameters); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(eisc.getClassName()), eq(eisc.getParameters())); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(closeableHttpAsyncCLient.getClassName()), eq(closeableHttpAsyncCLient.getParameters())); verifyNoMoreInteractions(registrationService); }
@Test public void classLoadingDelegation() throws Exception { long agentId = 13L; long sensorId = 17L; when(environment.isClassLoadingDelegation()).thenReturn(true); ClassLoadingDelegationSensorConfig cldConfig = ClassLoadingDelegationSensorConfig.INSTANCE; when(registrationService.registerMethodSensorTypeIdent(agentId, cldConfig.getClassName(), cldConfig.getParameters())).thenReturn(sensorId); AgentConfig agentConfiguration = creator.environmentToConfiguration(environment, agentId); Collection<MethodSensorTypeConfig> sensorTypeConfigs = agentConfiguration.getSpecialMethodSensorTypeConfigs(); assertThat(sensorTypeConfigs, hasSize(3)); // first element will be class loading config MethodSensorTypeConfig sensorTypeConfig = sensorTypeConfigs.iterator().next(); assertThat(sensorTypeConfig.getId(), is(sensorId)); assertThat(sensorTypeConfig.getName(), is(cldConfig.getName())); assertThat(sensorTypeConfig.getClassName(), is(cldConfig.getClassName())); assertThat(sensorTypeConfig.getParameters(), is(cldConfig.getParameters())); assertThat(sensorTypeConfig.getPriority(), is(cldConfig.getPriority())); verify(registrationService).registerMethodSensorTypeIdent(agentId, cldConfig.getClassName(), cldConfig.getParameters()); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(eisc.getClassName()), eq(eisc.getParameters())); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(closeableHttpAsyncCLient.getClassName()), eq(closeableHttpAsyncCLient.getParameters())); verifyNoMoreInteractions(registrationService); }
@Test public void mbeanServerInterceptor() throws Exception { long agentId = 13L; long sensorId = 17L; JmxSensorConfig jmxSensorConfig = mock(JmxSensorConfig.class); when(jmxSensorConfig.isActive()).thenReturn(true); when(environment.getJmxSensorConfig()).thenReturn(jmxSensorConfig); MBeanServerInterceptorSensorConfig msiConfig = MBeanServerInterceptorSensorConfig.INSTANCE; when(registrationService.registerMethodSensorTypeIdent(agentId, msiConfig.getClassName(), msiConfig.getParameters())).thenReturn(sensorId); AgentConfig agentConfiguration = creator.environmentToConfiguration(environment, agentId); Collection<MethodSensorTypeConfig> sensorTypeConfigs = agentConfiguration.getSpecialMethodSensorTypeConfigs(); assertThat(sensorTypeConfigs, hasSize(3)); // first element will be mbean server interceptor config MethodSensorTypeConfig sensorTypeConfig = sensorTypeConfigs.iterator().next(); assertThat(sensorTypeConfig.getId(), is(sensorId)); assertThat(sensorTypeConfig.getName(), is(msiConfig.getName())); assertThat(sensorTypeConfig.getClassName(), is(msiConfig.getClassName())); assertThat(sensorTypeConfig.getParameters(), is(msiConfig.getParameters())); assertThat(sensorTypeConfig.getPriority(), is(msiConfig.getPriority())); verify(registrationService).registerMethodSensorTypeIdent(agentId, msiConfig.getClassName(), msiConfig.getParameters()); // needed because jmx sensor will be also registered verify(registrationService).registerJmxSensorTypeIdent(anyLong(), anyString()); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(eisc.getClassName()), eq(eisc.getParameters())); verify(registrationService).registerMethodSensorTypeIdent(anyLong(), eq(closeableHttpAsyncCLient.getClassName()), eq(closeableHttpAsyncCLient.getParameters())); verifyNoMoreInteractions(registrationService); }