diff --git a/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/udp/SpanStreamUDPSenderTest.java b/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/udp/SpanStreamUDPSenderTest.java index c2464836c..2a10f67a4 100644 --- a/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/udp/SpanStreamUDPSenderTest.java +++ b/collector/src/test/java/com/navercorp/pinpoint/collector/receiver/udp/SpanStreamUDPSenderTest.java @@ -25,6 +25,7 @@ import com.navercorp.pinpoint.common.trace.ServiceType; import com.navercorp.pinpoint.common.util.JvmUtils; import com.navercorp.pinpoint.common.util.SystemPropertyKey; import com.navercorp.pinpoint.profiler.AgentInformation; +import com.navercorp.pinpoint.profiler.DefaultAgentInformation; import com.navercorp.pinpoint.profiler.context.Span; import com.navercorp.pinpoint.profiler.context.SpanChunk; import com.navercorp.pinpoint.profiler.context.SpanChunkFactory; @@ -141,7 +142,7 @@ public class SpanStreamUDPSenderTest { } private Span createSpan(int spanEventSize) throws InterruptedException { - AgentInformation agentInformation = new AgentInformation("agentId", "applicationName", 0, 0, "machineName", "127.0.0.1", ServiceType.STAND_ALONE, + AgentInformation agentInformation = new DefaultAgentInformation("agentId", "applicationName", 0, 0, "machineName", "127.0.0.1", ServiceType.STAND_ALONE, JvmUtils.getSystemProperty(SystemPropertyKey.JAVA_VERSION), Version.VERSION); SpanChunkFactory spanChunkFactory = new SpanChunkFactory(agentInformation); @@ -157,7 +158,7 @@ public class SpanStreamUDPSenderTest { } private SpanChunk createSpanChunk(int spanEventSize) throws InterruptedException { - AgentInformation agentInformation = new AgentInformation("agentId", "applicationName", 0, 0, "machineName", "127.0.0.1", ServiceType.STAND_ALONE, + AgentInformation agentInformation = new DefaultAgentInformation("agentId", "applicationName", 0, 0, "machineName", "127.0.0.1", ServiceType.STAND_ALONE, JvmUtils.getSystemProperty(SystemPropertyKey.JAVA_VERSION), Version.VERSION); SpanChunkFactory spanChunkFactory = new SpanChunkFactory(agentInformation); diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockApiMetaDataService.java b/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockApiMetaDataService.java deleted file mode 100644 index 80461dbda..000000000 --- a/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockApiMetaDataService.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright 2017 NAVER Corp. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.navercorp.pinpoint.test; - -import com.google.common.collect.BiMap; -import com.google.common.collect.HashBiMap; -import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor; -import com.navercorp.pinpoint.profiler.metadata.ApiMetaDataService; - -import java.io.PrintStream; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.Map; - -/** - * @author Woonduk Kang(emeroad) - */ -public class MockApiMetaDataService implements ApiMetaDataService { - private static final Comparator> COMPARATOR = new Comparator>() { - - @Override - public int compare(Map.Entry o1, Map.Entry o2) { - return o1.getKey() > o2.getKey() ? 1 : (o1.getKey() < o2.getKey() ? -1 : 0); - } - - }; - - private final BiMap apiIdMap = HashBiMap.create(); - - private static final int INITIAL_ID = 1; - private int nextId = INITIAL_ID; - - - public MockApiMetaDataService() { - } - - - @Override - public int cacheApi(MethodDescriptor methodDescriptor) { - int apiId1 = methodDescriptor.getApiId(); - - final String apiDescriptor = MethodDescriptionUtils.toJavaMethodDescriptor(methodDescriptor.getApiDescriptor()); - MethodInfo methodInfo = new MethodInfo(apiDescriptor, methodDescriptor); - synchronized (this.apiIdMap) { - final MethodInfo exist = this.apiIdMap.get(methodInfo); - if (exist != null) { - return exist.getMethodDescriptor().getApiId(); - } - - final int apiId = nextId(); - this.apiIdMap.put(apiId, methodInfo); - return apiId; - } - } - - private class MethodInfo { - private final String apiDescriptor; - private final MethodDescriptor methodDescriptor; - - public MethodInfo(String apiDescriptor, MethodDescriptor methodDescriptor) { - if (apiDescriptor == null) { - throw new NullPointerException("apiDescriptor must not be null"); - } - this.apiDescriptor = apiDescriptor; - this.methodDescriptor = methodDescriptor; - } - - public String getApiDescriptor() { - return apiDescriptor; - } - - public MethodDescriptor getMethodDescriptor() { - return methodDescriptor; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - MethodInfo that = (MethodInfo) o; - - return apiDescriptor != null ? apiDescriptor.equals(that.apiDescriptor) : that.apiDescriptor == null; - } - - @Override - public int hashCode() { - return apiDescriptor != null ? apiDescriptor.hashCode() : 0; - } - - @Override - public String toString() { - return "MethodInfo{" + - "apiDescriptor='" + apiDescriptor + '\'' + - ", methodDescriptor=" + methodDescriptor + - '}'; - } - } - - private int nextId() { - synchronized (apiIdMap) { - return nextId++; - } - } - - public int getApiId(String methodDescriptor) { - - MethodInfo key = new MethodInfo(methodDescriptor, null); - synchronized (this.apiIdMap) { - BiMap apiIdMap = this.apiIdMap.inverse(); - final Integer id = apiIdMap.get(key); - if (id == null) { - throw new NullPointerException("apiMetaDataNotFound " + key); - } - System.out.println("getApiId " + methodDescriptor + " id:" + id); - return id; - } - } - - public void clear() { - synchronized (apiIdMap) { - this.apiIdMap.clear(); - nextId = INITIAL_ID; - } - } - - public void print(PrintStream out) { - out.println("API(" + apiIdMap.size() + "):"); - printApis(out); - - } - - public void printApis(PrintStream out) { - synchronized (this.apiIdMap) { - List> apis = new ArrayList>(apiIdMap.entrySet()); - printEntries(out, apis); - } - } - - - private void printEntries(PrintStream out, List> entries) { - Collections.sort(entries, COMPARATOR); - - for (Map.Entry e : entries) { - MethodInfo methodInfo = e.getValue(); - out.println(e.getKey() + ": " + methodInfo.getApiDescriptor()); - } - } -} diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockApplicationContext.java b/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockApplicationContext.java index 077244dbe..5c3962242 100644 --- a/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockApplicationContext.java +++ b/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockApplicationContext.java @@ -19,42 +19,24 @@ package com.navercorp.pinpoint.test; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URL; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import com.google.inject.Module; +import com.google.inject.util.Modules; import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig; -import com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext; -import com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformTemplate; -import com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformTemplateAware; -import com.navercorp.pinpoint.profiler.AgentInformation; import com.navercorp.pinpoint.profiler.context.DefaultApplicationContext; -import com.navercorp.pinpoint.profiler.context.provider.Provider; -import com.navercorp.pinpoint.profiler.plugin.GuardProfilerPluginContext; -import com.navercorp.pinpoint.profiler.receiver.CommandDispatcher; -import com.navercorp.pinpoint.rpc.client.PinpointClient; -import com.navercorp.pinpoint.rpc.client.PinpointClientFactory; -import org.apache.thrift.TBase; import com.navercorp.pinpoint.bootstrap.AgentOption; import com.navercorp.pinpoint.bootstrap.DefaultAgentOption; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; -import com.navercorp.pinpoint.bootstrap.context.ServerMetaDataHolder; -import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPlugin; import com.navercorp.pinpoint.bootstrap.plugin.test.ExpectedAnnotation; -import com.navercorp.pinpoint.common.plugin.PluginLoader; import com.navercorp.pinpoint.common.service.DefaultAnnotationKeyRegistryService; import com.navercorp.pinpoint.common.service.DefaultServiceTypeRegistryService; import com.navercorp.pinpoint.common.trace.ServiceType; import com.navercorp.pinpoint.profiler.context.Span; import com.navercorp.pinpoint.profiler.context.SpanEvent; -import com.navercorp.pinpoint.profiler.context.storage.StorageFactory; -import com.navercorp.pinpoint.profiler.instrument.ClassInjector; import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder; -import com.navercorp.pinpoint.profiler.plugin.DefaultProfilerPluginContext; -import com.navercorp.pinpoint.profiler.sender.DataSender; -import com.navercorp.pinpoint.profiler.sender.EnhancedDataSender; -import com.navercorp.pinpoint.profiler.util.RuntimeMXBeanUtils; import com.navercorp.pinpoint.thrift.dto.TAnnotation; /** @@ -68,7 +50,7 @@ public class MockApplicationContext extends DefaultApplicationContext { public static MockApplicationContext of(String configPath) { ProfilerConfig profilerConfig = null; try { - URL resource = MockApplicationContext.class.getClassLoader().getResource(configPath); + final URL resource = MockApplicationContext.class.getClassLoader().getResource(configPath); if (resource == null) { throw new FileNotFoundException("pinpoint.config not found. configPath:" + configPath); } @@ -83,85 +65,28 @@ public class MockApplicationContext extends DefaultApplicationContext { public static MockApplicationContext of(ProfilerConfig config) { AgentOption agentOption = new DefaultAgentOption(new DummyInstrumentation(), "mockAgent", "mockApplicationName", config, new URL[0], null, new DefaultServiceTypeRegistryService(), new DefaultAnnotationKeyRegistryService()); InterceptorRegistryBinder binder = new TestInterceptorRegistryBinder(); + binder.bind(); - return new MockApplicationContext(agentOption, binder); } + public MockApplicationContext(AgentOption agentOption, InterceptorRegistryBinder binder) { super(agentOption, binder); this.interceptorRegistryBinder = binder; - binder.bind(); } @Override - protected Provider newUdpStatDataSenderProvider() { + protected Module newApplicationContextModule(AgentOption agentOption, InterceptorRegistryBinder interceptorRegistryBinder) { + Module applicationContextModule = super.newApplicationContextModule(agentOption, interceptorRegistryBinder); + MockApplicationContextModule mockApplicationContextModule = new MockApplicationContextModule(); - DataSender dataSender = new ListenableDataSender>("StatDataSender"); - return new DelegateProvider(dataSender); - } - - @Override - protected Provider newUdpSpanDataSenderProvider() { - DataSender dataSender = new ListenableDataSender>("SpanDataSender"); - return new DelegateProvider(dataSender); + return Modules.override(applicationContextModule).with(mockApplicationContextModule); } - @Override - protected Provider newStorageFactoryProvider(ProfilerConfig profilerConfig, DataSender spanDataSender, AgentInformation agentInformation) { - StorageFactory storageFactory = new SimpleSpanStorageFactory(spanDataSender); - return new DelegateProvider(storageFactory); - } - @Override - protected Provider newPinpointClientFactoryProvider(ProfilerConfig profilerConfig, AgentInformation agentInformation, CommandDispatcher commandDispatcher) { - return new NullProvider(); - } - - @Override - protected Provider newPinpointClientProvider(ProfilerConfig profilerConfig, PinpointClientFactory clientFactory) { - return new NullProvider(); - } - - @Override - protected Provider newTcpDataSenderProvider(PinpointClient client) { - EnhancedDataSender enhancedDataSender = new TestTcpDataSender(); - return new DelegateProvider(enhancedDataSender); - } - - - @Override - protected Provider newServerMetaDataHolderProvider() { - List vmArgs = RuntimeMXBeanUtils.getVmArgs(); - ServerMetaDataHolder serverMetaDataHolder = new ResettableServerMetaDataHolder(vmArgs); - return new DelegateProvider(serverMetaDataHolder); - } - - @Override - protected List loadPlugins(AgentOption agentOption) { - List pluginContexts = new ArrayList(); - ClassInjector classInjector = new TestProfilerPluginClassLoader(); - - List plugins = PluginLoader.load(ProfilerPlugin.class, ClassLoader.getSystemClassLoader()); - - for (ProfilerPlugin plugin : plugins) { - final DefaultProfilerPluginContext context = new DefaultProfilerPluginContext(this, classInjector); - final GuardProfilerPluginContext guard = new GuardProfilerPluginContext(context); - try { - preparePlugin(plugin, context); - plugin.setup(guard); - } finally { - guard.close(); - } - pluginContexts.add(context); - } - - - return pluginContexts; - - } @Override public void close() { @@ -171,18 +96,7 @@ public class MockApplicationContext extends DefaultApplicationContext { } } - /** - * TODO duplicated code : com/navercorp/pinpoint/profiler/plugin/ProfilerPluginLoader.java - * @param plugin - * @param context - */ - private void preparePlugin(ProfilerPlugin plugin, InstrumentContext context) { - if (plugin instanceof TransformTemplateAware) { - final TransformTemplate transformTemplate = new TransformTemplate(context); - ((TransformTemplateAware) plugin).setTransformTemplate(transformTemplate); - } - } public static String toString(Span span) { diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockApplicationContextModule.java b/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockApplicationContextModule.java new file mode 100644 index 000000000..709f874c7 --- /dev/null +++ b/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockApplicationContextModule.java @@ -0,0 +1,108 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.test; + +import com.google.inject.AbstractModule; +import com.google.inject.Scopes; +import com.navercorp.pinpoint.bootstrap.context.ServerMetaDataHolder; +import com.navercorp.pinpoint.profiler.context.module.SpanDataSender; +import com.navercorp.pinpoint.profiler.context.module.StatDataSender; +import com.navercorp.pinpoint.profiler.context.storage.StorageFactory; +import com.navercorp.pinpoint.profiler.plugin.PluginContextLoadResult; +import com.navercorp.pinpoint.profiler.plugin.PluginSetup; +import com.navercorp.pinpoint.profiler.sender.DataSender; +import com.navercorp.pinpoint.profiler.sender.EnhancedDataSender; +import com.navercorp.pinpoint.profiler.util.RuntimeMXBeanUtils; +import com.navercorp.pinpoint.rpc.client.PinpointClient; +import com.navercorp.pinpoint.rpc.client.PinpointClientFactory; +import com.navercorp.pinpoint.test.provder.NullPinpointClientFactoryProvider; +import com.navercorp.pinpoint.test.provder.NullPinpointClientProvider; +import org.apache.thrift.TBase; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/** + * @author Woonduk Kang(emeroad) + */ +public class MockApplicationContextModule extends AbstractModule { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + public MockApplicationContextModule() { + } + + @Override + protected void configure() { + + final DataSender spanDataSender = newUdpSpanDataSender(); + logger.debug("spanDataSender:{}", spanDataSender); + bind(DataSender.class).annotatedWith(SpanDataSender.class).toInstance(spanDataSender); + + final DataSender statDataSender = newUdpStatDataSender(); + logger.debug("statDataSender:{}", statDataSender); + bind(DataSender.class).annotatedWith(StatDataSender.class).toInstance(statDataSender); + + StorageFactory storageFactory = newStorageFactory(spanDataSender); + logger.debug("spanFactory:{}", spanDataSender); + bind(StorageFactory.class).toInstance(storageFactory); + + bind(PinpointClientFactory.class).toProvider(NullPinpointClientFactoryProvider.class); + bind(PinpointClient.class).toProvider(NullPinpointClientProvider.class); + + EnhancedDataSender enhancedDataSender = newTcpDataSender(); + logger.debug("enhancedDataSender:{}", enhancedDataSender); + bind(EnhancedDataSender.class).toInstance(enhancedDataSender); + + ServerMetaDataHolder serverMetaDataHolder = newServerMetaDataHolder(); + logger.debug("serverMetaDataHolder:{}", serverMetaDataHolder); + bind(ServerMetaDataHolder.class).toInstance(serverMetaDataHolder); + + + bind(PluginSetup.class).to(MockPluginSetup.class).in(Scopes.SINGLETON); + bind(PluginContextLoadResult.class).toProvider(MockPluginContextLoadResult.class).in(Scopes.SINGLETON); + } + + + protected DataSender newUdpStatDataSender() { + DataSender dataSender = new ListenableDataSender>("StatDataSender"); + return dataSender; + } + + + protected DataSender newUdpSpanDataSender() { + DataSender dataSender = new ListenableDataSender>("SpanDataSender"); + return dataSender; + } + + protected EnhancedDataSender newTcpDataSender() { + return new TestTcpDataSender(); + } + + protected StorageFactory newStorageFactory(DataSender spanDataSender) { + logger.debug("newStorageFactory dataSender:{}", spanDataSender); + StorageFactory storageFactory = new SimpleSpanStorageFactory(spanDataSender); + return storageFactory; + } + + protected ServerMetaDataHolder newServerMetaDataHolder() { + List vmArgs = RuntimeMXBeanUtils.getVmArgs(); + ServerMetaDataHolder serverMetaDataHolder = new ResettableServerMetaDataHolder(vmArgs); + return serverMetaDataHolder; + } +} diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockPluginContextLoadResult.java b/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockPluginContextLoadResult.java new file mode 100644 index 000000000..33517e295 --- /dev/null +++ b/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockPluginContextLoadResult.java @@ -0,0 +1,63 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.test; + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPlugin; +import com.navercorp.pinpoint.common.plugin.PluginLoader; +import com.navercorp.pinpoint.profiler.instrument.ClassInjector; +import com.navercorp.pinpoint.profiler.plugin.DefaultPluginContextLoadResult; +import com.navercorp.pinpoint.profiler.plugin.DefaultProfilerPluginContext; +import com.navercorp.pinpoint.profiler.plugin.PluginContextLoadResult; +import com.navercorp.pinpoint.profiler.plugin.PluginSetup; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Woonduk Kang(emeroad) + */ +public class MockPluginContextLoadResult implements Provider { + + + private PluginSetup pluginSetup; + + @Inject + public MockPluginContextLoadResult(PluginSetup pluginSetup) { + + this.pluginSetup = pluginSetup; + } + + @Override + public PluginContextLoadResult get() { + List pluginContexts = new ArrayList(); + ClassInjector classInjector = new TestProfilerPluginClassLoader(); + + List plugins = PluginLoader.load(ProfilerPlugin.class, ClassLoader.getSystemClassLoader()); + + for (ProfilerPlugin plugin : plugins) { + DefaultProfilerPluginContext context = pluginSetup.setupPlugin(plugin, classInjector); + pluginContexts.add(context); + } + + + return new DefaultPluginContextLoadResult(pluginContexts); + } + + +} diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockPluginSetup.java b/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockPluginSetup.java new file mode 100644 index 000000000..d58ddb15c --- /dev/null +++ b/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockPluginSetup.java @@ -0,0 +1,69 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.test; + +import com.google.inject.Inject; +import com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext; +import com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformTemplate; +import com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformTemplateAware; +import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPlugin; +import com.navercorp.pinpoint.profiler.context.ApplicationContext; +import com.navercorp.pinpoint.profiler.instrument.ClassInjector; +import com.navercorp.pinpoint.profiler.plugin.DefaultProfilerPluginContext; +import com.navercorp.pinpoint.profiler.plugin.GuardProfilerPluginContext; +import com.navercorp.pinpoint.profiler.plugin.PluginSetup; + +/** + * @author Woonduk Kang(emeroad) + */ +public class MockPluginSetup implements PluginSetup { + + + private final ApplicationContext applicationContext; + + @Inject + public MockPluginSetup(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + @Override + public DefaultProfilerPluginContext setupPlugin(ProfilerPlugin plugin, ClassInjector classInjector) { + final DefaultProfilerPluginContext context = new DefaultProfilerPluginContext(applicationContext, classInjector); + + final GuardProfilerPluginContext guard = new GuardProfilerPluginContext(context); + try { + preparePlugin(plugin, context); + plugin.setup(guard); + } finally { + guard.close(); + } + return context; + } + + /** + * TODO duplicated code : com/navercorp/pinpoint/profiler/plugin/ProfilerPluginLoader.java + * @param plugin + * @param context + */ + private void preparePlugin(ProfilerPlugin plugin, InstrumentContext context) { + + if (plugin instanceof TransformTemplateAware) { + final TransformTemplate transformTemplate = new TransformTemplate(context); + ((TransformTemplateAware) plugin).setTransformTemplate(transformTemplate); + } + } +} diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockTraceContextFactory.java b/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockTraceContextFactory.java index 534034291..cab1171e7 100644 --- a/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockTraceContextFactory.java +++ b/profiler-test/src/main/java/com/navercorp/pinpoint/test/MockTraceContextFactory.java @@ -21,19 +21,23 @@ import com.navercorp.pinpoint.bootstrap.context.ServerMetaDataHolder; import com.navercorp.pinpoint.bootstrap.context.TraceContext; import com.navercorp.pinpoint.bootstrap.sampler.Sampler; import com.navercorp.pinpoint.profiler.AgentInformation; +import com.navercorp.pinpoint.profiler.context.AtomicIdGenerator; import com.navercorp.pinpoint.profiler.context.DefaultServerMetaDataHolder; import com.navercorp.pinpoint.profiler.context.DefaultTraceContext; import com.navercorp.pinpoint.profiler.context.DefaultTraceFactoryBuilder; import com.navercorp.pinpoint.profiler.context.IdGenerator; -import com.navercorp.pinpoint.profiler.context.PluginMonitorContextBuilder; +import com.navercorp.pinpoint.profiler.context.monitor.DefaultPluginMonitorContext; import com.navercorp.pinpoint.profiler.context.TraceFactoryBuilder; import com.navercorp.pinpoint.profiler.context.active.ActiveTraceRepository; import com.navercorp.pinpoint.profiler.context.monitor.PluginMonitorContext; import com.navercorp.pinpoint.profiler.context.storage.LogStorageFactory; import com.navercorp.pinpoint.profiler.context.storage.StorageFactory; import com.navercorp.pinpoint.profiler.metadata.ApiMetaDataCacheService; +import com.navercorp.pinpoint.profiler.metadata.ApiMetaDataService; import com.navercorp.pinpoint.profiler.metadata.SqlMetaDataCacheService; +import com.navercorp.pinpoint.profiler.metadata.SqlMetaDataService; import com.navercorp.pinpoint.profiler.metadata.StringMetaDataCacheService; +import com.navercorp.pinpoint.profiler.metadata.StringMetaDataService; import com.navercorp.pinpoint.profiler.sampler.SamplerFactory; import com.navercorp.pinpoint.profiler.sender.EnhancedDataSender; import com.navercorp.pinpoint.profiler.sender.LoggingDataSender; @@ -52,7 +56,7 @@ public class MockTraceContextFactory { private final StorageFactory storageFactory; - private final IdGenerator idGenerator; + private final AtomicIdGenerator idGenerator; private final Sampler sampler; private final ActiveTraceRepository activeTraceRepository; @@ -89,12 +93,11 @@ public class MockTraceContextFactory { final SamplerFactory samplerFactory = new SamplerFactory(); this.sampler = createSampler(profilerConfig, samplerFactory); - this.idGenerator = new IdGenerator(); + this.idGenerator = new AtomicIdGenerator(); this.activeTraceRepository = newActiveTraceRepository(); final TraceFactoryBuilder traceFactoryBuilder = new DefaultTraceFactoryBuilder(storageFactory, sampler, idGenerator, activeTraceRepository); - final PluginMonitorContextBuilder pluginMonitorContextBuilder = new PluginMonitorContextBuilder(TRACE_DATASOURCE); - this.pluginMonitorContext = pluginMonitorContextBuilder.build(); + this.pluginMonitorContext = new DefaultPluginMonitorContext(); this.serverMetaDataHolder = new DefaultServerMetaDataHolder(RuntimeMXBeanUtils.getVmArgs()); diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/PluginApplicationContextModule.java b/profiler-test/src/main/java/com/navercorp/pinpoint/test/PluginApplicationContextModule.java new file mode 100644 index 000000000..4f0e2bd3c --- /dev/null +++ b/profiler-test/src/main/java/com/navercorp/pinpoint/test/PluginApplicationContextModule.java @@ -0,0 +1,127 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.test; + +import com.google.inject.AbstractModule; +import com.navercorp.pinpoint.bootstrap.context.ServerMetaDataHolder; +import com.navercorp.pinpoint.profiler.context.module.SpanDataSender; +import com.navercorp.pinpoint.profiler.context.module.StatDataSender; +import com.navercorp.pinpoint.profiler.context.storage.StorageFactory; +import com.navercorp.pinpoint.profiler.sender.DataSender; +import com.navercorp.pinpoint.profiler.sender.EnhancedDataSender; +import com.navercorp.pinpoint.profiler.util.RuntimeMXBeanUtils; +import com.navercorp.pinpoint.rpc.client.PinpointClient; +import com.navercorp.pinpoint.rpc.client.PinpointClientFactory; +import com.navercorp.pinpoint.test.provder.NullPinpointClientFactoryProvider; +import com.navercorp.pinpoint.test.provder.NullPinpointClientProvider; +import org.apache.thrift.TBase; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/** + * @author Woonduk Kang(emeroad) + */ +public class PluginApplicationContextModule extends AbstractModule { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + private TestableServerMetaDataListener serverMetaDataListener; + private TestTcpDataSender tcpDataSender; + private OrderedSpanRecorder orderedSpanRecorder; + private ServerMetaDataHolder serverMetaDataHolder; + + public PluginApplicationContextModule() { + } + + @Override + protected void configure() { + + final DataSender spanDataSender = newUdpSpanDataSender(); + logger.debug("spanDataSender:{}", spanDataSender); + bind(DataSender.class).annotatedWith(SpanDataSender.class).toInstance(spanDataSender); + + final DataSender statDataSender = newUdpStatDataSender(); + logger.debug("statDataSender:{}", statDataSender); + bind(DataSender.class).annotatedWith(StatDataSender.class).toInstance(statDataSender); + + StorageFactory storageFactory = newStorageFactory(spanDataSender); + logger.debug("spanFactory:{}", spanDataSender); + bind(StorageFactory.class).toInstance(storageFactory); + + bind(PinpointClientFactory.class).toProvider(NullPinpointClientFactoryProvider.class); + bind(PinpointClient.class).toProvider(NullPinpointClientProvider.class); + + EnhancedDataSender enhancedDataSender = newTcpDataSender(); + logger.debug("enhancedDataSender:{}", enhancedDataSender); + bind(EnhancedDataSender.class).toInstance(enhancedDataSender); + + ServerMetaDataHolder serverMetaDataHolder = newServerMetaDataHolder(); + logger.debug("serverMetaDataHolder:{}", serverMetaDataHolder); + bind(ServerMetaDataHolder.class).toInstance(serverMetaDataHolder); + + } + + + private DataSender newUdpStatDataSender() { + return new ListenableDataSender>("StatDataSender"); + } + + private DataSender newUdpSpanDataSender() { + + ListenableDataSender> sender = new ListenableDataSender>("SpanDataSender"); + OrderedSpanRecorder orderedSpanRecorder = new OrderedSpanRecorder(); + sender.setListener(orderedSpanRecorder); + this.orderedSpanRecorder = orderedSpanRecorder; + return sender; + } + + protected EnhancedDataSender newTcpDataSender() { + TestTcpDataSender tcpDataSender = new TestTcpDataSender(); + this.tcpDataSender = tcpDataSender; + return tcpDataSender; + } + + + private ServerMetaDataHolder newServerMetaDataHolder() { + List vmArgs = RuntimeMXBeanUtils.getVmArgs(); + ServerMetaDataHolder serverMetaDataHolder = new ResettableServerMetaDataHolder(vmArgs); + this.serverMetaDataListener = new TestableServerMetaDataListener(); + this.serverMetaDataHolder = serverMetaDataHolder; + serverMetaDataHolder.addListener(this.serverMetaDataListener); + return serverMetaDataHolder; + } + + protected StorageFactory newStorageFactory(DataSender spanDataSender) { + logger.debug("newStorageFactory dataSender:{}", spanDataSender); + StorageFactory storageFactory = new SimpleSpanStorageFactory(spanDataSender); + return storageFactory; + } + + public TestableServerMetaDataListener getServerMetaDataListener() { + return serverMetaDataListener; + } + + public TestTcpDataSender getTcpDataSender() { + return tcpDataSender; + } + + public OrderedSpanRecorder getOrderedSpanRecorder() { + return orderedSpanRecorder; + } +} diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/PluginTestAgent.java b/profiler-test/src/main/java/com/navercorp/pinpoint/test/PluginTestAgent.java index d54ff24c6..8f3435963 100644 --- a/profiler-test/src/main/java/com/navercorp/pinpoint/test/PluginTestAgent.java +++ b/profiler-test/src/main/java/com/navercorp/pinpoint/test/PluginTestAgent.java @@ -27,21 +27,16 @@ import java.util.List; import java.util.Map; import java.util.NoSuchElementException; -import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.google.inject.Module; +import com.google.inject.util.Modules; import com.navercorp.pinpoint.bootstrap.context.TraceContext; import com.navercorp.pinpoint.common.util.AnnotationKeyUtils; -import com.navercorp.pinpoint.profiler.AgentInformation; import com.navercorp.pinpoint.profiler.context.ApplicationContext; import com.navercorp.pinpoint.profiler.context.DefaultApplicationContext; -import com.navercorp.pinpoint.profiler.context.provider.Provider; import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder; -import com.navercorp.pinpoint.rpc.client.PinpointClient; -import com.navercorp.pinpoint.rpc.client.PinpointClientFactory; -import org.apache.thrift.TBase; import com.google.common.base.Objects; import com.navercorp.pinpoint.bootstrap.AgentOption; -import com.navercorp.pinpoint.bootstrap.context.ServerMetaDataHolder; import com.navercorp.pinpoint.bootstrap.context.ServiceInfo; import com.navercorp.pinpoint.bootstrap.plugin.test.Expectations; import com.navercorp.pinpoint.bootstrap.plugin.test.ExpectedAnnotation; @@ -57,12 +52,8 @@ import com.navercorp.pinpoint.common.trace.ServiceType; import com.navercorp.pinpoint.profiler.DefaultAgent; import com.navercorp.pinpoint.profiler.context.Span; import com.navercorp.pinpoint.profiler.context.SpanEvent; -import com.navercorp.pinpoint.profiler.context.storage.StorageFactory; import com.navercorp.pinpoint.profiler.interceptor.registry.DefaultInterceptorRegistryBinder; -import com.navercorp.pinpoint.profiler.sender.DataSender; -import com.navercorp.pinpoint.profiler.sender.EnhancedDataSender; import com.navercorp.pinpoint.profiler.util.JavaAssistUtils; -import com.navercorp.pinpoint.profiler.util.RuntimeMXBeanUtils; import com.navercorp.pinpoint.thrift.dto.TAnnotation; import com.navercorp.pinpoint.thrift.dto.TIntStringStringValue; import com.navercorp.pinpoint.thrift.dto.TSpan; @@ -76,13 +67,12 @@ import com.navercorp.pinpoint.thrift.dto.TSpanEvent; */ public class PluginTestAgent extends DefaultAgent implements PluginTestVerifier { - private TestableServerMetaDataListener serverMetaDataListener; + private AnnotationKeyRegistryService annotationKeyRegistryService; private final List ignoredServiceTypes = new ArrayList(); - private TestTcpDataSender tcpDataSender; - private OrderedSpanRecorder orderedSpanRecorder; + private PluginApplicationContextModule pluginApplicationContextModule; public PluginTestAgent(AgentOption agentOption) { @@ -93,50 +83,17 @@ public class PluginTestAgent extends DefaultAgent implements PluginTestVerifier @Override protected ApplicationContext newApplicationContext(AgentOption agentOption, InterceptorRegistryBinder interceptorRegistryBinder) { - final DataSender spanDataSender = createUdpSpanDataSender(); - final DataSender statDataSender = createUdpStatDataSender(); - final ServerMetaDataHolder serverMetaDataHolder = createServerMetaDataHolder(); - final EnhancedDataSender tcpDataSender = createTcpDataSender(); + this.pluginApplicationContextModule = new PluginApplicationContextModule(); ApplicationContext applicationContext = new DefaultApplicationContext(agentOption, interceptorRegistryBinder) { - @Override - protected Provider newUdpSpanDataSenderProvider() { - return new DelegateProvider(spanDataSender); - } @Override - protected Provider newUdpStatDataSenderProvider() { - return new DelegateProvider(statDataSender); - } + protected Module newApplicationContextModule(AgentOption agentOption, InterceptorRegistryBinder interceptorRegistryBinder) { + Module applicationContextModule = super.newApplicationContextModule(agentOption, interceptorRegistryBinder); - @Override - protected Provider newStorageFactoryProvider(ProfilerConfig profilerConfig, DataSender spanDataSender, AgentInformation agentInformation) { - System.out.println("spanDataSender:" + spanDataSender); - StorageFactory storageFactory = new SimpleSpanStorageFactory(spanDataSender); - return new DelegateProvider(storageFactory); + return Modules.override(applicationContextModule).with(pluginApplicationContextModule); } - - @Override - protected Provider newServerMetaDataHolderProvider() { - return new DelegateProvider(serverMetaDataHolder); - } - - // skip tcp connection - public Provider newPinpointClientFactoryProvider() { - return new NullProvider(); - } - - // skip tcp connection - public Provider newPinpointClientProvider(ProfilerConfig profilerConfig, PinpointClientFactory clientFactory) { - return new NullProvider(); - } - - @Override - protected Provider newTcpDataSenderProvider(PinpointClient client) { - return new DelegateProvider(tcpDataSender); - } - }; @@ -144,38 +101,6 @@ public class PluginTestAgent extends DefaultAgent implements PluginTestVerifier } - private DataSender createUdpStatDataSender() { - return new ListenableDataSender>("StatDataSender"); - } - - private DataSender createUdpSpanDataSender() { - - ListenableDataSender> sender = new ListenableDataSender>("SpanDataSender"); - OrderedSpanRecorder orderedSpanRecorder = new OrderedSpanRecorder(); - sender.setListener(orderedSpanRecorder); - this.orderedSpanRecorder = orderedSpanRecorder; - return sender; - } - - private StorageFactory createStorageFactory(DataSender dataSender) { - return new SimpleSpanStorageFactory(dataSender); - } - - protected EnhancedDataSender createTcpDataSender() { - TestTcpDataSender tcpDataSender = new TestTcpDataSender(); - this.tcpDataSender = tcpDataSender; - return tcpDataSender; - } - - - private ServerMetaDataHolder createServerMetaDataHolder() { - List vmArgs = RuntimeMXBeanUtils.getVmArgs(); - ServerMetaDataHolder serverMetaDataHolder = new ResettableServerMetaDataHolder(vmArgs); - this.serverMetaDataListener = new TestableServerMetaDataListener(); - serverMetaDataHolder.addListener(this.serverMetaDataListener); - return serverMetaDataHolder; - } - @Override public void verifyServerType(String serviceTypeName) { final ApplicationContext applicationContext = getApplicationContext(); @@ -190,7 +115,7 @@ public class PluginTestAgent extends DefaultAgent implements PluginTestVerifier @Override public void verifyServerInfo(String expected) { - String actualName = this.serverMetaDataListener.getServerMetaData().getServerInfo(); + String actualName = this.pluginApplicationContextModule.getServerMetaDataListener().getServerMetaData().getServerInfo(); if (!actualName.equals(expected)) { throw new AssertionError("ResolvedExpectedTrace server name [" + expected + "] but was [" + actualName + "]"); @@ -199,7 +124,7 @@ public class PluginTestAgent extends DefaultAgent implements PluginTestVerifier @Override public void verifyConnector(String protocol, int port) { - Map connectorMap = this.serverMetaDataListener.getServerMetaData().getConnectors(); + Map connectorMap = this.pluginApplicationContextModule.getServerMetaDataListener().getServerMetaData().getConnectors(); String actualProtocol = connectorMap.get(port); if (actualProtocol == null || !actualProtocol.equals(protocol)) { @@ -209,7 +134,7 @@ public class PluginTestAgent extends DefaultAgent implements PluginTestVerifier @Override public void verifyService(String name, List libs) { - List serviceInfos = this.serverMetaDataListener.getServerMetaData().getServiceInfos(); + List serviceInfos = this.pluginApplicationContextModule.getServerMetaDataListener().getServerMetaData().getServiceInfos(); for (ServiceInfo serviceInfo : serviceInfos) { if (serviceInfo.getServiceName().equals(name)) { @@ -785,11 +710,11 @@ public class PluginTestAgent extends DefaultAgent implements PluginTestVerifier } private TestTcpDataSender getTestTcpDataSender() { - return tcpDataSender; + return this.pluginApplicationContextModule.getTcpDataSender(); } private OrderedSpanRecorder getRecorder() { - return this.orderedSpanRecorder; + return this.pluginApplicationContextModule.getOrderedSpanRecorder(); } private Object popSpan() { diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/TestAgentInformation.java b/profiler-test/src/main/java/com/navercorp/pinpoint/test/TestAgentInformation.java index ad2ed9739..a560d44d7 100644 --- a/profiler-test/src/main/java/com/navercorp/pinpoint/test/TestAgentInformation.java +++ b/profiler-test/src/main/java/com/navercorp/pinpoint/test/TestAgentInformation.java @@ -20,12 +20,12 @@ import com.navercorp.pinpoint.common.Version; import com.navercorp.pinpoint.common.trace.ServiceType; import com.navercorp.pinpoint.common.util.JvmUtils; import com.navercorp.pinpoint.common.util.SystemPropertyKey; -import com.navercorp.pinpoint.profiler.AgentInformation; +import com.navercorp.pinpoint.profiler.DefaultAgentInformation; /** * @author HyunGil Jeong */ -public class TestAgentInformation extends AgentInformation { +public class TestAgentInformation extends DefaultAgentInformation { private static final String AGENT_ID = "test-agent"; private static final String APPLICATION_NAME = "TEST_APPLICATION"; diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/DefaultTranslator.java b/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/DefaultTranslator.java index 7c795d123..77645ad5a 100644 --- a/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/DefaultTranslator.java +++ b/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/DefaultTranslator.java @@ -21,6 +21,7 @@ import com.navercorp.pinpoint.bootstrap.instrument.matcher.ClassNameMatcher; import com.navercorp.pinpoint.bootstrap.instrument.matcher.Matcher; import com.navercorp.pinpoint.bootstrap.instrument.matcher.MultiClassNameMatcher; import com.navercorp.pinpoint.profiler.ClassFileTransformerDispatcher; +import com.navercorp.pinpoint.profiler.DefaultClassFileTransformerDispatcher; import com.navercorp.pinpoint.profiler.plugin.xml.transformer.MatchableClassFileTransformer; import com.navercorp.pinpoint.profiler.util.JavaAssistUtils; import com.navercorp.pinpoint.test.util.BytecodeUtils; diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/JavassistTranslator.java b/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/JavassistTranslator.java index 14ec62ac8..d2c5e9fab 100644 --- a/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/JavassistTranslator.java +++ b/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/JavassistTranslator.java @@ -24,6 +24,8 @@ import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import com.navercorp.pinpoint.profiler.ClassFileTransformerDispatcher; +import com.navercorp.pinpoint.profiler.DefaultClassFileTransformerDispatcher; import javassist.ClassPool; import javassist.CtClass; @@ -33,7 +35,6 @@ import org.slf4j.LoggerFactory; import com.navercorp.pinpoint.bootstrap.instrument.matcher.ClassNameMatcher; import com.navercorp.pinpoint.bootstrap.instrument.matcher.Matcher; import com.navercorp.pinpoint.bootstrap.instrument.matcher.MultiClassNameMatcher; -import com.navercorp.pinpoint.profiler.ClassFileTransformerDispatcher; import com.navercorp.pinpoint.profiler.plugin.xml.transformer.MatchableClassFileTransformer; import com.navercorp.pinpoint.profiler.util.JavaAssistUtils; diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/TestClassLoader.java b/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/TestClassLoader.java index e992195ff..0409d33ad 100644 --- a/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/TestClassLoader.java +++ b/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/TestClassLoader.java @@ -27,6 +27,7 @@ import com.navercorp.pinpoint.profiler.context.ApplicationContext; import com.navercorp.pinpoint.profiler.instrument.ASMClassPool; import com.navercorp.pinpoint.profiler.instrument.JavassistClassPool; import com.navercorp.pinpoint.profiler.plugin.MatchableClassFileTransformerGuardDelegate; +import com.navercorp.pinpoint.test.MockApplicationContext; import javassist.ClassPool; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; @@ -45,15 +46,16 @@ public class TestClassLoader extends TransformClassLoader { private final Logger logger = Logger.getLogger(TestClassLoader.class.getName()); - private final ApplicationContext applicationContext; + private final MockApplicationContext applicationContext; private Translator instrumentTranslator; private final DefaultProfilerPluginContext context; private final List delegateClass; - public TestClassLoader(ApplicationContext applicationContext) { + public TestClassLoader(MockApplicationContext applicationContext) { Asserts.notNull(applicationContext, "applicationContext"); this.applicationContext = applicationContext; + this.context = new DefaultProfilerPluginContext(applicationContext, new LegacyProfilerPluginClassInjector(getClass().getClassLoader())); this.delegateClass = new ArrayList(); diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/TestClassLoaderFactory.java b/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/TestClassLoaderFactory.java index 91275730d..b63410079 100644 --- a/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/TestClassLoaderFactory.java +++ b/profiler-test/src/main/java/com/navercorp/pinpoint/test/classloader/TestClassLoaderFactory.java @@ -18,6 +18,7 @@ package com.navercorp.pinpoint.test.classloader; import com.navercorp.pinpoint.profiler.context.ApplicationContext; +import com.navercorp.pinpoint.test.MockApplicationContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,7 +37,7 @@ public class TestClassLoaderFactory { private static final String ATLASSIAN_CLOVER = "com_atlassian_clover.Clover"; - public static TestClassLoader createTestClassLoader(ApplicationContext applicationContext) { + public static TestClassLoader createTestClassLoader(MockApplicationContext applicationContext) { final TestClassLoader testClassLoader = new TestClassLoader(applicationContext); addCloverPackage(testClassLoader, CENQUA_CLOVER); addCloverPackage(testClassLoader, ATLASSIAN_CLOVER); diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/NullProvider.java b/profiler-test/src/main/java/com/navercorp/pinpoint/test/provder/NullPinpointClientFactoryProvider.java similarity index 71% rename from profiler-test/src/main/java/com/navercorp/pinpoint/test/NullProvider.java rename to profiler-test/src/main/java/com/navercorp/pinpoint/test/provder/NullPinpointClientFactoryProvider.java index 4ebf8331f..10d1897ff 100644 --- a/profiler-test/src/main/java/com/navercorp/pinpoint/test/NullProvider.java +++ b/profiler-test/src/main/java/com/navercorp/pinpoint/test/provder/NullPinpointClientFactoryProvider.java @@ -14,18 +14,18 @@ * limitations under the License. */ -package com.navercorp.pinpoint.test; +package com.navercorp.pinpoint.test.provder; - -import com.navercorp.pinpoint.profiler.context.provider.Provider; +import com.google.inject.Provider; +import com.navercorp.pinpoint.rpc.client.PinpointClientFactory; /** * @author Woonduk Kang(emeroad) */ -public class NullProvider implements Provider { +public class NullPinpointClientFactoryProvider implements Provider { @Override - public T get() { + public PinpointClientFactory get() { return null; } } diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/provder/NullPinpointClientProvider.java b/profiler-test/src/main/java/com/navercorp/pinpoint/test/provder/NullPinpointClientProvider.java new file mode 100644 index 000000000..3f43f258c --- /dev/null +++ b/profiler-test/src/main/java/com/navercorp/pinpoint/test/provder/NullPinpointClientProvider.java @@ -0,0 +1,32 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.test.provder; + +import com.google.inject.Provider; +import com.navercorp.pinpoint.rpc.client.PinpointClient; +import com.navercorp.pinpoint.rpc.client.PinpointClientFactory; + +/** + * @author Woonduk Kang(emeroad) + */ +public class NullPinpointClientProvider implements Provider { + + @Override + public PinpointClient get() { + return null; + } +} diff --git a/profiler-test/src/test/java/com/navercorp/pinpoint/test/MockApplicationContextModuleTest.java b/profiler-test/src/test/java/com/navercorp/pinpoint/test/MockApplicationContextModuleTest.java new file mode 100644 index 000000000..376948e7e --- /dev/null +++ b/profiler-test/src/test/java/com/navercorp/pinpoint/test/MockApplicationContextModuleTest.java @@ -0,0 +1,101 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.test; + +import com.google.inject.Injector; +import com.google.inject.Module; +import com.google.inject.util.Modules; +import com.navercorp.pinpoint.bootstrap.AgentOption; +import com.navercorp.pinpoint.bootstrap.DefaultAgentOption; +import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.common.service.DefaultAnnotationKeyRegistryService; +import com.navercorp.pinpoint.common.service.DefaultServiceTypeRegistryService; +import com.navercorp.pinpoint.profiler.AgentInfoSender; +import com.navercorp.pinpoint.profiler.ClassFileTransformerDispatcher; +import com.navercorp.pinpoint.profiler.context.ApplicationContext; +import com.navercorp.pinpoint.profiler.context.DefaultApplicationContext; +import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder; +import org.junit.Assert; +import org.junit.Test; + +import java.net.URL; + +/** + * @author Woonduk Kang(emeroad) + */ +public class MockApplicationContextModuleTest { + + @Test + public void test() { + ProfilerConfig profilerConfig = new DefaultProfilerConfig(); + InterceptorRegistryBinder binder = new TestInterceptorRegistryBinder(); + AgentOption agentOption = new DefaultAgentOption(new DummyInstrumentation(), + "mockAgent", "mockApplicationName", profilerConfig, new URL[0], + null, new DefaultServiceTypeRegistryService(), new DefaultAnnotationKeyRegistryService()); + + final PluginApplicationContextModule pluginApplicationContextModule = new PluginApplicationContextModule(); + PluginTestAgent pluginTestAgent = new PluginTestAgent(agentOption) { + @Override + protected ApplicationContext newApplicationContext(AgentOption agentOption, InterceptorRegistryBinder interceptorRegistryBinder) { + + + ApplicationContext applicationContext = new DefaultApplicationContext(agentOption, interceptorRegistryBinder) { + + @Override + protected Module newApplicationContextModule(AgentOption agentOption, InterceptorRegistryBinder interceptorRegistryBinder) { + Module applicationContextModule = super.newApplicationContextModule(agentOption, interceptorRegistryBinder); +// PluginApplicationContextModule pluginApplicationContextModule = new PluginApplicationContextModule(); + return Modules.override(applicationContextModule).with(pluginApplicationContextModule); + } + }; + + + return applicationContext; + } + }; +// pluginTestAgent.start(); + } + + @Test + public void testMockApplicationContext() { + ProfilerConfig profilerConfig = new DefaultProfilerConfig(); + InterceptorRegistryBinder binder = new TestInterceptorRegistryBinder(); + AgentOption agentOption = new DefaultAgentOption(new DummyInstrumentation(), + "mockAgent", "mockApplicationName", profilerConfig, new URL[0], + null, new DefaultServiceTypeRegistryService(), new DefaultAnnotationKeyRegistryService()); + DefaultApplicationContext applicationContext = new DefaultApplicationContext(agentOption, binder) { + @Override + protected Module newApplicationContextModule(AgentOption agentOption, InterceptorRegistryBinder interceptorRegistryBinder) { + Module module = super.newApplicationContextModule(agentOption, interceptorRegistryBinder); + PluginApplicationContextModule pluginApplicationContextModule = new PluginApplicationContextModule(); + + return Modules.override(module).with(pluginApplicationContextModule); + } + }; + + Injector injector = applicationContext.getInjector(); + AgentInfoSender instance1 = injector.getInstance(AgentInfoSender.class); + AgentInfoSender instance2 = injector.getInstance(AgentInfoSender.class); + Assert.assertSame(instance1, instance2); + + ClassFileTransformerDispatcher instance4 = injector.getInstance(ClassFileTransformerDispatcher.class); + } + + + +} \ No newline at end of file diff --git a/profiler-test/src/test/java/com/navercorp/pinpoint/test/javasssit/JavassistClassTest.java b/profiler-test/src/test/java/com/navercorp/pinpoint/test/javasssit/JavassistClassTest.java index 1fb6d5683..cedb577cd 100644 --- a/profiler-test/src/test/java/com/navercorp/pinpoint/test/javasssit/JavassistClassTest.java +++ b/profiler-test/src/test/java/com/navercorp/pinpoint/test/javasssit/JavassistClassTest.java @@ -190,7 +190,7 @@ public class JavassistClassTest { DefaultProfilerConfig profilerConfig = new DefaultProfilerConfig(); profilerConfig.setApplicationServerType(ServiceType.TEST_STAND_ALONE.getName()); - ApplicationContext applicationContext = MockApplicationContext.of(profilerConfig); + MockApplicationContext applicationContext = MockApplicationContext.of(profilerConfig); TestClassLoader testClassLoader = new TestClassLoader(applicationContext); testClassLoader.initialize(); diff --git a/profiler-test/src/test/java/com/navercorp/pinpoint/test/javasssit/accessor/AccessorInjectionTest.java b/profiler-test/src/test/java/com/navercorp/pinpoint/test/javasssit/accessor/AccessorInjectionTest.java index 634a8d3ac..33791a4d8 100644 --- a/profiler-test/src/test/java/com/navercorp/pinpoint/test/javasssit/accessor/AccessorInjectionTest.java +++ b/profiler-test/src/test/java/com/navercorp/pinpoint/test/javasssit/accessor/AccessorInjectionTest.java @@ -26,7 +26,6 @@ import com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory; import com.navercorp.pinpoint.bootstrap.plugin.jdbc.UnKnownDatabaseInfo; import com.navercorp.pinpoint.common.trace.ServiceType; -import com.navercorp.pinpoint.profiler.context.ApplicationContext; import com.navercorp.pinpoint.profiler.logging.Slf4jLoggerBinder; import com.navercorp.pinpoint.test.MockApplicationContext; import com.navercorp.pinpoint.test.classloader.TestClassLoader; @@ -52,7 +51,7 @@ public class AccessorInjectionTest { DefaultProfilerConfig profilerConfig = new DefaultProfilerConfig(); profilerConfig.setApplicationServerType(ServiceType.TEST_STAND_ALONE.getName()); - ApplicationContext applicationContext = MockApplicationContext.of(profilerConfig); + MockApplicationContext applicationContext = MockApplicationContext.of(profilerConfig); TestClassLoader testClassLoader = new TestClassLoader(applicationContext); testClassLoader.initialize(); diff --git a/profiler-test/src/test/java/com/navercorp/pinpoint/test/monitor/AgentStatMonitorTest.java b/profiler-test/src/test/java/com/navercorp/pinpoint/test/monitor/AgentStatMonitorTest.java index d4a8cfd66..2be4b4e81 100644 --- a/profiler-test/src/test/java/com/navercorp/pinpoint/test/monitor/AgentStatMonitorTest.java +++ b/profiler-test/src/test/java/com/navercorp/pinpoint/test/monitor/AgentStatMonitorTest.java @@ -19,14 +19,16 @@ package com.navercorp.pinpoint.test.monitor; import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.profiler.context.AtomicIdGenerator; import com.navercorp.pinpoint.profiler.context.DefaultTransactionCounter; -import com.navercorp.pinpoint.profiler.context.IdGenerator; import com.navercorp.pinpoint.profiler.context.TransactionCounter; import com.navercorp.pinpoint.profiler.context.active.ActiveTraceRepository; import com.navercorp.pinpoint.profiler.context.monitor.DefaultPluginMonitorContext; import com.navercorp.pinpoint.profiler.context.monitor.PluginMonitorContext; import com.navercorp.pinpoint.profiler.monitor.AgentStatMonitor; +import com.navercorp.pinpoint.profiler.monitor.DefaultAgentStatMonitor; import com.navercorp.pinpoint.profiler.monitor.codahale.AgentStatCollectorFactory; +import com.navercorp.pinpoint.profiler.monitor.codahale.DefaultAgentStatCollectorFactory; import com.navercorp.pinpoint.profiler.sender.DataSender; import com.navercorp.pinpoint.test.ListenableDataSender; import com.navercorp.pinpoint.test.TBaseRecorder; @@ -72,7 +74,7 @@ public class AgentStatMonitorTest { // When AgentStatCollectorFactory agentStatCollectorFactory = newAgentStatCollectorFactory(); - AgentStatMonitor monitor = new AgentStatMonitor(this.dataSender, "agentId", System.currentTimeMillis(), + AgentStatMonitor monitor = new DefaultAgentStatMonitor(this.dataSender, "agentId", System.currentTimeMillis(), agentStatCollectorFactory, collectionIntervalMs, numCollectionsPerBatch); monitor.start(); Thread.sleep(totalTestDurationMs); @@ -88,10 +90,10 @@ public class AgentStatMonitorTest { private AgentStatCollectorFactory newAgentStatCollectorFactory() { ProfilerConfig profilerConfig = new DefaultProfilerConfig(); ActiveTraceRepository activeTraceRepository = new ActiveTraceRepository(); - IdGenerator idGenerator = new IdGenerator(); + AtomicIdGenerator idGenerator = new AtomicIdGenerator(); TransactionCounter transactionCounter = new DefaultTransactionCounter(idGenerator); PluginMonitorContext pluginMonitorContext = new DefaultPluginMonitorContext(); - return new AgentStatCollectorFactory(profilerConfig, activeTraceRepository, transactionCounter, pluginMonitorContext); + return new DefaultAgentStatCollectorFactory(profilerConfig, activeTraceRepository, transactionCounter, pluginMonitorContext); } } diff --git a/profiler-test/src/test/java/com/navercorp/pinpoint/test/plugin/DefaultClassEditorBuilderTest.java b/profiler-test/src/test/java/com/navercorp/pinpoint/test/plugin/DefaultClassEditorBuilderTest.java index 81c95401d..49cd48de3 100644 --- a/profiler-test/src/test/java/com/navercorp/pinpoint/test/plugin/DefaultClassEditorBuilderTest.java +++ b/profiler-test/src/test/java/com/navercorp/pinpoint/test/plugin/DefaultClassEditorBuilderTest.java @@ -22,7 +22,6 @@ import com.navercorp.pinpoint.bootstrap.context.TraceContext; import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass; import com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod; import com.navercorp.pinpoint.profiler.context.ApplicationContext; -import com.navercorp.pinpoint.profiler.context.DefaultApplicationContext; import com.navercorp.pinpoint.profiler.instrument.JavassistClassPool; import com.navercorp.pinpoint.profiler.plugin.DefaultProfilerPluginContext; import com.navercorp.pinpoint.profiler.util.TypeUtils; diff --git a/profiler/pom.xml b/profiler/pom.xml index f71b9547e..8f546bfe0 100644 --- a/profiler/pom.xml +++ b/profiler/pom.xml @@ -60,7 +60,12 @@ com.navercorp.pinpoint pinpoint-google-guava - + + com.google.inject + guice + 4.1.0 + + org.springframework diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInfoSender.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInfoSender.java index 9e5c8eb86..07089f049 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInfoSender.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInfoSender.java @@ -26,6 +26,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import com.navercorp.pinpoint.profiler.context.provider.JvmInformationProvider; import com.navercorp.pinpoint.thrift.dto.TJvmGcType; import com.navercorp.pinpoint.thrift.dto.TJvmInfo; import org.slf4j.Logger; @@ -215,7 +216,7 @@ public class AgentInfoSender implements ServerMetaDataListener { private int maxTryPerAttempt = DEFAULT_MAX_TRY_COUNT_PER_ATTEMPT; Builder(EnhancedDataSender dataSender, AgentInformation agentInformation) { - this(dataSender, agentInformation, new JvmInformationFactory().createJvmInformation()); + this(dataSender, agentInformation, new JvmInformationProvider().get()); } public Builder(EnhancedDataSender dataSender, AgentInformation agentInformation, JvmInformation jvmInformation) { diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInformation.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInformation.java index b473a1a0e..d11c21d6b 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInformation.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInformation.java @@ -1,11 +1,11 @@ /* - * Copyright 2014 NAVER Corp. + * Copyright 2017 NAVER Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -17,110 +17,26 @@ package com.navercorp.pinpoint.profiler; import com.navercorp.pinpoint.common.trace.ServiceType; -import com.navercorp.pinpoint.rpc.packet.HandshakePropertyType; - -import java.util.HashMap; -import java.util.Map; /** - * @author emeroad - * @author koo.taejin - * @author hyungil.jeong + * @author Woonduk Kang(emeroad) */ -public class AgentInformation { - private final String agentId; - private final String applicationName; - private final long startTime; - private final int pid; - private final String machineName; - private final String hostIp; - private final ServiceType serverType; - private final String jvmVersion; - private final String agentVersion; +public interface AgentInformation { + String getAgentId(); - public AgentInformation( - String agentId, - String applicationName, - long startTime, - int pid, - String machineName, - String hostIp, - ServiceType serverType, - String jvmVersion, - String agentVersion) { - if (agentId == null) { - throw new NullPointerException("agentId must not be null"); - } - if (applicationName == null) { - throw new NullPointerException("applicationName must not be null"); - } - if (machineName == null) { - throw new NullPointerException("machineName must not be null"); - } - if (agentVersion == null) { - throw new NullPointerException("version must not be null"); - } - this.agentId = agentId; - this.applicationName = applicationName; - this.startTime = startTime; - this.pid = pid; - this.machineName = machineName; - this.hostIp = hostIp; - this.serverType = serverType; - this.jvmVersion = jvmVersion; - this.agentVersion = agentVersion; - } + String getApplicationName(); - public String getAgentId() { - return agentId; - } + long getStartTime(); - public String getApplicationName() { - return applicationName; - } + int getPid(); - public long getStartTime() { - return startTime; - } + String getMachineName(); - public int getPid() { - return pid; - } + String getHostIp(); - public String getMachineName() { - return machineName; - } + ServiceType getServerType(); - public String getHostIp() { - return hostIp; - } + String getJvmVersion(); - public ServiceType getServerType() { - return serverType; - } - - public String getJvmVersion() { - return this.jvmVersion; - } - - public String getAgentVersion() { - return agentVersion; - } - - - @Override - public String toString() { - final StringBuilder sb = new StringBuilder("AgentInformation{"); - sb.append("agentId='").append(agentId).append('\''); - sb.append(", applicationName='").append(applicationName).append('\''); - sb.append(", startTime=").append(startTime); - sb.append(", pid=").append(pid); - sb.append(", machineName='").append(machineName).append('\''); - sb.append(", hostIp='").append(hostIp).append('\''); - sb.append(", serverType=").append(serverType); - sb.append(", jvmVersion='").append(jvmVersion).append('\''); - sb.append(", agentVersion='").append(agentVersion).append('\''); - sb.append('}'); - return sb.toString(); - } + String getAgentVersion(); } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInformationFactory.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInformationFactory.java index 7dd5f7b2d..7643afaac 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInformationFactory.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInformationFactory.java @@ -1,11 +1,11 @@ /* - * Copyright 2014 NAVER Corp. + * Copyright 2017 NAVER Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -16,53 +16,9 @@ package com.navercorp.pinpoint.profiler; -import com.navercorp.pinpoint.bootstrap.util.IdValidateUtils; -import com.navercorp.pinpoint.bootstrap.util.NetworkUtils; -import com.navercorp.pinpoint.common.Version; -import com.navercorp.pinpoint.common.trace.ServiceType; -import com.navercorp.pinpoint.common.util.JvmUtils; -import com.navercorp.pinpoint.common.util.SystemPropertyKey; -import com.navercorp.pinpoint.profiler.util.RuntimeMXBeanUtils; - - /** - * @author emeroad + * @author Woonduk Kang(emeroad) */ -public class AgentInformationFactory { - - private final String agentId; - private final String applicationName; - - public AgentInformationFactory(String agentId, String applicationName) { - if (agentId == null) { - throw new NullPointerException("agentId must not be null"); - } - if (applicationName == null) { - throw new NullPointerException("applicationName must not be null"); - } - - this.agentId = checkId(agentId); - this.applicationName = checkId(applicationName); - } - - public AgentInformation createAgentInformation(ServiceType serverType) { - if (serverType == null) { - throw new NullPointerException("serverType must not be null"); - } - final String machineName = NetworkUtils.getHostName(); - final String hostIp = NetworkUtils.getRepresentationHostIp(); - final long startTime = RuntimeMXBeanUtils.getVmStartTime(); - final int pid = RuntimeMXBeanUtils.getPid(); - final String jvmVersion = JvmUtils.getSystemProperty(SystemPropertyKey.JAVA_VERSION); - return new AgentInformation(agentId, applicationName, startTime, pid, machineName, hostIp, serverType, jvmVersion, Version.VERSION); - } - - private String checkId(String id) { - if (!IdValidateUtils.validateId(id)) { - throw new IllegalStateException("invalid Id=" + id); - } - return id; - } - - +public interface AgentInformationFactory { + AgentInformation createAgentInformation(); } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/ClassFileTransformerDispatcher.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/ClassFileTransformerDispatcher.java index 1d392dad6..90f1c9271 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/ClassFileTransformerDispatcher.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/ClassFileTransformerDispatcher.java @@ -1,175 +1,22 @@ -/* - * Copyright 2014 NAVER Corp. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package com.navercorp.pinpoint.profiler; +import com.navercorp.pinpoint.bootstrap.instrument.DynamicTransformRequestListener; +import com.navercorp.pinpoint.bootstrap.instrument.RequestHandle; + import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; import java.security.ProtectionDomain; -import java.util.List; - -import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; -import com.navercorp.pinpoint.bootstrap.instrument.RequestHandle; -import com.navercorp.pinpoint.profiler.context.ApplicationContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.navercorp.pinpoint.bootstrap.config.Filter; -import com.navercorp.pinpoint.bootstrap.instrument.DynamicTransformRequestListener; -import com.navercorp.pinpoint.profiler.instrument.LegacyProfilerPluginClassInjector; -import com.navercorp.pinpoint.profiler.instrument.transformer.DebugTransformer; -import com.navercorp.pinpoint.profiler.instrument.transformer.DefaultTransformerRegistry; -import com.navercorp.pinpoint.profiler.instrument.transformer.TransformerRegistry; -import com.navercorp.pinpoint.profiler.plugin.DefaultProfilerPluginContext; -import com.navercorp.pinpoint.profiler.plugin.xml.transformer.MatchableClassFileTransformer; -import com.navercorp.pinpoint.profiler.util.JavaAssistUtils; /** - * @author emeroad - * @author netspider - * @author jaehong.kim + * @author Woonduk Kang(emeroad) */ -public class ClassFileTransformerDispatcher implements ClassFileTransformer, DynamicTransformRequestListener { - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - private final boolean isDebug = logger.isDebugEnabled(); - - private final ClassLoader agentClassLoader = this.getClass().getClassLoader(); - - private final TransformerRegistry transformerRegistry; - private final DynamicTransformerRegistry dynamicTransformerRegistry; - - private final DefaultProfilerPluginContext globalContext; - private final Filter debugTargetFilter; - private final DebugTransformer debugTransformer; - - private final ClassFileFilter pinpointClassFilter; - private final ClassFileFilter unmodifiableFilter; - - public ClassFileTransformerDispatcher(ApplicationContext applicationContext, List pluginContexts) { - if (applicationContext == null) { - throw new NullPointerException("applicationContext must not be null"); - } - - this.globalContext = new DefaultProfilerPluginContext(applicationContext, new LegacyProfilerPluginClassInjector(getClass().getClassLoader())); - ProfilerConfig profilerConfig = applicationContext.getProfilerConfig(); - this.debugTargetFilter = profilerConfig.getProfilableClassFilter(); - this.debugTransformer = new DebugTransformer(globalContext); - - this.pinpointClassFilter = new PinpointClassFilter(agentClassLoader); - this.unmodifiableFilter = new UnmodifiableClassFilter(); - - this.transformerRegistry = createTransformerRegistry(pluginContexts); - this.dynamicTransformerRegistry = new DefaultDynamicTransformerRegistry(); - } +public interface ClassFileTransformerDispatcher extends ClassFileTransformer, DynamicTransformRequestListener { + @Override + byte[] transform(ClassLoader classLoader, String classInternalName, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws IllegalClassFormatException; @Override - public byte[] transform(ClassLoader classLoader, String classInternalName, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws IllegalClassFormatException { - if (!pinpointClassFilter.accept(classLoader, classInternalName, classBeingRedefined, protectionDomain, classFileBuffer)) { - return null; - } - - final ClassFileTransformer dynamicTransformer = dynamicTransformerRegistry.getTransformer(classLoader, classInternalName); - if (dynamicTransformer != null) { - return transform0(classLoader, classInternalName, classBeingRedefined, protectionDomain, classFileBuffer, dynamicTransformer); - } - - if (!unmodifiableFilter.accept(classLoader, classInternalName, classBeingRedefined, protectionDomain, classFileBuffer)) { - return null; - } - - ClassFileTransformer transformer = this.transformerRegistry.findTransformer(classInternalName); - if (transformer == null) { - // For debug - // TODO What if a modifier is duplicated? - if (this.debugTargetFilter.filter(classInternalName)) { - // Added to see if call stack view is OK on a test machine. - transformer = debugTransformer; - } else { - return null; - } - } - - return transform0(classLoader, classInternalName, classBeingRedefined, protectionDomain, classFileBuffer, transformer); - } - - private byte[] transform0(ClassLoader classLoader, String classInternalName, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer, ClassFileTransformer transformer) { - final String className = JavaAssistUtils.jvmNameToJavaName(classInternalName); - - if (isDebug) { - if (classBeingRedefined == null) { - logger.debug("[transform] classLoader:{} className:{} transformer:{}", classLoader, className, transformer.getClass().getName()); - } else { - logger.debug("[retransform] classLoader:{} className:{} transformer:{}", classLoader, className, transformer.getClass().getName()); - } - } - - try { - final Thread thread = Thread.currentThread(); - final ClassLoader before = getContextClassLoader(thread); - thread.setContextClassLoader(this.agentClassLoader); - try { - return transformer.transform(classLoader, className, classBeingRedefined, protectionDomain, classFileBuffer); - } finally { - // The context class loader have to be recovered even if it was null. - thread.setContextClassLoader(before); - } - } catch (Throwable e) { - logger.error("Transformer:{} threw an exception. cl:{} ctxCl:{} agentCl:{} Cause:{}", - transformer.getClass().getName(), classLoader, Thread.currentThread().getContextClassLoader(), agentClassLoader, e.getMessage(), e); - return null; - } - } + RequestHandle onRetransformRequest(Class target, ClassFileTransformer transformer); @Override - public RequestHandle onRetransformRequest(Class target, final ClassFileTransformer transformer) { - return this.dynamicTransformerRegistry.onRetransformRequest(target, transformer); - } - - @Override - public void onTransformRequest(ClassLoader classLoader, String targetClassName, ClassFileTransformer transformer) { - this.dynamicTransformerRegistry.onTransformRequest(classLoader, targetClassName, transformer); - } - - private ClassLoader getContextClassLoader(Thread thread) throws Throwable { - try { - return thread.getContextClassLoader(); - } catch (SecurityException se) { - throw se; - } catch (Throwable th) { - if (isDebug) { - logger.debug("getContextClassLoader(). Caused:{}", th.getMessage(), th); - } - throw th; - } - } - - private TransformerRegistry createTransformerRegistry(List pluginContexts) { - DefaultTransformerRegistry registry = new DefaultTransformerRegistry(); - - for (DefaultProfilerPluginContext pluginContext : pluginContexts) { - for (ClassFileTransformer transformer : pluginContext.getClassEditors()) { - if (transformer instanceof MatchableClassFileTransformer) { - MatchableClassFileTransformer t = (MatchableClassFileTransformer) transformer; - logger.info("Registering class file transformer {} for {} ", t, t.getMatcher()); - registry.addTransformer(t.getMatcher(), t); - } else { - logger.warn("Ignore class file transformer {}", transformer); - } - } - } - - return registry; - } -} \ No newline at end of file + void onTransformRequest(ClassLoader classLoader, String targetClassName, ClassFileTransformer transformer); +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgent.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgent.java index 69f3c89a3..b735f9377 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgent.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgent.java @@ -16,6 +16,7 @@ package com.navercorp.pinpoint.profiler; +import com.google.inject.Guice; import com.navercorp.pinpoint.ProductInfo; import com.navercorp.pinpoint.bootstrap.Agent; import com.navercorp.pinpoint.bootstrap.AgentOption; @@ -141,10 +142,6 @@ public class DefaultAgent implements Agent { } } - public ProfilerConfig getProfilerConfig() { - return profilerConfig; - } - private void changeStatus(AgentStatus status) { this.agentStatus = status; if (logger.isDebugEnabled()) { diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgentInformation.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgentInformation.java new file mode 100644 index 000000000..e120a9ff6 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgentInformation.java @@ -0,0 +1,131 @@ +/* + * Copyright 2014 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler; + +import com.navercorp.pinpoint.common.trace.ServiceType; + +/** + * @author emeroad + * @author koo.taejin + * @author hyungil.jeong + */ +public class DefaultAgentInformation implements AgentInformation { + private final String agentId; + private final String applicationName; + private final long startTime; + private final int pid; + private final String machineName; + private final String hostIp; + private final ServiceType serverType; + private final String jvmVersion; + private final String agentVersion; + + public DefaultAgentInformation( + String agentId, + String applicationName, + long startTime, + int pid, + String machineName, + String hostIp, + ServiceType serverType, + String jvmVersion, + String agentVersion) { + if (agentId == null) { + throw new NullPointerException("agentId must not be null"); + } + if (applicationName == null) { + throw new NullPointerException("applicationName must not be null"); + } + if (machineName == null) { + throw new NullPointerException("machineName must not be null"); + } + if (agentVersion == null) { + throw new NullPointerException("version must not be null"); + } + this.agentId = agentId; + this.applicationName = applicationName; + this.startTime = startTime; + this.pid = pid; + this.machineName = machineName; + this.hostIp = hostIp; + this.serverType = serverType; + this.jvmVersion = jvmVersion; + this.agentVersion = agentVersion; + } + + @Override + public String getAgentId() { + return agentId; + } + + @Override + public String getApplicationName() { + return applicationName; + } + + @Override + public long getStartTime() { + return startTime; + } + + @Override + public int getPid() { + return pid; + } + + @Override + public String getMachineName() { + return machineName; + } + + @Override + public String getHostIp() { + return hostIp; + } + + @Override + public ServiceType getServerType() { + return serverType; + } + + @Override + public String getJvmVersion() { + return this.jvmVersion; + } + + @Override + public String getAgentVersion() { + return agentVersion; + } + + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("AgentInformation{"); + sb.append("agentId='").append(agentId).append('\''); + sb.append(", applicationName='").append(applicationName).append('\''); + sb.append(", startTime=").append(startTime); + sb.append(", pid=").append(pid); + sb.append(", machineName='").append(machineName).append('\''); + sb.append(", hostIp='").append(hostIp).append('\''); + sb.append(", serverType=").append(serverType); + sb.append(", jvmVersion='").append(jvmVersion).append('\''); + sb.append(", agentVersion='").append(agentVersion).append('\''); + sb.append('}'); + return sb.toString(); + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgentInformationFactory.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgentInformationFactory.java new file mode 100644 index 000000000..03d1e2248 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgentInformationFactory.java @@ -0,0 +1,78 @@ +/* + * Copyright 2014 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler; + +import com.google.inject.Inject; +import com.navercorp.pinpoint.bootstrap.util.IdValidateUtils; +import com.navercorp.pinpoint.bootstrap.util.NetworkUtils; +import com.navercorp.pinpoint.common.Version; +import com.navercorp.pinpoint.common.trace.ServiceType; +import com.navercorp.pinpoint.common.util.JvmUtils; +import com.navercorp.pinpoint.common.util.SystemPropertyKey; +import com.navercorp.pinpoint.profiler.context.module.AgentId; +import com.navercorp.pinpoint.profiler.context.module.AgentServiceType; +import com.navercorp.pinpoint.profiler.context.module.AgentStartTime; +import com.navercorp.pinpoint.profiler.context.module.ApplicationName; +import com.navercorp.pinpoint.profiler.util.RuntimeMXBeanUtils; + + +/** + * @author emeroad + */ +public class DefaultAgentInformationFactory implements AgentInformationFactory { + + private final String agentId; + private final String applicationName; + private final long agentStartTime; + private final ServiceType serverType; + + @Inject + public DefaultAgentInformationFactory(@AgentId String agentId, @ApplicationName String applicationName, @AgentStartTime long agentStartTime, @AgentServiceType ServiceType serverType) { + if (agentId == null) { + throw new NullPointerException("agentId must not be null"); + } + if (applicationName == null) { + throw new NullPointerException("applicationName must not be null"); + } + if (serverType == null) { + throw new NullPointerException("serverType must not be null"); + } + + this.agentId = checkId(agentId); + this.applicationName = checkId(applicationName); + this.serverType = serverType; + this.agentStartTime = agentStartTime; + } + + public AgentInformation createAgentInformation() { + + final String machineName = NetworkUtils.getHostName(); + final String hostIp = NetworkUtils.getRepresentationHostIp(); + + final int pid = RuntimeMXBeanUtils.getPid(); + final String jvmVersion = JvmUtils.getSystemProperty(SystemPropertyKey.JAVA_VERSION); + return new DefaultAgentInformation(agentId, applicationName, agentStartTime, pid, machineName, hostIp, serverType, jvmVersion, Version.VERSION); + } + + private String checkId(String id) { + if (!IdValidateUtils.validateId(id)) { + throw new IllegalStateException("invalid Id=" + id); + } + return id; + } + +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultClassFileTransformerDispatcher.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultClassFileTransformerDispatcher.java new file mode 100644 index 000000000..6b937fceb --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultClassFileTransformerDispatcher.java @@ -0,0 +1,179 @@ +/* + * Copyright 2014 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.navercorp.pinpoint.profiler; + +import java.lang.instrument.ClassFileTransformer; +import java.lang.instrument.IllegalClassFormatException; +import java.security.ProtectionDomain; +import java.util.List; + +import com.google.inject.Inject; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.bootstrap.context.TraceContext; +import com.navercorp.pinpoint.bootstrap.instrument.DynamicTransformTrigger; +import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClassPool; +import com.navercorp.pinpoint.bootstrap.instrument.RequestHandle; +import com.navercorp.pinpoint.profiler.context.ApplicationContext; +import com.navercorp.pinpoint.profiler.instrument.ClassInjector; +import com.navercorp.pinpoint.profiler.plugin.PluginContextLoadResult; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.navercorp.pinpoint.bootstrap.config.Filter; +import com.navercorp.pinpoint.profiler.instrument.LegacyProfilerPluginClassInjector; +import com.navercorp.pinpoint.profiler.instrument.transformer.DebugTransformer; +import com.navercorp.pinpoint.profiler.instrument.transformer.DefaultTransformerRegistry; +import com.navercorp.pinpoint.profiler.instrument.transformer.TransformerRegistry; +import com.navercorp.pinpoint.profiler.plugin.DefaultProfilerPluginContext; +import com.navercorp.pinpoint.profiler.plugin.xml.transformer.MatchableClassFileTransformer; +import com.navercorp.pinpoint.profiler.util.JavaAssistUtils; + +/** + * @author emeroad + * @author netspider + * @author jaehong.kim + */ +public class DefaultClassFileTransformerDispatcher implements ClassFileTransformerDispatcher { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final boolean isDebug = logger.isDebugEnabled(); + + private final ClassLoader agentClassLoader = this.getClass().getClassLoader(); + + private final TransformerRegistry transformerRegistry; + private final DynamicTransformerRegistry dynamicTransformerRegistry; + + private final DefaultProfilerPluginContext globalContext; + private final Filter debugTargetFilter; + private final DebugTransformer debugTransformer; + + private final ClassFileFilter pinpointClassFilter; + private final ClassFileFilter unmodifiableFilter; + + @Inject + public DefaultClassFileTransformerDispatcher(ApplicationContext applicationContext, PluginContextLoadResult pluginContexts) { + + + this.globalContext = new DefaultProfilerPluginContext(applicationContext, new LegacyProfilerPluginClassInjector(getClass().getClassLoader())); + this.debugTargetFilter = applicationContext.getProfilerConfig().getProfilableClassFilter(); + this.debugTransformer = new DebugTransformer(globalContext); + + this.pinpointClassFilter = new PinpointClassFilter(agentClassLoader); + this.unmodifiableFilter = new UnmodifiableClassFilter(); + + this.transformerRegistry = createTransformerRegistry(pluginContexts); + this.dynamicTransformerRegistry = new DefaultDynamicTransformerRegistry(); + } + + @Override + public byte[] transform(ClassLoader classLoader, String classInternalName, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws IllegalClassFormatException { + if (!pinpointClassFilter.accept(classLoader, classInternalName, classBeingRedefined, protectionDomain, classFileBuffer)) { + return null; + } + + final ClassFileTransformer dynamicTransformer = dynamicTransformerRegistry.getTransformer(classLoader, classInternalName); + if (dynamicTransformer != null) { + return transform0(classLoader, classInternalName, classBeingRedefined, protectionDomain, classFileBuffer, dynamicTransformer); + } + + if (!unmodifiableFilter.accept(classLoader, classInternalName, classBeingRedefined, protectionDomain, classFileBuffer)) { + return null; + } + + ClassFileTransformer transformer = this.transformerRegistry.findTransformer(classInternalName); + if (transformer == null) { + // For debug + // TODO What if a modifier is duplicated? + if (this.debugTargetFilter.filter(classInternalName)) { + // Added to see if call stack view is OK on a test machine. + transformer = debugTransformer; + } else { + return null; + } + } + + return transform0(classLoader, classInternalName, classBeingRedefined, protectionDomain, classFileBuffer, transformer); + } + + private byte[] transform0(ClassLoader classLoader, String classInternalName, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer, ClassFileTransformer transformer) { + final String className = JavaAssistUtils.jvmNameToJavaName(classInternalName); + + if (isDebug) { + if (classBeingRedefined == null) { + logger.debug("[transform] classLoader:{} className:{} transformer:{}", classLoader, className, transformer.getClass().getName()); + } else { + logger.debug("[retransform] classLoader:{} className:{} transformer:{}", classLoader, className, transformer.getClass().getName()); + } + } + + try { + final Thread thread = Thread.currentThread(); + final ClassLoader before = getContextClassLoader(thread); + thread.setContextClassLoader(this.agentClassLoader); + try { + return transformer.transform(classLoader, className, classBeingRedefined, protectionDomain, classFileBuffer); + } finally { + // The context class loader have to be recovered even if it was null. + thread.setContextClassLoader(before); + } + } catch (Throwable e) { + logger.error("Transformer:{} threw an exception. cl:{} ctxCl:{} agentCl:{} Cause:{}", + transformer.getClass().getName(), classLoader, Thread.currentThread().getContextClassLoader(), agentClassLoader, e.getMessage(), e); + return null; + } + } + + @Override + public RequestHandle onRetransformRequest(Class target, final ClassFileTransformer transformer) { + return this.dynamicTransformerRegistry.onRetransformRequest(target, transformer); + } + + @Override + public void onTransformRequest(ClassLoader classLoader, String targetClassName, ClassFileTransformer transformer) { + this.dynamicTransformerRegistry.onTransformRequest(classLoader, targetClassName, transformer); + } + + private ClassLoader getContextClassLoader(Thread thread) throws Throwable { + try { + return thread.getContextClassLoader(); + } catch (SecurityException se) { + throw se; + } catch (Throwable th) { + if (isDebug) { + logger.debug("getContextClassLoader(). Caused:{}", th.getMessage(), th); + } + throw th; + } + } + + private TransformerRegistry createTransformerRegistry(PluginContextLoadResult pluginContexts) { + DefaultTransformerRegistry registry = new DefaultTransformerRegistry(); + + List profilerPluginContextList = pluginContexts.getProfilerPluginContextList(); + for (DefaultProfilerPluginContext pluginContext : profilerPluginContextList) { + for (ClassFileTransformer transformer : pluginContext.getClassEditors()) { + if (transformer instanceof MatchableClassFileTransformer) { + MatchableClassFileTransformer t = (MatchableClassFileTransformer) transformer; + logger.info("Registering class file transformer {} for {} ", t, t.getMatcher()); + registry.addTransformer(t.getMatcher(), t); + } else { + logger.warn("Ignore class file transformer {}", transformer); + } + } + } + + return registry; + } +} \ No newline at end of file diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/DynamicTransformService.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/DynamicTransformService.java index 4381fe3c1..fff415ab8 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/DynamicTransformService.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/DynamicTransformService.java @@ -20,6 +20,7 @@ import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.Instrumentation; import java.lang.instrument.UnmodifiableClassException; +import com.google.inject.Inject; import com.navercorp.pinpoint.bootstrap.instrument.RequestHandle; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,6 +40,11 @@ public class DynamicTransformService implements DynamicTransformTrigger { private DynamicTransformRequestListener dynamicTransformRequestListener; + @Inject + public DynamicTransformService(Instrumentation instrumentation, ClassFileTransformerDispatcher listener) { + this(instrumentation, (DynamicTransformRequestListener)listener); + } + public DynamicTransformService(Instrumentation instrumentation, DynamicTransformRequestListener listener) { Asserts.notNull(instrumentation, "instrumentation"); Asserts.notNull(listener, "listener"); diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/JvmInformation.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/JvmInformation.java index 75dcb2ce9..f89932150 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/JvmInformation.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/JvmInformation.java @@ -24,7 +24,7 @@ public class JvmInformation { private final String jvmVersion; private final int gcTypeCode; - JvmInformation(String jvmVersion, int gcTypeCode) { + public JvmInformation(String jvmVersion, int gcTypeCode) { this.jvmVersion = jvmVersion; this.gcTypeCode = gcTypeCode; } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/ApplicationContext.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/ApplicationContext.java index 468e8aad9..65b59e64e 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/ApplicationContext.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/ApplicationContext.java @@ -18,10 +18,10 @@ package com.navercorp.pinpoint.profiler.context; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; import com.navercorp.pinpoint.bootstrap.context.TraceContext; +import com.navercorp.pinpoint.bootstrap.instrument.DynamicTransformTrigger; import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClassPool; import com.navercorp.pinpoint.profiler.AgentInformation; import com.navercorp.pinpoint.profiler.ClassFileTransformerDispatcher; -import com.navercorp.pinpoint.profiler.DynamicTransformService; import java.lang.instrument.Instrumentation; import java.util.List; @@ -39,7 +39,7 @@ public interface ApplicationContext { List getBootstrapJarPaths(); - DynamicTransformService getDynamicTransformService(); + DynamicTransformTrigger getDynamicTransformTrigger(); Instrumentation getInstrumentation(); diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/ApplicationContextModule.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/ApplicationContextModule.java new file mode 100644 index 000000000..ce879eba9 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/ApplicationContextModule.java @@ -0,0 +1,196 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context; + +import com.google.inject.AbstractModule; +import com.google.inject.Scopes; +import com.google.inject.TypeLiteral; +import com.navercorp.pinpoint.bootstrap.AgentOption; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.bootstrap.context.ServerMetaDataHolder; +import com.navercorp.pinpoint.bootstrap.context.TraceContext; +import com.navercorp.pinpoint.bootstrap.instrument.DynamicTransformTrigger; +import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClassPool; +import com.navercorp.pinpoint.bootstrap.sampler.Sampler; +import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService; +import com.navercorp.pinpoint.common.trace.ServiceType; +import com.navercorp.pinpoint.profiler.AgentInfoSender; +import com.navercorp.pinpoint.profiler.AgentInformation; +import com.navercorp.pinpoint.profiler.ClassFileTransformerDispatcher; +import com.navercorp.pinpoint.profiler.DefaultClassFileTransformerDispatcher; +import com.navercorp.pinpoint.profiler.DynamicTransformService; +import com.navercorp.pinpoint.profiler.JvmInformation; +import com.navercorp.pinpoint.profiler.context.module.AgentId; +import com.navercorp.pinpoint.profiler.context.module.AgentServiceType; +import com.navercorp.pinpoint.profiler.context.module.AgentStartTime; +import com.navercorp.pinpoint.profiler.context.module.ApplicationName; +import com.navercorp.pinpoint.profiler.context.module.BootstrapJarPaths; +import com.navercorp.pinpoint.profiler.context.module.PluginJars; +import com.navercorp.pinpoint.profiler.context.module.SpanDataSender; +import com.navercorp.pinpoint.profiler.context.module.StatDataSender; +import com.navercorp.pinpoint.profiler.context.monitor.PluginMonitorContext; +import com.navercorp.pinpoint.profiler.context.provider.AgentInfoSenderProvider; +import com.navercorp.pinpoint.profiler.context.provider.AgentInformationProvider; +import com.navercorp.pinpoint.profiler.context.provider.AgentServiceTypeProvider; +import com.navercorp.pinpoint.profiler.context.provider.AgentStartTimeProvider; +import com.navercorp.pinpoint.profiler.context.provider.ApplicationServerTypeResolverProvider; +import com.navercorp.pinpoint.profiler.context.provider.ClassFileTransformerDispatcherProvider; +import com.navercorp.pinpoint.profiler.context.provider.ClassFileTransformerWrapProvider; +import com.navercorp.pinpoint.profiler.context.provider.CommandDispatcherProvider; +import com.navercorp.pinpoint.profiler.context.provider.DynamicTransformTriggerProvider; +import com.navercorp.pinpoint.profiler.context.provider.InstrumentEngineProvider; +import com.navercorp.pinpoint.profiler.context.provider.JvmInformationProvider; +import com.navercorp.pinpoint.profiler.context.provider.PinpointClientFactoryProvider; +import com.navercorp.pinpoint.profiler.context.provider.PinpointClientProvider; +import com.navercorp.pinpoint.profiler.context.provider.PluginContextLoadResultProvider; +import com.navercorp.pinpoint.profiler.context.provider.PluginMonitorContextProvider; +import com.navercorp.pinpoint.profiler.context.provider.PluginSetupProvider; +import com.navercorp.pinpoint.profiler.context.provider.SamplerProvider; +import com.navercorp.pinpoint.profiler.context.provider.ServerMetaDataHolderProvider; +import com.navercorp.pinpoint.profiler.context.provider.StorageFactoryProvider; +import com.navercorp.pinpoint.profiler.context.provider.TcpDataSenderProvider; +import com.navercorp.pinpoint.profiler.context.provider.UdpSpanDataSenderProvider; +import com.navercorp.pinpoint.profiler.context.provider.UdpStatDataSenderProvider; +import com.navercorp.pinpoint.profiler.context.storage.StorageFactory; +import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder; +import com.navercorp.pinpoint.profiler.metadata.ApiMetaDataCacheService; +import com.navercorp.pinpoint.profiler.metadata.ApiMetaDataService; +import com.navercorp.pinpoint.profiler.metadata.SqlMetaDataCacheService; +import com.navercorp.pinpoint.profiler.metadata.SqlMetaDataService; +import com.navercorp.pinpoint.profiler.metadata.StringMetaDataCacheService; +import com.navercorp.pinpoint.profiler.metadata.StringMetaDataService; +import com.navercorp.pinpoint.profiler.monitor.AgentStatMonitor; +import com.navercorp.pinpoint.profiler.monitor.DefaultAgentStatMonitor; +import com.navercorp.pinpoint.profiler.monitor.codahale.AgentStatCollectorFactory; +import com.navercorp.pinpoint.profiler.monitor.codahale.DefaultAgentStatCollectorFactory; +import com.navercorp.pinpoint.profiler.plugin.PluginContextLoadResult; +import com.navercorp.pinpoint.profiler.plugin.PluginSetup; +import com.navercorp.pinpoint.profiler.receiver.CommandDispatcher; +import com.navercorp.pinpoint.profiler.sender.DataSender; +import com.navercorp.pinpoint.profiler.sender.EnhancedDataSender; +import com.navercorp.pinpoint.profiler.util.ApplicationServerTypeResolver; +import com.navercorp.pinpoint.rpc.client.PinpointClient; +import com.navercorp.pinpoint.rpc.client.PinpointClientFactory; + +import java.lang.instrument.ClassFileTransformer; +import java.lang.instrument.Instrumentation; +import java.net.URL; +import java.util.List; + + +/** + * @author Woonduk Kang(emeroad) + */ +public class ApplicationContextModule extends AbstractModule { + private final ProfilerConfig profilerConfig; + private final ApplicationContext applicationContext; + private final ServiceTypeRegistryService serviceTypeRegistryService; + private final AgentOption agentOption; + private final InterceptorRegistryBinder interceptorRegistryBinder; + + public ApplicationContextModule(ApplicationContext applicationContext, AgentOption agentOption, ProfilerConfig profilerConfig, + ServiceTypeRegistryService serviceTypeRegistryService, InterceptorRegistryBinder interceptorRegistryBinder) { + if (profilerConfig == null) { + throw new NullPointerException("profilerConfig must not be null"); + } + this.agentOption = agentOption; + this.applicationContext = applicationContext; + this.profilerConfig = profilerConfig; + this.serviceTypeRegistryService = serviceTypeRegistryService; + this.interceptorRegistryBinder = interceptorRegistryBinder; + } + + @Override + protected void configure() { + bind(ApplicationContext.class).toInstance(applicationContext); + bind(ProfilerConfig.class).toInstance(profilerConfig); + bind(ServiceTypeRegistryService.class).toInstance(serviceTypeRegistryService); + bind(AgentOption.class).toInstance(agentOption); + bind(Instrumentation.class).toInstance(agentOption.getInstrumentation()); + bind(InterceptorRegistryBinder.class).toInstance(interceptorRegistryBinder); + + bind(URL[].class).annotatedWith(PluginJars.class).toInstance(agentOption.getPluginJars()); + + TypeLiteral> listString = new TypeLiteral>() {}; + bind(listString).annotatedWith(BootstrapJarPaths.class).toInstance(agentOption.getBootstrapJarPaths()); + + bindAgentInformation(agentOption.getAgentId(), agentOption.getApplicationName()); + + bindDataTransferComponent(); + + bind(ServerMetaDataHolder.class).toProvider(ServerMetaDataHolderProvider.class).in(Scopes.SINGLETON); + bind(StorageFactory.class).toProvider(StorageFactoryProvider.class).in(Scopes.SINGLETON); + + + bindServiceComponent(); + + bind(PluginMonitorContext.class).toProvider(PluginMonitorContextProvider.class).in(Scopes.SINGLETON); + + bind(IdGenerator.class).to(AtomicIdGenerator.class); + bind(TransactionCounter.class).to(DefaultTransactionCounter.class).in(Scopes.SINGLETON); + + bind(Sampler.class).toProvider(SamplerProvider.class).in(Scopes.SINGLETON); + bind(TraceFactoryBuilder.class).to(DefaultTraceFactoryBuilder.class).in(Scopes.SINGLETON); + bind(TraceContext.class).to(DefaultTraceContext.class).in(Scopes.SINGLETON); + bind(AgentStatCollectorFactory.class).to(DefaultAgentStatCollectorFactory.class).in(Scopes.SINGLETON); + bind(AgentStatMonitor.class).to(DefaultAgentStatMonitor.class).in(Scopes.SINGLETON); + + bind(PluginSetup.class).toProvider(PluginSetupProvider.class).in(Scopes.SINGLETON); + bind(PluginContextLoadResult.class).toProvider(PluginContextLoadResultProvider.class).in(Scopes.SINGLETON); + bind(ApplicationServerTypeResolver.class).toProvider(ApplicationServerTypeResolverProvider.class).in(Scopes.SINGLETON); + bind(AgentInformation.class).toProvider(AgentInformationProvider.class).in(Scopes.SINGLETON); +// bind(DefaultClassFileTransformerDispatcher.class).to(DefaultClassFileTransformerDispatcher.class).in(Scopes.SINGLETON); + bind(JvmInformation.class).toProvider(JvmInformationProvider.class).in(Scopes.SINGLETON); + bind(AgentInfoSender.class).toProvider(AgentInfoSenderProvider.class).in(Scopes.SINGLETON); + + + bind(InstrumentClassPool.class).toProvider(InstrumentEngineProvider.class).in(Scopes.SINGLETON); + bind(ClassFileTransformerDispatcher.class).toProvider(ClassFileTransformerDispatcherProvider.class).in(Scopes.SINGLETON); + bind(DynamicTransformTrigger.class).toProvider(DynamicTransformTriggerProvider.class).in(Scopes.SINGLETON); + bind(ClassFileTransformer.class).toProvider(ClassFileTransformerWrapProvider.class).in(Scopes.SINGLETON); + } + + private void bindDataTransferComponent() { + // create tcp channel + + bind(PinpointClientFactory.class).toProvider(PinpointClientFactoryProvider.class).in(Scopes.SINGLETON); + bind(EnhancedDataSender.class).toProvider(TcpDataSenderProvider.class).in(Scopes.SINGLETON); + bind(PinpointClient.class).toProvider(PinpointClientProvider.class).in(Scopes.SINGLETON); + + bind(CommandDispatcher.class).toProvider(CommandDispatcherProvider.class).in(Scopes.SINGLETON); + + bind(DataSender.class).annotatedWith(SpanDataSender.class) + .toProvider(UdpSpanDataSenderProvider.class).in(Scopes.SINGLETON); + bind(DataSender.class).annotatedWith(StatDataSender.class) + .toProvider(UdpStatDataSenderProvider.class).in(Scopes.SINGLETON); + } + + private void bindServiceComponent() { + + bind(StringMetaDataService.class).to(StringMetaDataCacheService.class).in(Scopes.SINGLETON); + bind(ApiMetaDataService.class).to(ApiMetaDataCacheService.class).in(Scopes.SINGLETON); + bind(SqlMetaDataService.class).to(SqlMetaDataCacheService.class).in(Scopes.SINGLETON); + } + + private void bindAgentInformation(String agentId, String applicationName) { + + bind(String.class).annotatedWith(AgentId.class).toInstance(agentId); + bind(String.class).annotatedWith(ApplicationName.class).toInstance(applicationName); + bind(Long.class).annotatedWith(AgentStartTime.class).toProvider(AgentStartTimeProvider.class); + bind(ServiceType.class).annotatedWith(AgentServiceType.class).toProvider(AgentServiceTypeProvider.class); + } +} \ No newline at end of file diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AtomicIdGenerator.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AtomicIdGenerator.java new file mode 100644 index 000000000..560802d8f --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AtomicIdGenerator.java @@ -0,0 +1,95 @@ +/* + * Copyright 2014 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context; + +import com.navercorp.pinpoint.profiler.util.jdk.LongAdder; + +import java.util.concurrent.atomic.AtomicLong; + +/** + * @author emeroad + * @author HyunGil Jeong + */ +public class AtomicIdGenerator implements IdGenerator { + // TODO might be a good idea to refactor these into SamplingType + + // Reserved negative space (0 ~ -1000) + public static final long UNTRACKED_ID = 0L; + public static final long RESERVED_MAX = 0L; + public static final long RESERVED_MIN = -1000; + + // Positive value for sampled new traces + public static final long INITIAL_TRANSACTION_ID = 1L; + // Negative value for sampled continuations, and unsampled new traces/continuations + public static final long INITIAL_CONTINUED_TRANSACTION_ID = RESERVED_MIN - 1; // -1001 + public static final long INITIAL_DISABLED_ID = RESERVED_MIN - 2; // -1002 + public static final long INITIAL_CONTINUED_DISABLED_ID = RESERVED_MIN - 3; // -1003 + + public static final int DECREMENT_CYCLE = 3; + public static final int NEGATIVE_DECREMENT_CYCLE = DECREMENT_CYCLE * -1; + + // Unique id for tracing a internal stacktrace and calculating a slow time of activethreadcount + // moved here in order to make codes simpler for now + // id generator for sampled new traces + private final AtomicLong transactionId = new AtomicLong(INITIAL_TRANSACTION_ID); + // id generator for sampled continued traces + private final AtomicLong continuedTransactionId = new AtomicLong(INITIAL_CONTINUED_TRANSACTION_ID); + // id generator for unsampled new traces + private final AtomicLong disabledId = new AtomicLong(INITIAL_DISABLED_ID); + // id generator for unsampled continued traces + private final AtomicLong continuedDisabledId = new AtomicLong(INITIAL_CONTINUED_DISABLED_ID); + + @Override + public long nextTransactionId() { + return this.transactionId.getAndIncrement(); + } + + @Override + public long nextContinuedTransactionId() { + return this.continuedTransactionId.getAndAdd(NEGATIVE_DECREMENT_CYCLE); + } + + @Override + public long nextDisabledId() { + return this.disabledId.getAndAdd(NEGATIVE_DECREMENT_CYCLE); + } + + @Override + public long nextContinuedDisabledId() { + return this.continuedDisabledId.getAndAdd(NEGATIVE_DECREMENT_CYCLE); + } + + @Override + public long currentTransactionId() { + return this.transactionId.get(); + } + + @Override + public long currentContinuedTransactionId() { + return this.continuedTransactionId.get(); + } + + @Override + public long currentDisabledId() { + return this.disabledId.get(); + } + + @Override + public long currentContinuedDisabledId() { + return this.continuedDisabledId.get(); + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultApplicationContext.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultApplicationContext.java index 7410e3412..dc175da13 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultApplicationContext.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultApplicationContext.java @@ -16,56 +16,26 @@ package com.navercorp.pinpoint.profiler.context; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.Module; +import com.google.inject.Stage; import com.navercorp.pinpoint.bootstrap.AgentOption; -import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; -import com.navercorp.pinpoint.bootstrap.context.ServerMetaDataHolder; import com.navercorp.pinpoint.bootstrap.context.TraceContext; +import com.navercorp.pinpoint.bootstrap.instrument.DynamicTransformTrigger; import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClassPool; -import com.navercorp.pinpoint.bootstrap.sampler.Sampler; import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService; -import com.navercorp.pinpoint.common.trace.ServiceType; import com.navercorp.pinpoint.profiler.AgentInfoSender; import com.navercorp.pinpoint.profiler.AgentInformation; -import com.navercorp.pinpoint.profiler.AgentInformationFactory; import com.navercorp.pinpoint.profiler.ClassFileTransformerDispatcher; -import com.navercorp.pinpoint.profiler.DynamicTransformService; -import com.navercorp.pinpoint.profiler.JvmInformationFactory; -import com.navercorp.pinpoint.profiler.context.active.ActiveTraceRepository; -import com.navercorp.pinpoint.profiler.context.monitor.PluginMonitorContext; -import com.navercorp.pinpoint.profiler.context.provider.PinpointClientFactoryProvider; -import com.navercorp.pinpoint.profiler.context.provider.PinpointClientProvider; -import com.navercorp.pinpoint.profiler.context.provider.Provider; -import com.navercorp.pinpoint.profiler.context.provider.ServerMetaDataHolderProvider; -import com.navercorp.pinpoint.profiler.context.provider.StorageFactoryProvider; -import com.navercorp.pinpoint.profiler.context.provider.TcpDataSenderProvider; -import com.navercorp.pinpoint.profiler.context.provider.UdpSpanDataSenderProvider; -import com.navercorp.pinpoint.profiler.context.provider.UdpStatDataSenderProvider; -import com.navercorp.pinpoint.profiler.context.storage.StorageFactory; -import com.navercorp.pinpoint.profiler.instrument.ASMBytecodeDumpService; -import com.navercorp.pinpoint.profiler.instrument.ASMClassPool; -import com.navercorp.pinpoint.profiler.instrument.BytecodeDumpTransformer; -import com.navercorp.pinpoint.profiler.instrument.JavassistClassPool; +import com.navercorp.pinpoint.profiler.context.module.SpanDataSender; +import com.navercorp.pinpoint.profiler.context.module.StatDataSender; import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder; -import com.navercorp.pinpoint.profiler.metadata.ApiMetaDataCacheService; -import com.navercorp.pinpoint.profiler.metadata.ApiMetaDataService; -import com.navercorp.pinpoint.profiler.metadata.SqlMetaDataCacheService; -import com.navercorp.pinpoint.profiler.metadata.SqlMetaDataService; -import com.navercorp.pinpoint.profiler.metadata.StringMetaDataCacheService; -import com.navercorp.pinpoint.profiler.metadata.StringMetaDataService; import com.navercorp.pinpoint.profiler.monitor.AgentStatMonitor; -import com.navercorp.pinpoint.profiler.monitor.codahale.AgentStatCollectorFactory; -import com.navercorp.pinpoint.profiler.plugin.DefaultProfilerPluginContext; -import com.navercorp.pinpoint.profiler.plugin.ProfilerPluginLoader; -import com.navercorp.pinpoint.profiler.receiver.CommandDispatcher; -import com.navercorp.pinpoint.profiler.receiver.ProfilerCommandLocatorBuilder; -import com.navercorp.pinpoint.profiler.receiver.ProfilerCommandServiceLocator; -import com.navercorp.pinpoint.profiler.receiver.service.ActiveThreadService; -import com.navercorp.pinpoint.profiler.receiver.service.EchoService; -import com.navercorp.pinpoint.profiler.sampler.SamplerFactory; import com.navercorp.pinpoint.profiler.sender.DataSender; import com.navercorp.pinpoint.profiler.sender.EnhancedDataSender; -import com.navercorp.pinpoint.profiler.util.ApplicationServerTypeResolver; import com.navercorp.pinpoint.rpc.client.PinpointClient; import com.navercorp.pinpoint.rpc.client.PinpointClientFactory; import org.slf4j.Logger; @@ -97,16 +67,18 @@ public class DefaultApplicationContext implements ApplicationContext { private final DataSender spanDataSender; private final AgentInformation agentInformation; - private final ServerMetaDataHolder serverMetaDataHolder; private final AgentOption agentOption; private final ServiceTypeRegistryService serviceTypeRegistryService; - private final ClassFileTransformerDispatcher classFileTransformer; + private final ClassFileTransformerDispatcher classFileDispatcher; private final Instrumentation instrumentation; private final InstrumentClassPool classPool; - private final DynamicTransformService dynamicTransformService; + private final DynamicTransformTrigger dynamicTransformTrigger; + + private final Injector injector; + public DefaultApplicationContext(AgentOption agentOption, final InterceptorRegistryBinder interceptorRegistryBinder) { if (agentOption == null) { @@ -121,78 +93,57 @@ public class DefaultApplicationContext implements ApplicationContext { this.instrumentation = agentOption.getInstrumentation(); this.serviceTypeRegistryService = agentOption.getServiceTypeRegistryService(); - this.classPool = createInstrumentEngine(this.profilerConfig, agentOption, interceptorRegistryBinder); - if (logger.isInfoEnabled()) { logger.info("DefaultAgent classLoader:{}", this.getClass().getClassLoader()); } - List pluginContexts = loadPlugins(agentOption); + final Module applicationContextModule = newApplicationContextModule(agentOption, interceptorRegistryBinder); + this.injector = Guice.createInjector(Stage.PRODUCTION, applicationContextModule); - this.classFileTransformer = new ClassFileTransformerDispatcher(this, pluginContexts); - this.dynamicTransformService = new DynamicTransformService(instrumentation, classFileTransformer); + this.classPool = injector.getInstance(InstrumentClassPool.class); - ClassFileTransformer wrappedTransformer = wrapClassFileTransformer(this.profilerConfig, classFileTransformer); - instrumentation.addTransformer(wrappedTransformer, true); + this.classFileDispatcher = injector.getInstance(ClassFileTransformerDispatcher.class); + this.dynamicTransformTrigger = injector.getInstance(DynamicTransformTrigger.class); + ClassFileTransformer classFileTransformer = injector.getInstance(ClassFileTransformer.class); + instrumentation.addTransformer(classFileTransformer, true); - String applicationServerTypeString = profilerConfig.getApplicationServerType(); - ServiceType applicationServerType = this.serviceTypeRegistryService.findServiceTypeByName(applicationServerTypeString); - - final ApplicationServerTypeResolver typeResolver = new ApplicationServerTypeResolver(pluginContexts, applicationServerType, profilerConfig.getApplicationTypeDetectOrder()); - - final AgentInformationFactory agentInformationFactory = new AgentInformationFactory(agentOption.getAgentId(), agentOption.getApplicationName()); - this.agentInformation = agentInformationFactory.createAgentInformation(typeResolver.resolve()); - logger.info("agentInformation:{}", agentInformation); - - final Provider serverMetaDataHolderProvider = newServerMetaDataHolderProvider(); - this.serverMetaDataHolder = serverMetaDataHolderProvider.get(); - - Provider udpSpanDataSenderProvider = newUdpSpanDataSenderProvider(); - this.spanDataSender = udpSpanDataSenderProvider.get(); + this.spanDataSender = newUdpSpanDataSender(); logger.info("spanDataSender:{}", spanDataSender); - Provider udpStatDataSenderProvider = newUdpStatDataSenderProvider(); - this.statDataSender = udpStatDataSenderProvider.get(); + this.statDataSender = newUdpStatDataSender(); logger.info("statDataSender:{}", statDataSender); - final ActiveTraceRepository activeTraceRepository = createActiveTraceRepository(profilerConfig); - final CommandDispatcher commandService = createCommandService(profilerConfig, activeTraceRepository); - - Provider pinpointClientFactoryProvider = newPinpointClientFactoryProvider(profilerConfig, this.agentInformation, commandService); - this.clientFactory = pinpointClientFactoryProvider.get(); + this.clientFactory = injector.getInstance(PinpointClientFactory.class); logger.info("clientFactory:{}", clientFactory); - Provider pinpointClientProvider = newPinpointClientProvider(profilerConfig, clientFactory); - this.client = pinpointClientProvider.get(); + this.client = injector.getInstance(PinpointClient.class); logger.info("client:{}", client); - Provider tcpDataSenderProvider = newTcpDataSenderProvider(client); - this.tcpDataSender = tcpDataSenderProvider.get(); + this.tcpDataSender = injector.getInstance(EnhancedDataSender.class); logger.info("tcpDataSender:{}", tcpDataSender); - final IdGenerator idGenerator = new IdGenerator(); - final TransactionCounter transactionCounter = new DefaultTransactionCounter(idGenerator); + this.traceContext = injector.getInstance(TraceContext.class); - final PluginMonitorContext pluginMonitorContext = createPluginMonitorContext(this.profilerConfig); + this.agentInformation = injector.getInstance(AgentInformation.class); + logger.info("agentInformation:{}", agentInformation); - Provider storageFactoryProvider = newStorageFactoryProvider(profilerConfig, spanDataSender, agentInformation); - final StorageFactory storageFactory = storageFactoryProvider.get(); - this.traceContext = newTraceContext(this.profilerConfig, storageFactory, this.serverMetaDataHolder, this.tcpDataSender, idGenerator, activeTraceRepository, pluginMonitorContext); - final AgentStatCollectorFactory agentStatCollectorFactory = new AgentStatCollectorFactory(profilerConfig, activeTraceRepository, transactionCounter, pluginMonitorContext); - - final JvmInformationFactory jvmInformationFactory = new JvmInformationFactory(agentStatCollectorFactory.getGarbageCollector()); - - this.agentInfoSender = new AgentInfoSender.Builder(this.tcpDataSender, this.agentInformation, jvmInformationFactory.createJvmInformation()).sendInterval(profilerConfig.getAgentInfoSendRetryInterval()).build(); - this.serverMetaDataHolder.addListener(this.agentInfoSender); - this.agentStatMonitor = new AgentStatMonitor(this.statDataSender, this.agentInformation.getAgentId(), this.agentInformation.getStartTime(), agentStatCollectorFactory); + this.agentInfoSender = injector.getInstance(AgentInfoSender.class); + this.agentStatMonitor = injector.getInstance(AgentStatMonitor.class); } - protected Provider newUdpStatDataSenderProvider() { - return new UdpStatDataSenderProvider(profilerConfig); + protected Module newApplicationContextModule(AgentOption agentOption, InterceptorRegistryBinder interceptorRegistryBinder) { + return new ApplicationContextModule(this, agentOption, profilerConfig, serviceTypeRegistryService, interceptorRegistryBinder); } - protected Provider newUdpSpanDataSenderProvider() { - return new UdpSpanDataSenderProvider(profilerConfig); + protected DataSender newUdpStatDataSender() { + + Key statDataSenderKey = Key.get(DataSender.class, StatDataSender.class); + return injector.getInstance(statDataSenderKey); + } + + protected DataSender newUdpSpanDataSender() { + Key spanDataSenderKey = Key.get(DataSender.class, SpanDataSender.class); + return injector.getInstance(spanDataSenderKey); } @Override @@ -200,8 +151,8 @@ public class DefaultApplicationContext implements ApplicationContext { return profilerConfig; } - protected Provider newServerMetaDataHolderProvider() { - return new ServerMetaDataHolderProvider(); + public Injector getInjector() { + return injector; } @Override @@ -219,68 +170,14 @@ public class DefaultApplicationContext implements ApplicationContext { } - private ActiveTraceRepository createActiveTraceRepository(ProfilerConfig profilerConfig) { - if (profilerConfig.isTraceAgentActiveThread()) { - return new ActiveTraceRepository(); - } - return null; - } - - private InstrumentClassPool createInstrumentEngine(ProfilerConfig profilerConfig, AgentOption agentOption, InterceptorRegistryBinder interceptorRegistryBinder) { - - final String instrumentEngine = profilerConfig.getProfileInstrumentEngine().toUpperCase(); - - if (DefaultProfilerConfig.INSTRUMENT_ENGINE_ASM.equals(instrumentEngine)) { - logger.info("ASM InstrumentEngine."); - - return new ASMClassPool(interceptorRegistryBinder, agentOption.getBootstrapJarPaths()); - - } else if (DefaultProfilerConfig.INSTRUMENT_ENGINE_JAVASSIST.equals(instrumentEngine)) { - logger.info("JAVASSIST InstrumentEngine."); - - return new JavassistClassPool(interceptorRegistryBinder, agentOption.getBootstrapJarPaths()); - } else { - logger.warn("Unknown InstrumentEngine:{}", instrumentEngine); - - throw new IllegalArgumentException("Unknown InstrumentEngine:" + instrumentEngine); - } - } - - private ClassFileTransformer wrapClassFileTransformer(ProfilerConfig profilerConfig, ClassFileTransformer classFileTransformerDispatcher) { - final boolean enableBytecodeDump = profilerConfig.readBoolean(ASMBytecodeDumpService.ENABLE_BYTECODE_DUMP, ASMBytecodeDumpService.ENABLE_BYTECODE_DUMP_DEFAULT_VALUE); - if (enableBytecodeDump) { - logger.info("wrapBytecodeDumpTransformer"); - return BytecodeDumpTransformer.wrap(classFileTransformerDispatcher, profilerConfig); - } - return classFileTransformerDispatcher; - } - @Override public List getBootstrapJarPaths() { return agentOption.getBootstrapJarPaths(); } - protected List loadPlugins(AgentOption agentOption) { - final ProfilerPluginLoader loader = new ProfilerPluginLoader(this); - return loader.load(agentOption.getPluginJars()); - } - - private CommandDispatcher createCommandService(ProfilerConfig profilerConfig, ActiveTraceRepository activeTraceRepository) { - ProfilerCommandLocatorBuilder builder = new ProfilerCommandLocatorBuilder(); - builder.addService(new EchoService()); - if (activeTraceRepository != null) { - ActiveThreadService activeThreadService = new ActiveThreadService(profilerConfig, activeTraceRepository); - builder.addService(activeThreadService); - } - - ProfilerCommandServiceLocator commandServiceLocator = builder.build(); - CommandDispatcher commandDispatcher = new CommandDispatcher(commandServiceLocator); - return commandDispatcher; - } - @Override - public DynamicTransformService getDynamicTransformService() { - return dynamicTransformService; + public DynamicTransformTrigger getDynamicTransformTrigger() { + return dynamicTransformTrigger; } @Override @@ -290,7 +187,7 @@ public class DefaultApplicationContext implements ApplicationContext { @Override public ClassFileTransformerDispatcher getClassFileTransformerDispatcher() { - return classFileTransformer; + return classFileDispatcher; } @Override @@ -300,85 +197,6 @@ public class DefaultApplicationContext implements ApplicationContext { - private TraceContext newTraceContext(ProfilerConfig profilerConfig, StorageFactory storageFactory, ServerMetaDataHolder serverMetaDataHolder, EnhancedDataSender enhancedDataSender, IdGenerator idGenerator, ActiveTraceRepository activeTraceRepository, PluginMonitorContext pluginMonitorContext) { - - - logger.info("StorageFactoryType:{}", storageFactory); - - final Sampler sampler = createSampler(profilerConfig); - logger.info("SamplerType:{}", sampler); - - final TraceFactoryBuilder traceFactoryBuilder = createTraceFactory(storageFactory, sampler, idGenerator, activeTraceRepository); - - final String agentId = this.agentInformation.getAgentId(); - final long agentStartTime = this.agentInformation.getStartTime(); - - final ApiMetaDataService apiMetaDataService = newApiMetaDataCacheService(profilerConfig, agentId, agentStartTime, enhancedDataSender); - final StringMetaDataService stringMetaDataService = newStringMetaDataCacheService(profilerConfig, agentId, agentStartTime, enhancedDataSender); - final SqlMetaDataService sqlMetaDataService = newSqlMetaDataService(profilerConfig, agentId, agentStartTime, enhancedDataSender); - - final TraceContext traceContext = new DefaultTraceContext(profilerConfig, this.agentInformation, - traceFactoryBuilder, pluginMonitorContext, serverMetaDataHolder, - apiMetaDataService, stringMetaDataService, sqlMetaDataService - ); - - return traceContext; - } - - private SqlMetaDataService newSqlMetaDataService(ProfilerConfig profilerConfig, String agentId, long agentStartTime, EnhancedDataSender enhancedDataSender) { - int jdbcSqlCacheSize = profilerConfig.getJdbcSqlCacheSize(); - return new SqlMetaDataCacheService(agentId, agentStartTime, enhancedDataSender, jdbcSqlCacheSize); - } - - private StringMetaDataCacheService newStringMetaDataCacheService(ProfilerConfig profilerConfig, String agentId, long agentStartTime, EnhancedDataSender enhancedDataSender) { - return new StringMetaDataCacheService(agentId, agentStartTime, enhancedDataSender); - } - - protected ApiMetaDataService newApiMetaDataCacheService(ProfilerConfig profilerConfig, String agentId, long agentStartTime, EnhancedDataSender enhancedDataSender) { - return new ApiMetaDataCacheService(agentId, agentStartTime, enhancedDataSender); - } - - - private PluginMonitorContext createPluginMonitorContext(ProfilerConfig profilerConfig) { - final boolean traceDataSource = profilerConfig.isTraceAgentDataSource(); - final PluginMonitorContextBuilder monitorContextBuilder = new PluginMonitorContextBuilder(traceDataSource); - return monitorContextBuilder.build(); - } - - private TraceFactoryBuilder createTraceFactory(StorageFactory storageFactory, Sampler sampler, IdGenerator idGenerator, ActiveTraceRepository activeTraceRepository) { - - final TraceFactoryBuilder builder = new DefaultTraceFactoryBuilder(storageFactory, sampler, idGenerator, activeTraceRepository); - return builder; - } - - - protected Provider newStorageFactoryProvider(ProfilerConfig profilerConfig, DataSender spanDataSender, AgentInformation agentInformation) { - return new StorageFactoryProvider(profilerConfig, spanDataSender, agentInformation); - } - - private Sampler createSampler(ProfilerConfig profilerConfig) { - boolean samplingEnable = profilerConfig.isSamplingEnable(); - int samplingRate = profilerConfig.getSamplingRate(); - - SamplerFactory samplerFactory = new SamplerFactory(); - return samplerFactory.createSampler(samplingEnable, samplingRate); - } - - protected Provider newPinpointClientFactoryProvider(ProfilerConfig profilerConfig, AgentInformation agentInformation, CommandDispatcher commandDispatcher) { - Provider pinpointClientFactoryProvider = new PinpointClientFactoryProvider(profilerConfig, agentInformation, commandDispatcher); - return pinpointClientFactoryProvider; - } - - protected Provider newPinpointClientProvider(ProfilerConfig profilerConfig, PinpointClientFactory clientFactory) { - return new PinpointClientProvider(profilerConfig, clientFactory); - } - - protected Provider newTcpDataSenderProvider(PinpointClient client) { - return new TcpDataSenderProvider(client); - } - - - @Override public void start() { this.agentInfoSender.start(); diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultBaseTraceFactory.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultBaseTraceFactory.java index 21a500e71..bedd6d2ff 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultBaseTraceFactory.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultBaseTraceFactory.java @@ -39,9 +39,9 @@ public class DefaultBaseTraceFactory implements BaseTraceFactory { private final StorageFactory storageFactory; private final Sampler sampler; - private final IdGenerator idGenerator; + private final AtomicIdGenerator idGenerator; - public DefaultBaseTraceFactory(TraceContext traceContext, StorageFactory storageFactory, Sampler sampler, IdGenerator idGenerator) { + public DefaultBaseTraceFactory(TraceContext traceContext, StorageFactory storageFactory, Sampler sampler, AtomicIdGenerator idGenerator) { if (traceContext == null) { throw new NullPointerException("traceContext must not be null"); } @@ -109,7 +109,7 @@ public class DefaultBaseTraceFactory implements BaseTraceFactory { final boolean sampling = true; final Storage storage = storageFactory.createStorage(); final Storage asyncStorage = new AsyncStorage(storage); - final Trace trace = new DefaultTrace(traceContext, asyncStorage, parentTraceId, IdGenerator.UNTRACKED_ID, sampling); + final Trace trace = new DefaultTrace(traceContext, asyncStorage, parentTraceId, AtomicIdGenerator.UNTRACKED_ID, sampling); final AsyncTrace asyncTrace = new AsyncTrace(trace, asyncId, traceId.nextAsyncSequence(), startTime); diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTraceContext.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTraceContext.java index db763d27b..cd4e15632 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTraceContext.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTraceContext.java @@ -16,6 +16,7 @@ package com.navercorp.pinpoint.profiler.context; +import com.google.inject.Inject; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; import com.navercorp.pinpoint.bootstrap.context.AsyncTraceId; import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor; @@ -58,6 +59,7 @@ public class DefaultTraceContext implements TraceContext { private final AsyncIdGenerator asyncIdGenerator = new AsyncIdGenerator(); + @Inject public DefaultTraceContext(ProfilerConfig profilerConfig, final AgentInformation agentInformation, TraceFactoryBuilder traceFactoryBuilder, PluginMonitorContext pluginMonitorContext, diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTraceFactoryBuilder.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTraceFactoryBuilder.java index 1fe134376..2ae81ed1b 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTraceFactoryBuilder.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTraceFactoryBuilder.java @@ -16,6 +16,7 @@ package com.navercorp.pinpoint.profiler.context; +import com.google.inject.Inject; import com.navercorp.pinpoint.bootstrap.context.TraceContext; import com.navercorp.pinpoint.bootstrap.sampler.Sampler; import com.navercorp.pinpoint.profiler.context.active.ActiveTraceFactory; @@ -33,10 +34,11 @@ public class DefaultTraceFactoryBuilder implements TraceFactoryBuilder { private final StorageFactory storageFactory; private final Sampler sampler; - private final IdGenerator idGenerator; + private final AtomicIdGenerator idGenerator; private final ActiveTraceRepository activeTraceRepository; - public DefaultTraceFactoryBuilder(StorageFactory storageFactory, Sampler sampler, IdGenerator idGenerator, ActiveTraceRepository activeTraceRepository) { + @Inject + public DefaultTraceFactoryBuilder(StorageFactory storageFactory, Sampler sampler, AtomicIdGenerator idGenerator, ActiveTraceRepository activeTraceRepository) { if (storageFactory == null) { throw new NullPointerException("storageFactory must not be null"); } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTransactionCounter.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTransactionCounter.java index 004ae45d6..dd50a482f 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTransactionCounter.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTransactionCounter.java @@ -16,6 +16,8 @@ package com.navercorp.pinpoint.profiler.context; +import com.google.inject.Inject; + /** * @author HyunGil Jeong */ @@ -23,6 +25,7 @@ public class DefaultTransactionCounter implements TransactionCounter { private final IdGenerator idGenerator; + @Inject public DefaultTransactionCounter(IdGenerator idGenerator) { if (idGenerator == null) { throw new NullPointerException("idGenerator cannot be null"); @@ -35,13 +38,13 @@ public class DefaultTransactionCounter implements TransactionCounter { // overflow improbable switch (samplingType) { case SAMPLED_NEW: - return idGenerator.currentTransactionId() - IdGenerator.INITIAL_TRANSACTION_ID; + return idGenerator.currentTransactionId() - AtomicIdGenerator.INITIAL_TRANSACTION_ID; case SAMPLED_CONTINUATION: - return Math.abs(idGenerator.currentContinuedTransactionId() - IdGenerator.INITIAL_CONTINUED_TRANSACTION_ID) / IdGenerator.DECREMENT_CYCLE; + return Math.abs(idGenerator.currentContinuedTransactionId() - AtomicIdGenerator.INITIAL_CONTINUED_TRANSACTION_ID) / AtomicIdGenerator.DECREMENT_CYCLE; case UNSAMPLED_NEW: - return Math.abs(idGenerator.currentDisabledId() - IdGenerator.INITIAL_DISABLED_ID) / IdGenerator.DECREMENT_CYCLE; + return Math.abs(idGenerator.currentDisabledId() - AtomicIdGenerator.INITIAL_DISABLED_ID) / AtomicIdGenerator.DECREMENT_CYCLE; case UNSAMPLED_CONTINUATION: - return Math.abs(idGenerator.currentContinuedDisabledId() - IdGenerator.INITIAL_CONTINUED_DISABLED_ID) / IdGenerator.DECREMENT_CYCLE; + return Math.abs(idGenerator.currentContinuedDisabledId() - AtomicIdGenerator.INITIAL_CONTINUED_DISABLED_ID) / AtomicIdGenerator.DECREMENT_CYCLE; default: return 0L; } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/IdGenerator.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/IdGenerator.java index 6fa0d2979..def6c90ae 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/IdGenerator.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/IdGenerator.java @@ -1,11 +1,11 @@ /* - * Copyright 2014 NAVER Corp. + * Copyright 2017 NAVER Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -16,71 +16,25 @@ package com.navercorp.pinpoint.profiler.context; -import java.util.concurrent.atomic.AtomicLong; - /** - * @author emeroad - * @author HyunGil Jeong + * @author Woonduk Kang(emeroad) */ -public class IdGenerator { +public interface IdGenerator { - // TODO might be a good idea to refactor these into SamplingType - // Reserved negative space (0 ~ -1000) - public static final long UNTRACKED_ID = 0L; - public static final long RESERVED_MAX = 0L; - public static final long RESERVED_MIN = -1000; + long nextTransactionId(); - // Positive value for sampled new traces - public static final long INITIAL_TRANSACTION_ID = 1L; - // Negative value for sampled continuations, and unsampled new traces/continuations - public static final long INITIAL_CONTINUED_TRANSACTION_ID = RESERVED_MIN - 1; // -1001 - public static final long INITIAL_DISABLED_ID = RESERVED_MIN - 2; // -1002 - public static final long INITIAL_CONTINUED_DISABLED_ID = RESERVED_MIN - 3; // -1003 + long nextContinuedTransactionId(); - public static final int DECREMENT_CYCLE = 3; - public static final int NEGATIVE_DECREMENT_CYCLE = DECREMENT_CYCLE * -1; + long nextDisabledId(); - // Unique id for tracing a internal stacktrace and calculating a slow time of activethreadcount - // moved here in order to make codes simpler for now - // id generator for sampled new traces - private final AtomicLong transactionId = new AtomicLong(INITIAL_TRANSACTION_ID); - // id generator for sampled continued traces - private final AtomicLong continuedTransactionId = new AtomicLong(INITIAL_CONTINUED_TRANSACTION_ID); - // id generator for unsampled new traces - private final AtomicLong disabledId = new AtomicLong(INITIAL_DISABLED_ID); - // id generator for unsampled continued traces - private final AtomicLong continuedDisabledId = new AtomicLong(INITIAL_CONTINUED_DISABLED_ID); + long nextContinuedDisabledId(); - public long nextTransactionId() { - return this.transactionId.getAndIncrement(); - } + long currentTransactionId(); - public long nextContinuedTransactionId() { - return this.continuedTransactionId.getAndAdd(NEGATIVE_DECREMENT_CYCLE); - } + long currentContinuedTransactionId(); - public long nextDisabledId() { - return this.disabledId.getAndAdd(NEGATIVE_DECREMENT_CYCLE); - } + long currentDisabledId(); - public long nextContinuedDisabledId() { - return this.continuedDisabledId.getAndAdd(NEGATIVE_DECREMENT_CYCLE); - } - - public long currentTransactionId() { - return this.transactionId.get(); - } - - public long currentContinuedTransactionId() { - return this.continuedTransactionId.get(); - } - - public long currentDisabledId() { - return this.disabledId.get(); - } - - public long currentContinuedDisabledId() { - return this.continuedDisabledId.get(); - } + long currentContinuedDisabledId(); } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/AgentId.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/AgentId.java new file mode 100644 index 000000000..e8fdc396a --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/AgentId.java @@ -0,0 +1,34 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.module; + +import com.google.inject.BindingAnnotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * @author Woonduk Kang(emeroad) + */ +@BindingAnnotation +@Target(PARAMETER) +@Retention(RUNTIME) +public @interface AgentId { +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/AgentServiceType.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/AgentServiceType.java new file mode 100644 index 000000000..bfc872b1b --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/AgentServiceType.java @@ -0,0 +1,34 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.module; + +import com.google.inject.BindingAnnotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * @author Woonduk Kang(emeroad) + */ +@BindingAnnotation +@Target(PARAMETER) +@Retention(RUNTIME) +public @interface AgentServiceType { +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/AgentStartTime.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/AgentStartTime.java new file mode 100644 index 000000000..d71e79467 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/AgentStartTime.java @@ -0,0 +1,34 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.module; + +import com.google.inject.BindingAnnotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * @author Woonduk Kang(emeroad) + */ +@BindingAnnotation +@Target(PARAMETER) +@Retention(RUNTIME) +public @interface AgentStartTime { +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ApplicationName.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ApplicationName.java new file mode 100644 index 000000000..f68b1928d --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ApplicationName.java @@ -0,0 +1,34 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.module; + +import com.google.inject.BindingAnnotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * @author Woonduk Kang(emeroad) + */ +@BindingAnnotation +@Target(PARAMETER) +@Retention(RUNTIME) +public @interface ApplicationName { +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/BootstrapJarPaths.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/BootstrapJarPaths.java new file mode 100644 index 000000000..fa1742513 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/BootstrapJarPaths.java @@ -0,0 +1,34 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.module; + +import com.google.inject.BindingAnnotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * @author Woonduk Kang(emeroad) + */ +@BindingAnnotation +@Target(PARAMETER) +@Retention(RUNTIME) +public @interface BootstrapJarPaths { +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/PluginJars.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/PluginJars.java new file mode 100644 index 000000000..af0160f2f --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/PluginJars.java @@ -0,0 +1,34 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.module; + +import com.google.inject.BindingAnnotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * @author Woonduk Kang(emeroad) + */ +@BindingAnnotation +@Target(PARAMETER) +@Retention(RUNTIME) +public @interface PluginJars { +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/SpanDataSender.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/SpanDataSender.java new file mode 100644 index 000000000..d46e416a1 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/SpanDataSender.java @@ -0,0 +1,34 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.module; + +import com.google.inject.BindingAnnotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * @author Woonduk Kang(emeroad) + */ +@BindingAnnotation +@Target(PARAMETER) +@Retention(RUNTIME) +public @interface SpanDataSender { +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/StatDataSender.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/StatDataSender.java new file mode 100644 index 000000000..e334486aa --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/StatDataSender.java @@ -0,0 +1,34 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.module; + +import com.google.inject.BindingAnnotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * @author Woonduk Kang(emeroad) + */ +@BindingAnnotation +@Target(PARAMETER) +@Retention(RUNTIME) +public @interface StatDataSender { +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ActiveTraceRepositoryProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ActiveTraceRepositoryProvider.java new file mode 100644 index 000000000..215782412 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ActiveTraceRepositoryProvider.java @@ -0,0 +1,51 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.provider; + + +import com.google.inject.Provider; + +import com.google.inject.Inject; +import com.google.inject.Singleton; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.profiler.context.active.ActiveTraceRepository; + + + +/** + * @author Woonduk Kang(emeroad) + */ +public class ActiveTraceRepositoryProvider implements Provider { + + private final ProfilerConfig profilerConfig; + + @Inject + private ActiveTraceRepositoryProvider(ProfilerConfig profilerConfig) { + if (profilerConfig == null) { + throw new NullPointerException("profilerConfig must not be null"); + } + this.profilerConfig = profilerConfig; + } + + public ActiveTraceRepository get() { + if (profilerConfig.isTraceAgentActiveThread()) { + return new ActiveTraceRepository(); + } + return null; + } + +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentInfoSenderProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentInfoSenderProvider.java new file mode 100644 index 000000000..98d0868d5 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentInfoSenderProvider.java @@ -0,0 +1,65 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.provider; + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.google.inject.Singleton; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.profiler.AgentInfoSender; +import com.navercorp.pinpoint.profiler.AgentInformation; +import com.navercorp.pinpoint.profiler.JvmInformation; +import com.navercorp.pinpoint.profiler.sender.EnhancedDataSender; + +/** + * @author Woonduk Kang(emeroad) + */ +public class AgentInfoSenderProvider implements Provider { + + private final ProfilerConfig profilerConfig; + private final EnhancedDataSender enhancedDataSender; + private final AgentInformation agentInformation; + private final JvmInformation jvmInformation; + + @Inject + public AgentInfoSenderProvider(ProfilerConfig profilerConfig, EnhancedDataSender enhancedDataSender, AgentInformation agentInformation, JvmInformation jvmInformation) { + if (profilerConfig == null) { + throw new NullPointerException("profilerConfig must not be null"); + } + if (enhancedDataSender == null) { + throw new NullPointerException("enhancedDataSender must not be null"); + } + if (agentInformation == null) { + throw new NullPointerException("agentInformation must not be null"); + } + if (jvmInformation == null) { + throw new NullPointerException("jvmInformation must not be null"); + } + + this.profilerConfig = profilerConfig; + this.enhancedDataSender = enhancedDataSender; + this.agentInformation = agentInformation; + this.jvmInformation = jvmInformation; + } + + @Override + public AgentInfoSender get() { + final AgentInfoSender.Builder builder = new AgentInfoSender.Builder(this.enhancedDataSender, this.agentInformation, jvmInformation); + builder.sendInterval(profilerConfig.getAgentInfoSendRetryInterval()); + return builder.build(); + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentInformationProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentInformationProvider.java new file mode 100644 index 000000000..39d7f20cd --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentInformationProvider.java @@ -0,0 +1,64 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.provider; + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.google.inject.Singleton; +import com.navercorp.pinpoint.common.trace.ServiceType; +import com.navercorp.pinpoint.profiler.AgentInformation; +import com.navercorp.pinpoint.profiler.AgentInformationFactory; +import com.navercorp.pinpoint.profiler.DefaultAgentInformationFactory; +import com.navercorp.pinpoint.profiler.context.module.AgentId; +import com.navercorp.pinpoint.profiler.context.module.AgentServiceType; +import com.navercorp.pinpoint.profiler.context.module.AgentStartTime; +import com.navercorp.pinpoint.profiler.context.module.ApplicationName; + +/** + * @author Woonduk Kang(emeroad) + */ +public class AgentInformationProvider implements Provider { + + private final String agentId; + private final String applicationName; + private final long agentStartTime; + private final ServiceType serverType; + + @Inject + public AgentInformationProvider(@AgentId String agentId, @ApplicationName String applicationName, @AgentStartTime long agentStartTime, @AgentServiceType ServiceType serverType) { + if (agentId == null) { + throw new NullPointerException("agentId must not be null"); + } + if (applicationName == null) { + throw new NullPointerException("applicationName must not be null"); + } + if (serverType == null) { + throw new NullPointerException("serverType must not be null"); + } + + this.agentId = agentId; + this.applicationName = applicationName; + this.agentStartTime = agentStartTime; + this.serverType = serverType; + + } + + public AgentInformation get() { + AgentInformationFactory agentInformationFactory = new DefaultAgentInformationFactory(agentId, applicationName, agentStartTime, serverType); + return agentInformationFactory.createAgentInformation(); + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentServiceTypeProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentServiceTypeProvider.java new file mode 100644 index 000000000..52186f39a --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentServiceTypeProvider.java @@ -0,0 +1,52 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.provider; + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.google.inject.Singleton; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService; +import com.navercorp.pinpoint.common.trace.ServiceType; + + +/** + * @author Woonduk Kang(emeroad) + */ +public class AgentServiceTypeProvider implements Provider { + + private final ProfilerConfig profilerConfig; + private final ServiceTypeRegistryService serviceTypeRegistryService; + + @Inject + public AgentServiceTypeProvider(ProfilerConfig profilerConfig, ServiceTypeRegistryService serviceTypeRegistryService) { + if (profilerConfig == null) { + throw new NullPointerException("profilerConfig must not be null"); + } + if (serviceTypeRegistryService == null) { + throw new NullPointerException("serviceTypeRegistryService must not be null"); + } + this.profilerConfig = profilerConfig; + this.serviceTypeRegistryService = serviceTypeRegistryService; + } + + @Override + public ServiceType get() { + String applicationServerTypeString = profilerConfig.getApplicationServerType(); + return this.serviceTypeRegistryService.findServiceTypeByName(applicationServerTypeString); + } +} diff --git a/profiler-test/src/main/java/com/navercorp/pinpoint/test/DelegateProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentStartTimeProvider.java similarity index 60% rename from profiler-test/src/main/java/com/navercorp/pinpoint/test/DelegateProvider.java rename to profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentStartTimeProvider.java index 97692e64b..4481a814a 100644 --- a/profiler-test/src/main/java/com/navercorp/pinpoint/test/DelegateProvider.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentStartTimeProvider.java @@ -14,26 +14,24 @@ * limitations under the License. */ -package com.navercorp.pinpoint.test; +package com.navercorp.pinpoint.profiler.context.provider; -import com.navercorp.pinpoint.profiler.context.provider.Provider; +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.google.inject.Singleton; +import com.navercorp.pinpoint.profiler.util.RuntimeMXBeanUtils; /** * @author Woonduk Kang(emeroad) */ -public class DelegateProvider implements Provider { - private final T delegate; +public class AgentStartTimeProvider implements Provider { - public DelegateProvider(T delegate) { - if (delegate == null) { - throw new NullPointerException("delegate must not be null"); - } - - this.delegate = delegate; + @Inject + public AgentStartTimeProvider() { } @Override - public T get() { - return delegate; + public Long get() { + return RuntimeMXBeanUtils.getVmStartTime(); } } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ApplicationServerTypeResolverProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ApplicationServerTypeResolverProvider.java new file mode 100644 index 000000000..7e793fa9e --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ApplicationServerTypeResolverProvider.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.provider; + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.google.inject.Singleton; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.common.trace.ServiceType; +import com.navercorp.pinpoint.profiler.context.module.AgentServiceType; +import com.navercorp.pinpoint.profiler.plugin.PluginContextLoadResult; +import com.navercorp.pinpoint.profiler.util.ApplicationServerTypeResolver; + +/** + * @author Woonduk Kang(emeroad) + */ +public class ApplicationServerTypeResolverProvider implements Provider { + + private final PluginContextLoadResult pluginContextLoadResult; + private final ServiceType serviceType; + private final ProfilerConfig profilerConfig; + + @Inject + public ApplicationServerTypeResolverProvider(PluginContextLoadResult pluginContextLoadResult, @AgentServiceType ServiceType serviceType, ProfilerConfig profilerConfig) { + if (pluginContextLoadResult == null) { + throw new NullPointerException("pluginContextLoadResult must not be null"); + } + if (serviceType == null) { + throw new NullPointerException("serviceType must not be null"); + } + if (profilerConfig == null) { + throw new NullPointerException("profilerConfig must not be null"); + } + this.pluginContextLoadResult = pluginContextLoadResult; + this.serviceType = serviceType; + this.profilerConfig = profilerConfig; + } + + @Override + public ApplicationServerTypeResolver get() { + return new ApplicationServerTypeResolver(pluginContextLoadResult, serviceType, profilerConfig.getApplicationTypeDetectOrder()); + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ClassFileTransformerDispatcherProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ClassFileTransformerDispatcherProvider.java new file mode 100644 index 000000000..5cba68033 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ClassFileTransformerDispatcherProvider.java @@ -0,0 +1,52 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.provider; + +import com.google.inject.Inject; +import com.google.inject.Singleton; +import com.navercorp.pinpoint.profiler.ClassFileTransformerDispatcher; +import com.navercorp.pinpoint.profiler.DefaultClassFileTransformerDispatcher; +import com.navercorp.pinpoint.profiler.context.ApplicationContext; +import com.navercorp.pinpoint.profiler.plugin.PluginContextLoadResult; + +import javax.inject.Provider; + +/** + * @author Woonduk Kang(emeroad) + */ +public class ClassFileTransformerDispatcherProvider implements Provider { + + private final ApplicationContext applicationContext; + private final PluginContextLoadResult pluginContextLoadResult; + + @Inject + public ClassFileTransformerDispatcherProvider(ApplicationContext applicationContext, PluginContextLoadResult pluginContextLoadResult) { + if (applicationContext == null) { + throw new NullPointerException("applicationContext must not be null"); + } + if (pluginContextLoadResult == null) { + throw new NullPointerException("pluginContextLoadResult must not be null"); + } + this.applicationContext = applicationContext; + this.pluginContextLoadResult = pluginContextLoadResult; + } + + @Override + public ClassFileTransformerDispatcher get() { + return new DefaultClassFileTransformerDispatcher(applicationContext, pluginContextLoadResult); + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ClassFileTransformerWrapProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ClassFileTransformerWrapProvider.java new file mode 100644 index 000000000..65c664f5a --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ClassFileTransformerWrapProvider.java @@ -0,0 +1,60 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.provider; + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.profiler.ClassFileTransformerDispatcher; +import com.navercorp.pinpoint.profiler.instrument.ASMBytecodeDumpService; +import com.navercorp.pinpoint.profiler.instrument.BytecodeDumpTransformer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.instrument.ClassFileTransformer; + +/** + * @author Woonduk Kang(emeroad) + */ +public class ClassFileTransformerWrapProvider implements Provider { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final ProfilerConfig profilerConfig; + private final ClassFileTransformerDispatcher classFileTransformerDispatcher; + + @Inject + public ClassFileTransformerWrapProvider(ProfilerConfig profilerConfig, ClassFileTransformerDispatcher classFileTransformerDispatcher) { + if (profilerConfig == null) { + throw new NullPointerException("profilerConfig must not be null"); + } + if (classFileTransformerDispatcher == null) { + throw new NullPointerException("classFileTransformerDispatcher must not be null"); + } + + this.profilerConfig = profilerConfig; + this.classFileTransformerDispatcher = classFileTransformerDispatcher; + } + + + public ClassFileTransformer get() { + final boolean enableBytecodeDump = profilerConfig.readBoolean(ASMBytecodeDumpService.ENABLE_BYTECODE_DUMP, ASMBytecodeDumpService.ENABLE_BYTECODE_DUMP_DEFAULT_VALUE); + if (enableBytecodeDump) { + logger.info("wrapBytecodeDumpTransformer"); + return BytecodeDumpTransformer.wrap(classFileTransformerDispatcher, profilerConfig); + } + return classFileTransformerDispatcher; + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/CommandDispatcherProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/CommandDispatcherProvider.java new file mode 100644 index 000000000..0ecc58d8b --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/CommandDispatcherProvider.java @@ -0,0 +1,62 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.provider; + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.profiler.context.active.ActiveTraceRepository; +import com.navercorp.pinpoint.profiler.receiver.CommandDispatcher; +import com.navercorp.pinpoint.profiler.receiver.ProfilerCommandLocatorBuilder; +import com.navercorp.pinpoint.profiler.receiver.ProfilerCommandServiceLocator; +import com.navercorp.pinpoint.profiler.receiver.service.ActiveThreadService; +import com.navercorp.pinpoint.profiler.receiver.service.EchoService; + +/** + * @author Woonduk Kang(emeroad) + */ +public class CommandDispatcherProvider implements Provider { + + private final ProfilerConfig profilerConfig; + private final ActiveTraceRepository activeTraceRepository; + + @Inject + public CommandDispatcherProvider(ProfilerConfig profilerConfig, ActiveTraceRepository activeTraceRepository) { + if (profilerConfig == null) { + throw new NullPointerException("profilerConfig must not be null"); + } + if (activeTraceRepository == null) { + throw new NullPointerException("activeTraceRepository must not be null"); + } + this.profilerConfig = profilerConfig; + this.activeTraceRepository = activeTraceRepository; + } + + @Override + public CommandDispatcher get() { + ProfilerCommandLocatorBuilder builder = new ProfilerCommandLocatorBuilder(); + builder.addService(new EchoService()); + if (activeTraceRepository != null) { + ActiveThreadService activeThreadService = new ActiveThreadService(profilerConfig, activeTraceRepository); + builder.addService(activeThreadService); + } + + ProfilerCommandServiceLocator commandServiceLocator = builder.build(); + CommandDispatcher commandDispatcher = new CommandDispatcher(commandServiceLocator); + return commandDispatcher; + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/DynamicTransformTriggerProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/DynamicTransformTriggerProvider.java new file mode 100644 index 000000000..41b92f043 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/DynamicTransformTriggerProvider.java @@ -0,0 +1,53 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.provider; + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.navercorp.pinpoint.bootstrap.instrument.DynamicTransformTrigger; +import com.navercorp.pinpoint.profiler.ClassFileTransformerDispatcher; +import com.navercorp.pinpoint.profiler.DynamicTransformService; + +import java.lang.instrument.Instrumentation; + +/** + * @author Woonduk Kang(emeroad) + */ +public class DynamicTransformTriggerProvider implements Provider { + + private final Instrumentation instrumentation; + private final ClassFileTransformerDispatcher listener; + + @Inject + public DynamicTransformTriggerProvider(Instrumentation instrumentation, ClassFileTransformerDispatcher listener) { + if (instrumentation == null) { + throw new NullPointerException("instrumentation must not be null"); + } + if (listener == null) { + throw new NullPointerException("listener must not be null"); + } + + this.instrumentation = instrumentation; + this.listener = listener; + } + + @Override + public DynamicTransformTrigger get() { + + return new DynamicTransformService(instrumentation, listener); + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/InstrumentEngineProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/InstrumentEngineProvider.java new file mode 100644 index 000000000..cac438209 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/InstrumentEngineProvider.java @@ -0,0 +1,78 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.provider; + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.navercorp.pinpoint.bootstrap.AgentOption; +import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClassPool; + +import com.navercorp.pinpoint.profiler.instrument.ASMClassPool; +import com.navercorp.pinpoint.profiler.instrument.JavassistClassPool; +import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Woonduk Kang(emeroad) + */ +public class InstrumentEngineProvider implements Provider { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + private final ProfilerConfig profilerConfig; + private final AgentOption agentOption; + private final InterceptorRegistryBinder interceptorRegistryBinder; + + @Inject + public InstrumentEngineProvider(ProfilerConfig profilerConfig, AgentOption agentOption, InterceptorRegistryBinder interceptorRegistryBinder) { + if (profilerConfig == null) { + throw new NullPointerException("profilerConfig must not be null"); + } + if (agentOption == null) { + throw new NullPointerException("agentOption must not be null"); + } + if (interceptorRegistryBinder == null) { + throw new NullPointerException("interceptorRegistryBinder must not be null"); + } + + this.profilerConfig = profilerConfig; + this.agentOption = agentOption; + this.interceptorRegistryBinder = interceptorRegistryBinder; + } + + public InstrumentClassPool get() { + final String instrumentEngine = profilerConfig.getProfileInstrumentEngine().toUpperCase(); + + if (DefaultProfilerConfig.INSTRUMENT_ENGINE_ASM.equals(instrumentEngine)) { + logger.info("ASM InstrumentEngine."); + + return new ASMClassPool(interceptorRegistryBinder, agentOption.getBootstrapJarPaths()); + + } else if (DefaultProfilerConfig.INSTRUMENT_ENGINE_JAVASSIST.equals(instrumentEngine)) { + logger.info("JAVASSIST InstrumentEngine."); + + return new JavassistClassPool(interceptorRegistryBinder, agentOption.getBootstrapJarPaths()); + } else { + logger.warn("Unknown InstrumentEngine:{}", instrumentEngine); + + throw new IllegalArgumentException("Unknown InstrumentEngine:" + instrumentEngine); + } + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/JvmInformationFactory.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/JvmInformationProvider.java similarity index 63% rename from profiler/src/main/java/com/navercorp/pinpoint/profiler/JvmInformationFactory.java rename to profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/JvmInformationProvider.java index 9309d53dd..fbb1d6bcf 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/JvmInformationFactory.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/JvmInformationProvider.java @@ -1,11 +1,11 @@ /* - * Copyright 2016 NAVER Corp. + * Copyright 2017 NAVER Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -14,26 +14,36 @@ * limitations under the License. */ -package com.navercorp.pinpoint.profiler; +package com.navercorp.pinpoint.profiler.context.provider; +import com.google.inject.Inject; +import com.google.inject.Provider; import com.navercorp.pinpoint.common.util.JvmUtils; import com.navercorp.pinpoint.common.util.SystemPropertyKey; +import com.navercorp.pinpoint.profiler.JvmInformation; +import com.navercorp.pinpoint.profiler.monitor.codahale.AgentStatCollectorFactory; import com.navercorp.pinpoint.profiler.monitor.codahale.gc.GarbageCollector; import com.navercorp.pinpoint.profiler.monitor.codahale.gc.UnknownGarbageCollector; /** * @author HyunGil Jeong */ -public class JvmInformationFactory { +public class JvmInformationProvider implements Provider { private final String jvmVersion; private final GarbageCollector garbageCollector; - JvmInformationFactory() { - this(null); + + @Inject + public JvmInformationProvider(AgentStatCollectorFactory garbageCollector) { + this(garbageCollector.getGarbageCollector()); } - public JvmInformationFactory(GarbageCollector garbageCollector) { + public JvmInformationProvider() { + this((GarbageCollector)null); + } + + public JvmInformationProvider(GarbageCollector garbageCollector) { this.jvmVersion = JvmUtils.getSystemProperty(SystemPropertyKey.JAVA_VERSION); if (garbageCollector == null) { this.garbageCollector = new UnknownGarbageCollector(); @@ -42,7 +52,7 @@ public class JvmInformationFactory { } } - public JvmInformation createJvmInformation() { + public JvmInformation get() { return new JvmInformation(this.jvmVersion, this.garbageCollector.getTypeCode()); } } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/PinpointClientFactoryProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/PinpointClientFactoryProvider.java index ad0a74816..2cec4ab1c 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/PinpointClientFactoryProvider.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/PinpointClientFactoryProvider.java @@ -16,6 +16,8 @@ package com.navercorp.pinpoint.profiler.context.provider; +import com.google.inject.Inject; +import com.google.inject.Provider; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; import com.navercorp.pinpoint.profiler.AgentInformation; import com.navercorp.pinpoint.profiler.receiver.CommandDispatcher; @@ -34,6 +36,7 @@ public class PinpointClientFactoryProvider implements Provider { private final ProfilerConfig profilerConfig; private final PinpointClientFactory clientFactory; + @Inject public PinpointClientProvider(ProfilerConfig profilerConfig, PinpointClientFactory clientFactory) { if (profilerConfig == null) { throw new NullPointerException("profilerConfig must not be null"); diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/PluginContextLoadResultProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/PluginContextLoadResultProvider.java new file mode 100644 index 000000000..5222ade32 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/PluginContextLoadResultProvider.java @@ -0,0 +1,69 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.provider; + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClassPool; +import com.navercorp.pinpoint.profiler.context.ApplicationContext; +import com.navercorp.pinpoint.profiler.context.module.BootstrapJarPaths; +import com.navercorp.pinpoint.profiler.context.module.PluginJars; +import com.navercorp.pinpoint.profiler.plugin.DefaultPluginContextLoadResult; +import com.navercorp.pinpoint.profiler.plugin.DefaultProfilerPluginContext; +import com.navercorp.pinpoint.profiler.plugin.PluginContextLoadResult; +import com.navercorp.pinpoint.profiler.plugin.PluginSetup; +import com.navercorp.pinpoint.profiler.plugin.ProfilerPluginLoader; + +import java.lang.instrument.Instrumentation; +import java.net.URL; +import java.util.List; + +/** + * @author Woonduk Kang(emeroad) + */ +public class PluginContextLoadResultProvider implements Provider { + + + private final URL[] pluginJars; + private final PluginSetup pluginSetup; + private final ApplicationContext applicationContext; + + @Inject + public PluginContextLoadResultProvider(ApplicationContext applicationContext, @PluginJars URL[] pluginJars, PluginSetup pluginSetup) { + if (applicationContext == null) { + throw new NullPointerException("applicationContext must not be null"); + } + if (pluginJars == null) { + throw new NullPointerException("pluginJars must not be null"); + } + if (pluginSetup == null) { + throw new NullPointerException("pluginSetup must not be null"); + } + this.applicationContext = applicationContext; + this.pluginJars = pluginJars; + this.pluginSetup = pluginSetup; + } + + @Override + public PluginContextLoadResult get() { + final ProfilerPluginLoader loader = new ProfilerPluginLoader(applicationContext, pluginSetup); + List load = loader.load(pluginJars); + return new DefaultPluginContextLoadResult(load); + + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/PluginMonitorContextProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/PluginMonitorContextProvider.java new file mode 100644 index 000000000..93e73e639 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/PluginMonitorContextProvider.java @@ -0,0 +1,50 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.provider; + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.profiler.context.monitor.DefaultPluginMonitorContext; +import com.navercorp.pinpoint.profiler.context.monitor.DisabledPluginMonitorContext; +import com.navercorp.pinpoint.profiler.context.monitor.PluginMonitorContext; + +/** + * @author Woonduk Kang(emeroad) + */ +public class PluginMonitorContextProvider implements Provider { + + private final boolean traceAgentDataSource; + + @Inject + public PluginMonitorContextProvider(ProfilerConfig profilerConfig) { + this(profilerConfig.isTraceAgentDataSource()); + } + + public PluginMonitorContextProvider(boolean traceAgentDataSource) { + this.traceAgentDataSource = traceAgentDataSource; + } + + + public PluginMonitorContext get() { + if (traceAgentDataSource) { + return new DefaultPluginMonitorContext(); + } + + return new DisabledPluginMonitorContext(); + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/PluginSetupProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/PluginSetupProvider.java new file mode 100644 index 000000000..a1c2ac4ce --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/PluginSetupProvider.java @@ -0,0 +1,45 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.provider; + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.navercorp.pinpoint.profiler.context.ApplicationContext; +import com.navercorp.pinpoint.profiler.plugin.DefaultPluginSetup; +import com.navercorp.pinpoint.profiler.plugin.PluginSetup; + +/** + * @author Woonduk Kang(emeroad) + */ +public class PluginSetupProvider implements Provider { + + private ApplicationContext applicationContext; + + @Inject + public PluginSetupProvider(ApplicationContext applicationContext) { + if (applicationContext == null) { + throw new NullPointerException("applicationContext must not be null"); + } + + this.applicationContext = applicationContext; + } + + @Override + public PluginSetup get() { + return new DefaultPluginSetup(applicationContext); + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/SamplerProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/SamplerProvider.java new file mode 100644 index 000000000..6b67fe00e --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/SamplerProvider.java @@ -0,0 +1,45 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context.provider; + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.bootstrap.sampler.Sampler; +import com.navercorp.pinpoint.profiler.sampler.SamplerFactory; + +/** + * @author Woonduk Kang(emeroad) + */ +public class SamplerProvider implements Provider { + + private final ProfilerConfig profilerConfig; + + @Inject + public SamplerProvider(ProfilerConfig profilerConfig) { + this.profilerConfig = profilerConfig; + } + + @Override + public Sampler get() { + boolean samplingEnable = profilerConfig.isSamplingEnable(); + int samplingRate = profilerConfig.getSamplingRate(); + + SamplerFactory samplerFactory = new SamplerFactory(); + return samplerFactory.createSampler(samplingEnable, samplingRate); + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ServerMetaDataHolderProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ServerMetaDataHolderProvider.java index e9aeaef6a..29a5e64f7 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ServerMetaDataHolderProvider.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/ServerMetaDataHolderProvider.java @@ -16,7 +16,10 @@ package com.navercorp.pinpoint.profiler.context.provider; +import com.google.inject.Inject; +import com.google.inject.Provider; import com.navercorp.pinpoint.bootstrap.context.ServerMetaDataHolder; +import com.navercorp.pinpoint.profiler.AgentInfoSender; import com.navercorp.pinpoint.profiler.context.DefaultServerMetaDataHolder; import com.navercorp.pinpoint.profiler.util.RuntimeMXBeanUtils; @@ -27,10 +30,18 @@ import java.util.List; */ public class ServerMetaDataHolderProvider implements Provider { + private final AgentInfoSender agentInfoSender; + + @Inject + public ServerMetaDataHolderProvider(AgentInfoSender agentInfoSender) { + this.agentInfoSender = agentInfoSender; + } + @Override public ServerMetaDataHolder get() { List vmArgs = RuntimeMXBeanUtils.getVmArgs(); ServerMetaDataHolder serverMetaDataHolder = new DefaultServerMetaDataHolder(vmArgs); + serverMetaDataHolder.addListener(agentInfoSender); return serverMetaDataHolder; } } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/StorageFactoryProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/StorageFactoryProvider.java index d9d43b2cc..e2b830619 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/StorageFactoryProvider.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/StorageFactoryProvider.java @@ -16,8 +16,11 @@ package com.navercorp.pinpoint.profiler.context.provider; +import com.google.inject.Inject; +import com.google.inject.Provider; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; import com.navercorp.pinpoint.profiler.AgentInformation; +import com.navercorp.pinpoint.profiler.context.module.SpanDataSender; import com.navercorp.pinpoint.profiler.context.storage.BufferedStorageFactory; import com.navercorp.pinpoint.profiler.context.storage.SpanStorageFactory; import com.navercorp.pinpoint.profiler.context.storage.StorageFactory; @@ -32,7 +35,8 @@ public class StorageFactoryProvider implements Provider { private final DataSender spanDataSender; private final AgentInformation agentInformation; - public StorageFactoryProvider(ProfilerConfig profilerConfig, DataSender spanDataSender, AgentInformation agentInformation) { + @Inject + public StorageFactoryProvider(ProfilerConfig profilerConfig, @SpanDataSender DataSender spanDataSender, AgentInformation agentInformation) { if (profilerConfig == null) { throw new NullPointerException("profilerConfig must not be null"); } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/TcpDataSenderProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/TcpDataSenderProvider.java index ca55271a5..b32ab74ff 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/TcpDataSenderProvider.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/TcpDataSenderProvider.java @@ -16,6 +16,8 @@ package com.navercorp.pinpoint.profiler.context.provider; +import com.google.inject.Inject; +import com.google.inject.Provider; import com.navercorp.pinpoint.profiler.sender.EnhancedDataSender; import com.navercorp.pinpoint.profiler.sender.TcpDataSender; import com.navercorp.pinpoint.rpc.client.PinpointClient; @@ -26,6 +28,7 @@ import com.navercorp.pinpoint.rpc.client.PinpointClient; public class TcpDataSenderProvider implements Provider { private final PinpointClient client; + @Inject public TcpDataSenderProvider(PinpointClient client) { if (client == null) { throw new NullPointerException("client must not be null"); diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/UdpSpanDataSenderProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/UdpSpanDataSenderProvider.java index bd7331f66..1a09b55cc 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/UdpSpanDataSenderProvider.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/UdpSpanDataSenderProvider.java @@ -16,6 +16,8 @@ package com.navercorp.pinpoint.profiler.context.provider; +import com.google.inject.Inject; +import com.google.inject.Provider; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; import com.navercorp.pinpoint.profiler.sender.DataSender; import com.navercorp.pinpoint.profiler.sender.UdpDataSenderFactory; @@ -34,7 +36,7 @@ public class UdpSpanDataSenderProvider implements Provider { private final int sendBufferSize; private final String senderType; - + @Inject public UdpSpanDataSenderProvider(ProfilerConfig profilerConfig) { if (profilerConfig == null) { throw new NullPointerException("profilerConfig must not be null"); diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/UdpStatDataSenderProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/UdpStatDataSenderProvider.java index 6b8c8aba6..c02eae250 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/UdpStatDataSenderProvider.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/UdpStatDataSenderProvider.java @@ -16,6 +16,8 @@ package com.navercorp.pinpoint.profiler.context.provider; +import com.google.inject.Inject; +import com.google.inject.Provider; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; import com.navercorp.pinpoint.profiler.sender.DataSender; import com.navercorp.pinpoint.profiler.sender.UdpDataSenderFactory; @@ -34,7 +36,7 @@ public class UdpStatDataSenderProvider implements Provider { private final int sendBufferSize; private final String senderType; - + @Inject public UdpStatDataSenderProvider(ProfilerConfig profilerConfig) { if (profilerConfig == null) { throw new NullPointerException("profilerConfig must not be null"); diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/ApiMetaDataCacheService.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/ApiMetaDataCacheService.java index caa8da916..76a5298a1 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/ApiMetaDataCacheService.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/ApiMetaDataCacheService.java @@ -16,7 +16,11 @@ package com.navercorp.pinpoint.profiler.metadata; +import com.google.inject.Inject; +import com.google.inject.name.Named; import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor; +import com.navercorp.pinpoint.profiler.context.module.AgentId; +import com.navercorp.pinpoint.profiler.context.module.AgentStartTime; import com.navercorp.pinpoint.profiler.sender.EnhancedDataSender; import com.navercorp.pinpoint.thrift.dto.TApiMetaData; @@ -31,7 +35,8 @@ public class ApiMetaDataCacheService implements ApiMetaDataService { private final long agentStartTime; private final EnhancedDataSender enhancedDataSender; - public ApiMetaDataCacheService(String agentId, long agentStartTime, EnhancedDataSender enhancedDataSender) { + @Inject + public ApiMetaDataCacheService(@AgentId String agentId, @AgentStartTime long agentStartTime, EnhancedDataSender enhancedDataSender) { if (agentId == null) { throw new NullPointerException("agentId must not be null"); } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/SqlMetaDataCacheService.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/SqlMetaDataCacheService.java index fe93733b5..e284170c3 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/SqlMetaDataCacheService.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/SqlMetaDataCacheService.java @@ -16,9 +16,13 @@ package com.navercorp.pinpoint.profiler.metadata; +import com.google.inject.Inject; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; import com.navercorp.pinpoint.bootstrap.context.ParsingResult; import com.navercorp.pinpoint.profiler.context.CachingSqlNormalizer; import com.navercorp.pinpoint.profiler.context.DefaultCachingSqlNormalizer; +import com.navercorp.pinpoint.profiler.context.module.AgentId; +import com.navercorp.pinpoint.profiler.context.module.AgentStartTime; import com.navercorp.pinpoint.profiler.sender.EnhancedDataSender; import com.navercorp.pinpoint.thrift.dto.TSqlMetaData; import org.slf4j.Logger; @@ -38,6 +42,12 @@ public class SqlMetaDataCacheService implements SqlMetaDataService { private final long agentStartTime; private final EnhancedDataSender enhancedDataSender; + @Inject + public SqlMetaDataCacheService(ProfilerConfig profilerConfig, @AgentId String agentId, + @AgentStartTime long agentStartTime, EnhancedDataSender enhancedDataSender) { + this(agentId, agentStartTime, enhancedDataSender, profilerConfig.getJdbcSqlCacheSize()); + } + public SqlMetaDataCacheService(String agentId, long agentStartTime, EnhancedDataSender enhancedDataSender, int jdbcSqlCacheSize) { if (agentId == null) { throw new NullPointerException("agentId must not be null"); diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/StringMetaDataCacheService.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/StringMetaDataCacheService.java index 4c6f51faa..3d249fffd 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/StringMetaDataCacheService.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/StringMetaDataCacheService.java @@ -16,6 +16,10 @@ package com.navercorp.pinpoint.profiler.metadata; +import com.google.inject.Inject; +import com.google.inject.name.Named; +import com.navercorp.pinpoint.profiler.context.module.AgentId; +import com.navercorp.pinpoint.profiler.context.module.AgentStartTime; import com.navercorp.pinpoint.profiler.sender.EnhancedDataSender; import com.navercorp.pinpoint.thrift.dto.TStringMetaData; @@ -30,7 +34,8 @@ public class StringMetaDataCacheService implements StringMetaDataService { private final long agentStartTime; private final EnhancedDataSender enhancedDataSender; - public StringMetaDataCacheService(String agentId, long agentStartTime, EnhancedDataSender enhancedDataSender) { + @Inject + public StringMetaDataCacheService(@AgentId String agentId, @AgentStartTime long agentStartTime, EnhancedDataSender enhancedDataSender) { if (agentId == null) { throw new NullPointerException("agentId must not be null"); } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/monitor/AgentStatMonitor.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/monitor/AgentStatMonitor.java index ba53fd560..7f0bb844c 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/monitor/AgentStatMonitor.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/monitor/AgentStatMonitor.java @@ -1,11 +1,11 @@ /* - * Copyright 2014 NAVER Corp. + * Copyright 2017 NAVER Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -16,169 +16,11 @@ package com.navercorp.pinpoint.profiler.monitor; -import com.navercorp.pinpoint.common.util.PinpointThreadFactory; -import com.navercorp.pinpoint.profiler.monitor.codahale.AgentStatCollectorFactory; -import com.navercorp.pinpoint.profiler.monitor.codahale.activetrace.ActiveTraceMetricCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.cpu.CpuLoadCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.datasource.DataSourceCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.gc.GarbageCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.tps.TransactionMetricCollector; -import com.navercorp.pinpoint.profiler.sender.DataSender; -import com.navercorp.pinpoint.thrift.dto.TActiveTrace; -import com.navercorp.pinpoint.thrift.dto.TAgentStat; -import com.navercorp.pinpoint.thrift.dto.TAgentStatBatch; -import com.navercorp.pinpoint.thrift.dto.TCpuLoad; -import com.navercorp.pinpoint.thrift.dto.TDataSourceList; -import com.navercorp.pinpoint.thrift.dto.TJvmGc; -import com.navercorp.pinpoint.thrift.dto.TTransaction; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - /** - * AgentStat monitor - * - * @author harebox - * @author hyungil.jeong + * @author Woonduk Kang(emeroad) */ -public class AgentStatMonitor { - - private static final long DEFAULT_COLLECTION_INTERVAL_MS = 1000 * 5; - private static final int DEFAULT_NUM_COLLECTIONS_PER_SEND = 6; - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - private final boolean isTrace = logger.isTraceEnabled(); - private final long collectionIntervalMs; - private final int numCollectionsPerBatch; - - private final ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1, new PinpointThreadFactory("Pinpoint-stat-monitor", true)); - - private final DataSender dataSender; - private final String agentId; - private final AgentStatCollectorFactory agentStatCollectorFactory; - private final long agentStartTime; - - public AgentStatMonitor(DataSender dataSender, String agentId, long startTime, AgentStatCollectorFactory agentStatCollectorFactory) { - this(dataSender, agentId, startTime, agentStatCollectorFactory, DEFAULT_COLLECTION_INTERVAL_MS, DEFAULT_NUM_COLLECTIONS_PER_SEND); - } - - public AgentStatMonitor(DataSender dataSender, String agentId, long startTime, AgentStatCollectorFactory agentStatCollectorFactory, long collectionInterval, int numCollectionsPerBatch) { - if (dataSender == null) { - throw new NullPointerException("dataSender must not be null"); - } - if (agentId == null) { - throw new NullPointerException("agentId must not be null"); - } - if (agentStatCollectorFactory == null) { - throw new NullPointerException("agentStatCollectorFactory must not be null"); - } - this.dataSender = dataSender; - this.agentId = agentId; - this.agentStartTime = startTime; - this.agentStatCollectorFactory = agentStatCollectorFactory; - this.collectionIntervalMs = collectionInterval; - this.numCollectionsPerBatch = numCollectionsPerBatch; - } - - public void start() { - CollectJob job = new CollectJob(this.numCollectionsPerBatch); - executor.scheduleAtFixedRate(job, this.collectionIntervalMs, this.collectionIntervalMs, TimeUnit.MILLISECONDS); - logger.info("AgentStat monitor started"); - } - - public void stop() { - executor.shutdown(); - try { - executor.awaitTermination(3000, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - logger.info("AgentStat monitor stopped"); - } - - // NotThreadSafe - private class CollectJob implements Runnable { - - private final GarbageCollector garbageCollector; - private final CpuLoadCollector cpuLoadCollector; - private final TransactionMetricCollector transactionMetricCollector; - private final ActiveTraceMetricCollector activeTraceMetricCollector; - private final DataSourceCollector dataSourceCollector; - - // Not thread safe. For use with single thread ONLY - private final int numStatsPerBatch; - private int collectCount = 0; - private long prevCollectionTimestamp = System.currentTimeMillis(); - private List agentStats; - - private CollectJob(int numStatsPerBatch) { - this.garbageCollector = agentStatCollectorFactory.getGarbageCollector(); - this.cpuLoadCollector = agentStatCollectorFactory.getCpuLoadCollector(); - this.transactionMetricCollector = agentStatCollectorFactory.getTransactionMetricCollector(); - this.activeTraceMetricCollector = agentStatCollectorFactory.getActiveTraceMetricCollector(); - this.dataSourceCollector = agentStatCollectorFactory.getDataSourceCollector(); - this.numStatsPerBatch = numStatsPerBatch; - this.agentStats = new ArrayList(this.numStatsPerBatch); - } - - @Override - public void run() { - final long currentCollectionTimestamp = System.currentTimeMillis(); - final long collectInterval = currentCollectionTimestamp - this.prevCollectionTimestamp; - try { - final TAgentStat agentStat = collectAgentStat(); - agentStat.setTimestamp(currentCollectionTimestamp); - agentStat.setCollectInterval(collectInterval); - this.agentStats.add(agentStat); - if (++this.collectCount >= this.numStatsPerBatch) { - sendAgentStats(); - this.collectCount = 0; - } - } catch (Exception ex) { - logger.warn("AgentStat collect failed. Caused:{}", ex.getMessage(), ex); - } finally { - this.prevCollectionTimestamp = currentCollectionTimestamp; - } - } - - private TAgentStat collectAgentStat() { - final TAgentStat agentStat = new TAgentStat(); - final TJvmGc gc = garbageCollector.collect(); - agentStat.setGc(gc); - final TCpuLoad cpuLoad = cpuLoadCollector.collect(); - agentStat.setCpuLoad(cpuLoad); - final TTransaction transaction = transactionMetricCollector.collect(); - agentStat.setTransaction(transaction); - final TActiveTrace activeTrace = activeTraceMetricCollector.collect(); - agentStat.setActiveTrace(activeTrace); - final TDataSourceList dataSourceList = dataSourceCollector.collect(); - agentStat.setDataSourceList(dataSourceList); - - return agentStat; - } - - private void sendAgentStats() { - // prepare TAgentStat object. - // TODO multi thread issue. - // If we reuse TAgentStat, there could be concurrency issue because data sender runs in a different thread. - final TAgentStatBatch agentStatBatch = new TAgentStatBatch(); - agentStatBatch.setAgentId(agentId); - agentStatBatch.setStartTimestamp(agentStartTime); - agentStatBatch.setAgentStats(this.agentStats); - // If we reuse agentStats list, there could be concurrency issue because data sender runs in a different - // thread. - // So create new list. - this.agentStats = new ArrayList(this.numStatsPerBatch); - if (isTrace) { - logger.trace("collect agentStat:{}", agentStatBatch); - } - dataSender.send(agentStatBatch); - } - } +public interface AgentStatMonitor { + void start(); + void stop(); } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/monitor/DefaultAgentStatMonitor.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/monitor/DefaultAgentStatMonitor.java new file mode 100644 index 000000000..9fe210dea --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/monitor/DefaultAgentStatMonitor.java @@ -0,0 +1,191 @@ +/* + * Copyright 2014 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.monitor; + +import com.google.inject.Inject; +import com.navercorp.pinpoint.common.util.PinpointThreadFactory; +import com.navercorp.pinpoint.profiler.context.module.AgentId; +import com.navercorp.pinpoint.profiler.context.module.AgentStartTime; +import com.navercorp.pinpoint.profiler.context.module.StatDataSender; +import com.navercorp.pinpoint.profiler.monitor.codahale.AgentStatCollectorFactory; +import com.navercorp.pinpoint.profiler.monitor.codahale.activetrace.ActiveTraceMetricCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.cpu.CpuLoadCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.datasource.DataSourceCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.gc.GarbageCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.tps.TransactionMetricCollector; +import com.navercorp.pinpoint.profiler.sender.DataSender; +import com.navercorp.pinpoint.thrift.dto.TActiveTrace; +import com.navercorp.pinpoint.thrift.dto.TAgentStat; +import com.navercorp.pinpoint.thrift.dto.TAgentStatBatch; +import com.navercorp.pinpoint.thrift.dto.TCpuLoad; +import com.navercorp.pinpoint.thrift.dto.TDataSourceList; +import com.navercorp.pinpoint.thrift.dto.TJvmGc; +import com.navercorp.pinpoint.thrift.dto.TTransaction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * AgentStat monitor + * + * @author harebox + * @author hyungil.jeong + */ +public class DefaultAgentStatMonitor implements AgentStatMonitor { + + private static final long DEFAULT_COLLECTION_INTERVAL_MS = 1000 * 5; + private static final int DEFAULT_NUM_COLLECTIONS_PER_SEND = 6; + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final boolean isTrace = logger.isTraceEnabled(); + private final long collectionIntervalMs; + private final int numCollectionsPerBatch; + + private final ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1, new PinpointThreadFactory("Pinpoint-stat-monitor", true)); + + private final DataSender dataSender; + private final String agentId; + private final AgentStatCollectorFactory agentStatCollectorFactory; + private final long agentStartTime; + + @Inject + public DefaultAgentStatMonitor(@StatDataSender DataSender dataSender, @AgentId String agentId, @AgentStartTime long startTime, AgentStatCollectorFactory agentStatCollectorFactory) { + this(dataSender, agentId, startTime, agentStatCollectorFactory, DEFAULT_COLLECTION_INTERVAL_MS, DEFAULT_NUM_COLLECTIONS_PER_SEND); + } + + public DefaultAgentStatMonitor(DataSender dataSender, String agentId, long startTime, AgentStatCollectorFactory agentStatCollectorFactory, long collectionInterval, int numCollectionsPerBatch) { + if (dataSender == null) { + throw new NullPointerException("dataSender must not be null"); + } + if (agentId == null) { + throw new NullPointerException("agentId must not be null"); + } + if (agentStatCollectorFactory == null) { + throw new NullPointerException("agentStatCollectorFactory must not be null"); + } + this.dataSender = dataSender; + this.agentId = agentId; + this.agentStartTime = startTime; + this.agentStatCollectorFactory = agentStatCollectorFactory; + this.collectionIntervalMs = collectionInterval; + this.numCollectionsPerBatch = numCollectionsPerBatch; + } + + @Override + public void start() { + CollectJob job = new CollectJob(this.numCollectionsPerBatch); + executor.scheduleAtFixedRate(job, this.collectionIntervalMs, this.collectionIntervalMs, TimeUnit.MILLISECONDS); + logger.info("AgentStat monitor started"); + } + + @Override + public void stop() { + executor.shutdown(); + try { + executor.awaitTermination(3000, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + logger.info("AgentStat monitor stopped"); + } + + // NotThreadSafe + private class CollectJob implements Runnable { + + private final GarbageCollector garbageCollector; + private final CpuLoadCollector cpuLoadCollector; + private final TransactionMetricCollector transactionMetricCollector; + private final ActiveTraceMetricCollector activeTraceMetricCollector; + private final DataSourceCollector dataSourceCollector; + + // Not thread safe. For use with single thread ONLY + private final int numStatsPerBatch; + private int collectCount = 0; + private long prevCollectionTimestamp = System.currentTimeMillis(); + private List agentStats; + + private CollectJob(int numStatsPerBatch) { + this.garbageCollector = agentStatCollectorFactory.getGarbageCollector(); + this.cpuLoadCollector = agentStatCollectorFactory.getCpuLoadCollector(); + this.transactionMetricCollector = agentStatCollectorFactory.getTransactionMetricCollector(); + this.activeTraceMetricCollector = agentStatCollectorFactory.getActiveTraceMetricCollector(); + this.dataSourceCollector = agentStatCollectorFactory.getDataSourceCollector(); + this.numStatsPerBatch = numStatsPerBatch; + this.agentStats = new ArrayList(this.numStatsPerBatch); + } + + @Override + public void run() { + final long currentCollectionTimestamp = System.currentTimeMillis(); + final long collectInterval = currentCollectionTimestamp - this.prevCollectionTimestamp; + try { + final TAgentStat agentStat = collectAgentStat(); + agentStat.setTimestamp(currentCollectionTimestamp); + agentStat.setCollectInterval(collectInterval); + this.agentStats.add(agentStat); + if (++this.collectCount >= this.numStatsPerBatch) { + sendAgentStats(); + this.collectCount = 0; + } + } catch (Exception ex) { + logger.warn("AgentStat collect failed. Caused:{}", ex.getMessage(), ex); + } finally { + this.prevCollectionTimestamp = currentCollectionTimestamp; + } + } + + private TAgentStat collectAgentStat() { + final TAgentStat agentStat = new TAgentStat(); + final TJvmGc gc = garbageCollector.collect(); + agentStat.setGc(gc); + final TCpuLoad cpuLoad = cpuLoadCollector.collect(); + agentStat.setCpuLoad(cpuLoad); + final TTransaction transaction = transactionMetricCollector.collect(); + agentStat.setTransaction(transaction); + final TActiveTrace activeTrace = activeTraceMetricCollector.collect(); + agentStat.setActiveTrace(activeTrace); + final TDataSourceList dataSourceList = dataSourceCollector.collect(); + agentStat.setDataSourceList(dataSourceList); + + return agentStat; + } + + private void sendAgentStats() { + // prepare TAgentStat object. + // TODO multi thread issue. + // If we reuse TAgentStat, there could be concurrency issue because data sender runs in a different thread. + final TAgentStatBatch agentStatBatch = new TAgentStatBatch(); + agentStatBatch.setAgentId(agentId); + agentStatBatch.setStartTimestamp(agentStartTime); + agentStatBatch.setAgentStats(this.agentStats); + // If we reuse agentStats list, there could be concurrency issue because data sender runs in a different + // thread. + // So create new list. + this.agentStats = new ArrayList(this.numStatsPerBatch); + if (isTrace) { + logger.trace("collect agentStat:{}", agentStatBatch); + } + dataSender.send(agentStatBatch); + } + } + +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/monitor/codahale/AgentStatCollectorFactory.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/monitor/codahale/AgentStatCollectorFactory.java index e8778b760..c2e3ee76c 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/monitor/codahale/AgentStatCollectorFactory.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/monitor/codahale/AgentStatCollectorFactory.java @@ -1,11 +1,11 @@ /* - * Copyright 2014 NAVER Corp. + * Copyright 2017 NAVER Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -16,192 +16,23 @@ package com.navercorp.pinpoint.profiler.monitor.codahale; -import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; -import com.navercorp.pinpoint.profiler.context.TransactionCounter; -import com.navercorp.pinpoint.profiler.context.active.ActiveTraceRepository; -import com.navercorp.pinpoint.profiler.context.monitor.DataSourceMonitorWrapper; -import com.navercorp.pinpoint.profiler.context.monitor.DefaultPluginMonitorContext; -import com.navercorp.pinpoint.profiler.context.monitor.PluginMonitorContext; -import com.navercorp.pinpoint.profiler.context.monitor.PluginMonitorWrapperLocator; -import com.navercorp.pinpoint.profiler.monitor.MonitorName; import com.navercorp.pinpoint.profiler.monitor.codahale.activetrace.ActiveTraceMetricCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.activetrace.DefaultActiveTraceMetricCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.activetrace.metric.ActiveTraceMetricSet; import com.navercorp.pinpoint.profiler.monitor.codahale.cpu.CpuLoadCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.cpu.DefaultCpuLoadCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.cpu.metric.CpuLoadMetricSet; import com.navercorp.pinpoint.profiler.monitor.codahale.datasource.DataSourceCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.datasource.DefaultDataSourceCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.datasource.metric.DataSourceMetricSet; -import com.navercorp.pinpoint.profiler.monitor.codahale.gc.CmsCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.gc.CmsDetailedMetricsCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.gc.G1Collector; -import com.navercorp.pinpoint.profiler.monitor.codahale.gc.G1DetailedMetricsCollector; import com.navercorp.pinpoint.profiler.monitor.codahale.gc.GarbageCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.gc.ParallelCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.gc.ParallelDetailedMetricsCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.gc.SerialCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.gc.SerialDetailedMetricsCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.gc.UnknownGarbageCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.tps.DefaultTransactionMetricCollector; import com.navercorp.pinpoint.profiler.monitor.codahale.tps.TransactionMetricCollector; -import com.navercorp.pinpoint.profiler.monitor.codahale.tps.metric.TransactionMetricSet; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Collection; - -import static com.navercorp.pinpoint.profiler.monitor.codahale.MetricMonitorValues.*; /** - * @author emeroad - * @author harebox - * @author hyungil.jeong + * @author Woonduk Kang(emeroad) */ -public class AgentStatCollectorFactory { +public interface AgentStatCollectorFactory { + GarbageCollector getGarbageCollector(); - private final Logger logger = LoggerFactory.getLogger(this.getClass()); + CpuLoadCollector getCpuLoadCollector(); - private final MetricMonitorRegistry monitorRegistry; - private final GarbageCollector garbageCollector; - private final CpuLoadCollector cpuLoadCollector; - private final TransactionMetricCollector transactionMetricCollector; - private final ActiveTraceMetricCollector activeTraceMetricCollector; - private final DataSourceCollector dataSourceCollector; + TransactionMetricCollector getTransactionMetricCollector(); - public AgentStatCollectorFactory(ProfilerConfig profilerConfig, ActiveTraceRepository activeTraceRepository, TransactionCounter transactionCounter, PluginMonitorContext pluginMonitorContext) { - if (profilerConfig == null) { - throw new NullPointerException("profilerConfig must not be null"); - } -// if (activeTraceRepository == null) { -// throw new NullPointerException("activeTraceRepository must not be null"); -// } - if (transactionCounter == null) { - throw new NullPointerException("transactionCounter must not be null"); - } -// if (pluginMonitorContext == null) { -// throw new NullPointerException("pluginMonitorContext must not be null"); -// } - this.monitorRegistry = createRegistry(); - this.garbageCollector = createGarbageCollector(profilerConfig.isProfilerJvmCollectDetailedMetrics()); - this.cpuLoadCollector = createCpuLoadCollector(profilerConfig.getProfilerJvmVendorName()); - this.transactionMetricCollector = createTransactionMetricCollector(transactionCounter); - this.activeTraceMetricCollector = createActiveTraceCollector(activeTraceRepository, profilerConfig.isTraceAgentActiveThread()); - this.dataSourceCollector = createDataSourceCollector(pluginMonitorContext); - } - - private MetricMonitorRegistry createRegistry() { - final MetricMonitorRegistry monitorRegistry = new MetricMonitorRegistry(); - return monitorRegistry; - } - - /** - * create with garbage collector types based on metric registry keys - */ - private GarbageCollector createGarbageCollector(boolean collectDetailedMetrics) { - MetricMonitorRegistry registry = this.monitorRegistry; - registry.registerJvmMemoryMonitor(new MonitorName(MetricMonitorValues.JVM_MEMORY)); - registry.registerJvmGcMonitor(new MonitorName(MetricMonitorValues.JVM_GC)); - - Collection keys = registry.getRegistry().getNames(); - GarbageCollector garbageCollectorToReturn = new UnknownGarbageCollector(); - - if (collectDetailedMetrics) { - if (keys.contains(JVM_GC_SERIAL_OLDGEN_COUNT)) { - garbageCollectorToReturn = new SerialDetailedMetricsCollector(registry); - } else if (keys.contains(JVM_GC_PS_OLDGEN_COUNT)) { - garbageCollectorToReturn = new ParallelDetailedMetricsCollector(registry); - } else if (keys.contains(JVM_GC_CMS_OLDGEN_COUNT)) { - garbageCollectorToReturn = new CmsDetailedMetricsCollector(registry); - } else if (keys.contains(JVM_GC_G1_OLDGEN_COUNT)) { - garbageCollectorToReturn = new G1DetailedMetricsCollector(registry); - } - } else { - if (keys.contains(JVM_GC_SERIAL_OLDGEN_COUNT)) { - garbageCollectorToReturn = new SerialCollector(registry); - } else if (keys.contains(JVM_GC_PS_OLDGEN_COUNT)) { - garbageCollectorToReturn = new ParallelCollector(registry); - } else if (keys.contains(JVM_GC_CMS_OLDGEN_COUNT)) { - garbageCollectorToReturn = new CmsCollector(registry); - } else if (keys.contains(JVM_GC_G1_OLDGEN_COUNT)) { - garbageCollectorToReturn = new G1Collector(registry); - } - } - - if (logger.isInfoEnabled()) { - logger.info("found : {}", garbageCollectorToReturn); - } - return garbageCollectorToReturn; - } - - private CpuLoadCollector createCpuLoadCollector(String vendorName) { - CpuLoadMetricSet cpuLoadMetricSet = this.monitorRegistry.registerCpuLoadMonitor(new MonitorName(MetricMonitorValues.CPU_LOAD), vendorName); - if (logger.isInfoEnabled()) { - logger.info("loaded : {}", cpuLoadMetricSet); - } - return new DefaultCpuLoadCollector(cpuLoadMetricSet); - } - - private TransactionMetricCollector createTransactionMetricCollector(TransactionCounter transactionCounter) { - if (transactionCounter == null) { - return TransactionMetricCollector.EMPTY_TRANSACTION_METRIC_COLLECTOR; - } - - MonitorName monitorName = new MonitorName(MetricMonitorValues.TRANSACTION); - TransactionMetricSet transactionMetricSet = this.monitorRegistry.registerTpsMonitor(monitorName, transactionCounter); - if (logger.isInfoEnabled()) { - logger.info("loaded : {}", transactionMetricSet); - } - return new DefaultTransactionMetricCollector(transactionMetricSet); - - } - - private ActiveTraceMetricCollector createActiveTraceCollector(ActiveTraceRepository activeTraceRepository, boolean isTraceAgentActiveThread) { - if (!isTraceAgentActiveThread) { - return ActiveTraceMetricCollector.EMPTY_ACTIVE_TRACE_COLLECTOR; - } - - if (activeTraceRepository != null) { - ActiveTraceMetricSet activeTraceMetricSet = this.monitorRegistry.registerActiveTraceMetricSet(new MonitorName(MetricMonitorValues.ACTIVE_TRACE), activeTraceRepository); - if (logger.isInfoEnabled()) { - logger.info("loaded : {}", activeTraceMetricSet); - } - return new DefaultActiveTraceMetricCollector(activeTraceMetricSet); - } else { - logger.warn("agent set to trace active threads but no ActiveTraceLocator found"); - } - return ActiveTraceMetricCollector.EMPTY_ACTIVE_TRACE_COLLECTOR; - } - - private DataSourceCollector createDataSourceCollector(PluginMonitorContext pluginMonitorContext) { - if (pluginMonitorContext instanceof DefaultPluginMonitorContext) { - PluginMonitorWrapperLocator dataSourceMonitorLocator = ((DefaultPluginMonitorContext) pluginMonitorContext).getDataSourceMonitorLocator(); - if (dataSourceMonitorLocator != null) { - DataSourceMetricSet dataSourceMetricSet = this.monitorRegistry.registerDataSourceMonitor(new MonitorName(MetricMonitorValues.DATASOURCE), dataSourceMonitorLocator); - return new DefaultDataSourceCollector(dataSourceMetricSet); - } - } - return DataSourceCollector.EMPTY_DATASOURCE_COLLECTOR; - } - - public GarbageCollector getGarbageCollector() { - return this.garbageCollector; - } - - public CpuLoadCollector getCpuLoadCollector() { - return this.cpuLoadCollector; - } - - public TransactionMetricCollector getTransactionMetricCollector() { - return this.transactionMetricCollector; - } - - public ActiveTraceMetricCollector getActiveTraceMetricCollector() { - return this.activeTraceMetricCollector; - } - - public DataSourceCollector getDataSourceCollector() { - return this.dataSourceCollector; - } + ActiveTraceMetricCollector getActiveTraceMetricCollector(); + DataSourceCollector getDataSourceCollector(); } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/monitor/codahale/DefaultAgentStatCollectorFactory.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/monitor/codahale/DefaultAgentStatCollectorFactory.java new file mode 100644 index 000000000..ba1aa43da --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/monitor/codahale/DefaultAgentStatCollectorFactory.java @@ -0,0 +1,214 @@ +/* + * Copyright 2014 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.monitor.codahale; + +import com.google.inject.Inject; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.profiler.context.TransactionCounter; +import com.navercorp.pinpoint.profiler.context.active.ActiveTraceRepository; +import com.navercorp.pinpoint.profiler.context.monitor.DataSourceMonitorWrapper; +import com.navercorp.pinpoint.profiler.context.monitor.DefaultPluginMonitorContext; +import com.navercorp.pinpoint.profiler.context.monitor.PluginMonitorContext; +import com.navercorp.pinpoint.profiler.context.monitor.PluginMonitorWrapperLocator; +import com.navercorp.pinpoint.profiler.monitor.MonitorName; +import com.navercorp.pinpoint.profiler.monitor.codahale.activetrace.ActiveTraceMetricCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.activetrace.DefaultActiveTraceMetricCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.activetrace.metric.ActiveTraceMetricSet; +import com.navercorp.pinpoint.profiler.monitor.codahale.cpu.CpuLoadCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.cpu.DefaultCpuLoadCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.cpu.metric.CpuLoadMetricSet; +import com.navercorp.pinpoint.profiler.monitor.codahale.datasource.DataSourceCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.datasource.DefaultDataSourceCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.datasource.metric.DataSourceMetricSet; +import com.navercorp.pinpoint.profiler.monitor.codahale.gc.CmsCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.gc.CmsDetailedMetricsCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.gc.G1Collector; +import com.navercorp.pinpoint.profiler.monitor.codahale.gc.G1DetailedMetricsCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.gc.GarbageCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.gc.ParallelCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.gc.ParallelDetailedMetricsCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.gc.SerialCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.gc.SerialDetailedMetricsCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.gc.UnknownGarbageCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.tps.DefaultTransactionMetricCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.tps.TransactionMetricCollector; +import com.navercorp.pinpoint.profiler.monitor.codahale.tps.metric.TransactionMetricSet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; + +import static com.navercorp.pinpoint.profiler.monitor.codahale.MetricMonitorValues.*; + +/** + * @author emeroad + * @author harebox + * @author hyungil.jeong + */ +public class DefaultAgentStatCollectorFactory implements AgentStatCollectorFactory { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + private final MetricMonitorRegistry monitorRegistry; + private final GarbageCollector garbageCollector; + private final CpuLoadCollector cpuLoadCollector; + private final TransactionMetricCollector transactionMetricCollector; + private final ActiveTraceMetricCollector activeTraceMetricCollector; + private final DataSourceCollector dataSourceCollector; + + @Inject + public DefaultAgentStatCollectorFactory(ProfilerConfig profilerConfig, ActiveTraceRepository activeTraceRepository, TransactionCounter transactionCounter, PluginMonitorContext pluginMonitorContext) { + if (profilerConfig == null) { + throw new NullPointerException("profilerConfig must not be null"); + } +// if (activeTraceRepository == null) { +// throw new NullPointerException("activeTraceRepository must not be null"); +// } + if (transactionCounter == null) { + throw new NullPointerException("transactionCounter must not be null"); + } +// if (pluginMonitorContext == null) { +// throw new NullPointerException("pluginMonitorContext must not be null"); +// } + this.monitorRegistry = createRegistry(); + this.garbageCollector = createGarbageCollector(profilerConfig.isProfilerJvmCollectDetailedMetrics()); + this.cpuLoadCollector = createCpuLoadCollector(profilerConfig.getProfilerJvmVendorName()); + this.transactionMetricCollector = createTransactionMetricCollector(transactionCounter); + this.activeTraceMetricCollector = createActiveTraceCollector(activeTraceRepository, profilerConfig.isTraceAgentActiveThread()); + this.dataSourceCollector = createDataSourceCollector(pluginMonitorContext); + } + + private MetricMonitorRegistry createRegistry() { + final MetricMonitorRegistry monitorRegistry = new MetricMonitorRegistry(); + return monitorRegistry; + } + + /** + * create with garbage collector types based on metric registry keys + */ + private GarbageCollector createGarbageCollector(boolean collectDetailedMetrics) { + MetricMonitorRegistry registry = this.monitorRegistry; + registry.registerJvmMemoryMonitor(new MonitorName(MetricMonitorValues.JVM_MEMORY)); + registry.registerJvmGcMonitor(new MonitorName(MetricMonitorValues.JVM_GC)); + + Collection keys = registry.getRegistry().getNames(); + GarbageCollector garbageCollectorToReturn = new UnknownGarbageCollector(); + + if (collectDetailedMetrics) { + if (keys.contains(JVM_GC_SERIAL_OLDGEN_COUNT)) { + garbageCollectorToReturn = new SerialDetailedMetricsCollector(registry); + } else if (keys.contains(JVM_GC_PS_OLDGEN_COUNT)) { + garbageCollectorToReturn = new ParallelDetailedMetricsCollector(registry); + } else if (keys.contains(JVM_GC_CMS_OLDGEN_COUNT)) { + garbageCollectorToReturn = new CmsDetailedMetricsCollector(registry); + } else if (keys.contains(JVM_GC_G1_OLDGEN_COUNT)) { + garbageCollectorToReturn = new G1DetailedMetricsCollector(registry); + } + } else { + if (keys.contains(JVM_GC_SERIAL_OLDGEN_COUNT)) { + garbageCollectorToReturn = new SerialCollector(registry); + } else if (keys.contains(JVM_GC_PS_OLDGEN_COUNT)) { + garbageCollectorToReturn = new ParallelCollector(registry); + } else if (keys.contains(JVM_GC_CMS_OLDGEN_COUNT)) { + garbageCollectorToReturn = new CmsCollector(registry); + } else if (keys.contains(JVM_GC_G1_OLDGEN_COUNT)) { + garbageCollectorToReturn = new G1Collector(registry); + } + } + + if (logger.isInfoEnabled()) { + logger.info("found : {}", garbageCollectorToReturn); + } + return garbageCollectorToReturn; + } + + private CpuLoadCollector createCpuLoadCollector(String vendorName) { + CpuLoadMetricSet cpuLoadMetricSet = this.monitorRegistry.registerCpuLoadMonitor(new MonitorName(MetricMonitorValues.CPU_LOAD), vendorName); + if (logger.isInfoEnabled()) { + logger.info("loaded : {}", cpuLoadMetricSet); + } + return new DefaultCpuLoadCollector(cpuLoadMetricSet); + } + + private TransactionMetricCollector createTransactionMetricCollector(TransactionCounter transactionCounter) { + if (transactionCounter == null) { + return TransactionMetricCollector.EMPTY_TRANSACTION_METRIC_COLLECTOR; + } + + MonitorName monitorName = new MonitorName(MetricMonitorValues.TRANSACTION); + TransactionMetricSet transactionMetricSet = this.monitorRegistry.registerTpsMonitor(monitorName, transactionCounter); + if (logger.isInfoEnabled()) { + logger.info("loaded : {}", transactionMetricSet); + } + return new DefaultTransactionMetricCollector(transactionMetricSet); + + } + + private ActiveTraceMetricCollector createActiveTraceCollector(ActiveTraceRepository activeTraceRepository, boolean isTraceAgentActiveThread) { + if (!isTraceAgentActiveThread) { + return ActiveTraceMetricCollector.EMPTY_ACTIVE_TRACE_COLLECTOR; + } + + if (activeTraceRepository != null) { + ActiveTraceMetricSet activeTraceMetricSet = this.monitorRegistry.registerActiveTraceMetricSet(new MonitorName(MetricMonitorValues.ACTIVE_TRACE), activeTraceRepository); + if (logger.isInfoEnabled()) { + logger.info("loaded : {}", activeTraceMetricSet); + } + return new DefaultActiveTraceMetricCollector(activeTraceMetricSet); + } else { + logger.warn("agent set to trace active threads but no ActiveTraceLocator found"); + } + return ActiveTraceMetricCollector.EMPTY_ACTIVE_TRACE_COLLECTOR; + } + + private DataSourceCollector createDataSourceCollector(PluginMonitorContext pluginMonitorContext) { + if (pluginMonitorContext instanceof DefaultPluginMonitorContext) { + PluginMonitorWrapperLocator dataSourceMonitorLocator = ((DefaultPluginMonitorContext) pluginMonitorContext).getDataSourceMonitorLocator(); + if (dataSourceMonitorLocator != null) { + DataSourceMetricSet dataSourceMetricSet = this.monitorRegistry.registerDataSourceMonitor(new MonitorName(MetricMonitorValues.DATASOURCE), dataSourceMonitorLocator); + return new DefaultDataSourceCollector(dataSourceMetricSet); + } + } + return DataSourceCollector.EMPTY_DATASOURCE_COLLECTOR; + } + + @Override + public GarbageCollector getGarbageCollector() { + return this.garbageCollector; + } + + @Override + public CpuLoadCollector getCpuLoadCollector() { + return this.cpuLoadCollector; + } + + @Override + public TransactionMetricCollector getTransactionMetricCollector() { + return this.transactionMetricCollector; + } + + @Override + public ActiveTraceMetricCollector getActiveTraceMetricCollector() { + return this.activeTraceMetricCollector; + } + + @Override + public DataSourceCollector getDataSourceCollector() { + return this.dataSourceCollector; + } + +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/DefaultPluginContextLoadResult.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/DefaultPluginContextLoadResult.java new file mode 100644 index 000000000..1b6059b8c --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/DefaultPluginContextLoadResult.java @@ -0,0 +1,35 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.plugin; + +import java.util.List; + +/** + * @author Woonduk Kang(emeroad) + */ +public class DefaultPluginContextLoadResult implements PluginContextLoadResult { + private final List pluginContextList; + + public DefaultPluginContextLoadResult(List pluginContextList) { + this.pluginContextList = pluginContextList; + } + + @Override + public List getProfilerPluginContextList() { + return pluginContextList; + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/DefaultPluginSetup.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/DefaultPluginSetup.java new file mode 100644 index 000000000..d684f8e3c --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/DefaultPluginSetup.java @@ -0,0 +1,74 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.plugin; + +import com.navercorp.pinpoint.bootstrap.instrument.GuardInstrumentContext; +import com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext; +import com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformTemplate; +import com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformTemplateAware; +import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPlugin; +import com.navercorp.pinpoint.profiler.context.ApplicationContext; +import com.navercorp.pinpoint.profiler.instrument.ClassInjector; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Woonduk Kang(emeroad) + */ +public class DefaultPluginSetup implements PluginSetup { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final ApplicationContext applicationContext; + + + public DefaultPluginSetup(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + @Override + public DefaultProfilerPluginContext setupPlugin(ProfilerPlugin plugin, ClassInjector classInjector) { + + final DefaultProfilerPluginContext context = new DefaultProfilerPluginContext(applicationContext, classInjector); + final GuardProfilerPluginContext guard = new GuardProfilerPluginContext(context); + final GuardInstrumentContext guardInstrumentContext = preparePlugin(plugin, context); + try { + // WARN external plugin api + if (logger.isInfoEnabled()) { + logger.info("{} Plugin setup", plugin.getClass().getName()); + } + plugin.setup(guard); + } finally { + guard.close(); + guardInstrumentContext.close(); + } + return context; + } + + private GuardInstrumentContext preparePlugin(ProfilerPlugin plugin, InstrumentContext context) { + + final GuardInstrumentContext guardInstrumentContext = new GuardInstrumentContext(context); + if (plugin instanceof TransformTemplateAware) { + if (logger.isDebugEnabled()) { + logger.debug("{}.setTransformTemplate", plugin.getClass().getName()); + } + final TransformTemplate transformTemplate = new TransformTemplate(guardInstrumentContext); + ((TransformTemplateAware) plugin).setTransformTemplate(transformTemplate); + } + return guardInstrumentContext; + } + +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/DefaultProfilerPluginContext.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/DefaultProfilerPluginContext.java index 9739e6b43..0a6b37d3c 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/DefaultProfilerPluginContext.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/DefaultProfilerPluginContext.java @@ -23,6 +23,7 @@ import java.util.List; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; import com.navercorp.pinpoint.bootstrap.context.TraceContext; +import com.navercorp.pinpoint.bootstrap.instrument.DynamicTransformTrigger; import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass; import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClassPool; import com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext; @@ -33,7 +34,6 @@ import com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback import com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScope; import com.navercorp.pinpoint.bootstrap.plugin.ApplicationTypeDetector; import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPluginSetupContext; -import com.navercorp.pinpoint.profiler.DynamicTransformService; import com.navercorp.pinpoint.profiler.context.ApplicationContext; import com.navercorp.pinpoint.profiler.context.scope.ConcurrentPool; import com.navercorp.pinpoint.profiler.context.scope.InterceptorScopeFactory; @@ -83,7 +83,7 @@ public class DefaultProfilerPluginContext implements ProfilerPluginSetupContext, if (context == null) { throw new IllegalStateException("TraceContext is not created yet"); } - + return context; } @@ -149,7 +149,7 @@ public class DefaultProfilerPluginContext implements ProfilerPluginSetupContext, final ClassFileTransformerGuardDelegate classFileTransformerGuardDelegate = new ClassFileTransformerGuardDelegate(this, transformCallback); - final DynamicTransformService dynamicTransformService = applicationContext.getDynamicTransformService(); + final DynamicTransformTrigger dynamicTransformService = applicationContext.getDynamicTransformTrigger(); dynamicTransformService.addClassFileTransformer(classLoader, targetClassName, classFileTransformerGuardDelegate); } @@ -165,7 +165,7 @@ public class DefaultProfilerPluginContext implements ProfilerPluginSetupContext, final ClassFileTransformerGuardDelegate classFileTransformerGuardDelegate = new ClassFileTransformerGuardDelegate(this, transformCallback); - final DynamicTransformService dynamicTransformService = applicationContext.getDynamicTransformService(); + final DynamicTransformTrigger dynamicTransformService = applicationContext.getDynamicTransformTrigger(); dynamicTransformService.retransform(target, classFileTransformerGuardDelegate); } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/Provider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/PluginContextLoadResult.java similarity index 77% rename from profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/Provider.java rename to profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/PluginContextLoadResult.java index 01c04e014..b92c5c2f5 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/Provider.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/PluginContextLoadResult.java @@ -14,11 +14,13 @@ * limitations under the License. */ -package com.navercorp.pinpoint.profiler.context.provider; +package com.navercorp.pinpoint.profiler.plugin; + +import java.util.List; /** * @author Woonduk Kang(emeroad) */ -public interface Provider { - T get(); +public interface PluginContextLoadResult { + List getProfilerPluginContextList(); } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/PluginSetup.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/PluginSetup.java new file mode 100644 index 000000000..4b142c4a2 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/PluginSetup.java @@ -0,0 +1,27 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.plugin; + +import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPlugin; +import com.navercorp.pinpoint.profiler.instrument.ClassInjector; + +/** + * @author Woonduk Kang(emeroad) + */ +public interface PluginSetup { + DefaultProfilerPluginContext setupPlugin(ProfilerPlugin plugin, ClassInjector classInjector); +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/ProfilerPluginLoader.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/ProfilerPluginLoader.java index b04583f3b..882e8f4ef 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/ProfilerPluginLoader.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/ProfilerPluginLoader.java @@ -16,6 +16,7 @@ package com.navercorp.pinpoint.profiler.plugin; import java.io.File; import java.io.IOException; +import java.lang.instrument.Instrumentation; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; @@ -26,10 +27,8 @@ import java.util.jar.Attributes; import java.util.jar.JarFile; import java.util.jar.Manifest; -import com.navercorp.pinpoint.bootstrap.instrument.GuardInstrumentContext; -import com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext; -import com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformTemplate; -import com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformTemplateAware; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClassPool; import com.navercorp.pinpoint.bootstrap.util.StringUtils; import com.navercorp.pinpoint.profiler.context.ApplicationContext; import org.slf4j.Logger; @@ -46,17 +45,20 @@ import com.navercorp.pinpoint.profiler.instrument.JarProfilerPluginClassInjector */ public class ProfilerPluginLoader { private final Logger logger = LoggerFactory.getLogger(getClass()); - private final ApplicationContext applicationContext; + private final ApplicationContext applicationContext; private final ClassNameFilter profilerPackageFilter = new PinpointProfilerPackageSkipFilter(); - public ProfilerPluginLoader(ApplicationContext applicationContext) { + private final PluginSetup pluginSetup; + + public ProfilerPluginLoader(ApplicationContext applicationContext, PluginSetup pluginSetup) { if (applicationContext == null) { throw new NullPointerException("applicationContext must not be null"); } this.applicationContext = applicationContext; + this.pluginSetup = pluginSetup; } - + public List load(URL[] pluginJars) { List pluginContexts = new ArrayList(pluginJars.length); List disabled = applicationContext.getProfilerConfig().getDisabledPlugins(); @@ -82,7 +84,8 @@ public class ProfilerPluginLoader { logger.info("Loading plugin:{} pluginPackage:{}", plugin.getClass().getName(), plugin); PluginConfig pluginConfig = new PluginConfig(jar, plugin, applicationContext.getInstrumentation(), applicationContext.getClassPool(), applicationContext.getBootstrapJarPaths(), pluginFilterChain); - final DefaultProfilerPluginContext context = setupPlugin(pluginConfig); + final ClassInjector classInjector = new JarProfilerPluginClassInjector(pluginConfig); + final DefaultProfilerPluginContext context = pluginSetup.setupPlugin(plugin, classInjector); pluginContexts.add(context); } } @@ -137,37 +140,4 @@ public class ProfilerPluginLoader { } - private GuardInstrumentContext preparePlugin(ProfilerPlugin plugin, InstrumentContext context) { - - final GuardInstrumentContext guardInstrumentContext = new GuardInstrumentContext(context); - if (plugin instanceof TransformTemplateAware) { - if (logger.isDebugEnabled()) { - logger.debug("{}.setTransformTemplate", plugin.getClass().getName()); - } - final TransformTemplate transformTemplate = new TransformTemplate(guardInstrumentContext); - ((TransformTemplateAware) plugin).setTransformTemplate(transformTemplate); - } - return guardInstrumentContext; - } - - private DefaultProfilerPluginContext setupPlugin(PluginConfig pluginConfig) { - final ClassInjector classInjector = new JarProfilerPluginClassInjector(pluginConfig); - final DefaultProfilerPluginContext context = new DefaultProfilerPluginContext(applicationContext, classInjector); - - final GuardProfilerPluginContext guardPluginContext = new GuardProfilerPluginContext(context); - final GuardInstrumentContext guardInstrumentContext = preparePlugin(pluginConfig.getPlugin(), context); - try { - // WARN external plugin api - final ProfilerPlugin plugin = pluginConfig.getPlugin(); - if (logger.isInfoEnabled()) { - logger.info("{} Plugin setup", plugin.getClass().getName()); - } - plugin.setup(guardPluginContext); - } finally { - guardPluginContext.close(); - guardInstrumentContext.close(); - } - return context; - } - } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/receiver/CommandDispatcher.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/receiver/CommandDispatcher.java index 2f63853b4..99b5e6806 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/receiver/CommandDispatcher.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/receiver/CommandDispatcher.java @@ -16,6 +16,7 @@ package com.navercorp.pinpoint.profiler.receiver; +import com.google.inject.Inject; import com.navercorp.pinpoint.rpc.MessageListener; import com.navercorp.pinpoint.rpc.PinpointSocket; import com.navercorp.pinpoint.rpc.packet.RequestPacket; @@ -42,6 +43,7 @@ public class CommandDispatcher implements MessageListener, ServerStreamChannelMe private final ProfilerCommandServiceLocator commandServiceLocator; + @Inject public CommandDispatcher(ProfilerCommandServiceLocator commandServiceLocator) { if (commandServiceLocator == null) { throw new NullPointerException("commandServiceLocator may not be null"); diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ApplicationServerTypeResolver.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ApplicationServerTypeResolver.java index 0e26d54b0..dc943a76e 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ApplicationServerTypeResolver.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ApplicationServerTypeResolver.java @@ -21,6 +21,10 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import com.google.inject.Inject; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.profiler.context.module.AgentServiceType; +import com.navercorp.pinpoint.profiler.plugin.PluginContextLoadResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,7 +46,12 @@ public class ApplicationServerTypeResolver { private final ApplicationServerTypePluginResolver resolver; private final List detectors = new ArrayList(); - public ApplicationServerTypeResolver(List plugins, ServiceType defaultType, List orderedDetectors) { + @Inject + public ApplicationServerTypeResolver(PluginContextLoadResult plugins, @AgentServiceType ServiceType defaultType, ProfilerConfig profilerConfig) { + this(plugins, defaultType, profilerConfig.getApplicationTypeDetectOrder()); + } + + public ApplicationServerTypeResolver(PluginContextLoadResult plugins, @AgentServiceType ServiceType defaultType, List orderedDetectors) { if (isValidApplicationServerType(defaultType)) { this.defaultType = defaultType; } else { @@ -58,9 +67,10 @@ public class ApplicationServerTypeResolver { this.resolver = new ApplicationServerTypePluginResolver(this.detectors); } - private Map getRegisteredServerTypeDetectors(List plugins) { + private Map getRegisteredServerTypeDetectors(PluginContextLoadResult plugins) { Map registeredDetectors = new HashMap(); - for (DefaultProfilerPluginContext context : plugins) { + List profilerPluginContextList = plugins.getProfilerPluginContextList(); + for (DefaultProfilerPluginContext context : profilerPluginContextList) { for (ApplicationTypeDetector detector : context.getApplicationTypeDetectors()) { registeredDetectors.put(detector.getClass().getName(), detector); } diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/AgentInfoSenderTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/AgentInfoSenderTest.java index 451b671b8..833ec0331 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/AgentInfoSenderTest.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/AgentInfoSenderTest.java @@ -473,7 +473,7 @@ public class AgentInfoSenderTest { } private AgentInformation getAgentInfo() { - AgentInformation agentInfo = new AgentInformation("agentId", "appName", System.currentTimeMillis(), 1111, "hostname", "127.0.0.1", ServiceType.USER, + AgentInformation agentInfo = new DefaultAgentInformation("agentId", "appName", System.currentTimeMillis(), 1111, "hostname", "127.0.0.1", ServiceType.USER, JvmUtils.getSystemProperty(SystemPropertyKey.JAVA_VERSION), Version.VERSION); return agentInfo; } diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultApplicationContextTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultApplicationContextTest.java new file mode 100644 index 000000000..efa3cd535 --- /dev/null +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultApplicationContextTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context; + +import com.google.inject.Injector; +import com.google.inject.Key; +import com.navercorp.pinpoint.bootstrap.AgentOption; +import com.navercorp.pinpoint.bootstrap.DefaultAgentOption; +import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.common.service.DefaultAnnotationKeyRegistryService; +import com.navercorp.pinpoint.common.service.DefaultServiceTypeRegistryService; +import com.navercorp.pinpoint.profiler.AgentInfoSender; +import com.navercorp.pinpoint.profiler.context.module.SpanDataSender; +import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder; +import com.navercorp.pinpoint.profiler.sender.DataSender; +import com.navercorp.pinpoint.profiler.util.TestInterceptorRegistryBinder; +import org.junit.Assert; +import org.junit.Test; + +import java.net.URL; + +import static org.junit.Assert.*; + +/** + * @author Woonduk Kang(emeroad) + */ +public class DefaultApplicationContextTest { + @Test + public void test() { + ProfilerConfig profilerConfig = new DefaultProfilerConfig(); + InterceptorRegistryBinder binder = new TestInterceptorRegistryBinder(); + AgentOption agentOption = new DefaultAgentOption(new DummyInstrumentation(), + "mockAgent", "mockApplicationName", profilerConfig, new URL[0], + null, new DefaultServiceTypeRegistryService(), new DefaultAnnotationKeyRegistryService()); + + DefaultApplicationContext applicationContext = new DefaultApplicationContext(agentOption, binder); + + Injector injector = applicationContext.getInjector(); + AgentInfoSender instance1 = injector.getInstance(AgentInfoSender.class); + AgentInfoSender instance2 = injector.getInstance(AgentInfoSender.class); + Assert.assertSame(instance1, instance2); + + + } + +} \ No newline at end of file diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultTransactionCounterTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultTransactionCounterTest.java index ddb7832e3..3a0f3e32e 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultTransactionCounterTest.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultTransactionCounterTest.java @@ -28,12 +28,12 @@ import com.navercorp.pinpoint.profiler.context.TransactionCounter.SamplingType; */ public class DefaultTransactionCounterTest { - private IdGenerator idGenerator; + private AtomicIdGenerator idGenerator; private TransactionCounter transactionCounter; @Before public void setUp() throws Exception { - this.idGenerator = new IdGenerator(); + this.idGenerator = new AtomicIdGenerator(); this.transactionCounter = new DefaultTransactionCounter(this.idGenerator); } diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DummyInstrumentation.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DummyInstrumentation.java new file mode 100644 index 000000000..ccd454459 --- /dev/null +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DummyInstrumentation.java @@ -0,0 +1,103 @@ +/* + * Copyright 2017 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.pinpoint.profiler.context; + +import java.lang.instrument.ClassDefinition; +import java.lang.instrument.ClassFileTransformer; +import java.lang.instrument.Instrumentation; +import java.lang.instrument.UnmodifiableClassException; +import java.util.jar.JarFile; + +/** + * @author emeroad + */ +public class DummyInstrumentation implements Instrumentation { + @Override + public void addTransformer(ClassFileTransformer transformer, boolean canRetransform) { + + } + + @Override + public void addTransformer(ClassFileTransformer transformer) { + + } + + @Override + public boolean removeTransformer(ClassFileTransformer transformer) { + return false; + } + + @Override + public boolean isRetransformClassesSupported() { + return false; + } + + @Override + public void retransformClasses(Class... classes) throws UnmodifiableClassException { + + } + + @Override + public boolean isRedefineClassesSupported() { + return false; + } + + @Override + public void redefineClasses(ClassDefinition... definitions) throws ClassNotFoundException, UnmodifiableClassException { + + } + + @Override + public boolean isModifiableClass(Class theClass) { + return false; + } + + @Override + public Class[] getAllLoadedClasses() { + return new Class[0]; + } + + @Override + public Class[] getInitiatedClasses(ClassLoader loader) { + return new Class[0]; + } + + @Override + public long getObjectSize(Object objectToSize) { + return 0; + } + + @Override + public void appendToBootstrapClassLoaderSearch(JarFile jarfile) { + + } + + @Override + public void appendToSystemClassLoaderSearch(JarFile jarfile) { + + } + + @Override + public boolean isNativeMethodPrefixSupported() { + return false; + } + + @Override + public void setNativeMethodPrefix(ClassFileTransformer transformer, String prefix) { + + } +} diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/MockTraceContextFactory.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/MockTraceContextFactory.java index dc046e06b..7d44bb621 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/MockTraceContextFactory.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/MockTraceContextFactory.java @@ -24,6 +24,7 @@ import com.navercorp.pinpoint.bootstrap.sampler.Sampler; import com.navercorp.pinpoint.profiler.AgentInformation; import com.navercorp.pinpoint.profiler.context.active.ActiveTraceRepository; import com.navercorp.pinpoint.profiler.context.monitor.PluginMonitorContext; +import com.navercorp.pinpoint.profiler.context.provider.PluginMonitorContextProvider; import com.navercorp.pinpoint.profiler.context.storage.LogStorageFactory; import com.navercorp.pinpoint.profiler.context.storage.StorageFactory; import com.navercorp.pinpoint.profiler.metadata.ApiMetaDataCacheService; @@ -46,7 +47,7 @@ public class MockTraceContextFactory { private final StorageFactory storageFactory; - private final IdGenerator idGenerator; + private final AtomicIdGenerator idGenerator; private final Sampler sampler; private final ActiveTraceRepository activeTraceRepository; @@ -81,12 +82,12 @@ public class MockTraceContextFactory { final SamplerFactory samplerFactory = new SamplerFactory(); this.sampler = createSampler(profilerConfig, samplerFactory); - this.idGenerator = new IdGenerator(); + this.idGenerator = new AtomicIdGenerator(); this.activeTraceRepository = newActiveTraceRepository(); final TraceFactoryBuilder traceFactoryBuilder = new DefaultTraceFactoryBuilder(storageFactory, sampler, idGenerator, activeTraceRepository); - final PluginMonitorContextBuilder pluginMonitorContextBuilder = new PluginMonitorContextBuilder(TRACE_DATASOURCE); - this.pluginMonitorContext = pluginMonitorContextBuilder.build(); + final PluginMonitorContextProvider pluginMonitorContextBuilder = new PluginMonitorContextProvider(TRACE_DATASOURCE); + this.pluginMonitorContext = pluginMonitorContextBuilder.get(); this.serverMetaDataHolder = new DefaultServerMetaDataHolder(RuntimeMXBeanUtils.getVmArgs()); diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/SpanChunkFactoryTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/SpanChunkFactoryTest.java index f97b15de4..112b6b635 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/SpanChunkFactoryTest.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/SpanChunkFactoryTest.java @@ -21,9 +21,7 @@ import com.navercorp.pinpoint.common.trace.ServiceType; import com.navercorp.pinpoint.common.util.JvmUtils; import com.navercorp.pinpoint.common.util.SystemPropertyKey; import com.navercorp.pinpoint.profiler.AgentInformation; -import com.navercorp.pinpoint.profiler.context.Span; -import com.navercorp.pinpoint.profiler.context.SpanChunkFactory; -import com.navercorp.pinpoint.profiler.context.SpanEvent; +import com.navercorp.pinpoint.profiler.DefaultAgentInformation; import org.junit.Assert; import org.junit.Test; @@ -37,7 +35,7 @@ import java.util.List; public class SpanChunkFactoryTest { @Test public void create() { - AgentInformation agentInformation = new AgentInformation("agentId", "applicationName", 0,0, "machineName", "127.0.0.1", ServiceType.STAND_ALONE, + AgentInformation agentInformation = new DefaultAgentInformation("agentId", "applicationName", 0,0, "machineName", "127.0.0.1", ServiceType.STAND_ALONE, JvmUtils.getSystemProperty(SystemPropertyKey.JAVA_VERSION), Version.VERSION); SpanChunkFactory spanChunkFactory = new SpanChunkFactory(agentInformation); diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/TestAgentInformation.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/TestAgentInformation.java index aaf7f8565..9beb95e6f 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/TestAgentInformation.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/TestAgentInformation.java @@ -21,13 +21,13 @@ import com.navercorp.pinpoint.common.Version; import com.navercorp.pinpoint.common.trace.ServiceType; import com.navercorp.pinpoint.common.util.JvmUtils; import com.navercorp.pinpoint.common.util.SystemPropertyKey; -import com.navercorp.pinpoint.profiler.AgentInformation; +import com.navercorp.pinpoint.profiler.DefaultAgentInformation; /** * TODO duplicate com.navercorp.pinpoint.test.TestAgentInformation * @author HyunGil Jeong */ -public class TestAgentInformation extends AgentInformation { +public class TestAgentInformation extends DefaultAgentInformation { private static final String AGENT_ID = "test-agent"; private static final String APPLICATION_NAME = "TEST_APPLICATION"; diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/storage/BufferedStorageTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/storage/BufferedStorageTest.java index 8bc548151..72f38609a 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/storage/BufferedStorageTest.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/storage/BufferedStorageTest.java @@ -21,10 +21,10 @@ import com.navercorp.pinpoint.common.trace.ServiceType; import com.navercorp.pinpoint.common.util.JvmUtils; import com.navercorp.pinpoint.common.util.SystemPropertyKey; import com.navercorp.pinpoint.profiler.AgentInformation; +import com.navercorp.pinpoint.profiler.DefaultAgentInformation; import com.navercorp.pinpoint.profiler.context.Span; import com.navercorp.pinpoint.profiler.context.SpanChunkFactory; import com.navercorp.pinpoint.profiler.context.SpanEvent; -import com.navercorp.pinpoint.profiler.context.storage.BufferedStorage; import com.navercorp.pinpoint.profiler.sender.CountingDataSender; import org.junit.Assert; @@ -33,7 +33,7 @@ import org.junit.Test; public class BufferedStorageTest { - private AgentInformation agentInformation = new AgentInformation("agentId", "applicationName", 0, 1, "hostName", "127.0.0.1", ServiceType.STAND_ALONE, + private AgentInformation agentInformation = new DefaultAgentInformation("agentId", "applicationName", 0, 1, "hostName", "127.0.0.1", ServiceType.STAND_ALONE, JvmUtils.getSystemProperty(SystemPropertyKey.JAVA_VERSION), Version.VERSION); private SpanChunkFactory spanChunkFactory = new SpanChunkFactory(agentInformation); private CountingDataSender countingDataSender = new CountingDataSender(); diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/monitor/codahale/gc/GarbageCollectorFactoryTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/monitor/codahale/gc/GarbageCollectorFactoryTest.java index aa5bb84ec..db86a2b5c 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/monitor/codahale/gc/GarbageCollectorFactoryTest.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/monitor/codahale/gc/GarbageCollectorFactoryTest.java @@ -18,13 +18,14 @@ package com.navercorp.pinpoint.profiler.monitor.codahale.gc; import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.profiler.context.AtomicIdGenerator; import com.navercorp.pinpoint.profiler.context.DefaultTransactionCounter; -import com.navercorp.pinpoint.profiler.context.IdGenerator; import com.navercorp.pinpoint.profiler.context.TransactionCounter; import com.navercorp.pinpoint.profiler.context.active.ActiveTraceRepository; import com.navercorp.pinpoint.profiler.context.monitor.DefaultPluginMonitorContext; import com.navercorp.pinpoint.profiler.context.monitor.PluginMonitorContext; import com.navercorp.pinpoint.profiler.monitor.codahale.AgentStatCollectorFactory; +import com.navercorp.pinpoint.profiler.monitor.codahale.DefaultAgentStatCollectorFactory; import com.navercorp.pinpoint.thrift.dto.TJvmGc; import org.junit.Test; import org.mockito.Mockito; @@ -43,11 +44,11 @@ public class GarbageCollectorFactoryTest { } ActiveTraceRepository activeTraceRepository = new ActiveTraceRepository(); - IdGenerator idGenerator = new IdGenerator(); + AtomicIdGenerator idGenerator = new AtomicIdGenerator(); TransactionCounter transactionCounter = new DefaultTransactionCounter(idGenerator); PluginMonitorContext pluginMonitorContext = new DefaultPluginMonitorContext(); - return new AgentStatCollectorFactory(profilerConfig, activeTraceRepository, transactionCounter, pluginMonitorContext); + return new DefaultAgentStatCollectorFactory(profilerConfig, activeTraceRepository, transactionCounter, pluginMonitorContext); } diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/SpanStreamSendDataSerializerTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/SpanStreamSendDataSerializerTest.java index d26d29e51..46a7bb2a7 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/SpanStreamSendDataSerializerTest.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/SpanStreamSendDataSerializerTest.java @@ -21,6 +21,7 @@ import com.navercorp.pinpoint.common.trace.ServiceType; import com.navercorp.pinpoint.common.util.JvmUtils; import com.navercorp.pinpoint.common.util.SystemPropertyKey; import com.navercorp.pinpoint.profiler.AgentInformation; +import com.navercorp.pinpoint.profiler.DefaultAgentInformation; import com.navercorp.pinpoint.profiler.context.DefaultTraceId; import com.navercorp.pinpoint.profiler.context.Span; import com.navercorp.pinpoint.profiler.context.SpanChunk; @@ -50,7 +51,7 @@ public class SpanStreamSendDataSerializerTest { @BeforeClass public static void setUp() { - AgentInformation agentInformation = new AgentInformation("agentId", "applicationName", 0, 0, "machineName", "127.0.0.1", ServiceType.STAND_ALONE, + AgentInformation agentInformation = new DefaultAgentInformation("agentId", "applicationName", 0, 0, "machineName", "127.0.0.1", ServiceType.STAND_ALONE, JvmUtils.getSystemProperty(SystemPropertyKey.JAVA_VERSION), Version.VERSION); spanChunkFactory = new SpanChunkFactory(agentInformation); } diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/planer/SpanChunkStreamSendDataPlanerTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/planer/SpanChunkStreamSendDataPlanerTest.java index b6ebf2c03..bd45a30cd 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/planer/SpanChunkStreamSendDataPlanerTest.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/planer/SpanChunkStreamSendDataPlanerTest.java @@ -5,6 +5,7 @@ import com.navercorp.pinpoint.common.trace.ServiceType; import com.navercorp.pinpoint.common.util.JvmUtils; import com.navercorp.pinpoint.common.util.SystemPropertyKey; import com.navercorp.pinpoint.profiler.AgentInformation; +import com.navercorp.pinpoint.profiler.DefaultAgentInformation; import com.navercorp.pinpoint.profiler.context.Span; import com.navercorp.pinpoint.profiler.context.SpanChunk; import com.navercorp.pinpoint.profiler.context.SpanChunkFactory; @@ -39,7 +40,7 @@ public class SpanChunkStreamSendDataPlanerTest { @BeforeClass public static void setUp() { - AgentInformation agentInformation = new AgentInformation("agentId", "applicationName", 0, 0, "machineName", "127.0.0.1", ServiceType.STAND_ALONE, + AgentInformation agentInformation = new DefaultAgentInformation("agentId", "applicationName", 0, 0, "machineName", "127.0.0.1", ServiceType.STAND_ALONE, JvmUtils.getSystemProperty(SystemPropertyKey.JAVA_VERSION), Version.VERSION); HeaderTBaseSerializerPoolFactory serializerFactory = new HeaderTBaseSerializerPoolFactory(true, 1000, true); diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/planer/SpanStreamSendDataPlanerTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/planer/SpanStreamSendDataPlanerTest.java index 07319d411..901838f65 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/planer/SpanStreamSendDataPlanerTest.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/planer/SpanStreamSendDataPlanerTest.java @@ -5,6 +5,7 @@ import com.navercorp.pinpoint.common.trace.ServiceType; import com.navercorp.pinpoint.common.util.JvmUtils; import com.navercorp.pinpoint.common.util.SystemPropertyKey; import com.navercorp.pinpoint.profiler.AgentInformation; +import com.navercorp.pinpoint.profiler.DefaultAgentInformation; import com.navercorp.pinpoint.profiler.context.DefaultTraceId; import com.navercorp.pinpoint.profiler.context.Span; import com.navercorp.pinpoint.profiler.context.SpanChunkFactory; @@ -39,7 +40,7 @@ public class SpanStreamSendDataPlanerTest { @BeforeClass public static void setUp() { - AgentInformation agentInformation = new AgentInformation("agentId", "applicationName", 0, 0, "machineName", "127.0.0.1", ServiceType.STAND_ALONE, + AgentInformation agentInformation = new DefaultAgentInformation("agentId", "applicationName", 0, 0, "machineName", "127.0.0.1", ServiceType.STAND_ALONE, JvmUtils.getSystemProperty(SystemPropertyKey.JAVA_VERSION), Version.VERSION); HeaderTBaseSerializerPoolFactory serializerFactory = new HeaderTBaseSerializerPoolFactory(true, 1000, true);