/** * {@inheritDoc} */ @Override public String toString() { return getId() + " :: name: " + name + " (" + priority + ")"; }
/** * Return the {@link MethodSensorTypeConfig} for given sensor class name. * * @param sensorClassName * Sensor class name. * @return {@link MethodSensorTypeConfig} or <code>null</code> if such does not exists in the * configuration. */ public MethodSensorTypeConfig getMethodSensorTypeConfig(String sensorClassName) { if (StringUtils.isNotBlank(sensorClassName)) { for (MethodSensorTypeConfig methodSensorTypeConfig : methodSensorTypeConfigs) { if (sensorClassName.equals(methodSensorTypeConfig.getClassName())) { return methodSensorTypeConfig; } } } return null; }
/** * Returns if jRebel property is activated on the sensor. * * @return Returns if jRebel property is activated on the sensor. */ public boolean isJRebelActive() { if (MapUtils.isNotEmpty(getParameters())) { Object jRebelValue = getParameters().get("jRebel"); if ("true".equals(jRebelValue)) { return true; } } return false; }
/** * Creates the agent based {@link MethodSensorTypeConfig} with correctly registered ID. * * @param platformId * ID of the agent. * @param methodSensorConfig * {@link IMethodSensorConfig} defined in the {@link Environment}. * @return {@link MethodSensorTypeConfig}. */ private MethodSensorTypeConfig getMethodSensorTypeConfig(long platformId, IMethodSensorConfig methodSensorConfig) { long id = registrationService.registerMethodSensorTypeIdent(platformId, methodSensorConfig.getClassName(), methodSensorConfig.getParameters()); MethodSensorTypeConfig methodSensorTypeConfig = new MethodSensorTypeConfig(); methodSensorTypeConfig.setId(id); methodSensorTypeConfig.setClassName(methodSensorConfig.getClassName()); methodSensorTypeConfig.setParameters(methodSensorConfig.getParameters()); methodSensorTypeConfig.setName(methodSensorConfig.getName()); methodSensorTypeConfig.setPriority(methodSensorConfig.getPriority()); return methodSensorTypeConfig; }
/** * {@inheritDoc} */ protected void applyAssignment(AgentConfig agentConfiguration, SensorInstrumentationPoint sensorInstrumentationPoint) { // first deal with sensor id MethodSensorTypeConfig methodSensorTypeConfig = getSensorTypeConfigFromConfiguration(agentConfiguration, environment, methodSensorAssignment); long sensorId = methodSensorTypeConfig.getId(); // set to rsc sensorInstrumentationPoint.addSensorId(sensorId, methodSensorTypeConfig.getPriority()); // add all settings sensorInstrumentationPoint.addSettings(methodSensorAssignment.getSettings()); }
@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); }
when(exceptionSensorTypeConfig.getName()).thenReturn("rocks.inspectit.agent.java.sensor.exception.ExceptionSensor"); when(exceptionSensorTypeConfig.getId()).thenReturn(exceptionSensorTypeId); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensor.getHook()).thenReturn(methodHook); when(methodSensorConfig.getId()).thenReturn(sensorTypeId);
@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); }
/** * {@inheritDoc} */ @Override protected void applyAssignment(AgentConfig agentConfiguration, SensorInstrumentationPoint registeredSensorConfig) { // call super first super.applyAssignment(agentConfiguration, registeredSensorConfig); // check for invocation starts if (assignment.isStartsInvocation()) { // find the id of invocation sensor and only mark if one is found IMethodSensorConfig invocationSensorConfig = environment.getMethodSensorTypeConfig(InvocationSequenceSensorConfig.class); if (null != invocationSensorConfig) { MethodSensorTypeConfig invocationSensorTypeConfig = agentConfiguration.getMethodSensorTypeConfig(invocationSensorConfig.getClassName()); registeredSensorConfig.addSensorId(invocationSensorTypeConfig.getId(), invocationSensorTypeConfig.getPriority()); registeredSensorConfig.setStartsInvocation(true); } } } }
long sensorTypeIdTwo = 13L; long sensorTypeIdThree = 15L; when(methodSensorConfigOne.getId()).thenReturn(sensorTypeIdOne); when(methodSensorConfigTwo.getId()).thenReturn(sensorTypeIdTwo); when(methodSensorConfigThree.getId()).thenReturn(sensorTypeIdThree); when(methodSensorOne.getHook()).thenReturn(methodHookOne); when(methodSensorTwo.getHook()).thenReturn(methodHookTwo); when(exceptionSensorTypeConfig.getName()).thenReturn("rocks.inspectit.agent.java.sensor.exception.ExceptionSensor"); when(exceptionSensorTypeConfig.getId()).thenReturn(exceptionSensorTypeId);
@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); }
/** * {@inheritDoc} */ @Override public void afterPropertiesSet() throws Exception { methodSensorMap = new HashMap<Long, IMethodSensor>(); for (IMethodSensor methodSensor : methodSensors) { methodSensorMap.put(methodSensor.getSensorTypeConfig().getId(), methodSensor); } }
@Test public void instrumentationExist() throws Exception { long agentId = 13L; long sensorId = 15L; String sensorClassName = "sensorClassName"; MethodSensorTypeConfig methodSensorTypeConfig = mock(MethodSensorTypeConfig.class); when(methodSensorTypeConfig.getId()).thenReturn(sensorId); when(methodSensorTypeConfig.getPriority()).thenReturn(PriorityEnum.NORMAL); AgentConfig agentConfiguration = mock(AgentConfig.class); when(agentConfiguration.getPlatformId()).thenReturn(agentId); when(agentConfiguration.getMethodSensorTypeConfig(sensorClassName)).thenReturn(methodSensorTypeConfig); Map<String, Object> settings = Collections.<String, Object> singletonMap("key", "value"); when(assignment.getSettings()).thenReturn(settings); IMethodSensorConfig methodSensorConfig = mock(IMethodSensorConfig.class); when(methodSensorConfig.getClassName()).thenReturn(sensorClassName); when(environment.getMethodSensorTypeConfig(Matchers.<Class<? extends IMethodSensorConfig>> any())).thenReturn(methodSensorConfig); SensorInstrumentationPoint rsc = mock(SensorInstrumentationPoint.class); when(rsc.getSensorIds()).thenReturn(new long[] { sensorId }); MethodInstrumentationConfig instrumentationConfig = mock(MethodInstrumentationConfig.class); when(instrumentationConfig.getSensorInstrumentationPoint()).thenReturn(rsc); when(methodType.getMethodInstrumentationConfig()).thenReturn(instrumentationConfig); boolean changed = applier.addInstrumentationPoints(agentConfiguration, classType); // verify results assertThat(changed, is(true)); // verify no interaction verifyZeroInteractions(registrationService); verify(instrumentationConfig, times(0)).setSensorInstrumentationPoint(Matchers.<SensorInstrumentationPoint> any()); verify(methodType, times(0)).setMethodInstrumentationConfig(Matchers.<MethodInstrumentationConfig> any()); }
/** * Return the {@link MethodSensorTypeConfig} for given special sensor class name. * * @param sensorClassName * Sensor class name. * @return {@link MethodSensorTypeConfig} or <code>null</code> if such does not exists in the * configuration. */ public MethodSensorTypeConfig getSpecialMethodSensorTypeConfig(String sensorClassName) { if (StringUtils.isNotBlank(sensorClassName)) { for (MethodSensorTypeConfig methodSensorTypeConfig : specialMethodSensorTypeConfigs) { if (sensorClassName.equals(methodSensorTypeConfig.getClassName())) { return methodSensorTypeConfig; } } } return null; }
when(exceptionSensorTypeConfig.getName()).thenReturn("rocks.inspectit.agent.java.sensor.exception.ExceptionSensor"); when(exceptionSensorTypeConfig.getId()).thenReturn(exceptionSensorTypeId); when(invocSensorType.getId()).thenReturn(invocSensorTypeId); InvocationSequenceHook invocHook = mock(InvocationSequenceHook.class); when(invocationSequenceSensor.getSensorTypeConfig()).thenReturn(invocSensorType); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensor.getHook()).thenReturn(methodHook); when(methodSensorConfig.getId()).thenReturn(methodSensorTypeId);
/** * {@inheritDoc} */ public void init(MethodSensorTypeConfig sensorTypeConfig) { this.sensorTypeConfig = sensorTypeConfig; initHook(sensorTypeConfig.getParameters()); }
/** * {@inheritDoc} */ @Override protected void applyAssignment(AgentConfig agentConfiguration, MethodType methodType, MethodInstrumentationConfig methodInstrumentationConfig) { SpecialInstrumentationPoint specialInstrumentationPoint = getOrCreateSpecialInstrumentationPoint(agentConfiguration, methodType, methodInstrumentationConfig); MethodSensorTypeConfig methodSensorTypeConfig = getSensorTypeConfigFromConfiguration(agentConfiguration); long sensorId = methodSensorTypeConfig.getId(); specialInstrumentationPoint.setSensorId(sensorId); specialInstrumentationPoint.setSubstitutionDescriptor(functionalAssignment.getSpecialMethodSensorConfig().getSubstitutionDescriptor()); }
when(methodSensorTypeConfig.getId()).thenReturn(sensorId); when(methodSensorTypeConfig.getPriority()).thenReturn(PriorityEnum.NORMAL); when(invocSensorTypeConfig.getId()).thenReturn(invocationSensorId); when(invocSensorTypeConfig.getPriority()).thenReturn(PriorityEnum.INVOC);
/** * {@inheritDoc} */ @Override public void afterPropertiesSet() throws Exception { for (MethodSensorTypeConfig config : configurationStorage.getMethodSensorTypes()) { if (config.getClassName().equals(this.getClass().getName())) { this.init(config); break; } } }
@Test public void dispatchOneSpecialHook() { long sensorTypeId = 7L; IMethodSensor methodSensor = mock(IMethodSensor.class); ISpecialHook specialHook = mock(ISpecialHook.class); MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getHook()).thenReturn(specialHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(sensorTypeId); SpecialSensorConfig specialSensorConfig = mock(SpecialSensorConfig.class); when(specialSensorConfig.getSensor()).thenReturn(methodSensor); int methodId = 3; Object object = mock(Object.class); Object[] parameters = new Object[0]; Object returnValue = mock(Object.class); hookDispatcher.addMapping(methodId, specialSensorConfig); Object result = hookDispatcher.dispatchSpecialMethodBeforeBody(methodId, object, parameters); verify(specialSensorConfig, times(1)).getSensor(); verify(specialHook, times(1)).beforeBody(methodId, object, parameters, specialSensorConfig); assertThat(result, is(nullValue())); result = hookDispatcher.dispatchSpecialMethodAfterBody(methodId, object, parameters, returnValue); verify(specialSensorConfig, times(2)).getSensor(); verify(specialHook, times(1)).afterBody(methodId, object, parameters, returnValue, specialSensorConfig); assertThat(result, is(nullValue())); verifyZeroInteractions(object, coreService, returnValue); verifyNoMoreInteractions(specialSensorConfig, specialHook); }