rsc.setId(sensorInstrumentationPoint.getId()); rsc.setStartsInvocation(sensorInstrumentationPoint.isStartsInvocation()); rsc.setSettings(sensorInstrumentationPoint.getSettings()); if (CollectionUtils.isNotEmpty(sensorInstrumentationPoint.getPropertyAccessorList())) { rsc.setPropertyAccessorList(new CopyOnWriteArrayList<PropertyPathStart>(sensorInstrumentationPoint.getPropertyAccessorList())); } else { rsc.setPropertyAccessorList(Collections.<PropertyPathStart> emptyList()); for (long sensorId : sensorInstrumentationPoint.getSensorIds()) { IMethodSensor sensor = methodSensorMap.get(sensorId); if (null != sensor) {
if (sensorInstrumentationPoint.isConstructor()) { return new ConstructorInstrumenter(superMethodVisitor, access, name, desc, sensorInstrumentationPoint.getId(), enhancedExceptionSensor); } else { return new MethodInstrumenter(superMethodVisitor, access, name, desc, sensorInstrumentationPoint.getId(), enhancedExceptionSensor);
@Test public void noDoubleId() { registeredSensorConfig.addSensorId(1, PriorityEnum.MIN); registeredSensorConfig.addSensorId(2, PriorityEnum.MAX); registeredSensorConfig.addSensorId(1, PriorityEnum.MAX); registeredSensorConfig.addSensorId(2, PriorityEnum.MIN); long[] sensorIds = registeredSensorConfig.getSensorIds(); assertThat(sensorIds.length, is(2)); }
@Test public void containsId() { registeredSensorConfig.addSensorId(1, PriorityEnum.MIN); registeredSensorConfig.addSensorId(2, PriorityEnum.MAX); boolean containsSensorId1 = registeredSensorConfig.containsSensorId(1); boolean containsSensorId2 = registeredSensorConfig.containsSensorId(2); assertThat(containsSensorId1, is(true)); assertThat(containsSensorId2, is(true)); } }
/** * {@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()); }
assertThat(rsc.getId(), is(methodId)); assertThat(rsc.getSensorIds().length, is(2)); assertThat(rsc.getSensorIds()[0], is(sensorId)); assertThat(rsc.getSensorIds()[1], is(invocationSensorId)); assertThat(rsc.isStartsInvocation(), is(true)); assertThat(rsc.isPropertyAccess(), is(false));
@Override public MethodVisitor answer(InvocationOnMock invocation) throws Throwable { Object[] arguments = invocation.getArguments(); SensorInstrumentationPoint sip = (SensorInstrumentationPoint) arguments[0]; return getMethodInstrumenter((MethodVisitor) arguments[1], (Integer) arguments[2], (String) arguments[3], (String) arguments[4], sip.getId(), (Boolean) arguments[5]); } };
assertThat(instrumentationConfig.getAllInstrumentationPoints(), hasSize(2)); SensorInstrumentationPoint rsc = instrumentationConfig.getSensorInstrumentationPoint(); assertThat(rsc.getId(), is(methodId)); assertThat(rsc.getSensorIds().length, is(0)); assertThat(rsc.isConstructor(), is(true)); SpecialInstrumentationPoint ssc = instrumentationConfig.getSpecialInstrumentationPoint(); assertThat(ssc.getId(), is(methodId));
assertThat(instrumentationConfig.getParameterTypes(), is(parameters)); assertThat(instrumentationConfig.getAllInstrumentationPoints(), hasSize(1)); assertThat(rsc.getId(), is(methodId)); assertThat(rsc.getSensorIds().length, is(1)); assertThat(rsc.getSensorIds()[0], is(sensorId)); assertThat(rsc.getSettings(), is(settings)); assertThat(instrumentationConfig.getSpecialInstrumentationPoint(), is(nullValue()));
@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()); }
/** * {@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); } } } }
if (containsSensorId(sensorId)) { return false;
assertThat(rsc.getId(), is(methodId)); assertThat(rsc.getSensorIds().length, is(2)); assertThat(rsc.getSensorIds()[0], is(sensorId)); assertThat(rsc.getSensorIds()[1], is(invocationSensorId)); assertThat(rsc.isStartsInvocation(), is(true)); assertThat(rsc.isPropertyAccess(), is(true)); assertThat(rsc.getPropertyAccessorList(), hasItem(propertyPathStart));
@Override public MethodVisitor answer(InvocationOnMock invocation) throws Throwable { Object[] arguments = invocation.getArguments(); SensorInstrumentationPoint sip = (SensorInstrumentationPoint) arguments[0]; return getConstructorInstrumenter((MethodVisitor) arguments[1], (Integer) arguments[2], (String) arguments[3], (String) arguments[4], sip.getId(), (Boolean) arguments[5]); } };
assertThat(instrumentationConfig.getAllInstrumentationPoints(), hasSize(2)); SensorInstrumentationPoint rsc = instrumentationConfig.getSensorInstrumentationPoint(); assertThat(rsc.getId(), is(methodId)); assertThat(rsc.getSensorIds().length, is(0)); assertThat(rsc.isConstructor(), is(false)); SpecialInstrumentationPoint ssc = instrumentationConfig.getSpecialInstrumentationPoint(); assertThat(ssc.getId(), is(methodId));
assertThat(instrumentationConfig.getParameterTypes(), is(parameters)); assertThat(instrumentationConfig.getAllInstrumentationPoints(), hasSize(1)); assertThat(rsc.getId(), is(methodId)); assertThat(rsc.getSensorIds().length, is(1)); assertThat(rsc.getSensorIds()[0], is(sensorId)); assertThat(rsc.getSettings(), is(settings)); assertThat(instrumentationConfig.getSpecialInstrumentationPoint(), is(nullValue()));
SensorInstrumentationPoint sensorInstrumentationPoint = config.getSensorInstrumentationPoint(); hookDispatcherMapper.addMapping(registeredSensorConfig.getId(), registeredSensorConfig); methodToSensorMap.put(Long.valueOf(registeredSensorConfig.getId()), sensorInstrumentationPoint.getSensorIds());
/** * {@inheritDoc} */ protected void applyAssignment(AgentConfig agentConfiguration, SensorInstrumentationPoint sensorInstrumentationPoint) { // there can be only one exception sensor so I just take the id ExceptionSensorTypeConfig exceptionSensorTypeConfig = agentConfiguration.getExceptionSensorTypeConfig(); long sensorId = exceptionSensorTypeConfig.getId(); // set to rsc sensorInstrumentationPoint.addSensorId(sensorId, exceptionSensorTypeConfig.getPriority()); // add all settings sensorInstrumentationPoint.addSettings(exceptionSensorAssignment.getSettings()); }
long rscId = 13L; long[] sensorIds = { 17L }; when(sensorInstrumentationPoint.getId()).thenReturn(rscId); when(sensorInstrumentationPoint.getSensorIds()).thenReturn(sensorIds); when(sensorInstrumentationPoint.isStartsInvocation()).thenReturn(false); when(sensorInstrumentationPoint.getSettings()).thenReturn(Collections.<String, Object> singletonMap("key", "value")); when(sensorInstrumentationPoint.getPropertyAccessorList()).thenReturn(Collections.<PropertyPathStart> emptyList()); when(methodSensorTypeConfig.getId()).thenReturn(sensorIds[0]);
@Test public void exceptionThrowerIsInstrumentedWhenConstructor() throws Exception { Object[] params = { "test" }; long constructorId = 11L; when(sip.getId()).thenReturn(constructorId); prepareConfigurationMockConstructor(config, InstrumentationExceptionTestClass.class, false, String.class); doAnswer(CONSTRUCTOR_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(EXCEPTION_TEST_CLASS_FQN); prepareWriter(cr, null, true, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); Class<?> clazz = createClass(EXCEPTION_TEST_CLASS_FQN, b); Constructor<?> constructor = clazz.getConstructor(new Class[] { String.class }); Object instance = constructor.newInstance(params); verify(hookDispatcher).dispatchConstructorBeforeBody(constructorId, params); verify(hookDispatcher).dispatchConstructorAfterBody(constructorId, instance, params); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); verify(hookDispatcher).dispatchConstructorBeforeCatch(eq(constructorId), captor.capture()); assertThat(captor.getValue().getClass().getName(), is(equalTo(MyTestException.class.getName()))); verifyNoMoreInteractions(hookDispatcher); }