/** * {@inheritDoc} */ @Override public String toString() { return getId() + " :: name: " + name + " (" + priority + ")"; }
/** * {@inheritDoc} */ @Override public void afterPropertiesSet() throws Exception { methodSensorMap = new HashMap<Long, IMethodSensor>(); for (IMethodSensor methodSensor : methodSensors) { methodSensorMap.put(methodSensor.getSensorTypeConfig().getId(), methodSensor); } }
/** * {@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()); }
/** * {@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()); }
@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); }
@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()); }
when(methodSensor.getHook()).thenReturn(specialHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(sensorTypeId);
/** * {@inheritDoc} */ @Override public void dispatchBeforeCatch(long id, Object exceptionObject) { if (!executionMarker.isActive()) { try { executionMarker.active(); // rsc contains the settings of the actual method where the exception is catched. RegisteredSensorConfig rsc = mappings.get(id); long sensorTypeId = exceptionSensor.getSensorTypeConfig().getId(); ICoreService invocCoreService = null; if (null != invocationSequenceHolder.get()) { // Need to replace the core service with the one from the invocation sequence so // that all data objects can be associated to that invocation record. invocCoreService = (ICoreService) invocationSequenceHolder.get(); } IExceptionSensorHook exceptionHook = (IExceptionSensorHook) exceptionSensor.getHook(); if (null != invocCoreService) { exceptionHook.dispatchBeforeCatchBody(invocCoreService, id, sensorTypeId, exceptionObject, rsc); } else { exceptionHook.dispatchBeforeCatchBody(coreService, id, sensorTypeId, exceptionObject, rsc); } } finally { executionMarker.deactive(); } } }
@Test public void dispatchOneConstructorHookWithoutInvocationTrace() { long sensorTypeId = 7L; IConstructorHook constructorHook = mock(IConstructorHook.class); IMethodSensor methodSensor = mock(IMethodSensor.class); MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getHook()).thenReturn(constructorHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(sensorTypeId); RegisteredSensorConfig registeredSensorConfig = mock(RegisteredSensorConfig.class); when(registeredSensorConfig.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(registeredSensorConfig.getMethodSensorsReverse()).thenReturn(Collections.singletonList(methodSensor)); int methodId = 3; Object object = mock(Object.class); Object[] parameters = new Object[0]; hookDispatcher.addMapping(methodId, registeredSensorConfig); hookDispatcher.dispatchConstructorBeforeBody(methodId, parameters); verify(registeredSensorConfig, times(1)).isStartsInvocation(); verify(registeredSensorConfig, times(1)).getMethodSensorsReverse(); verify(constructorHook, times(1)).beforeConstructor(methodId, sensorTypeId, parameters, registeredSensorConfig); hookDispatcher.dispatchConstructorAfterBody(methodId, object, parameters); verify(registeredSensorConfig, times(2)).isStartsInvocation(); verify(registeredSensorConfig, times(1)).getMethodSensors(); verify(constructorHook, times(1)).afterConstructor(coreService, methodId, sensorTypeId, object, parameters, registeredSensorConfig); verifyZeroInteractions(object, coreService); verifyNoMoreInteractions(registeredSensorConfig, constructorHook); }
when(sensorInstrumentationPoint.getSettings()).thenReturn(Collections.<String, Object> singletonMap("key", "value")); when(sensorInstrumentationPoint.getPropertyAccessorList()).thenReturn(Collections.<PropertyPathStart> emptyList()); when(methodSensorTypeConfig.getId()).thenReturn(sensorIds[0]);
/** * {@inheritDoc} */ @Override public void dispatchOnThrowInBody(long id, Object object, Object[] parameters, Object exceptionObject) { if (!executionMarker.isActive()) { try { executionMarker.active(); // rsc contains the settings for the actual method where the exception was thrown. RegisteredSensorConfig rsc = mappings.get(id); MethodSensorTypeConfig sensorTypeConfig = exceptionSensor.getSensorTypeConfig(); long sensorTypeId = sensorTypeConfig.getId(); ICoreService invocCoreService = null; if (null != invocationSequenceHolder.get()) { // Need to replace the core service with the one from the invocation sequence so // that all data objects can be associated to that invocation record. invocCoreService = (ICoreService) invocationSequenceHolder.get(); } IExceptionSensorHook exceptionHook = (IExceptionSensorHook) exceptionSensor.getHook(); if (null != invocCoreService) { exceptionHook.dispatchOnThrowInBody(invocCoreService, id, sensorTypeId, object, exceptionObject, parameters, rsc); } else { exceptionHook.dispatchOnThrowInBody(coreService, id, sensorTypeId, object, exceptionObject, parameters, rsc); } } finally { executionMarker.deactive(); } } }
/** * {@inheritDoc} */ @Override public void dispatchConstructorOnThrowInBody(long id, Object object, Object[] parameters, Object exceptionObject) { if (!executionMarker.isActive()) { try { executionMarker.active(); // rsc contains the settings for the actual constructor where the exception was // thrown. RegisteredSensorConfig rsc = mappings.get(id); long sensorTypeId = exceptionSensor.getSensorTypeConfig().getId(); ICoreService invocCoreService = null; if (null != invocationSequenceHolder.get()) { // Need to replace the core service with the one from the invocation sequence so // that all data objects can be associated to that invocation record. invocCoreService = (ICoreService) invocationSequenceHolder.get(); } IExceptionSensorHook exceptionHook = (IExceptionSensorHook) exceptionSensor.getHook(); if (null != invocCoreService) { exceptionHook.dispatchOnThrowInBody(invocCoreService, id, sensorTypeId, object, exceptionObject, parameters, rsc); } else { exceptionHook.dispatchOnThrowInBody(coreService, id, sensorTypeId, object, exceptionObject, parameters, rsc); } } finally { executionMarker.deactive(); } } }
/** * {@inheritDoc} */ @Override public void dispatchConstructorBeforeCatch(long id, Object exceptionObject) { if (!executionMarker.isActive()) { try { executionMarker.active(); // rsc contains the settings of the actual constructor where the exception is // catched. RegisteredSensorConfig rsc = mappings.get(id); long sensorTypeId = exceptionSensor.getSensorTypeConfig().getId(); ICoreService invocCoreService = null; if (null != invocationSequenceHolder.get()) { // Need to replace the core service with the one from the invocation sequence so // that all data objects can be associated to that invocation record. invocCoreService = (ICoreService) invocationSequenceHolder.get(); } IExceptionSensorHook exceptionHook = (IExceptionSensorHook) exceptionSensor.getHook(); if (null != invocCoreService) { exceptionHook.dispatchBeforeCatchBody(invocCoreService, id, sensorTypeId, exceptionObject, rsc); } else { exceptionHook.dispatchBeforeCatchBody(coreService, id, sensorTypeId, exceptionObject, rsc); } } finally { executionMarker.deactive(); } } }
constructorHook.beforeConstructor(id, methodSensor.getSensorTypeConfig().getId(), parameters, rsc);
when(methodSensor.getHook()).thenReturn(methodHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(sensorTypeId);
@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); }
/** * {@inheritDoc} */ @Override public void dispatchFirstMethodAfterBody(long id, Object object, Object[] parameters, Object returnValue, boolean exception) { if (!executionMarker.isActive()) { try { executionMarker.active(); try { RegisteredSensorConfig rsc = mappings.get(id); // Now iterate over all registered sensor types and execute them // normal execution (sensor with highest priority first) for (IMethodSensor methodSensor : rsc.getMethodSensors()) { IMethodHook methodHook = (IMethodHook) methodSensor.getHook(); methodHook.firstAfterBody(id, methodSensor.getSensorTypeConfig().getId(), object, parameters, returnValue, exception, rsc); } } catch (Throwable throwable) { // NOPMD log.error("An error happened in the Hook Dispatcher! (after body)", throwable); } } finally { executionMarker.deactive(); } } }
@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); }
/** * {@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); } } } }