/** * Removes duplicate {@link InstrumentationDefinition}s which are out-dated or would have been * overwritten by a newer {@link InstrumentationDefinition}. * * @param instrumentationDefinitions * the {@link InstrumentationDefinition}s of union * @return {@link Collection} of union {@link InstrumentationDefinition}s */ private Collection<InstrumentationDefinition> removeOutdatedInstrumentationDefinitions(List<InstrumentationDefinition> instrumentationDefinitions) { Map<String, InstrumentationDefinition> definitionMap = new HashMap<String, InstrumentationDefinition>(); for (InstrumentationDefinition iDefinition : instrumentationDefinitions) { definitionMap.put(iDefinition.getClassName(), iDefinition); } return definitionMap.values(); }
instrumentationDefinitionMap.put(definition.getClassName(), definition); classHashHelper.registerInstrumentationDefinition(definition.getClassName(), definition); log.debug("|-{} : {} (is instrumented: {})", className, classLoaderName, !instrumentationDefinitionMap.get(className).getMethodInstrumentationConfigs().isEmpty());
/** * Creates {@link InstrumentationDefinition} for the given {@link ImmutableClassType}. Returns * <code>null</code> if class has no instrumentation points. * * @param classType * {@link ImmutableClassType} to create {@link InstrumentationDefinition} for. * @return {@link InstrumentationDefinition} for this class type or <code>null</code> if class * has no instrumentation points. */ private InstrumentationDefinition createInstrumentationResult(ImmutableClassType classType) { // if there are no instrumentation points return null if (!classType.hasInstrumentationPoints()) { return null; } InstrumentationDefinition instrumentationResult = new InstrumentationDefinition(classType.getFQN()); if (classType.hasInstrumentationPoints()) { instrumentationResult.setMethodInstrumentationConfigs(classType.getInstrumentationPoints()); } return instrumentationResult; }
when(connection.isConnected()).thenReturn(true); when(connection.analyze(eq(platformId.longValue()), anyString(), classCaptor.capture())).thenReturn(instrumentationResult); when(instrumentationResult.getMethodInstrumentationConfigs()).thenReturn(Collections.singleton(methodInstrumentationConfig)); long rscId = 13L; when(sensorInstrumentationPoint.getId()).thenReturn(rscId);
if ((null == instrumentationResult) || instrumentationResult.isEmpty()) { return null; Collection<MethodInstrumentationConfig> instrumentationConfigs = instrumentationResult.getMethodInstrumentationConfigs();
/** * {@inheritDoc} */ @Override public boolean shouldAnalyzeOnStartup(String className) { if ((null != classHashHelper) && classHashHelper.isAnalyzed(className)) { InstrumentationDefinition instrumentationDefinition = classHashHelper.getInstrumentationDefinition(className); return (null != instrumentationDefinition) && !instrumentationDefinition.isEmpty(); } return true; }
log.debug("|-[X]{}", type.getFQN()); InstrumentationDefinition emptyDefinition = new InstrumentationDefinition(type.getFQN()); instrumentationDefinitions.add(emptyDefinition); } else {
when(connection.isConnected()).thenReturn(true); when(connection.analyze(eq(platformId.longValue()), anyString(), classCaptor.capture())).thenReturn(instrumentationResult); when(instrumentationResult.getMethodInstrumentationConfigs()).thenReturn(Collections.singleton(methodInstrumentationConfig)); long rscId = 13L; long[] sensorIds = { 17L };
/** * Registers the instrumentation result for the class with the given FQn. * * @param fqn * Class fully qualified name. * @param instrumentationResult * {@link InstrumentationDefinition} */ public void registerInstrumentationDefinition(String fqn, InstrumentationDefinition instrumentationResult) { ClassEntry entry = getOrCreateEntry(fqn); if ((null != instrumentationResult) && !instrumentationResult.isEmpty()) { entry.setInstrumentationResult(instrumentationResult); } else { entry.setInstrumentationResult(null); // NOPMD } }
/** * Updates the timestamp of all method idents matching the {@link InstrumentationDefinition}s in * the given message. * * @param platformId * the platform id * @param messages * all agent messages */ private void updateMethodIdentTimestamps(long platformId, List<IAgentMessage<?>> messages) { for (IAgentMessage<?> agentMessage : messages) { if (agentMessage instanceof UpdatedInstrumentationMessage) { UpdatedInstrumentationMessage message = (UpdatedInstrumentationMessage) agentMessage; for (InstrumentationDefinition definition : message.getMessageContent()) { String fqn = definition.getClassName(); int index = fqn.lastIndexOf('.'); String packageName = fqn.substring(0, index); String className = fqn.substring(index + 1, fqn.length()); registrationService.updateMethodIdentTimestamp(platformId, packageName, className); } } } }
@Test public void instrumented() { Collection<MethodInstrumentationConfig> configs = mock(Collection.class); when(classType.isInitialized()).thenReturn(true); when(classType.getFQN()).thenReturn(FQN); when(classType.hasInstrumentationPoints()).thenReturn(true); when(classType.getInstrumentationPoints()).thenReturn(configs); when(instrumentationApplier.addInstrumentationPoints(agentConfiguration, classType)).thenReturn(true); InstrumentationDefinition result = instrumentation.addAndGetInstrumentationResult(classType, agentConfiguration, Collections.singleton(instrumentationApplier)); assertThat(result, is(notNullValue())); assertThat(result.getClassName(), is(FQN)); assertThat(result.getMethodInstrumentationConfigs(), is(configs)); }
when(connection.isConnected()).thenReturn(true); when(connection.analyze(eq(platformId.longValue()), anyString(), classCaptor.capture())).thenReturn(instrumentationResult); when(instrumentationResult.getMethodInstrumentationConfigs()).thenReturn(Collections.singleton(methodInstrumentationConfig)); long sscId = 13L; long sensorId = 17L;
@Test @SuppressWarnings("unchecked") public void changeEventNoAgentStatusData() throws Exception { when(definitionOne.getClassName()).thenReturn("class.one"); ClassInstrumentationChangedEvent eventOne = new ClassInstrumentationChangedEvent(this, 10L, Arrays.asList(definitionOne)); when(agentStatusDataProvider.getAgentStatusDataMap()).thenReturn(Collections.EMPTY_MAP); messageGate.onApplicationEvent(eventOne); verify(agentStatusDataProvider).getAgentStatusDataMap(); verifyNoMoreInteractions(agentStatusDataProvider); verifyZeroInteractions(statusData, messageProvider); }
@Test public void collect() throws Exception { Collection<MethodInstrumentationConfig> configs = mock(Collection.class); when(classType.isClass()).thenReturn(true); when(classType.castToClass()).thenReturn(classType); when(classType.isInitialized()).thenReturn(true); when(classType.getFQN()).thenReturn(FQN); when(classType.hasInstrumentationPoints()).thenReturn(true); when(classType.getInstrumentationPoints()).thenReturn(configs); doReturn(Collections.singleton(classType)).when(lookup).findAll(); Collection<InstrumentationDefinition> result = instrumentation.getInstrumentationResults(); // assert result assertThat(result, hasSize(1)); InstrumentationDefinition instrumentationResult = result.iterator().next(); assertThat(instrumentationResult.getClassName(), is(FQN)); assertThat(instrumentationResult.getMethodInstrumentationConfigs(), is(configs)); // read lock is enough verify(classCache, times(1)).executeWithReadLock(Matchers.<Callable<?>> any()); }
when(classHashHelper.getInstrumentationDefinition(fqnCaptor.capture())).thenReturn(instrumentationResult); when(instrumentationResult.getMethodInstrumentationConfigs()).thenReturn(Collections.singleton(methodInstrumentationConfig)); long rscId = 13L; long[] sensorIds = { 17L };
/** * Handles an event of type {@link ClassInstrumentationChangedEvent}. * * @param event * the event instance */ private void handleClassInstrumentationChangedEvent(ClassInstrumentationChangedEvent event) { if (log.isDebugEnabled()) { log.debug("Putting instrumentation definitions for agent {} into the definition buffer.", event.getAgentId()); } Map<String, InstrumentationDefinition> pendingDefinitions = definitionBuffer.get(event.getAgentId()); if (pendingDefinitions == null) { pendingDefinitions = new HashMap<>(); definitionBuffer.put(event.getAgentId(), pendingDefinitions); } for (InstrumentationDefinition definition : event.getInstrumentationDefinitions()) { pendingDefinitions.put(definition.getClassName(), definition); } AgentStatusData agentStatusData = agentStatusDataProvider.getAgentStatusDataMap().get(event.getAgentId()); if (agentStatusData != null) { if (agentStatusData.getInstrumentationStatus() != InstrumentationStatus.PENDING) { agentStatusData.setInstrumentationStatus(InstrumentationStatus.PENDING); agentStatusData.setPendingSinceTime(System.currentTimeMillis()); } } }
@Test public void collect() throws Exception { Collection<MethodInstrumentationConfig> configs = mock(Collection.class); Set<String> hashes = mock(Set.class); when(classType.isClass()).thenReturn(true); when(classType.castToClass()).thenReturn(classType); when(classType.isInitialized()).thenReturn(true); when(classType.getFQN()).thenReturn(FQN); when(classType.hasInstrumentationPoints()).thenReturn(true); when(classType.getInstrumentationPoints()).thenReturn(configs); when(classType.getHashes()).thenReturn(hashes); doReturn(Collections.singleton(classType)).when(lookup).findAll(); Map<Collection<String>, InstrumentationDefinition> result = instrumentation.getInstrumentationResultsWithHashes(); // assert result assertThat(result.size(), is(1)); Entry<Collection<String>, InstrumentationDefinition> entry = result.entrySet().iterator().next(); assertThat((Set<String>) entry.getKey(), is(hashes)); assertThat(entry.getValue().getClassName(), is(FQN)); assertThat(entry.getValue().getMethodInstrumentationConfigs(), is(configs)); // read lock is enough verify(classCache, times(1)).executeWithReadLock(Matchers.<Callable<?>> any()); }
when(connection.isConnected()).thenReturn(true); when(connection.analyze(eq(platformId.longValue()), anyString(), classCaptor.capture())).thenReturn(instrumentationResult); when(instrumentationResult.getMethodInstrumentationConfigs()).thenReturn(Collections.singleton(methodInstrumentationConfig)); long rscId = 13L; long[] sensorIds = { 17L };
@Test public void unknownPlatformId() throws Exception { when(definition.getClassName()).thenReturn("class.one"); ClassInstrumentationChangedEvent event = new ClassInstrumentationChangedEvent(this, 10L, Arrays.asList(definition)); messageGate.onApplicationEvent(event); messageGate.clear(20L); verifyZeroInteractions(messageProvider); assertThat(getDefinitionBuffer().get(10L).entrySet(), hasSize(1)); } }
when(connection.isConnected()).thenReturn(true); when(connection.analyze(eq(platformId.longValue()), anyString(), classCaptor.capture())).thenReturn(instrumentationResult); when(instrumentationResult.getMethodInstrumentationConfigs()).thenReturn(Collections.singleton(methodInstrumentationConfig)); long rscId = 13L; long[] sensorIds = { 17L };