From cda0888352a8e064ea3933c820e7bae82e8faaae Mon Sep 17 00:00:00 2001 From: emeroad Date: Fri, 27 Dec 2024 17:57:15 +0900 Subject: [PATCH] [#11877] Improved Agent initialization --- .../PinpointClassLoaderFactory.java | 25 +++- .../config/DefaultProfilerConfig.java | 140 ++++++++---------- .../bootstrap/config/ProfilerConfig.java | 22 +-- .../config/ProfilerConfigLoader.java | 3 +- .../pinpoint/bootstrap/config/Profiles.java | 19 +-- .../bootstrap/context/TraceContext.java | 1 + .../request/DefaultRequestTraceWriter.java | 14 +- .../plugin/request/RequestTraceReader.java | 4 +- agent-module/bootstraps/bootstrap/pom.xml | 5 - .../com/navercorp/pinpoint/ProductInfo.java | 24 --- .../pinpoint/bootstrap/AgentBootLoader.java | 31 ++-- .../pinpoint/bootstrap/AgentOption.java | 19 ++- .../bootstrap/DefaultAgentOption.java | 81 +++++----- .../pinpoint/bootstrap/PinpointBootStrap.java | 3 +- .../pinpoint/bootstrap/PinpointStarter.java | 115 ++++---------- .../bootstrap/agentdir/AgentDirectory.java | 19 +-- .../pinpoint/bootstrap/config/LogConfig.java | 35 ----- .../config/ProfilePropertyLoader.java | 75 +++------- .../config/PropertyLoaderFactory.java | 16 +- .../bootstrap/config/PropertyLoaderUtils.java | 34 ++++- .../config/SimpleLogConfigResolver.java | 36 ----- .../config/SimplePropertyLoader.java | 43 ++---- .../bootstrap/util/ProfileConstants.java | 21 +++ .../bootstrap/AgentBootLoaderTest.java | 47 +++--- .../bootstrap/AgentIdResolverTest.java | 107 ------------- .../bootstrap/AgentIdSourceTypeTest.java | 12 -- .../pinpoint/bootstrap/DummyAgent.java | 10 +- .../bootstrap/agentdir/LogDirCleanerTest.java | 96 ------------ .../bootstrap/config/PropertyUtilsTest.java | 0 .../navercorp/pinpoint/profiler/Agent.java | 17 +++ agent-module/bootstraps/pom.xml | 6 + .../plugins-test-module/plugins-test/pom.xml | 4 - .../profiler/logging/Log4j2LoggingSystem.java | 37 ++--- .../logging/Log4j2LoggingSystemTest.java | 9 +- .../profiler-optional-parent/pom.xml | 14 -- agent-module/profiler-test/pom.xml | 4 - .../test/MockApplicationContextFactory.java | 45 +++--- .../test/MockTraceContextFactory.java | 2 +- .../profiler/test/OverrideModuleFactory.java | 6 +- .../profiler/test/PluginTestAgent.java | 45 +++++- .../profiler/test/TestAgentInformation.java | 5 +- .../MockApplicationContextModuleTest.java | 56 +++---- agent-module/profiler/pom.xml | 8 +- .../pinpoint/profiler/VersionTemplate.java | 7 + .../navercorp/pinpoint/profiler}/Agent.java | 9 +- .../pinpoint/profiler/AgentContextOption.java | 28 ++++ .../profiler/AgentContextOptionBuilder.java | 23 +++ .../pinpoint/profiler/AgentInformation.java | 2 + .../pinpoint/profiler/AgentOption.java | 100 +++++++++++++ .../pinpoint/profiler/DefaultAgent.java | 117 +++++++++++---- .../profiler/DefaultAgentContextOption.java | 104 +++++++++++++ .../profiler/DefaultAgentInformation.java | 39 +++-- .../navercorp/pinpoint/profiler/Version.java | 5 + .../profiler}/config/AgentSystemConfig.java | 14 +- .../pinpoint/profiler/config/LogConfig.java | 40 +++++ .../profiler/config}/LogDirCleaner.java | 23 ++- .../profiler/context/DefaultTraceContext.java | 10 +- .../grpc/config/GrpcTransportConfig.java | 4 +- .../context/module/ApplicationContext.java | 5 +- .../ApplicationContextModuleFactory.java | 6 +- .../context/module/ClusterNamespace.java} | 18 ++- .../module/DefaultApplicationContext.java | 9 +- .../profiler/context/module/GrpcModule.java | 6 +- .../context/module/ModuleFactory.java | 4 +- .../config/ClusterNamespaceProvider.java | 43 ++++++ .../context/module/config/ConfigModule.java | 37 ++--- .../provider/AgentInformationProvider.java | 14 +- .../provider/TraceContextProvider.java | 6 +- .../provider/plugin/PluginJarsProvider.java | 9 +- .../classloading/BootstrapCore.java | 3 +- .../config/DefaultInstrumentConfig.java | 5 +- .../lambda/LambdaBytecodeLogger.java | 7 +- .../instrument/scanner/JarFileRepository.java | 7 +- .../profiler/name}/AgentIdResolver.java | 27 ++-- .../name}/AgentIdResolverBuilder.java | 2 +- .../profiler/name}/AgentIdSourceType.java | 2 +- .../pinpoint/profiler/name}/AgentIds.java | 2 +- .../profiler/name}/AgentProperties.java | 59 ++++---- .../pinpoint/profiler/name}/IdValidator.java | 8 +- .../pinpoint/profiler/plugin/PluginJar.java | 11 +- .../ChannelzScheduledReporterBuilder.java | 12 +- .../profiler/util/ContainerResolver.java | 12 +- .../profiler/config/LogDirCleanerTest.java | 0 .../context/DefaultTraceContextTest.java | 27 +--- .../MockApplicationContextFactory.java | 13 +- .../context/TestAgentInformation.java | 5 +- .../context/graph/DependencyGraph.java | 24 +-- .../module/DefaultApplicationContextTest.java | 28 ++-- .../module/ModuleFactoryResolverTest.java | 4 +- .../context/module/OverrideModuleFactory.java | 4 +- .../JarProfilerPluginClassInjectorTest.java | 6 +- .../profiler/name/AgentIdResolverTest.java | 0 .../grpc/AgentGrpcDataSenderTestMain.java | 14 +- .../main/resources/pinpoint-banner/banner.txt | 1 + collector/pom.xml | 4 - .../common/profiler/name}/AgentUuidUtils.java | 4 +- .../profiler/name}/AgentUuidUtilsTest.java | 3 +- 97 files changed, 1209 insertions(+), 1082 deletions(-) delete mode 100644 agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/ProductInfo.java delete mode 100644 agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/LogConfig.java delete mode 100644 agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/SimpleLogConfigResolver.java create mode 100644 agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/util/ProfileConstants.java delete mode 100644 agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/AgentIdResolverTest.java delete mode 100644 agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/AgentIdSourceTypeTest.java delete mode 100644 agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/agentdir/LogDirCleanerTest.java create mode 100644 agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/config/PropertyUtilsTest.java create mode 100644 agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/profiler/Agent.java create mode 100644 agent-module/profiler/src/main/java-templates/com/navercorp/pinpoint/profiler/VersionTemplate.java rename agent-module/{bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap => profiler/src/main/java/com/navercorp/pinpoint/profiler}/Agent.java (85%) create mode 100644 agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentContextOption.java create mode 100644 agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentContextOptionBuilder.java create mode 100644 agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentOption.java create mode 100644 agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgentContextOption.java create mode 100644 agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/Version.java rename agent-module/{bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap => profiler/src/main/java/com/navercorp/pinpoint/profiler}/config/AgentSystemConfig.java (57%) create mode 100644 agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/config/LogConfig.java rename agent-module/{bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/agentdir => profiler/src/main/java/com/navercorp/pinpoint/profiler/config}/LogDirCleaner.java (77%) rename agent-module/{bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/LogConfigResolver.java => profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ClusterNamespace.java} (59%) create mode 100644 agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/config/ClusterNamespaceProvider.java rename agent-module/{bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap => profiler/src/main/java/com/navercorp/pinpoint/profiler/name}/AgentIdResolver.java (76%) rename agent-module/{bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap => profiler/src/main/java/com/navercorp/pinpoint/profiler/name}/AgentIdResolverBuilder.java (96%) rename agent-module/{bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap => profiler/src/main/java/com/navercorp/pinpoint/profiler/name}/AgentIdSourceType.java (97%) rename agent-module/{bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap => profiler/src/main/java/com/navercorp/pinpoint/profiler/name}/AgentIds.java (96%) rename agent-module/{bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap => profiler/src/main/java/com/navercorp/pinpoint/profiler/name}/AgentProperties.java (52%) rename agent-module/{bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap => profiler/src/main/java/com/navercorp/pinpoint/profiler/name}/IdValidator.java (90%) create mode 100644 agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/config/LogDirCleanerTest.java create mode 100644 agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/name/AgentIdResolverTest.java rename {commons/src/main/java/com/navercorp/pinpoint/common/util => commons-profiler/src/main/java/com/navercorp/pinpoint/common/profiler/name}/AgentUuidUtils.java (97%) rename {commons/src/test/java/com/navercorp/pinpoint/common/util => commons-profiler/src/test/java/com/navercorp/pinpoint/common/profiler/name}/AgentUuidUtilsTest.java (95%) diff --git a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/classloader/PinpointClassLoaderFactory.java b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/classloader/PinpointClassLoaderFactory.java index 8a76974d42e5..5b32d455da3d 100644 --- a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/classloader/PinpointClassLoaderFactory.java +++ b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/classloader/PinpointClassLoaderFactory.java @@ -18,10 +18,15 @@ import com.navercorp.pinpoint.common.util.JvmUtils; import com.navercorp.pinpoint.common.util.JvmVersion; +import com.navercorp.pinpoint.common.util.StringUtils; import java.lang.reflect.Constructor; import java.net.URL; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Properties; +import java.util.Set; /** * @author Taejin Koo @@ -80,9 +85,27 @@ private static ClassLoaderFactory newParallelClassLoaderFactory() { } } - public static ClassLoader createClassLoader(String name, URL[] urls, ClassLoader parent, List libClass) { return CLASS_LOADER_FACTORY.createClassLoader(name, urls, parent, libClass); } + + public static ClassLoader createClassLoader(String name, URL[] urls, ClassLoader parent, Properties properties) { + List libClass = getAgentClassloaderLibs(properties); + return createClassLoader(name, urls, parent, libClass); + } + + public static final String AGENT_CLASSLOADER_ADDITIONAL_LIBS = "profiler.agent.classloader.additional-libs"; + + private static List getAgentClassloaderLibs(Properties properties) { + Set libs = new HashSet<>(ProfilerLibs.PINPOINT_PROFILER_CLASS); + + String libsString = properties.getProperty(AGENT_CLASSLOADER_ADDITIONAL_LIBS, ""); + List additionalLibs = StringUtils.tokenizeToStringList(libsString, ","); + libs.addAll(additionalLibs); + + List copy = new ArrayList<>(libs); + copy.sort(String.CASE_INSENSITIVE_ORDER); + return copy; + } } diff --git a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/DefaultProfilerConfig.java b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/DefaultProfilerConfig.java index 4e65628f8f84..2ac0a22b5a44 100644 --- a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/DefaultProfilerConfig.java +++ b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/DefaultProfilerConfig.java @@ -18,8 +18,6 @@ import com.navercorp.pinpoint.bootstrap.plugin.jdbc.JdbcOption; import com.navercorp.pinpoint.bootstrap.util.NumberUtils; -import com.navercorp.pinpoint.common.annotations.VisibleForTesting; -import com.navercorp.pinpoint.common.config.Value; import com.navercorp.pinpoint.common.config.util.PlaceHolder; import com.navercorp.pinpoint.common.config.util.spring.PropertyPlaceholderHelper; import com.navercorp.pinpoint.common.util.StringUtils; @@ -37,43 +35,17 @@ * @author netspider */ public class DefaultProfilerConfig implements ProfilerConfig { - public static final String PROFILER_INTERCEPTOR_EXCEPTION_PROPAGATE = "profiler.interceptor.exception.propagate"; + public static final String AGENT_ROOT_PATH_KEY = "pinpoint.agent.root.path"; // TestAgent only public static final String IMPORT_PLUGIN = "profiler.plugin.import-plugin"; - public static final String AGENT_CLASSLOADER_ADDITIONAL_LIBS = "profiler.agent.classloader.additional-libs"; - private final PropertyPlaceholderHelper placeholder; private final Properties properties; private final JdbcOption jdbcOption; - @Value("${pinpoint.disable:false}") - private String pinpointDisable = "false"; - - @Value("${profiler.logdir.maxbackupsize}") - private int logDirMaxBackupSize = 5; - - @Value("${" + Profiles.ACTIVE_PROFILE_KEY + " }") - private String activeProfile = ""; - - @VisibleForTesting - private boolean staticResourceCleanup = false; - - @Value("${profiler.transport.grpc.stats.logging.period}") - private String grpcStatLoggingPeriod = "PT1M"; - - @Value("${profiler.guice.module.factory}") - private String injectionModuleFactoryClazzName = null; - @Value("${profiler.application.namespace}") - private String applicationNamespace = ""; - - @Value("${" + AGENT_CLASSLOADER_ADDITIONAL_LIBS + "}") - private String agentClassloaderAdditionalLibs = ""; - - public DefaultProfilerConfig() { this.properties = new Properties(); this.jdbcOption = JdbcOption.empty(); @@ -91,7 +63,6 @@ private PropertyPlaceholderHelper newPlaceholder(boolean ignoreUnresolvablePlace return new PropertyPlaceholderHelper(PlaceHolder.START, PlaceHolder.END, PlaceHolder.DELIMITER, ignoreUnresolvablePlaceholders); } - @Override public Properties getProperties() { return properties; } @@ -101,46 +72,67 @@ public JdbcOption getJdbcOption() { return jdbcOption; } - @Override - public String getActiveProfile() { - return activeProfile; - } - - @Override - public String getPinpointDisable() { - return pinpointDisable; - } - - @Override - public String getGrpcStatLoggingPeriod() { - return grpcStatLoggingPeriod; - } - - @Override - public boolean getStaticResourceCleanup() { - return staticResourceCleanup; - } - - @Override - public String getInjectionModuleFactoryClazzName() { - return injectionModuleFactoryClazzName; - } - - @Override - public String getApplicationNamespace() { - return applicationNamespace; - } - - - @Override - public int getLogDirMaxBackupSize() { - return logDirMaxBackupSize; - } - - @Override - public List getAgentClassloaderAdditionalLibs() { - return StringUtils.tokenizeToStringList(agentClassloaderAdditionalLibs, ","); - } + // +// @Override +// public String getActiveProfile() { +// return activeProfile; +// } +// +// @Override +// public int getJdbcSqlCacheSize() { +// return jdbcSqlCacheSize; +// } +// +// @Override +// public boolean isTraceSqlBindValue() { +// return traceSqlBindValue; +// } +// +// @Override +// public int getMaxSqlBindValueSize() { +// return maxSqlBindValueSize; +// } +// +// @Override +// public int getMaxSqlCacheLength() { +// return maxSqlCacheLength; +// } +// +// @Override +// public int getMaxSqlLength() { +// return maxSqlLength; +// } +// +// @Override +// public String getGrpcStatLoggingPeriod() { +// return grpcStatLoggingPeriod; +// } +// +// @Override +// public HttpStatusCodeErrors getHttpStatusCodeErrors() { +// return httpStatusCodeErrors; +// } +// +// @Value("${profiler.http.status.code.errors}") +// void setHttpStatusCodeErrors(String httpStatusCodeErrors) { +// List httpStatusCodeErrorList = StringUtils.tokenizeToStringList(httpStatusCodeErrors, ","); +// this.httpStatusCodeErrors = new HttpStatusCodeErrors(httpStatusCodeErrorList); +// } +// +// @Override +// public String getInjectionModuleFactoryClazzName() { +// return injectionModuleFactoryClazzName; +// } +// +// @Override +// public String getApplicationNamespace() { +// return applicationNamespace; +// } +// +// @Override +// public String getAgentRootPath() { +// return agentRootPath; +// } @Override @@ -223,14 +215,6 @@ public Map readPattern(String propertyNamePatternRegex) { public String toString() { return "DefaultProfilerConfig{" + "properties=" + properties + - ", pinpointDisable='" + pinpointDisable + '\'' + - ", logDirMaxBackupSize=" + logDirMaxBackupSize + - ", activeProfile='" + activeProfile + '\'' + - ", staticResourceCleanup=" + staticResourceCleanup + - ", grpcStatLoggingPeriod='" + grpcStatLoggingPeriod + '\'' + - ", injectionModuleFactoryClazzName='" + injectionModuleFactoryClazzName + '\'' + - ", applicationNamespace='" + applicationNamespace + '\'' + - ", agentClassloaderAdditionalLibs='" + agentClassloaderAdditionalLibs + '\'' + '}'; } } diff --git a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilerConfig.java b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilerConfig.java index 235002244afd..6119a6f05d4d 100644 --- a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilerConfig.java +++ b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilerConfig.java @@ -17,8 +17,6 @@ package com.navercorp.pinpoint.bootstrap.config; import com.navercorp.pinpoint.bootstrap.plugin.jdbc.JdbcOption; -import com.navercorp.pinpoint.common.annotations.InterfaceAudience; -import com.navercorp.pinpoint.common.annotations.VisibleForTesting; import java.util.List; import java.util.Map; @@ -29,25 +27,9 @@ */ public interface ProfilerConfig { - String getActiveProfile(); - - JdbcOption getJdbcOption(); - Properties getProperties(); - String getPinpointDisable(); - - String getGrpcStatLoggingPeriod(); - - @InterfaceAudience.Private - @VisibleForTesting - boolean getStaticResourceCleanup(); - - String getInjectionModuleFactoryClazzName(); - - String getApplicationNamespace(); - - List getAgentClassloaderAdditionalLibs(); + JdbcOption getJdbcOption(); String readString(String propertyName); @@ -63,6 +45,4 @@ public interface ProfilerConfig { Map readPattern(String propertyNamePatternRegex); - int getLogDirMaxBackupSize(); - } diff --git a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilerConfigLoader.java b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilerConfigLoader.java index 59aa9fe3582d..6cd95449ad3d 100644 --- a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilerConfigLoader.java +++ b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilerConfigLoader.java @@ -32,7 +32,7 @@ public static ProfilerConfig load(InputStream inputStream) { return load(properties); } - private static Properties loadProperties(InputStream inputStream) { + public static Properties loadProperties(InputStream inputStream) { try { return PropertyUtils.loadProperty(inputStream); } catch (IOException ex) { @@ -51,5 +51,4 @@ public static ProfilerConfig load(Properties properties) { return profilerConfig; } - } diff --git a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/Profiles.java b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/Profiles.java index 0a38901a0f2b..f7ece555713a 100644 --- a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/Profiles.java +++ b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/Profiles.java @@ -23,22 +23,9 @@ public final class Profiles { private Profiles() { } + /** + * see : ProfileConstants + */ public static final String LOG_CONFIG_LOCATION_KEY = "pinpoint.profiler.log.config.location"; - public static final String CONFIG_LOAD_MODE_KEY = "pinpoint.config.load.mode"; - - public enum CONFIG_LOAD_MODE { - PROFILE, - // for IT TEST - SIMPLE - } - - public static final String ACTIVE_PROFILE_KEY = "pinpoint.profiler.profiles.active"; - - // 1. default config - public static final String CONFIG_FILE_NAME = "pinpoint-root.config"; - // 2. profile config - public static final String PROFILE_CONFIG_FILE_NAME = "pinpoint.config"; - // 3. external config - public static final String EXTERNAL_CONFIG_KEY = "pinpoint.config"; } diff --git a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/context/TraceContext.java b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/context/TraceContext.java index a87161b7fda6..7379b9a2a416 100644 --- a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/context/TraceContext.java +++ b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/context/TraceContext.java @@ -100,4 +100,5 @@ public interface TraceContext { JdbcContext getJdbcContext(); + String getClusterNamespace(); } diff --git a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/DefaultRequestTraceWriter.java b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/DefaultRequestTraceWriter.java index 8400db509efd..b61768543e61 100644 --- a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/DefaultRequestTraceWriter.java +++ b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/DefaultRequestTraceWriter.java @@ -38,18 +38,18 @@ public class DefaultRequestTraceWriter implements RequestTraceWriter { private final ClientHeaderAdaptor clientHeaderAdaptor; private final String applicationName; private final short serverTypeCode; - private final String applicationNamespace; + private final String clusterNamespace; public DefaultRequestTraceWriter(ClientHeaderAdaptor clientHeaderAdaptor, TraceContext traceContext) { - this(clientHeaderAdaptor, traceContext.getApplicationName(), traceContext.getServerTypeCode(), traceContext.getProfilerConfig().getApplicationNamespace()); + this(clientHeaderAdaptor, traceContext.getApplicationName(), traceContext.getServerTypeCode(), traceContext.getClusterNamespace()); } - public DefaultRequestTraceWriter(ClientHeaderAdaptor clientHeaderAdaptor, String applicationName, short serverTypeCode, String applicationNamespace) { + public DefaultRequestTraceWriter(ClientHeaderAdaptor clientHeaderAdaptor, String applicationName, short serverTypeCode, String clusterNamespace) { this.clientHeaderAdaptor = Objects.requireNonNull(clientHeaderAdaptor, "clientHeaderAdaptor"); this.applicationName = Objects.requireNonNull(applicationName, "applicationName"); this.serverTypeCode = serverTypeCode; - this.applicationNamespace = StringUtils.defaultIfEmpty(applicationNamespace, NOT_SET); + this.clusterNamespace = StringUtils.defaultIfEmpty(clusterNamespace, NOT_SET); } @Override @@ -66,7 +66,7 @@ public void write(T header, final TraceId traceId, final String host) { Objects.requireNonNull(traceId, "traceId"); if (isDebug) { - logger.debug("Set request header. traceId={}, applicationName={}, serverTypeCode={}, applicationNamespace={}", traceId, applicationName, serverTypeCode, applicationNamespace); + logger.debug("Set request header. traceId={}, applicationName={}, serverTypeCode={}, clusterNamespace={}", traceId, applicationName, serverTypeCode, clusterNamespace); } clientHeaderAdaptor.setHeader(header, Header.HTTP_TRACE_ID.toString(), traceId.getTransactionId()); clientHeaderAdaptor.setHeader(header, Header.HTTP_SPAN_ID.toString(), String.valueOf(traceId.getSpanId())); @@ -75,8 +75,8 @@ public void write(T header, final TraceId traceId, final String host) { clientHeaderAdaptor.setHeader(header, Header.HTTP_PARENT_APPLICATION_NAME.toString(), applicationName); clientHeaderAdaptor.setHeader(header, Header.HTTP_PARENT_APPLICATION_TYPE.toString(), Short.toString(serverTypeCode)); - if (applicationNamespace != NOT_SET) { - clientHeaderAdaptor.setHeader(header, Header.HTTP_PARENT_APPLICATION_NAMESPACE.toString(), applicationNamespace); + if (clusterNamespace != NOT_SET) { + clientHeaderAdaptor.setHeader(header, Header.HTTP_PARENT_APPLICATION_NAMESPACE.toString(), clusterNamespace); } if (host != null) { diff --git a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/RequestTraceReader.java b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/RequestTraceReader.java index 83d51bf827df..f7a484f34784 100644 --- a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/RequestTraceReader.java +++ b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/RequestTraceReader.java @@ -49,8 +49,8 @@ public RequestTraceReader(final TraceContext traceContext, RequestAdaptor req this.requestAdaptor = Objects.requireNonNull(requestAdaptor, "requestAdaptor"); this.traceHeaderReader = new DefaultTraceHeaderReader(requestAdaptor); this.async = async; - String applicationNamespace = traceContext.getProfilerConfig().getApplicationNamespace(); - this.nameSpaceChecker = NameSpaceCheckFactory.newNamespace(requestAdaptor, applicationNamespace); + String clusterNamespace = traceContext.getClusterNamespace(); + this.nameSpaceChecker = NameSpaceCheckFactory.newNamespace(requestAdaptor, clusterNamespace); } // Read the transaction information from the request. diff --git a/agent-module/bootstraps/bootstrap/pom.xml b/agent-module/bootstraps/bootstrap/pom.xml index cf5e63ff3745..06fb673ac61d 100644 --- a/agent-module/bootstraps/bootstrap/pom.xml +++ b/agent-module/bootstraps/bootstrap/pom.xml @@ -13,11 +13,6 @@ jar - - com.navercorp.pinpoint - pinpoint-bootstrap-core - - org.apache.logging.log4j diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/ProductInfo.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/ProductInfo.java deleted file mode 100644 index 45983dca6e4a..000000000000 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/ProductInfo.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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; - -/** - * @author emeroad - */ -public final class ProductInfo { - public static final String NAME = "pinpoint"; -} diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentBootLoader.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentBootLoader.java index 99bb8883d0b9..0ad8e4025d64 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentBootLoader.java +++ b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentBootLoader.java @@ -17,6 +17,7 @@ package com.navercorp.pinpoint.bootstrap; import java.lang.reflect.Constructor; +import java.util.Map; import java.util.Objects; import java.util.concurrent.Callable; @@ -38,16 +39,20 @@ public AgentBootLoader(String bootClass, ClassLoader agentClassLoader) { this.executeTemplate = new ContextClassLoaderExecuteTemplate<>(agentClassLoader); } - public Agent boot(final AgentOption agentOption) { + public Object boot(final AgentOption agentOption) { - final Class bootStrapClazz = getBootStrapClass(); + final Class agentClazz = getBootStrapClass("com.navercorp.pinpoint.profiler.Agent"); + final Class bootStrapClazz = getBootStrapClass(bootClass); + if (!agentClazz.isAssignableFrom(bootStrapClazz)) { + throw new IllegalStateException("Invalid AgentClass:" + bootStrapClazz); + } - final Object agent = executeTemplate.execute(new Callable() { + return executeTemplate.execute(new Callable() { @Override public Object call() throws Exception { try { - Constructor constructor = bootStrapClazz.getDeclaredConstructor(AgentOption.class); - return constructor.newInstance(agentOption); + Constructor constructor = bootStrapClazz.getDeclaredConstructor(Map.class); + return constructor.newInstance(agentOption.toMap()); } catch (InstantiationException e) { throw new BootStrapException("boot create failed. Error:" + e.getMessage(), e); } catch (IllegalAccessException e) { @@ -55,23 +60,11 @@ public Object call() throws Exception { } } }); - - if (agent instanceof Agent) { - return (Agent) agent; - } else { - String agentClassName; - if (agent == null) { - agentClassName = "Agent is null"; - } else { - agentClassName = agent.getClass().getName(); - } - throw new BootStrapException("Invalid AgentType. boot failed. AgentClass:" + agentClassName); - } } - private Class getBootStrapClass() { + private Class getBootStrapClass(String clazzName) { try { - return this.classLoader.loadClass(bootClass); + return this.classLoader.loadClass(clazzName); } catch (ClassNotFoundException e) { throw new BootStrapException("boot class not found. bootClass:" + bootClass + " Error:" + e.getMessage(), e); } diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentOption.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentOption.java index 56b0f63415b5..630c35f58937 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentOption.java +++ b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentOption.java @@ -16,10 +16,11 @@ package com.navercorp.pinpoint.bootstrap; -import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; - import java.lang.instrument.Instrumentation; +import java.nio.file.Path; import java.util.List; +import java.util.Map; +import java.util.Properties; /** * @author emeroad @@ -28,17 +29,15 @@ public interface AgentOption { Instrumentation getInstrumentation(); - String getAgentId(); - - String getAgentName(); - - String getApplicationName(); + Properties getProperties(); + Map getAgentArgs(); - ProfilerConfig getProfilerConfig(); + Path getAgentPath(); - List getPluginJars(); + List getPluginJars(); - List getBootstrapJarPaths(); + List getBootstrapJarPaths(); + Map toMap(); } diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/DefaultAgentOption.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/DefaultAgentOption.java index 5a99860f8222..4f497d0524d3 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/DefaultAgentOption.java +++ b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/DefaultAgentOption.java @@ -16,11 +16,14 @@ package com.navercorp.pinpoint.bootstrap; -import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; import java.lang.instrument.Instrumentation; +import java.nio.file.Path; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Objects; +import java.util.Properties; /** * @author emeroad @@ -29,23 +32,24 @@ public class DefaultAgentOption implements AgentOption { private final Instrumentation instrumentation; - private final String agentId; - private final String agentName; - private final String applicationName; + private final Properties properties; + private final Map agentArgs; - private final ProfilerConfig profilerConfig; - private final List pluginJars; - private final List bootstrapJarPaths; + private final Path agentPath; + private final List pluginJars; + private final List bootstrapJarPaths; public DefaultAgentOption(final Instrumentation instrumentation, - String agentId, String agentName, String applicationName, - final ProfilerConfig profilerConfig, final List pluginJars, - final List bootstrapJarPaths) { + final Properties properties, + final Map agentArgs, + final Path agentPath, + final List pluginJars, + final List bootstrapJarPaths) { this.instrumentation = Objects.requireNonNull(instrumentation, "instrumentation"); - this.agentId = Objects.requireNonNull(agentId, "agentId"); - this.agentName = Objects.requireNonNull(agentName, "agentName"); - this.applicationName = Objects.requireNonNull(applicationName, "applicationName"); - this.profilerConfig = Objects.requireNonNull(profilerConfig, "profilerConfig"); + this.properties = Objects.requireNonNull(properties, "properties"); + this.agentArgs = Objects.requireNonNull(agentArgs, "agentArgs"); + + this.agentPath = Objects.requireNonNull(agentPath, "agentPath"); this.pluginJars = Objects.requireNonNull(pluginJars, "pluginJars"); this.bootstrapJarPaths = Objects.requireNonNull(bootstrapJarPaths, "bootstrapJarPaths"); } @@ -56,46 +60,49 @@ public Instrumentation getInstrumentation() { } @Override - public String getAgentId() { - return agentId; + public List getPluginJars() { + return this.pluginJars; } @Override - public String getAgentName() { - return agentName; + public List getBootstrapJarPaths() { + return this.bootstrapJarPaths; } @Override - public String getApplicationName() { - return applicationName; + public Properties getProperties() { + return properties; } @Override - public List getPluginJars() { - return this.pluginJars; + public Map getAgentArgs() { + return agentArgs; } @Override - public List getBootstrapJarPaths() { - return this.bootstrapJarPaths; + public Path getAgentPath() { + return agentPath; } - @Override - public ProfilerConfig getProfilerConfig() { - return this.profilerConfig; + public Map toMap() { + Map map = new HashMap<>(); + map.put("instrumentation", instrumentation); + map.put("properties", properties); + map.put("agentArgs", agentArgs); + map.put("agentPath", agentPath); + map.put("pluginJars", pluginJars); + map.put("bootstrapJarPaths", bootstrapJarPaths); + return map; } @Override public String toString() { - final StringBuilder sb = new StringBuilder("DefaultAgentOption{"); - sb.append("instrumentation=").append(instrumentation); - sb.append(", agentId='").append(agentId).append('\''); - sb.append(", agentName='").append(agentName).append('\''); - sb.append(", applicationName='").append(applicationName).append('\''); - sb.append(", profilerConfig=").append(profilerConfig); - sb.append(", pluginJars=").append(pluginJars); - sb.append(", bootstrapJarPaths=").append(bootstrapJarPaths); - sb.append('}'); - return sb.toString(); + return "DefaultAgentOption{" + + "instrumentation=" + instrumentation + + ", properties=" + properties + + ", agentArgs=" + agentArgs + + ", pluginJars=" + pluginJars + + ", bootstrapJarPaths=" + bootstrapJarPaths + + '}'; } } diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointBootStrap.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointBootStrap.java index dd9e6ea53ddd..dda0c073ab85 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointBootStrap.java +++ b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointBootStrap.java @@ -16,7 +16,6 @@ package com.navercorp.pinpoint.bootstrap; -import com.navercorp.pinpoint.ProductInfo; import com.navercorp.pinpoint.bootstrap.agentdir.AgentDirBaseClassPathResolver; import com.navercorp.pinpoint.bootstrap.agentdir.AgentDirectory; import com.navercorp.pinpoint.bootstrap.agentdir.BootDir; @@ -74,7 +73,7 @@ private PinpointBootStrap(String agentArgs, Instrumentation instrumentation) { private void start() { - logger.info(ProductInfo.NAME + " agentArgs:" + agentArgs); + logger.info("pinpoint agentArgs:" + agentArgs); logger.info("PinpointBootStrap.ClassLoader:" + PinpointBootStrap.class.getClassLoader()); logger.info("ContextClassLoader:" + Thread.currentThread().getContextClassLoader()); diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointStarter.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointStarter.java index d92a7e46d369..3d783178e46a 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointStarter.java +++ b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointStarter.java @@ -14,31 +14,22 @@ */ package com.navercorp.pinpoint.bootstrap; -import com.navercorp.pinpoint.ProductInfo; import com.navercorp.pinpoint.bootstrap.agentdir.AgentDirectory; import com.navercorp.pinpoint.bootstrap.agentdir.FileUtils; -import com.navercorp.pinpoint.bootstrap.classloader.PinpointClassLoaderFactory; -import com.navercorp.pinpoint.bootstrap.classloader.ProfilerLibs; -import com.navercorp.pinpoint.bootstrap.config.AgentSystemConfig; -import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig; import com.navercorp.pinpoint.bootstrap.config.DisableOptions; -import com.navercorp.pinpoint.bootstrap.config.LogConfig; import com.navercorp.pinpoint.bootstrap.config.OsEnvSimpleProperty; -import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; -import com.navercorp.pinpoint.bootstrap.config.ProfilerConfigLoader; import com.navercorp.pinpoint.bootstrap.config.PropertyLoader; import com.navercorp.pinpoint.bootstrap.config.PropertyLoaderFactory; import java.lang.instrument.Instrumentation; +import java.lang.reflect.Method; import java.net.URL; import java.nio.file.Path; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Properties; -import java.util.Set; /** * @author Jongho Moon @@ -87,45 +78,18 @@ private ModuleBootLoader loadModuleBootLoader(Instrumentation instrumentation, C boolean start() { - final AgentIds agentIds = resolveAgentIds(); - if (agentIds == null) { - logger.warn("Failed to resolve AgentId and ApplicationId"); - return false; - } - - final String agentId = agentIds.getAgentId(); - if (agentId == null) { - logger.warn("agentId is null"); - return false; - } - final String applicationName = agentIds.getApplicationName(); - if (applicationName == null) { - logger.warn("applicationName is null"); - return false; - } try { final Properties properties = loadProperties(); - ProfilerConfig profilerConfig = ProfilerConfigLoader.load(properties); if (DisableOptions.isDisabled(properties::getProperty, DisableOptions.CONFIG)) { - this.logger.warn("value of disable property is not false, pinpoint.disable=" + profilerConfig.getPinpointDisable()); + this.logger.warn("value of disable property is not false, pinpoint.disable=true"); return false; } - // set the path of log file as a system property - AgentSystemConfig agentSystemConfig = new AgentSystemConfig(); - agentSystemConfig.saveAgentIdForLog(agentIds.getAgentId()); - agentSystemConfig.savePinpointVersion(Version.VERSION); - - LogConfig logConfig = new LogConfig(); - logConfig.saveLogFilePath(agentDirectory.getAgentLogFilePath()); - logConfig.cleanLogDir(agentDirectory.getAgentLogFilePath(), profilerConfig.getLogDirMaxBackupSize()); - // this is the library list that must be loaded URL[] urls = resolveLib(agentDirectory); - List agentClassloaderLibs = getAgentClassloaderLibs(profilerConfig); - final ClassLoader agentClassLoader = createClassLoader("pinpoint.agent", urls, parentClassLoader, agentClassloaderLibs); + final ClassLoader agentClassLoader = createClassLoader("pinpoint.agent", urls, parentClassLoader, properties); final ModuleBootLoader moduleBootLoader = loadModuleBootLoader(instrumentation, parentClassLoader); if (moduleBootLoader != null) { this.logger.info("defineAgentModule"); @@ -136,49 +100,35 @@ boolean start() { logger.info(String.format("pinpoint agent [%s] starting...", agentType)); final List pluginJars = agentDirectory.getPlugins(); - final String agentName = agentIds.getAgentName(); - AgentOption option = createAgentOption(agentId, agentName, applicationName, - profilerConfig, + AgentOption option = createAgentOption( instrumentation, + properties, + agentArgs, + agentDirectory.getAgentDirPath(), pluginJars, agentDirectory.getBootDir().getJarPath()); - Agent pinpointAgent = agentBootLoader.boot(option); - pinpointAgent.start(); - pinpointAgent.registerStopHandler(); - + Object agent = agentBootLoader.boot(option); + Class agentClazz = agent.getClass(); + agentClazz.getMethod("start").invoke(agent); + agentClazz.getMethod("registerStopHandler").invoke(agent); logger.info("pinpoint agent started normally."); } catch (Exception e) { // unexpected exception that did not be checked above - logger.warn(ProductInfo.NAME + " start failed.", e); + logger.warn("pinpoint start failed.", e); return false; } return true; } - private List getAgentClassloaderLibs(ProfilerConfig profilerConfig) { - Set libs = new HashSet<>(); - libs.addAll(ProfilerLibs.PINPOINT_PROFILER_CLASS); - libs.addAll(profilerConfig.getAgentClassloaderAdditionalLibs()); + // TestAgent only + public static final String IMPORT_PLUGIN = "profiler.plugin.import-plugin"; - List copy = new ArrayList<>(libs); - copy.sort(String.CASE_INSENSITIVE_ORDER); - return copy; - } - - private AgentIds resolveAgentIds() { - AgentIdResolverBuilder builder = new AgentIdResolverBuilder(); - builder.addProperties(AgentIdSourceType.SYSTEM, System.getProperties()::getProperty); - builder.addProperties(AgentIdSourceType.SYSTEM_ENV, System.getenv()::get); - builder.addProperties(AgentIdSourceType.AGENT_ARGUMENT, agentArgs::get); - AgentIdResolver agentIdResolver = builder.build(); - return agentIdResolver.resolve(); - } private Properties loadProperties() { final Path agentDirPath = agentDirectory.getAgentDirPath(); final Path profilesPath = agentDirectory.getProfilesPath(); - final String[] profileDirs = agentDirectory.getProfileDirs(); + final List profileDirs = agentDirectory.getProfileDirs(); final Properties javaSystemProperty = copyJavaSystemProperty(); final Properties osEnvProperty = copyOSEnvVariables(); @@ -188,11 +138,8 @@ private Properties loadProperties() { final PropertyLoader loader = factory.newPropertyLoader(); final Properties properties = loader.load(); - if (this.agentType == AgentType.PLUGIN_TEST) { - properties.put(DefaultProfilerConfig.PROFILER_INTERCEPTOR_EXCEPTION_PROPAGATE, "true"); - } final String importPluginIds = this.agentArgs.getOrDefault(AgentParameter.IMPORT_PLUGIN, ""); - properties.put(DefaultProfilerConfig.IMPORT_PLUGIN, importPluginIds); + properties.put(IMPORT_PLUGIN, importPluginIds); return properties; } @@ -205,27 +152,25 @@ private Properties copyOSEnvVariables() { return new OsEnvSimpleProperty().toProperties(System.getenv()); } - private ClassLoader createClassLoader(final String name, final URL[] urls, final ClassLoader parentClassLoader, List libClass) { - return PinpointClassLoaderFactory.createClassLoader(name, urls, parentClassLoader, libClass); + private ClassLoader createClassLoader(final String name, final URL[] urls, final ClassLoader parentClassLoader, Properties properties) { + try { + Class classLoaderFactory = Class.forName("com.navercorp.pinpoint.bootstrap.classloader.PinpointClassLoaderFactory"); + Method method = classLoaderFactory.getMethod("createClassLoader", String.class, URL[].class, ClassLoader.class, Properties.class); + return (ClassLoader) method.invoke(classLoaderFactory, name, urls, parentClassLoader, properties); + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } } - private AgentOption createAgentOption(String agentId, String agentName, String applicationName, - ProfilerConfig profilerConfig, - Instrumentation instrumentation, + private AgentOption createAgentOption(Instrumentation instrumentation, + Properties profilerConfig, + Map agentArgs, + + Path agentPath, List pluginJars, List bootstrapJarPaths) { - List pluginJarStrPath = toPathList(pluginJars); - List bootstrapJarPathStrPath = toPathList(bootstrapJarPaths); - return new DefaultAgentOption(instrumentation, agentId, agentName, applicationName, profilerConfig, pluginJarStrPath, bootstrapJarPathStrPath); - } - - private List toPathList(List paths) { - List list = new ArrayList<>(paths.size()); - for (Path path : paths) { - list.add(path.toString()); - } - return list; + return new DefaultAgentOption(instrumentation, profilerConfig, agentArgs, agentPath, pluginJars, bootstrapJarPaths); } private URL[] resolveLib(AgentDirectory classPathResolver) { diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/agentdir/AgentDirectory.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/agentdir/AgentDirectory.java index ec599d76d5af..19153422d54c 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/agentdir/AgentDirectory.java +++ b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/agentdir/AgentDirectory.java @@ -17,7 +17,7 @@ package com.navercorp.pinpoint.bootstrap.agentdir; -import com.navercorp.pinpoint.bootstrap.config.Profiles; +import com.navercorp.pinpoint.bootstrap.util.ProfileConstants; import java.io.IOException; import java.nio.file.DirectoryStream; @@ -33,11 +33,8 @@ */ public class AgentDirectory { - public static final String AGENT_ROOT_PATH_KEY = "pinpoint.agent.root.path"; - public static final Path LIB_DIR = Paths.get("lib"); public static final Path PLUGIN_DIR = Paths.get("plugin"); - public static final Path LOGS_DIR = Paths.get("logs"); public static final Path PROFILES_DIR = Paths.get("profiles"); private final Path agentJarName; @@ -93,16 +90,12 @@ public Path getAgentLibPath() { return appendAgentDirPath(LIB_DIR); } - public Path getAgentLogFilePath() { - return appendAgentDirPath(LOGS_DIR); - } - public Path getAgentPluginPath() { return appendAgentDirPath(PLUGIN_DIR); } public Path getAgentConfigPath() { - return appendAgentDirPath(Paths.get(Profiles.CONFIG_FILE_NAME)); + return appendAgentDirPath(Paths.get(ProfileConstants.CONFIG_FILE_NAME)); } public Path getProfilesPath() { @@ -113,8 +106,8 @@ private Path appendAgentDirPath(Path fileName) { return this.agentDirPath.resolve(fileName); } - public String[] getProfileDirs() { - List fileList = new ArrayList<>(); + public List getProfileDirs() { + List fileList = new ArrayList<>(); final Path profilesPath = getProfilesPath(); @@ -123,14 +116,14 @@ public String[] getProfileDirs() { if (Files.isDirectory(path)) { final Path fileName = path.getFileName(); if (fileName != null) { - fileList.add(fileName.toString()); + fileList.add(fileName); } } } } catch (IOException e) { throw new RuntimeException("profileDirs traverse error " + profilesPath, e); } - return fileList.toArray(new String[0]); + return fileList; } } diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/LogConfig.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/LogConfig.java deleted file mode 100644 index 81452d265051..000000000000 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/LogConfig.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.navercorp.pinpoint.bootstrap.config; - -import com.navercorp.pinpoint.ProductInfo; -import com.navercorp.pinpoint.bootstrap.BootLogger; -import com.navercorp.pinpoint.bootstrap.agentdir.LogDirCleaner; - -import java.nio.file.Path; -import java.util.Objects; -import java.util.Properties; - -public class LogConfig { - private final BootLogger logger = BootLogger.getLogger(getClass()); - - private final Properties systemProperty; - - public LogConfig() { - this(System.getProperties()); - } - - public LogConfig(Properties properties) { - this.systemProperty = Objects.requireNonNull(properties, "properties"); - } - - public void saveLogFilePath(Path agentLogFilePath) { - final String path = agentLogFilePath.toString(); - logger.info(String.format("logPath:%s", path)); - systemProperty.setProperty(ProductInfo.NAME + ".log", path); - } - - public void cleanLogDir(Path agentLogFilePath, final int logDirMaxBackupSize) { - logger.info("Log directory maxbackupsize=" + logDirMaxBackupSize); - LogDirCleaner logDirCleaner = new LogDirCleaner(agentLogFilePath, logDirMaxBackupSize); - logDirCleaner.clean(); - } -} diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilePropertyLoader.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilePropertyLoader.java index 03fbbb716ddc..352ca45c1796 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilePropertyLoader.java +++ b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilePropertyLoader.java @@ -17,14 +17,14 @@ package com.navercorp.pinpoint.bootstrap.config; import com.navercorp.pinpoint.bootstrap.BootLogger; -import com.navercorp.pinpoint.bootstrap.agentdir.AgentDirectory; -import com.navercorp.pinpoint.bootstrap.util.StringUtils; +import com.navercorp.pinpoint.bootstrap.util.ProfileConstants; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Properties; -import java.util.Set; /** * @author yjqg6666 @@ -32,6 +32,8 @@ */ class ProfilePropertyLoader implements PropertyLoader { + public static final String AGENT_ROOT_PATH_KEY = "pinpoint.agent.root.path"; + private final BootLogger logger = BootLogger.getLogger(getClass()); private final Properties javaSystemProperty; @@ -40,11 +42,9 @@ class ProfilePropertyLoader implements PropertyLoader { private final Path agentRootPath; private final Path profilesPath; - private final String[] supportedProfiles; - - public static final String[] ALLOWED_PROPERTY_PREFIX = new String[]{"bytecode.", "profiler.", "pinpoint."}; + private final List supportedProfiles; - public ProfilePropertyLoader(Properties javaSystemProperty, Properties osEnvProperty, Path agentRootPath, Path profilesPath, String[] supportedProfiles) { + public ProfilePropertyLoader(Properties javaSystemProperty, Properties osEnvProperty, Path agentRootPath, Path profilesPath, List supportedProfiles) { this.javaSystemProperty = Objects.requireNonNull(javaSystemProperty, "javaSystemProperty"); this.osEnvProperty = Objects.requireNonNull(osEnvProperty, "osEnvProperty"); @@ -72,28 +72,28 @@ public ProfilePropertyLoader(Properties javaSystemProperty, Properties osEnvProp */ @Override public Properties load() { - final Path defaultConfigPath = this.agentRootPath.resolve(Profiles.CONFIG_FILE_NAME); + final Path defaultConfigPath = this.agentRootPath.resolve(ProfileConstants.CONFIG_FILE_NAME); final Properties defaultProperties = new Properties(); // 1. load default Properties logger.info(String.format("load default config:%s", defaultConfigPath)); - PropertyLoaderUtils.loadFileProperties(defaultProperties, defaultConfigPath); + defaultProperties.putAll(PropertyLoaderUtils.loadFileProperties(defaultConfigPath)); // 2. load profile final String activeProfile = getActiveProfile(defaultProperties); logger.info(String.format("active profile:%s", activeProfile)); - final Path profilePath = Paths.get(profilesPath.toString(), activeProfile, Profiles.PROFILE_CONFIG_FILE_NAME); + final Path profilePath = Paths.get(profilesPath.toString(), activeProfile, ProfileConstants.PROFILE_CONFIG_FILE_NAME); logger.info(String.format("load profile:%s", profilePath)); - PropertyLoaderUtils.loadFileProperties(defaultProperties, profilePath); + defaultProperties.putAll(PropertyLoaderUtils.loadFileProperties(profilePath)); - defaultProperties.setProperty(Profiles.ACTIVE_PROFILE_KEY, activeProfile); + defaultProperties.setProperty(ProfileConstants.ACTIVE_PROFILE_KEY, activeProfile); // 3. load external config - final String externalConfig = this.javaSystemProperty.getProperty(Profiles.EXTERNAL_CONFIG_KEY); + final String externalConfig = this.javaSystemProperty.getProperty(ProfileConstants.EXTERNAL_CONFIG_KEY); if (externalConfig != null) { logger.info(String.format("load external config:%s", externalConfig)); - PropertyLoaderUtils.loadFileProperties(defaultProperties, Paths.get(externalConfig)); + defaultProperties.putAll(PropertyLoaderUtils.loadFileProperties(Paths.get(externalConfig))); } // 4 OS environment variables @@ -106,66 +106,39 @@ public Properties load() { saveAgentRootPath(agentRootPath, defaultProperties); // log path - saveLogConfigLocation(activeProfile, defaultProperties); return defaultProperties; } private void saveAgentRootPath(Path agentRootPath, Properties properties) { - properties.put(AgentDirectory.AGENT_ROOT_PATH_KEY, agentRootPath); + properties.put(AGENT_ROOT_PATH_KEY, agentRootPath); logger.info(String.format("agent root path:%s", agentRootPath)); } - private void saveLogConfigLocation(String activeProfile, Properties properties) { - String log4jLocation = properties.getProperty(Profiles.LOG_CONFIG_LOCATION_KEY); - if (StringUtils.isEmpty(log4jLocation)) { - LogConfigResolver logConfigResolver = new SimpleLogConfigResolver(agentRootPath); - log4jLocation = logConfigResolver.getLogPath().toString(); - - properties.put(Profiles.LOG_CONFIG_LOCATION_KEY, log4jLocation); - } - - logger.info(String.format("logConfig path:%s", log4jLocation)); - } private String getActiveProfile(Properties defaultProperties) { // env option support?? // String envProfile = System.getenv(ACTIVE_PROFILE_KEY); - String profile = javaSystemProperty.getProperty(Profiles.ACTIVE_PROFILE_KEY); + String profile = javaSystemProperty.getProperty(ProfileConstants.ACTIVE_PROFILE_KEY); if (profile == null) { - profile = defaultProperties.getProperty(Profiles.ACTIVE_PROFILE_KEY); + profile = defaultProperties.getProperty(ProfileConstants.ACTIVE_PROFILE_KEY); } if (profile == null) { throw new RuntimeException("Failed to detect pinpoint profile. Please add -D" + - Profiles.ACTIVE_PROFILE_KEY + - "= to VM option. Valid profiles are \"" + String.join(" | ", supportedProfiles) + "\""); + ProfileConstants.ACTIVE_PROFILE_KEY + + "= to VM option. Valid profiles are \"" + supportedProfiles + "\""); } // prevent directory traversal attack - for (String supportedProfile : supportedProfiles) { - if (supportedProfile.equalsIgnoreCase(profile)) { - return supportedProfile; + for (Path supportedProfile : supportedProfiles) { + if (supportedProfile.toString().equalsIgnoreCase(profile)) { + return supportedProfile.toString(); } } throw new IllegalStateException("unsupported profile:" + profile); } private void loadProperties(Properties dstProperties, Properties property) { - Set stringPropertyNames = property.stringPropertyNames(); - for (String propertyName : stringPropertyNames) { - if (isAllowPinpointProperty(propertyName)) { - String val = property.getProperty(propertyName); - dstProperties.setProperty(propertyName, val); - } - } - } - - boolean isAllowPinpointProperty(String propertyName) { - for (String prefix : ALLOWED_PROPERTY_PREFIX) { - if (propertyName.startsWith(prefix)) { - return true; - } - } - return false; + Map copy = PropertyLoaderUtils.filterAllowedPrefix(property); + dstProperties.putAll(copy); } - } diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/PropertyLoaderFactory.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/PropertyLoaderFactory.java index 5bfb667bd80b..51498450f9e2 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/PropertyLoaderFactory.java +++ b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/PropertyLoaderFactory.java @@ -17,7 +17,10 @@ package com.navercorp.pinpoint.bootstrap.config; +import com.navercorp.pinpoint.bootstrap.util.ProfileConstants; + import java.nio.file.Path; +import java.util.List; import java.util.Objects; import java.util.Properties; @@ -34,10 +37,11 @@ public class PropertyLoaderFactory { // @Optional private final Path profilesPath; // @Optional - private final String[] supportedProfiles; + private final List supportedProfiles; - public PropertyLoaderFactory(Properties javaSystemProperty, Properties osEnvProperty, - Path agentRootPath, Path profilesPath, String[] supportedProfiles) { + public PropertyLoaderFactory(Properties javaSystemProperty, + Properties osEnvProperty, + Path agentRootPath, Path profilesPath, List supportedProfiles) { this.javaSystemProperty = Objects.requireNonNull(javaSystemProperty, "javaSystemProperty"); this.osEnvProperty = Objects.requireNonNull(osEnvProperty, "osEnvProperty"); this.agentRootPath = Objects.requireNonNull(agentRootPath, "agentRootPath"); @@ -47,7 +51,7 @@ public PropertyLoaderFactory(Properties javaSystemProperty, Properties osEnvProp public PropertyLoader newPropertyLoader() { if (isSimpleMode()) { - return new SimplePropertyLoader(javaSystemProperty, agentRootPath, profilesPath); + return new SimplePropertyLoader(javaSystemProperty, agentRootPath); } return new ProfilePropertyLoader(javaSystemProperty, osEnvProperty, agentRootPath, profilesPath, supportedProfiles); } @@ -55,7 +59,7 @@ public PropertyLoader newPropertyLoader() { private boolean isSimpleMode() { - final String mode = javaSystemProperty.getProperty(Profiles.CONFIG_LOAD_MODE_KEY, Profiles.CONFIG_LOAD_MODE.PROFILE.toString()); - return Profiles.CONFIG_LOAD_MODE.SIMPLE.toString().equalsIgnoreCase(mode); + final String mode = javaSystemProperty.getProperty(ProfileConstants.CONFIG_LOAD_MODE_KEY, ProfileConstants.CONFIG_LOAD_MODE.PROFILE.toString()); + return ProfileConstants.CONFIG_LOAD_MODE.SIMPLE.toString().equalsIgnoreCase(mode); } } diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/PropertyLoaderUtils.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/PropertyLoaderUtils.java index 8ee445547ab7..b7aaf2f86355 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/PropertyLoaderUtils.java +++ b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/PropertyLoaderUtils.java @@ -5,14 +5,46 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; import java.util.Properties; public class PropertyLoaderUtils { - static void loadFileProperties(Properties properties, Path filePath) { + + public static final String[] ALLOWED_PROPERTY_PREFIX = new String[]{"bytecode.", "profiler.", "pinpoint."}; + + public static Properties loadFileProperties(Path filePath) { + Properties properties = new Properties(); try (BufferedReader reader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8)) { properties.load(reader); } catch (IOException e) { throw new IllegalStateException(String.format("%s load fail Caused by:%s", filePath, e.getMessage()), e); } + return properties; + } + + + public static Map filterAllowedPrefix(Map properties) { + final Map copy = new HashMap<>(); + for (Map.Entry entry : properties.entrySet()) { + final K key = entry.getKey(); + final V value = entry.getValue(); + if (key instanceof String && value instanceof String) { + final String name = (String) key; + if (filter(name)) { + copy.put(key, value); + } + } + } + return copy; + } + + private static boolean filter(String name) { + for (String prefix : ALLOWED_PROPERTY_PREFIX) { + if (name.startsWith(prefix)) { + return true; + } + } + return false; } } diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/SimpleLogConfigResolver.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/SimpleLogConfigResolver.java deleted file mode 100644 index 951d02a30e44..000000000000 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/SimpleLogConfigResolver.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2019 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.bootstrap.config; - -import java.nio.file.Path; -import java.util.Objects; - -/** - * @author Woonduk Kang(emeroad) - */ -public class SimpleLogConfigResolver implements LogConfigResolver{ - - private final Path agentDirPath; - - public SimpleLogConfigResolver(Path agentDirPath) { - this.agentDirPath = Objects.requireNonNull(agentDirPath, "agentDirPath"); - } - - @Override - public Path getLogPath() { - return this.agentDirPath; - } -} diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/SimplePropertyLoader.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/SimplePropertyLoader.java index 7800a95cf08f..923ccb2d802d 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/SimplePropertyLoader.java +++ b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/SimplePropertyLoader.java @@ -17,12 +17,13 @@ package com.navercorp.pinpoint.bootstrap.config; import com.navercorp.pinpoint.bootstrap.BootLogger; +import com.navercorp.pinpoint.bootstrap.util.ProfileConstants; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Map; import java.util.Objects; import java.util.Properties; -import java.util.Set; /** * @author Woonduk Kang(emeroad) @@ -35,57 +36,37 @@ class SimplePropertyLoader implements PropertyLoader { private final Properties systemProperty; private final Path agentRootPath; - private final Path profilesPath; - public SimplePropertyLoader(Properties systemProperty, Path agentRootPath, Path profilesPath) { + public SimplePropertyLoader(Properties systemProperty, Path agentRootPath) { this.systemProperty = Objects.requireNonNull(systemProperty, "systemProperty"); this.agentRootPath = Objects.requireNonNull(agentRootPath, "agentRootPath"); - this.profilesPath = profilesPath; } @Override public Properties load() { - final Path defaultConfigPath = this.agentRootPath.resolve(Profiles.CONFIG_FILE_NAME); + final Path defaultConfigPath = this.agentRootPath.resolve(ProfileConstants.CONFIG_FILE_NAME); final Properties defaultProperties = new Properties(); - final String externalConfig = this.systemProperty.getProperty(Profiles.EXTERNAL_CONFIG_KEY); + final String externalConfig = this.systemProperty.getProperty(ProfileConstants.EXTERNAL_CONFIG_KEY); if (externalConfig != null) { logger.info(String.format("load external config:%s", externalConfig)); - PropertyLoaderUtils.loadFileProperties(defaultProperties, Paths.get(externalConfig)); + defaultProperties.putAll(PropertyLoaderUtils.loadFileProperties(Paths.get(externalConfig))); } else { logger.info(String.format("load default config:%s", defaultConfigPath)); - PropertyLoaderUtils.loadFileProperties(defaultProperties, defaultConfigPath); + defaultProperties.putAll(PropertyLoaderUtils.loadFileProperties(defaultConfigPath)); } - loadSystemProperties(defaultProperties); - saveLogConfigLocation(defaultProperties); + // systemProperty + loadProperties(defaultProperties, this.systemProperty); return defaultProperties; } - private void saveLogConfigLocation(Properties properties) { - String activeProfile = systemProperty.getProperty(Profiles.ACTIVE_PROFILE_KEY); - if (activeProfile == null) { - throw new RuntimeException("Failed to read " + Profiles.ACTIVE_PROFILE_KEY + " from systemProperty"); - } - - LogConfigResolver logConfigResolver = new SimpleLogConfigResolver(agentRootPath); - final Path log4jLocation = logConfigResolver.getLogPath(); - properties.put(Profiles.LOG_CONFIG_LOCATION_KEY, log4jLocation.toString()); - logger.info(String.format("logConfig path:%s", log4jLocation)); - } - - private void loadSystemProperties(Properties dstProperties) { - Set stringPropertyNames = this.systemProperty.stringPropertyNames(); - for (String propertyName : stringPropertyNames) { - boolean isPinpointProperty = propertyName.startsWith("bytecode.") || propertyName.startsWith("profiler.") || propertyName.startsWith("pinpoint."); - if (isPinpointProperty) { - String val = this.systemProperty.getProperty(propertyName); - dstProperties.setProperty(propertyName, val); - } - } + private void loadProperties(Properties dstProperties, Properties property) { + Map copy = PropertyLoaderUtils.filterAllowedPrefix(property); + dstProperties.putAll(copy); } } diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/util/ProfileConstants.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/util/ProfileConstants.java new file mode 100644 index 000000000000..e4618c4ca84d --- /dev/null +++ b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/util/ProfileConstants.java @@ -0,0 +1,21 @@ +package com.navercorp.pinpoint.bootstrap.util; + +public class ProfileConstants { + + public static final String CONFIG_LOAD_MODE_KEY = "pinpoint.config.load.mode"; + + public enum CONFIG_LOAD_MODE { + PROFILE, + // for IT TEST + SIMPLE + } + + public static final String ACTIVE_PROFILE_KEY = "pinpoint.profiler.profiles.active"; + + // 1. default config + public static final String CONFIG_FILE_NAME = "pinpoint-root.config"; + // 2. profile config + public static final String PROFILE_CONFIG_FILE_NAME = "pinpoint.config"; + // 3. external config + public static final String EXTERNAL_CONFIG_KEY = "pinpoint.config"; +} diff --git a/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/AgentBootLoaderTest.java b/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/AgentBootLoaderTest.java index 40edcca54085..949dbbe11b6d 100644 --- a/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/AgentBootLoaderTest.java +++ b/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/AgentBootLoaderTest.java @@ -17,15 +17,15 @@ package com.navercorp.pinpoint.bootstrap; -import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig; -import com.navercorp.pinpoint.common.util.CodeSourceUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.junit.jupiter.api.Test; import java.lang.instrument.Instrumentation; import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Collections; +import java.util.Objects; +import java.util.Properties; import static org.mockito.Mockito.mock; @@ -34,8 +34,6 @@ */ public class AgentBootLoaderTest { - private final Logger logger = LogManager.getLogger(this.getClass()); - @Test public void boot() { boot("testAgentName"); @@ -43,32 +41,29 @@ public void boot() { @Test public void bootNoAgentName() { - boot(""); + boot("testNoAgentName"); } private void boot(String agentName) { ClassLoader classLoader = AgentBootLoaderTest.class.getClassLoader(); + URL testClassPath = classLoader.getResource(""); + Objects.requireNonNull(testClassPath, "testClassPath"); + Path agentPath = Paths.get(testClassPath.getPath()); + AgentBootLoader agentBootLoader = new AgentBootLoader("com.navercorp.pinpoint.bootstrap.DummyAgent", classLoader); Instrumentation instrumentation = mock(Instrumentation.class); - AgentOption option = new DefaultAgentOption(instrumentation, "testCaseAgent", agentName, "testCaseAppName", - new DefaultProfilerConfig(), Collections.emptyList(), Collections.emptyList()); - Agent boot = agentBootLoader.boot(option); - boot.start(); - boot.stop(); - } - - private String getProjectLibDir() { - // not really necessary, but useful for testing protectionDomain - URL location = CodeSourceUtils.getCodeLocation(AgentBootLoader.class); - - logger.debug("lib location:{}", location); - String path = location.getPath(); - // file:/D:/nhn_source/pinpoint_project/pinpoint-tomcat-profiler/target/classes/ - int dirPath = path.lastIndexOf("target/classes/"); - if (dirPath == -1) { - throw new RuntimeException("target/classes/ not found"); + AgentOption option = new DefaultAgentOption(instrumentation, + new Properties(), Collections.emptyMap(), + agentPath, + Collections.emptyList(), Collections.emptyList()); + Object boot = agentBootLoader.boot(option); + try { + Class agentClazz = boot.getClass(); + agentClazz.getMethod("start").invoke(boot); + agentClazz.getMethod("close").invoke(boot); + } catch (Exception e) { + throw new RuntimeException("agent boot failed", e); } - String projectDir = path.substring(1, dirPath); - return projectDir + "src/test/lib"; } + } diff --git a/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/AgentIdResolverTest.java b/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/AgentIdResolverTest.java deleted file mode 100644 index a464b989c78b..000000000000 --- a/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/AgentIdResolverTest.java +++ /dev/null @@ -1,107 +0,0 @@ -package com.navercorp.pinpoint.bootstrap; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public class AgentIdResolverTest { - - @Test - public void resolve() { - AgentIdSourceType argument = AgentIdSourceType.AGENT_ARGUMENT; - Map properties = new HashMap<>(); - properties.put(argument.getAgentId(), "agentId"); - properties.put(argument.getAgentName(), "agentName"); - properties.put(argument.getApplicationName(), "appName"); - AgentProperties ap = new AgentProperties(AgentIdSourceType.AGENT_ARGUMENT, properties::get); - - AgentIdResolver resolver = new AgentIdResolver(Collections.singletonList(ap)); - AgentIds resolve = resolver.resolve(); - - Assertions.assertEquals("agentId", resolve.getAgentId()); - Assertions.assertEquals("agentName", resolve.getAgentName()); - Assertions.assertEquals("appName", resolve.getApplicationName()); - } - - @Test - public void resolve_optional_agent_name() { - AgentIdSourceType argument = AgentIdSourceType.AGENT_ARGUMENT; - - Map properties = new HashMap<>(); - properties.put(argument.getAgentId(), "agentId"); - properties.put(argument.getApplicationName(), "appName"); - AgentProperties ap = new AgentProperties(AgentIdSourceType.AGENT_ARGUMENT, properties::get); - - AgentIdResolver resolver = new AgentIdResolver(Collections.singletonList(ap)); - AgentIds resolve = resolver.resolve(); - - Assertions.assertEquals("agentId", resolve.getAgentId()); - Assertions.assertEquals("appName", resolve.getApplicationName()); - Assertions.assertEquals("", resolve.getAgentName()); - } - - @Test - public void resolve_fail() { - AgentIdSourceType argument = AgentIdSourceType.AGENT_ARGUMENT; - - Map properties = new HashMap<>(); - properties.put(argument.getAgentId(), "agentId"); - - AgentProperties ap = new AgentProperties(AgentIdSourceType.AGENT_ARGUMENT, properties::get); - - AgentIdResolver resolver = new AgentIdResolver(Collections.singletonList(ap)); - AgentIds resolve = resolver.resolve(); - - Assertions.assertNull(resolve); - } - - @Test - public void resolve_multi_source() { - AgentIdSourceType argument = AgentIdSourceType.AGENT_ARGUMENT; - Map properties1 = new HashMap<>(); - properties1.put(argument.getAgentId(), "agentId1"); - properties1.put(argument.getAgentName(), "agentName1"); - AgentProperties ap = new AgentProperties(AgentIdSourceType.AGENT_ARGUMENT, properties1::get); - - AgentIdSourceType system = AgentIdSourceType.SYSTEM; - Map properties2 = new HashMap<>(); - properties2.put(system.getApplicationName(), "appName2"); - AgentProperties ap2 = new AgentProperties(AgentIdSourceType.SYSTEM, properties2::get); - - AgentIdResolver resolver = new AgentIdResolver(Arrays.asList(ap2, ap)); - AgentIds resolve = resolver.resolve(); - - Assertions.assertEquals("agentId1", resolve.getAgentId()); - Assertions.assertEquals("appName2", resolve.getApplicationName()); - Assertions.assertEquals("agentName1", resolve.getAgentName()); - } - - @Test - public void resolve_multi_source_2() { - AgentIdSourceType argument = AgentIdSourceType.AGENT_ARGUMENT; - - Map properties1 = new HashMap<>(); - properties1.put(argument.getAgentId(), "agentId1"); - properties1.put(argument.getAgentName(), "agentName1"); - AgentProperties ap = new AgentProperties(AgentIdSourceType.AGENT_ARGUMENT, properties1::get); - - AgentIdSourceType system = AgentIdSourceType.SYSTEM; - Map properties2 = new HashMap<>(); - properties2.put(system.getAgentId(), "agentId2"); - properties2.put(system.getAgentName(), "agentName2"); - properties2.put(system.getApplicationName(), "appName2"); - - AgentProperties ap2 = new AgentProperties(AgentIdSourceType.SYSTEM, properties2::get); - - AgentIdResolver resolver = new AgentIdResolver(Arrays.asList(ap2, ap)); - AgentIds resolve = resolver.resolve(); - - Assertions.assertEquals("agentId2", resolve.getAgentId()); - Assertions.assertEquals("appName2", resolve.getApplicationName()); - Assertions.assertEquals("agentName2", resolve.getAgentName()); - } -} \ No newline at end of file diff --git a/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/AgentIdSourceTypeTest.java b/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/AgentIdSourceTypeTest.java deleted file mode 100644 index 8ea3f3d8206a..000000000000 --- a/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/AgentIdSourceTypeTest.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.navercorp.pinpoint.bootstrap; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -class AgentIdSourceTypeTest { - - @Test - void agentId() { - Assertions.assertNotNull(AgentIdSourceType.AGENT_ARGUMENT.getAgentId()); - } -} \ No newline at end of file diff --git a/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/DummyAgent.java b/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/DummyAgent.java index 7da245a966c6..4a4a8dfad0af 100644 --- a/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/DummyAgent.java +++ b/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/DummyAgent.java @@ -16,14 +16,17 @@ package com.navercorp.pinpoint.bootstrap; +import com.navercorp.pinpoint.profiler.Agent; + +import java.util.Map; + /** * @author emeroad * @author hyungil.jeong */ public class DummyAgent implements Agent { - public DummyAgent(AgentOption option) { - + public DummyAgent(Map map) { } @Override @@ -31,8 +34,7 @@ public void start() { } @Override - public void stop() { - + public void close() { } @Override diff --git a/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/agentdir/LogDirCleanerTest.java b/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/agentdir/LogDirCleanerTest.java deleted file mode 100644 index 65044f83670e..000000000000 --- a/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/agentdir/LogDirCleanerTest.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.navercorp.pinpoint.bootstrap.agentdir; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.attribute.FileTime; -import java.util.List; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static org.assertj.core.api.Assertions.assertThat; - - -public class LogDirCleanerTest { - @TempDir - public Path temp; - - private long time = System.currentTimeMillis(); - - private FileTime nextTime() { - return FileTime.from(time += 10000, TimeUnit.MILLISECONDS); - } - -// private static File getRootDir(Class clazz) { -// String classPath = clazz.getName().replace('.', '/') + ".class"; -// URL resource = clazz.getClassLoader().getResource(classPath); -// int index = resource.getPath().indexOf(classPath); -// if (index == -1) { -// throw new RuntimeException("RootDir error " + resource); -// } -// String rootPath = resource.getPath().substring(0, index); -// return new File(rootPath); -// } - - - @BeforeEach - public void setUp() throws Exception { - Path agentDir1 = newFolder("agentDir1"); - - Path temp = agentDir1.resolve("tempFile1.txt"); - Files.createFile(temp); - - Path agentDir2 = newFolder("agentDir2"); - Path agentDir3 = newFolder("agentDir3"); - } - - private Path newFolder(String agentDir1) throws IOException { - Path file = temp.resolve(agentDir1); - Files.createDirectory(file); - Files.setLastModifiedTime(file, nextTime()); - return file; - } - - @Test - public void clean0() { - LogDirCleaner logDirCleaner = new LogDirCleaner(temp, 0); - logDirCleaner.clean(); - - String[] files = temp.toFile().list(); - assertThat(files).isEmpty(); - } - - @Test - public void clean2() throws IOException { - LogDirCleaner logDirCleaner = new LogDirCleaner(temp, 2); - logDirCleaner.clean(); - - List files = fileList(temp); - assertThat(files).hasSize(2) - .contains(Paths.get("agentDir2")) - .contains(Paths.get("agentDir3")); - } - - @Test - public void clean5() throws IOException { - LogDirCleaner logDirCleaner = new LogDirCleaner(temp, 5); - logDirCleaner.clean(); - - List files = fileList(temp); - assertThat(files).hasSize(3); - } - - public List fileList(Path path) throws IOException { - try (Stream stream = Files.list(path)) { - return stream - .map(Path::getFileName) - .collect(Collectors.toList()); - } - } -} \ No newline at end of file diff --git a/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/config/PropertyUtilsTest.java b/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/config/PropertyUtilsTest.java new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/profiler/Agent.java b/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/profiler/Agent.java new file mode 100644 index 000000000000..09cf1adb379b --- /dev/null +++ b/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/profiler/Agent.java @@ -0,0 +1,17 @@ +package com.navercorp.pinpoint.profiler; + +import java.io.Closeable; + +/** + * For Test + */ +public interface Agent extends Closeable { + + void start(); + + @Override + void close(); + + void registerStopHandler(); +} + diff --git a/agent-module/bootstraps/pom.xml b/agent-module/bootstraps/pom.xml index 132c44e3b84b..04746d0c2088 100644 --- a/agent-module/bootstraps/pom.xml +++ b/agent-module/bootstraps/pom.xml @@ -38,6 +38,12 @@ + + com.navercorp.pinpoint + pinpoint-bootstrap + ${project.version} + compile + com.navercorp.pinpoint pinpoint-bootstrap-core diff --git a/agent-module/plugins-test-module/plugins-test/pom.xml b/agent-module/plugins-test-module/plugins-test/pom.xml index 4905d3929ec3..7150b19279b8 100644 --- a/agent-module/plugins-test-module/plugins-test/pom.xml +++ b/agent-module/plugins-test-module/plugins-test/pom.xml @@ -18,10 +18,6 @@ - - com.navercorp.pinpoint - pinpoint-bootstrap - com.navercorp.pinpoint pinpoint-profiler-test diff --git a/agent-module/profiler-logging/src/main/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystem.java b/agent-module/profiler-logging/src/main/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystem.java index b2c69f5290e9..cb89fc6dac30 100644 --- a/agent-module/profiler-logging/src/main/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystem.java +++ b/agent-module/profiler-logging/src/main/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystem.java @@ -30,9 +30,23 @@ public class Log4j2LoggingSystem implements LoggingSystem { private PluginLoggerBinder binder; - public Log4j2LoggingSystem(Path agentPath) { - Objects.requireNonNull(agentPath, "agentPath"); - this.configLocation = getConfigPath(agentPath); + public static Log4j2LoggingSystem searchPath(Path agentPath) { + Path configPath = getConfiguration(agentPath); + return new Log4j2LoggingSystem(configPath); + } + + private static Path getConfiguration(Path profilePath) { + for (String configFile : LOOKUP) { + Path configLocation = profilePath.resolve(configFile); + if (Files.exists(configLocation)) { + return configLocation; + } + } + throw new IllegalStateException("'log4j2-agent.xml' not found. agentPath:" + profilePath); + } + + Log4j2LoggingSystem(Path configLocation) { + this.configLocation = Objects.requireNonNull(configLocation, "configLocation"); } @Override @@ -49,8 +63,7 @@ public void start() { Logger logger = getLoggerContextLogger(); logger.info("{} start logPath:{}", this.getClass().getSimpleName(), configLocation); - logger.info("LoggerContextFactory:{}", LogManager.getFactory().getClass().getName()); - logger.info("LoggerContext:{}", loggerContext.getClass().getName()); + logger.info("LoggerContextFactory:{} LoggerContext:{}", LogManager.getFactory().getClass().getName(), loggerContext.getClass().getName()); this.binder = new Log4j2Binder(loggerContext); bindPluginLogFactory(this.binder); @@ -87,22 +100,12 @@ private Logger getLoggerContextLogger() { } - private Path getConfigPath(Path profilePath) { - for (String configFile : LOOKUP) { - Path configLocation = profilePath.resolve(configFile); - if (Files.exists(configLocation)) { - return configLocation; - } - } - throw new IllegalStateException("'log4j2-agent.xml' not found. agentPath:" + profilePath); - } - private LoggerContext getLoggerContext() { Log4jEnvExecutor executor = new Log4jEnvExecutor(); - return executor.call(this::newLoggerContext); + return executor.call(this::initLoggerContext); } - public LoggerContext newLoggerContext() { + public LoggerContext initLoggerContext() { ClassLoader classLoader = this.getClass().getClassLoader(); URI uri = configLocation.toUri(); return LogManager.getContext(classLoader, false, null, uri, CONTEXT_NAME); diff --git a/agent-module/profiler-logging/src/test/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystemTest.java b/agent-module/profiler-logging/src/test/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystemTest.java index 69da3de28a70..93c1774751db 100644 --- a/agent-module/profiler-logging/src/test/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystemTest.java +++ b/agent-module/profiler-logging/src/test/java/com/navercorp/pinpoint/profiler/logging/Log4j2LoggingSystemTest.java @@ -18,17 +18,16 @@ public void start() throws URISyntaxException { URL resource = this.getClass().getClassLoader().getResource(""); Objects.requireNonNull(resource, "resource"); Path profilePath = Paths.get(resource.toURI()); - String log4jFile = profilePath.resolve("log4j2-test.xml").toString(); + final Path log4j2Xml = profilePath.resolve("log4j2-test.xml"); - - try (Log4j2LoggingSystem loggingSystem = new Log4j2LoggingSystem(profilePath)) { + try (Log4j2LoggingSystem loggingSystem = Log4j2LoggingSystem.searchPath(profilePath)) { loggingSystem.start(); Logger test = LogManager.getLogger("test"); test.debug("test"); String configLocation = loggingSystem.getConfigLocation(); - Assertions.assertEquals(log4jFile, configLocation); + Assertions.assertEquals(log4j2Xml, Paths.get(configLocation)); } } -} \ No newline at end of file +} diff --git a/agent-module/profiler-optional/profiler-optional-parent/pom.xml b/agent-module/profiler-optional/profiler-optional-parent/pom.xml index dd1f9262dc57..823c565fb7e0 100644 --- a/agent-module/profiler-optional/profiler-optional-parent/pom.xml +++ b/agent-module/profiler-optional/profiler-optional-parent/pom.xml @@ -21,20 +21,6 @@ com.navercorp.pinpoint pinpoint-profiler provided - - - com.navercorp.pinpoint - pinpoint-bootstrap - - - com.navercorp.pinpoint - pinpoint-rpc - - - com.navercorp.pinpoint - pinpoint-thrift - - diff --git a/agent-module/profiler-test/pom.xml b/agent-module/profiler-test/pom.xml index c41eeeffb053..7a438692eb3b 100644 --- a/agent-module/profiler-test/pom.xml +++ b/agent-module/profiler-test/pom.xml @@ -47,10 +47,6 @@ com.navercorp.pinpoint pinpoint-bootstrap-core - - com.navercorp.pinpoint - pinpoint-bootstrap - org.apache.commons diff --git a/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/MockApplicationContextFactory.java b/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/MockApplicationContextFactory.java index 89296018abf6..f5bc77dee39f 100644 --- a/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/MockApplicationContextFactory.java +++ b/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/MockApplicationContextFactory.java @@ -17,20 +17,23 @@ package com.navercorp.pinpoint.profiler.test; import com.google.inject.Module; -import com.navercorp.pinpoint.bootstrap.AgentOption; -import com.navercorp.pinpoint.bootstrap.DefaultAgentOption; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfigLoader; +import com.navercorp.pinpoint.profiler.AgentContextOption; +import com.navercorp.pinpoint.profiler.AgentContextOptionBuilder; +import com.navercorp.pinpoint.profiler.AgentOption; import com.navercorp.pinpoint.profiler.context.module.DefaultApplicationContext; import com.navercorp.pinpoint.profiler.context.module.ModuleFactory; import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; import java.lang.instrument.Instrumentation; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Collections; +import java.util.Properties; /** * @author Woonduk Kang(emeroad) @@ -41,37 +44,42 @@ public MockApplicationContextFactory() { } public DefaultApplicationContext build(String configPath) { - ProfilerConfig profilerConfig = loadProfilerConfig(configPath); + Properties profilerConfig = loadProfilerConfig(configPath); return build(profilerConfig); } - private ProfilerConfig loadProfilerConfig(String configPath) { + private Properties loadProfilerConfig(String configPath) { final InputStream stream = openStream(configPath); - return ProfilerConfigLoader.load(stream); + return ProfilerConfigLoader.loadProperties(stream); } private InputStream openStream(String configPath) { - File file = new File(configPath); + Path file = Paths.get(configPath); try { - return new FileInputStream(file); - } catch (FileNotFoundException e) { - throw new RuntimeException("pinpoint.config not found. configPath:" + configPath); + return Files.newInputStream(file); + } catch (IOException e) { + throw new RuntimeException("pinpoint.config IOError. configPath:" + configPath); } } - public DefaultApplicationContext build(ProfilerConfig config) { + public DefaultApplicationContext build(Properties config) { return build(config, newModuleFactory()); } - public DefaultApplicationContext build(ProfilerConfig config, ModuleFactory moduleFactory) { + public DefaultApplicationContext build(Properties config, ModuleFactory moduleFactory) { Instrumentation instrumentation = new DummyInstrumentation(); String mockAgentId = "mockAgentId"; String mockAgentName = "mockAgentName"; String mockApplicationName = "mockApplicationName"; - AgentOption agentOption = new DefaultAgentOption(instrumentation, mockAgentId, mockAgentName, mockApplicationName, - config, Collections.emptyList(), Collections.emptyList()); - return new DefaultApplicationContext(agentOption, moduleFactory); + AgentOption agentOption = new com.navercorp.pinpoint.profiler.AgentOption(instrumentation, + config, Collections.emptyMap(), null, + Collections.emptyList(), Collections.emptyList(), false); + ProfilerConfig profilerConfig = ProfilerConfigLoader.load(config); + AgentContextOption agentContextOption = AgentContextOptionBuilder.build(agentOption, + mockAgentId, mockAgentName, mockApplicationName, + profilerConfig); + return new DefaultApplicationContext(agentContextOption, moduleFactory); } @@ -80,8 +88,7 @@ private ModuleFactory newModuleFactory() { InterceptorRegistryBinder binder = new TestInterceptorRegistryBinder(); Module interceptorRegistryModule = InterceptorRegistryModule.wrap(binder); - ModuleFactory moduleFactory = new OverrideModuleFactory(pluginModule, interceptorRegistryModule); - return moduleFactory; + return new OverrideModuleFactory(pluginModule, interceptorRegistryModule); } } diff --git a/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/MockTraceContextFactory.java b/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/MockTraceContextFactory.java index 2dd5c740c29e..852b529c7e20 100644 --- a/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/MockTraceContextFactory.java +++ b/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/MockTraceContextFactory.java @@ -47,7 +47,7 @@ public static DefaultApplicationContext newMockApplicationContext(ProfilerConfig ModuleFactory moduleFactory = new OverrideModuleFactory(loggingModule, interceptorRegistryModule); MockApplicationContextFactory factory = new MockApplicationContextFactory(); - return factory.build(profilerConfig, moduleFactory); + return factory.build(profilerConfig.getProperties(), moduleFactory); } public static class LoggingModule extends AbstractModule { diff --git a/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/OverrideModuleFactory.java b/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/OverrideModuleFactory.java index 9715d14d1ae2..b960f24e3ee4 100644 --- a/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/OverrideModuleFactory.java +++ b/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/OverrideModuleFactory.java @@ -18,7 +18,7 @@ import com.google.inject.Module; import com.google.inject.util.Modules; -import com.navercorp.pinpoint.bootstrap.AgentOption; +import com.navercorp.pinpoint.profiler.AgentContextOption; import com.navercorp.pinpoint.profiler.context.module.ApplicationContextModuleFactory; import com.navercorp.pinpoint.profiler.context.module.ModuleFactory; import com.navercorp.pinpoint.profiler.test.rpc.MockRpcModule; @@ -40,11 +40,11 @@ public OverrideModuleFactory(Module... overrideModule) { } @Override - public Module newModule(AgentOption agentOption) { + public Module newModule(AgentContextOption agentOption) { ModuleFactory moduleFactory = new ApplicationContextModuleFactory() { @Override - protected Module newRpcModule(AgentOption agentOption) { + protected Module newRpcModule(AgentContextOption agentOption) { logger.info("load {}", MockRpcModule.class.getName()); return new MockRpcModule(); } diff --git a/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/PluginTestAgent.java b/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/PluginTestAgent.java index c1dbc3db3b31..f7c2dfb8ee24 100644 --- a/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/PluginTestAgent.java +++ b/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/PluginTestAgent.java @@ -16,12 +16,24 @@ package com.navercorp.pinpoint.profiler.test; -import com.navercorp.pinpoint.bootstrap.AgentOption; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder; +import com.navercorp.pinpoint.profiler.AgentContextOption; +import com.navercorp.pinpoint.profiler.AgentContextOptionBuilder; +import com.navercorp.pinpoint.profiler.AgentOption; import com.navercorp.pinpoint.profiler.DefaultAgent; import com.navercorp.pinpoint.profiler.context.module.ApplicationContext; import com.navercorp.pinpoint.profiler.context.module.DefaultApplicationContext; import com.navercorp.pinpoint.profiler.context.module.ModuleFactory; +import com.navercorp.pinpoint.profiler.instrument.config.DefaultInstrumentConfig; + +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; +import java.util.Objects; +import java.util.Properties; /** * @author emeroad @@ -31,12 +43,13 @@ */ public class PluginTestAgent extends DefaultAgent { - public PluginTestAgent(AgentOption agentOption) { + public PluginTestAgent(Map agentOption) { super(agentOption); } @Override - protected ApplicationContext newApplicationContext(AgentOption agentOption) { + protected ApplicationContext newApplicationContext(AgentContextOption agentOption) { + PluginApplicationContextModule pluginApplicationContextModule = new PluginApplicationContextModule(); ModuleFactory moduleFactory = new OverrideModuleFactory(pluginApplicationContextModule); @@ -48,6 +61,32 @@ protected ApplicationContext newApplicationContext(AgentOption agentOption) { } + protected Path getLogConfigPath(ProfilerConfig config, Path agentPath) { + if (agentPath == null) { + URL classPathRoot = getClass().getResource("/"); + Objects.requireNonNull(classPathRoot, "classPathRoot"); + try { + return Paths.get(classPathRoot.toURI()); + } catch (URISyntaxException e) { + throw new RuntimeException("Failed to get agentPath", e); + } + } + return agentPath; + } + + @Override + protected AgentContextOption buildContextOption(AgentOption agentOption, ProfilerConfig profilerConfig) { + Properties properties = profilerConfig.getProperties(); + properties.put(DefaultInstrumentConfig.PROFILER_INTERCEPTOR_EXCEPTION_PROPAGATE, "true"); + + String agentId = "mockAgentId"; + String agentName = "mockAgentName"; + String applicationName = "mockApplicationName"; + return AgentContextOptionBuilder.build(agentOption, + agentId, agentName, applicationName, + profilerConfig); + } + private void exportVerifier(DefaultApplicationContext applicationContext) { PluginVerifierExternalAdaptor adaptor = new PluginVerifierExternalAdaptor(applicationContext); PluginTestVerifierHolder.setInstance(adaptor); diff --git a/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/TestAgentInformation.java b/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/TestAgentInformation.java index 978b503c45b4..e21432f86d5d 100644 --- a/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/TestAgentInformation.java +++ b/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/TestAgentInformation.java @@ -37,8 +37,11 @@ public class TestAgentInformation extends DefaultAgentInformation { private static final ServiceType SERVICE_TYPE = ServiceType.TEST_STAND_ALONE; private static final String JVM_VERSION = JvmUtils.getSystemProperty(SystemPropertyKey.JAVA_VERSION); private static final String AGENT_VERSION = Version.VERSION; + private static final String CLUSTER_NAMESPACE = "cluster-namespace"; public TestAgentInformation() { - super(AGENT_ID, AGENT_NAME, APPLICATION_NAME, IS_CONTAINER, System.currentTimeMillis(), PID, MACHINE_NAME, HOST_IP, SERVICE_TYPE, JVM_VERSION, AGENT_VERSION); + super(AGENT_ID, AGENT_NAME, APPLICATION_NAME, IS_CONTAINER, System.currentTimeMillis(), + PID, MACHINE_NAME, HOST_IP, SERVICE_TYPE, JVM_VERSION, AGENT_VERSION, + CLUSTER_NAMESPACE); } } diff --git a/agent-module/profiler-test/src/test/java/com/navercorp/pinpoint/profiler/test/MockApplicationContextModuleTest.java b/agent-module/profiler-test/src/test/java/com/navercorp/pinpoint/profiler/test/MockApplicationContextModuleTest.java index 3df0954d5c21..fbd6fd08b27e 100644 --- a/agent-module/profiler-test/src/test/java/com/navercorp/pinpoint/profiler/test/MockApplicationContextModuleTest.java +++ b/agent-module/profiler-test/src/test/java/com/navercorp/pinpoint/profiler/test/MockApplicationContextModuleTest.java @@ -18,12 +18,12 @@ import com.google.inject.Injector; import com.google.inject.Module; -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.bootstrap.config.Profiles; +import com.navercorp.pinpoint.profiler.AgentContextOption; +import com.navercorp.pinpoint.profiler.AgentContextOptionBuilder; import com.navercorp.pinpoint.profiler.AgentInfoSender; +import com.navercorp.pinpoint.profiler.AgentOption; import com.navercorp.pinpoint.profiler.context.module.DefaultApplicationContext; import com.navercorp.pinpoint.profiler.context.module.ModuleFactory; import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder; @@ -33,12 +33,8 @@ import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.Instrumentation; -import java.net.URL; import java.util.Collections; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; - /** * @author Woonduk Kang(emeroad) */ @@ -46,51 +42,47 @@ public class MockApplicationContextModuleTest { @Test public void test() { - ProfilerConfig profilerConfig = spy(new DefaultProfilerConfig()); - when(profilerConfig.getStaticResourceCleanup()).thenReturn(true); - URL resource = getClass().getResource("/"); - when(profilerConfig.readString(Profiles.LOG_CONFIG_LOCATION_KEY, null)).thenReturn(resource.getPath()); + ProfilerConfig profilerConfig = new DefaultProfilerConfig(); + Instrumentation instrumentation = Mockito.mock(Instrumentation.class); - AgentOption agentOption = new DefaultAgentOption(instrumentation, - "mockAgentId", "mockAgentName", "mockApplicationName", - profilerConfig, Collections.emptyList(), Collections.emptyList()); + AgentOption agentOption = new AgentOption(instrumentation, + profilerConfig.getProperties(), Collections.emptyMap(), null, + Collections.emptyList(), Collections.emptyList(), false); - PluginTestAgent pluginTestAgent = new PluginTestAgent(agentOption); - try { + try (PluginTestAgent pluginTestAgent = new PluginTestAgent(agentOption.toMap())) { pluginTestAgent.start(); - } finally { - pluginTestAgent.stop(); } } @Test public void testMockApplicationContext() { - ProfilerConfig profilerConfig = spy(new DefaultProfilerConfig()); - when(profilerConfig.getStaticResourceCleanup()).thenReturn(true); + ProfilerConfig profilerConfig = new DefaultProfilerConfig(); Instrumentation instrumentation = Mockito.mock(Instrumentation.class); - AgentOption agentOption = new DefaultAgentOption(instrumentation, - "mockAgentId", "mockAgentName", "mockApplicationName", - profilerConfig, Collections.emptyList(), Collections.emptyList()); + AgentOption agentOption = new AgentOption(instrumentation, + profilerConfig.getProperties(), Collections.emptyMap(), null, + Collections.emptyList(), Collections.emptyList(), false); Module pluginModule = new PluginApplicationContextModule(); InterceptorRegistryBinder interceptorRegistryBinder = new TestInterceptorRegistryBinder(); Module testInterceptorRegistryModule = InterceptorRegistryModule.wrap(interceptorRegistryBinder); ModuleFactory moduleFactory = new OverrideModuleFactory(pluginModule, testInterceptorRegistryModule); - DefaultApplicationContext applicationContext = new DefaultApplicationContext(agentOption, moduleFactory); + AgentContextOption agentContextOption = AgentContextOptionBuilder.build(agentOption, + "mockAgentId", "mockAgentName", "mockApplicationName", profilerConfig); + try (DefaultApplicationContext applicationContext = new DefaultApplicationContext(agentContextOption, moduleFactory)) { - Injector injector = applicationContext.getInjector(); - // singleton check - AgentInfoSender instance1 = injector.getInstance(AgentInfoSender.class); - AgentInfoSender instance2 = injector.getInstance(AgentInfoSender.class); - Assertions.assertSame(instance1, instance2); + Injector injector = applicationContext.getInjector(); + // singleton check + AgentInfoSender instance1 = injector.getInstance(AgentInfoSender.class); + AgentInfoSender instance2 = injector.getInstance(AgentInfoSender.class); + Assertions.assertSame(instance1, instance2); - ClassFileTransformer instance4 = injector.getInstance(ClassFileTransformer.class); + ClassFileTransformer instance4 = injector.getInstance(ClassFileTransformer.class); - applicationContext.close(); + } } -} \ No newline at end of file +} diff --git a/agent-module/profiler/pom.xml b/agent-module/profiler/pom.xml index bc5664dfe226..a0d3f0fbe60a 100644 --- a/agent-module/profiler/pom.xml +++ b/agent-module/profiler/pom.xml @@ -67,10 +67,6 @@ com.navercorp.pinpoint pinpoint-bootstrap-core - - com.navercorp.pinpoint - pinpoint-bootstrap - com.navercorp.pinpoint pinpoint-plugins-loader @@ -205,6 +201,10 @@ + + org.codehaus.mojo + templating-maven-plugin + diff --git a/agent-module/profiler/src/main/java-templates/com/navercorp/pinpoint/profiler/VersionTemplate.java b/agent-module/profiler/src/main/java-templates/com/navercorp/pinpoint/profiler/VersionTemplate.java new file mode 100644 index 000000000000..9c84e14fa9dd --- /dev/null +++ b/agent-module/profiler/src/main/java-templates/com/navercorp/pinpoint/profiler/VersionTemplate.java @@ -0,0 +1,7 @@ +package com.navercorp.pinpoint.profiler; +final class VersionTemplate { + static final String VERSION_TEMPLATE = "${project.version}"; + + private VersionTemplate() { + } +} \ No newline at end of file diff --git a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/Agent.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/Agent.java similarity index 85% rename from agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/Agent.java rename to agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/Agent.java index cdb36b875ffa..49c0ab59335d 100644 --- a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/Agent.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/Agent.java @@ -14,13 +14,15 @@ * limitations under the License. */ -package com.navercorp.pinpoint.bootstrap; +package com.navercorp.pinpoint.profiler; + +import java.io.Closeable; /** * @author emeroad * @author hyungil.jeong */ -public interface Agent { +public interface Agent extends Closeable { /** * // caution @@ -29,7 +31,8 @@ public interface Agent { */ void start(); - void stop(); + @Override + void close(); void registerStopHandler(); } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentContextOption.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentContextOption.java new file mode 100644 index 000000000000..a2ab4a901168 --- /dev/null +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentContextOption.java @@ -0,0 +1,28 @@ +package com.navercorp.pinpoint.profiler; + +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; + +import java.lang.instrument.Instrumentation; +import java.nio.file.Path; +import java.util.List; + +public interface AgentContextOption { + + Instrumentation getInstrumentation(); + + String getAgentId(); + + String getAgentName(); + + String getApplicationName(); + + ProfilerConfig getProfilerConfig(); + + Path getAgentPath(); + + List getPluginJars(); + + List getBootstrapJarPaths(); + + boolean getStaticResourceCleanup(); +} diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentContextOptionBuilder.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentContextOptionBuilder.java new file mode 100644 index 000000000000..ed65f94ebb11 --- /dev/null +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentContextOptionBuilder.java @@ -0,0 +1,23 @@ +package com.navercorp.pinpoint.profiler; + +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; + +public class AgentContextOptionBuilder { + + public static AgentContextOption build(AgentOption agentOption, + String agentId, + String agentName, + String applicationName, + ProfilerConfig profilerConfig) { + return new DefaultAgentContextOption( + agentOption.getInstrumentation(), + agentId, + agentName, + applicationName, + profilerConfig, + agentOption.getAgentPath(), + agentOption.getPluginJars(), + agentOption.getBootstrapJarPaths(), + agentOption.isStaticResourceCleanup()); + } +} diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInformation.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInformation.java index e5bf26a75703..777d8edba62c 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInformation.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentInformation.java @@ -31,6 +31,8 @@ public interface AgentInformation { boolean isContainer(); + String getClusterNamespace(); + long getStartTime(); int getPid(); diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentOption.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentOption.java new file mode 100644 index 000000000000..f96be75c4f65 --- /dev/null +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/AgentOption.java @@ -0,0 +1,100 @@ +package com.navercorp.pinpoint.profiler; + +import java.lang.instrument.Instrumentation; +import java.nio.file.Path; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Properties; + +public class AgentOption { + + private final Instrumentation instrumentation; + private final Properties properties; + private final Map agentArgs; + private final Path agentPath; + private final List pluginJars; + private final List bootstrapJarPaths; + private final boolean staticResourceCleanup; + + @SuppressWarnings("unchecked") + public static AgentOption of(Map map) { + Instrumentation instrumentation = (Instrumentation) map.get("instrumentation"); + Properties properties = (Properties) map.get("properties"); + Map agentArgs = (Map) map.getOrDefault("agentArgs", Collections.emptyMap()); + Path agentPath = (Path) map.get("agentPath"); + List pluginJars = (List) map.getOrDefault("pluginJars", Collections.emptyList()); + List bootstrapJarPaths = (List) map.getOrDefault("bootstrapJarPaths", Collections.emptyList()); + return new AgentOption(instrumentation, properties, agentArgs, agentPath, pluginJars, bootstrapJarPaths, true); + } + + public AgentOption(Instrumentation instrumentation, + Properties properties, + Map agentArgs, + Path agentPath, + List pluginJars, + List bootstrapJarPaths, + boolean staticResourceCleanup) { + this.instrumentation = Objects.requireNonNull(instrumentation, "instrumentation"); + this.properties = Objects.requireNonNull(properties, "properties"); + this.agentArgs = Objects.requireNonNull(agentArgs, "agentArgs"); + this.agentPath = agentPath; + this.pluginJars = pluginJars; + this.bootstrapJarPaths = bootstrapJarPaths; + this.staticResourceCleanup = staticResourceCleanup; + } + + public Instrumentation getInstrumentation() { + return instrumentation; + } + + public Properties getProperties() { + return properties; + } + + public Map getAgentArgs() { + return agentArgs; + } + + public Path getAgentPath() { + return agentPath; + } + + public List getPluginJars() { + return pluginJars; + } + + public List getBootstrapJarPaths() { + return bootstrapJarPaths; + } + + public boolean isStaticResourceCleanup() { + return staticResourceCleanup; + } + + public Map toMap() { + Map map = new HashMap<>(); + map.put("instrumentation", instrumentation); + map.put("properties", properties); + map.put("agentArgs", agentArgs); + map.put("pluginJars", pluginJars); + map.put("agentPath", agentPath); + map.put("bootstrapJarPaths", bootstrapJarPaths); + map.put("staticResourceCleanup", staticResourceCleanup); + return map; + } + + @Override + public String toString() { + return "AgentOption{" + + "instrumentation=" + instrumentation + + ", properties=" + properties + + ", agentArgs=" + agentArgs + + ", pluginJars=" + pluginJars + + ", bootstrapJarPaths=" + bootstrapJarPaths + + ", staticResourceCleanup=" + staticResourceCleanup + + '}'; + } +} diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgent.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgent.java index 8d89e20adcfe..90eb52adb460 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgent.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgent.java @@ -16,16 +16,16 @@ package com.navercorp.pinpoint.profiler; -import com.navercorp.pinpoint.ProductInfo; import com.navercorp.pinpoint.banner.Banner; import com.navercorp.pinpoint.banner.Mode; import com.navercorp.pinpoint.banner.PinpointBanner; -import com.navercorp.pinpoint.bootstrap.Agent; -import com.navercorp.pinpoint.bootstrap.AgentOption; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfigLoader; import com.navercorp.pinpoint.bootstrap.config.Profiles; import com.navercorp.pinpoint.bootstrap.plugin.util.SocketAddressUtils; import com.navercorp.pinpoint.common.profiler.concurrent.PinpointThreadFactory; +import com.navercorp.pinpoint.profiler.config.AgentSystemConfig; +import com.navercorp.pinpoint.profiler.config.LogConfig; import com.navercorp.pinpoint.profiler.context.module.ApplicationContext; import com.navercorp.pinpoint.profiler.context.module.DefaultApplicationContext; import com.navercorp.pinpoint.profiler.context.module.DefaultModuleFactoryResolver; @@ -34,6 +34,10 @@ import com.navercorp.pinpoint.profiler.context.provider.ShutdownHookRegisterProvider; import com.navercorp.pinpoint.profiler.logging.Log4j2LoggingSystem; import com.navercorp.pinpoint.profiler.logging.LoggingSystem; +import com.navercorp.pinpoint.profiler.name.AgentIdResolver; +import com.navercorp.pinpoint.profiler.name.AgentIdResolverBuilder; +import com.navercorp.pinpoint.profiler.name.AgentIdSourceType; +import com.navercorp.pinpoint.profiler.name.AgentIds; import com.navercorp.pinpoint.profiler.util.SystemPropertyDumper; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -56,6 +60,7 @@ public class DefaultAgent implements Agent { private final LoggingSystem loggingSystem; private final Logger logger; + private final AgentOption agentOption; private final ProfilerConfig profilerConfig; private final ApplicationContext applicationContext; @@ -64,32 +69,87 @@ public class DefaultAgent implements Agent { private volatile AgentStatus agentStatus; - public DefaultAgent(AgentOption agentOption) { - Objects.requireNonNull(agentOption, "agentOption"); - Objects.requireNonNull(agentOption.getInstrumentation(), "instrumentation"); - Objects.requireNonNull(agentOption.getProfilerConfig(), "profilerConfig"); + public DefaultAgent(Map map) { + Objects.requireNonNull(map, "map"); + + this.agentOption = AgentOption.of(map); + + Properties properties = agentOption.getProperties(); + this.profilerConfig = ProfilerConfigLoader.load(properties); - this.profilerConfig = agentOption.getProfilerConfig(); + final Path agentPath = agentOption.getAgentPath(); + final Path logConfigPath = getLogConfigPath(profilerConfig, agentPath); + + LogConfig logConfig = new LogConfig(logConfigPath); + logConfig.saveLogFilePath(); + logConfig.cleanLogDir(properties); - final Path logConfigPath = getLogConfigPath(profilerConfig); this.loggingSystem = newLoggingSystem(logConfigPath); this.loggingSystem.start(); logger = LogManager.getLogger(this.getClass()); - dumpAgentOption(agentOption); + + logger.info("logConfig path:{}", loggingSystem.getConfigLocation()); + + AgentContextOption agentContextOption = buildContextOption(agentOption, profilerConfig); + + dumpAgentOption(agentContextOption); dumpSystemProperties(); - dumpConfig(agentOption.getProfilerConfig()); + dumpConfig(profilerConfig); + + // save system config + saveSystemConfig(agentContextOption); changeStatus(AgentStatus.INITIALIZING); preloadOnStartup(); - this.applicationContext = newApplicationContext(agentOption); + this.applicationContext = newApplicationContext(agentContextOption); + } + + protected AgentContextOption buildContextOption(AgentOption agentOption, ProfilerConfig profilerConfig) { + final AgentIds agentIds = resolveAgentIds(); + if (agentIds == null) { + logger.warn("Failed to resolve AgentId and ApplicationId"); + throw new RuntimeException("Failed to resolve AgentId and ApplicationId"); + } + final String agentId = agentIds.getAgentId(); + if (agentId == null) { + logger.warn("agentId is null"); + throw new RuntimeException("agentId is null"); + } + final String agentName = agentIds.getAgentName(); + final String applicationName = agentIds.getApplicationName(); + if (applicationName == null) { + logger.warn("applicationName is null"); + throw new RuntimeException("applicationName is null"); + } + + return AgentContextOptionBuilder.build(agentOption, + agentId, agentName, applicationName, + profilerConfig); + } + + private AgentIds resolveAgentIds() { + AgentIdResolverBuilder builder = new AgentIdResolverBuilder(); + builder.addProperties(AgentIdSourceType.SYSTEM, System.getProperties()::getProperty); + builder.addProperties(AgentIdSourceType.SYSTEM_ENV, System.getenv()::get); + builder.addProperties(AgentIdSourceType.AGENT_ARGUMENT, agentOption.getAgentArgs()::get); + AgentIdResolver agentIdResolver = builder.build(); + return agentIdResolver.resolve(); } - private void dumpAgentOption(AgentOption agentOption) { + + private void saveSystemConfig(AgentContextOption option) { + // set the path of log file as a system property + AgentSystemConfig agentSystemConfig = new AgentSystemConfig(); + agentSystemConfig.saveAgentIdForLog(option.getAgentId()); + agentSystemConfig.savePinpointVersion(Version.VERSION); + } + + private void dumpAgentOption(AgentContextOption agentOption) { logger.info("AgentOption"); logger.info("- agentId:{}", agentOption.getAgentId()); logger.info("- applicationName:{}", agentOption.getApplicationName()); @@ -97,19 +157,24 @@ private void dumpAgentOption(AgentOption agentOption) { logger.info("- instrumentation:{}", agentOption.getInstrumentation()); } - private LoggingSystem newLoggingSystem(Path profilePath) { - return new Log4j2LoggingSystem(profilePath); + private LoggingSystem newLoggingSystem(Path agentPath) { + return Log4j2LoggingSystem.searchPath(agentPath); } - protected ApplicationContext newApplicationContext(AgentOption agentOption) { + protected ApplicationContext newApplicationContext(AgentContextOption agentOption) { Objects.requireNonNull(agentOption, "agentOption"); ProfilerConfig profilerConfig = Objects.requireNonNull(agentOption.getProfilerConfig(), "profilerConfig"); - ModuleFactoryResolver moduleFactoryResolver = new DefaultModuleFactoryResolver(profilerConfig.getInjectionModuleFactoryClazzName()); + String factoryClazzName = getInjectionModuleFactoryClazzName(profilerConfig); + ModuleFactoryResolver moduleFactoryResolver = new DefaultModuleFactoryResolver(factoryClazzName); ModuleFactory moduleFactory = moduleFactoryResolver.resolve(); return new DefaultApplicationContext(agentOption, moduleFactory); } + private String getInjectionModuleFactoryClazzName(ProfilerConfig profilerConfig) { + return profilerConfig.readString("profiler.guice.module.factory", null); + } + protected ApplicationContext getApplicationContext() { return applicationContext; } @@ -136,12 +201,12 @@ private void changeStatus(AgentStatus status) { } } - private Path getLogConfigPath(ProfilerConfig config) { + protected Path getLogConfigPath(ProfilerConfig config, Path agentPath) { final String location = config.readString(Profiles.LOG_CONFIG_LOCATION_KEY, null); - if (location == null) { - throw new IllegalStateException("logPath($PINPOINT_DIR/profiles/${profile}/) not found"); + if (location != null) { + return Paths.get(location); } - return Paths.get(location); + return agentPath; } @@ -162,7 +227,7 @@ public void start() { } } - logger.info("Starting {} Agent.", ProductInfo.NAME); + logger.info("Starting pinpoint Agent."); this.applicationContext.start(); printBanner(); } @@ -194,7 +259,7 @@ public void registerStopHandler() { @Override public void run() { logger.info("stop() started. threadName:" + Thread.currentThread().getName()); - DefaultAgent.this.stop(); + DefaultAgent.this.close(); } }); @@ -205,7 +270,7 @@ public void run() { } @Override - public void stop() { + public void close() { synchronized (agentStatusLock) { if (this.agentStatus == AgentStatus.RUNNING) { changeStatus(AgentStatus.STOPPED); @@ -214,11 +279,11 @@ public void stop() { return; } } - logger.info("Stopping {} Agent.", ProductInfo.NAME); + logger.info("Stopping pinpoint Agent."); this.applicationContext.close(); // for testcase - if (profilerConfig.getStaticResourceCleanup()) { + if (agentOption.isStaticResourceCleanup()) { this.loggingSystem.close(); } } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgentContextOption.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgentContextOption.java new file mode 100644 index 000000000000..d099cd61f5b5 --- /dev/null +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgentContextOption.java @@ -0,0 +1,104 @@ +package com.navercorp.pinpoint.profiler; + +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; + +import java.lang.instrument.Instrumentation; +import java.nio.file.Path; +import java.util.List; +import java.util.Objects; + +public class DefaultAgentContextOption implements AgentContextOption { + private final Instrumentation instrumentation; + + private final String agentId; + private final String agentName; + private final String applicationName; + + private final ProfilerConfig profilerConfig; + + private final Path agentPath; + private final List pluginJars; + private final List bootstrapJarPaths; + + private final boolean staticResourceCleanup; + + public DefaultAgentContextOption(final Instrumentation instrumentation, + String agentId, + String agentName, + String applicationName, + final ProfilerConfig profilerConfig, + final Path agentPath, + final List pluginJars, + final List bootstrapJarPaths, + boolean staticResourceCleanup) { + this.instrumentation = Objects.requireNonNull(instrumentation, "instrumentation"); + this.agentId = Objects.requireNonNull(agentId, "agentId"); + this.agentName = agentName; + this.applicationName = Objects.requireNonNull(applicationName, "applicationName"); + this.profilerConfig = Objects.requireNonNull(profilerConfig, "profilerConfig"); + + this.agentPath = agentPath; + this.pluginJars = Objects.requireNonNull(pluginJars, "pluginJars"); + this.bootstrapJarPaths = Objects.requireNonNull(bootstrapJarPaths, "bootstrapJarPaths"); + this.staticResourceCleanup = staticResourceCleanup; + } + + @Override + public Instrumentation getInstrumentation() { + return this.instrumentation; + } + + @Override + public String getAgentId() { + return agentId; + } + + @Override + public String getAgentName() { + return agentName; + } + + @Override + public String getApplicationName() { + return applicationName; + } + + @Override + public Path getAgentPath() { + return agentPath; + } + + @Override + public List getPluginJars() { + return this.pluginJars; + } + + @Override + public List getBootstrapJarPaths() { + return this.bootstrapJarPaths; + } + + @Override + public ProfilerConfig getProfilerConfig() { + return profilerConfig; + } + + @Override + public boolean getStaticResourceCleanup() { + return staticResourceCleanup; + } + + @Override + public String toString() { + return "DefaultAgentOption{" + + "instrumentation=" + instrumentation + + ", agentId='" + agentId + '\'' + + ", agentName='" + agentName + '\'' + + ", applicationName='" + applicationName + '\'' + + ", profilerConfig=" + profilerConfig + + ", pluginJars=" + pluginJars + + ", bootstrapJarPaths=" + bootstrapJarPaths + + ", staticResourceCleanup=" + staticResourceCleanup + + '}'; + } +} diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgentInformation.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgentInformation.java index 83ee586bb9b0..a76f529660b5 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgentInformation.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgentInformation.java @@ -38,6 +38,8 @@ public class DefaultAgentInformation implements AgentInformation { private final String jvmVersion; private final String agentVersion; + private final String clusterNamespace; + public DefaultAgentInformation( String agentId, String agentName, @@ -49,7 +51,8 @@ public DefaultAgentInformation( String hostIp, ServiceType serverType, String jvmVersion, - String agentVersion) { + String agentVersion, + String clusterNamespace) { this.agentId = Objects.requireNonNull(agentId, "agentId"); this.agentName = Objects.requireNonNull(agentName, "agentName"); this.applicationName = Objects.requireNonNull(applicationName, "applicationName"); @@ -61,6 +64,8 @@ public DefaultAgentInformation( this.serverType = serverType; this.jvmVersion = jvmVersion; this.agentVersion = Objects.requireNonNull(agentVersion, "agentVersion"); + + this.clusterNamespace = clusterNamespace; } @Override @@ -118,21 +123,25 @@ public String getAgentVersion() { return agentVersion; } + @Override + public String getClusterNamespace() { + return clusterNamespace; + } + @Override public String toString() { - final StringBuilder sb = new StringBuilder("{"); - sb.append("agentId='").append(agentId).append('\''); - sb.append(", agentName='").append(agentName).append('\''); - sb.append(", applicationName='").append(applicationName).append('\''); - sb.append(", isContainer=").append(isContainer); - 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(); + return "{" + "agentId='" + agentId + '\'' + + ", agentName='" + agentName + '\'' + + ", applicationName='" + applicationName + '\'' + + ", isContainer=" + isContainer + + ", startTime=" + startTime + + ", pid=" + pid + + ", machineName='" + machineName + '\'' + + ", hostIp='" + hostIp + '\'' + + ", serverType=" + serverType + + ", jvmVersion='" + jvmVersion + '\'' + + ", agentVersion='" + agentVersion + '\'' + + ", clusterNamespace='" + clusterNamespace + '\'' + + '}'; } } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/Version.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/Version.java new file mode 100644 index 000000000000..ada3d19be44e --- /dev/null +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/Version.java @@ -0,0 +1,5 @@ +package com.navercorp.pinpoint.profiler; + +public final class Version { + public static final String VERSION = VersionTemplate.VERSION_TEMPLATE; +} diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/AgentSystemConfig.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/config/AgentSystemConfig.java similarity index 57% rename from agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/AgentSystemConfig.java rename to agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/config/AgentSystemConfig.java index a854424989bd..b4cbb74ae781 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/AgentSystemConfig.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/config/AgentSystemConfig.java @@ -1,14 +1,14 @@ -package com.navercorp.pinpoint.bootstrap.config; +package com.navercorp.pinpoint.profiler.config; -import com.navercorp.pinpoint.ProductInfo; -import com.navercorp.pinpoint.bootstrap.AgentIdSourceType; -import com.navercorp.pinpoint.bootstrap.BootLogger; +import com.navercorp.pinpoint.profiler.name.AgentIdSourceType; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.Objects; import java.util.Properties; public class AgentSystemConfig { - private final BootLogger logger = BootLogger.getLogger(getClass()); + private final Logger logger = LogManager.getLogger(getClass()); private final Properties systemProperty; @@ -25,8 +25,8 @@ public void saveAgentIdForLog(String agentId) { } public void savePinpointVersion(String version) { - logger.info(String.format("pinpoint version:%s", version)); - systemProperty.setProperty(ProductInfo.NAME + ".version", version); + logger.info("pinpoint version:{}", version); + systemProperty.setProperty("pinpoint.version", version); } } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/config/LogConfig.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/config/LogConfig.java new file mode 100644 index 000000000000..bd2d273a5fc5 --- /dev/null +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/config/LogConfig.java @@ -0,0 +1,40 @@ +package com.navercorp.pinpoint.profiler.config; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Objects; +import java.util.Properties; + +public class LogConfig { + + public static final Path LOGS_DIR = Paths.get("logs"); + + private static final String LOG_MAX_BACKUP_SIZE = "profiler.logdir.maxbackupsize"; + private static final int DEFAULT_LOG_MAX_BACKUP_SIZE = 5; + + private final Path logsPath; + + + public LogConfig(Path agentLogPath) { + + Objects.requireNonNull(agentLogPath, "agentPath"); + this.logsPath = agentLogPath.resolve(LOGS_DIR); + } + + public void saveLogFilePath() { + saveLogFilePath(System.getProperties()); + } + + public void saveLogFilePath(Properties systemProperty) { + final String path = logsPath.toString(); + // export for log4j2.xml + systemProperty.setProperty("pinpoint.log", path); + } + + public void cleanLogDir(Properties properties) { + String value = properties.getProperty(LOG_MAX_BACKUP_SIZE, String.valueOf(DEFAULT_LOG_MAX_BACKUP_SIZE)); + int logDirMaxBackupSize = Integer.parseInt(value); + LogDirCleaner logDirCleaner = new LogDirCleaner(logsPath, logDirMaxBackupSize); + logDirCleaner.clean(); + } +} diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/agentdir/LogDirCleaner.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/config/LogDirCleaner.java similarity index 77% rename from agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/agentdir/LogDirCleaner.java rename to agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/config/LogDirCleaner.java index 1d275cd00466..71219761c645 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/agentdir/LogDirCleaner.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/config/LogDirCleaner.java @@ -1,8 +1,5 @@ -package com.navercorp.pinpoint.bootstrap.agentdir; +package com.navercorp.pinpoint.profiler.config; -import com.navercorp.pinpoint.bootstrap.BootLogger; - -import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -15,8 +12,6 @@ import java.util.stream.Stream; public class LogDirCleaner { - private final BootLogger logger = BootLogger.getLogger(this.getClass()); - private final Path logPath; private final int maxSize; @@ -30,7 +25,6 @@ public void clean() { return; } if (!Files.isDirectory(logPath)) { - logger.warn(logPath + " is not directory"); return; } List agentDirectories = directoryList(); @@ -49,7 +43,6 @@ private List directoryList() { .sorted(Comparator.comparing(Path::toFile)) .collect(Collectors.toList()); } catch (IOException e) { - logger.warn("directoryList error:" + logPath + " " + e.getMessage()); return Collections.emptyList(); } } @@ -61,7 +54,6 @@ private void delete(List agentDirectories) { List deleteTargets = agentDirectories.subList(0, removeSize); for (Path file : deleteTargets) { - logger.info("delete agent dir:" + file.toAbsolutePath()); deleteAll(file); } @@ -80,10 +72,15 @@ private long getLastModifiedTime(Path path) { private void deleteAll(Path file) { try (Stream paths = Files.walk(file)) { paths.sorted(Comparator.reverseOrder()) - .map(Path::toFile) - .forEach(File::delete); - } catch (IOException e) { - logger.info("delete error :" + file + " " + e.getMessage()); + .forEach(this::deletePath); + } catch (IOException ignore) { + } + } + + public void deletePath(Path path) { + try { + Files.delete(path); + } catch (IOException ignore) { } } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTraceContext.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTraceContext.java index 5aebc48168e4..64d94bca71df 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTraceContext.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTraceContext.java @@ -68,8 +68,7 @@ public DefaultTraceContext(final ProfilerConfig profilerConfig, final ApiMetaDataService apiMetaDataService, final StringMetaDataService stringMetaDataService, final SqlMetaDataService sqlMetaDataService, - final JdbcContext jdbcContext - ) { + final JdbcContext jdbcContext) { this.profilerConfig = Objects.requireNonNull(profilerConfig, "profilerConfig"); this.agentInformation = Objects.requireNonNull(agentInformation, "agentInformation"); this.serverMetaDataHolder = Objects.requireNonNull(serverMetaDataHolder, "serverMetaDataHolder"); @@ -154,8 +153,6 @@ public Trace continueAsyncTraceObject(final TraceId traceId) { } - - @Override public Trace removeTraceObject() { return removeTraceObject(true); @@ -235,7 +232,6 @@ public ParsingResult parseSql(final String sql) { } - @Override public ServerMetaDataHolder getServerMetaDataHolder() { return this.serverMetaDataHolder; @@ -247,4 +243,8 @@ public JdbcContext getJdbcContext() { return jdbcContext; } + @Override + public String getClusterNamespace() { + return this.agentInformation.getClusterNamespace(); + } } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/grpc/config/GrpcTransportConfig.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/grpc/config/GrpcTransportConfig.java index 053bb4c0ae4d..f95ff1666510 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/grpc/config/GrpcTransportConfig.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/grpc/config/GrpcTransportConfig.java @@ -16,7 +16,7 @@ package com.navercorp.pinpoint.profiler.context.grpc.config; -import com.navercorp.pinpoint.bootstrap.agentdir.AgentDirectory; +import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig; import com.navercorp.pinpoint.bootstrap.module.JavaModule; import com.navercorp.pinpoint.common.config.Value; import com.navercorp.pinpoint.common.config.util.ValueAnnotationProcessor; @@ -226,7 +226,7 @@ public String apply(String placeholderName) { public SslOption readSslOption(final Function properties) { final String sslPrefix = "profiler.transport.grpc.ssl."; - String agentRootPath = properties.apply(AgentDirectory.AGENT_ROOT_PATH_KEY); + String agentRootPath = properties.apply(DefaultProfilerConfig.AGENT_ROOT_PATH_KEY); final SslOption.Builder builder = new SslOption.Builder(agentRootPath); diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ApplicationContext.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ApplicationContext.java index 16e61a034df6..fdc369172ebb 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ApplicationContext.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ApplicationContext.java @@ -16,12 +16,15 @@ package com.navercorp.pinpoint.profiler.context.module; +import java.io.Closeable; + /** * @author Woonduk Kang(emeroad) */ -public interface ApplicationContext { +public interface ApplicationContext extends Closeable { void start(); + @Override void close(); } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ApplicationContextModuleFactory.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ApplicationContextModuleFactory.java index 8cc5de131545..a90f7d6befd0 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ApplicationContextModuleFactory.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ApplicationContextModuleFactory.java @@ -18,8 +18,8 @@ import com.google.inject.Module; import com.google.inject.util.Modules; -import com.navercorp.pinpoint.bootstrap.AgentOption; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.profiler.AgentContextOption; import com.navercorp.pinpoint.profiler.context.module.config.ConfigModule; import com.navercorp.pinpoint.profiler.context.module.config.ConfigurationLoader; import com.navercorp.pinpoint.profiler.context.monitor.config.DefaultExceptionTraceConfig; @@ -36,7 +36,7 @@ public class ApplicationContextModuleFactory implements ModuleFactory { private final Logger logger = LogManager.getLogger(this.getClass()); @Override - public Module newModule(AgentOption agentOption) { + public Module newModule(AgentContextOption agentOption) { final Module config = new ConfigModule(agentOption); final Module pluginModule = new PluginModule(); final Module applicationContextModule = new ApplicationContextModule(); @@ -68,7 +68,7 @@ protected Module newExceptionTraceModule(Properties properties) { } } - protected Module newRpcModule(AgentOption agentOption) { + protected Module newRpcModule(AgentContextOption agentOption) { ProfilerConfig profilerConfig = agentOption.getProfilerConfig(); logger.info("load GrpcModule"); return new GrpcModule(profilerConfig); diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/LogConfigResolver.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ClusterNamespace.java similarity index 59% rename from agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/LogConfigResolver.java rename to agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ClusterNamespace.java index c52acc152392..a97bf2746190 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/LogConfigResolver.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ClusterNamespace.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 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. @@ -14,13 +14,21 @@ * limitations under the License. */ -package com.navercorp.pinpoint.bootstrap.config; +package com.navercorp.pinpoint.profiler.context.module; -import java.nio.file.Path; +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) */ -public interface LogConfigResolver { - Path getLogPath(); +@BindingAnnotation +@Target(PARAMETER) +@Retention(RUNTIME) +public @interface ClusterNamespace { } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/DefaultApplicationContext.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/DefaultApplicationContext.java index 738ed95c3c57..110364036dfd 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/DefaultApplicationContext.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/DefaultApplicationContext.java @@ -23,7 +23,6 @@ import com.google.inject.Stage; import com.google.inject.TypeLiteral; import com.google.inject.name.Names; -import com.navercorp.pinpoint.bootstrap.AgentOption; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; import com.navercorp.pinpoint.bootstrap.context.TraceContext; import com.navercorp.pinpoint.bootstrap.instrument.DynamicTransformTrigger; @@ -32,6 +31,7 @@ import com.navercorp.pinpoint.common.profiler.message.DataSender; import com.navercorp.pinpoint.common.util.JvmUtils; import com.navercorp.pinpoint.common.util.JvmVersion; +import com.navercorp.pinpoint.profiler.AgentContextOption; import com.navercorp.pinpoint.profiler.AgentInfoSender; import com.navercorp.pinpoint.profiler.AgentInformation; import com.navercorp.pinpoint.profiler.context.ServerMetaDataRegistryService; @@ -84,10 +84,13 @@ public class DefaultApplicationContext implements ApplicationContext { private final Injector injector; - public DefaultApplicationContext(AgentOption agentOption, ModuleFactory moduleFactory) { + private final boolean staticResourceCleanup; + + public DefaultApplicationContext(AgentContextOption agentOption, ModuleFactory moduleFactory) { Objects.requireNonNull(agentOption, "agentOption"); Objects.requireNonNull(moduleFactory, "moduleFactory"); Objects.requireNonNull(agentOption.getProfilerConfig(), "profilerConfig"); + this.staticResourceCleanup = agentOption.getStaticResourceCleanup(); final Instrumentation instrumentation = agentOption.getInstrumentation(); if (logger.isInfoEnabled()) { @@ -245,7 +248,7 @@ public void close() { this.rpcModuleLifeCycle.shutdown(); } - if (profilerConfig.getStaticResourceCleanup()) { + if (staticResourceCleanup) { this.interceptorRegistryBinder.unbind(); } } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/GrpcModule.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/GrpcModule.java index ef65d3699157..e6272fd49e6f 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/GrpcModule.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/GrpcModule.java @@ -92,7 +92,11 @@ public class GrpcModule extends PrivateModule { public GrpcModule(ProfilerConfig profilerConfig) { this.profilerConfig = Objects.requireNonNull(profilerConfig, "profilerConfig"); - this.reporter = new ChannelzScheduledReporterBuilder().acceptConfig(this.profilerConfig).build(); + + ChannelzScheduledReporterBuilder builder = new ChannelzScheduledReporterBuilder(); + builder.acceptConfig(this.profilerConfig.getProperties()::getProperty); + this.reporter = builder.build(); + } @Override diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ModuleFactory.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ModuleFactory.java index 6506d999897f..434dbf4557b8 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ModuleFactory.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/ModuleFactory.java @@ -17,11 +17,11 @@ package com.navercorp.pinpoint.profiler.context.module; import com.google.inject.Module; -import com.navercorp.pinpoint.bootstrap.AgentOption; +import com.navercorp.pinpoint.profiler.AgentContextOption; /** * @author Woonduk Kang(emeroad) */ public interface ModuleFactory { - Module newModule(AgentOption agentOption); + Module newModule(AgentContextOption agentOption); } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/config/ClusterNamespaceProvider.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/config/ClusterNamespaceProvider.java new file mode 100644 index 000000000000..f4eb89e2d178 --- /dev/null +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/config/ClusterNamespaceProvider.java @@ -0,0 +1,43 @@ +package com.navercorp.pinpoint.profiler.context.module.config; + +import com.google.inject.Inject; +import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import jakarta.inject.Provider; + +import java.util.function.Function; + +public class ClusterNamespaceProvider implements Provider { + + private static final String[] clusterNamespaceKeys = new String[] { + // pinpoint V4 : property key of cluster namespace + "profiler.cluster.namespace", + // legacy key V1~V3 + "profiler.application.namespace", + }; + + private final String clusterNamespace; + + @Inject + public ClusterNamespaceProvider(ProfilerConfig properties) { + this(properties::readString); + } + + public ClusterNamespaceProvider(Function properties) { + this.clusterNamespace = getNamespace(properties); + } + + private static String getNamespace(Function profilerConfig) { + for (String namespaceKey : clusterNamespaceKeys) { + String namespace = profilerConfig.apply(namespaceKey); + if (namespace != null) { + return namespace; + } + } + return null; + } + + @Override + public String get() { + return clusterNamespace; + } +} diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/config/ConfigModule.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/config/ConfigModule.java index 9581d5dc337c..e53e845714ae 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/config/ConfigModule.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/module/config/ConfigModule.java @@ -19,9 +19,9 @@ 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.common.trace.ServiceType; +import com.navercorp.pinpoint.profiler.AgentContextOption; import com.navercorp.pinpoint.profiler.context.TraceDataFormatVersion; import com.navercorp.pinpoint.profiler.context.config.ContextConfig; import com.navercorp.pinpoint.profiler.context.config.DefaultContextConfig; @@ -30,6 +30,7 @@ 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.ClusterNamespace; import com.navercorp.pinpoint.profiler.context.module.ConfiguredApplicationType; import com.navercorp.pinpoint.profiler.context.module.Container; import com.navercorp.pinpoint.profiler.context.module.PluginJarPaths; @@ -58,6 +59,7 @@ import org.apache.logging.log4j.Logger; import java.lang.instrument.Instrumentation; +import java.nio.file.Path; import java.util.List; import java.util.Objects; import java.util.Properties; @@ -68,9 +70,9 @@ public class ConfigModule extends AbstractModule { private final Logger logger = LogManager.getLogger(this.getClass()); - private final AgentOption agentOption; + private final AgentContextOption agentOption; - public ConfigModule(AgentOption agentOption) { + public ConfigModule(AgentContextOption agentOption) { this.agentOption = Objects.requireNonNull(agentOption, "profilerConfig"); Objects.requireNonNull(agentOption.getProfilerConfig(), "profilerConfig"); } @@ -85,7 +87,6 @@ protected void configure() { ProfilerConfig profilerConfig = agentOption.getProfilerConfig(); bind(ProfilerConfig.class).toInstance(profilerConfig); - Properties properties = profilerConfig.getProperties(); ConfigurationLoader configurationLoader = new ConfigurationLoader(properties); @@ -126,26 +127,23 @@ protected void configure() { bind(InterceptorRegistryBinder.class).toProvider(InterceptorRegistryBinderProvider.class).in(Scopes.SINGLETON); - TypeLiteral> pluginJarFile = new TypeLiteral>() {}; + TypeLiteral> pluginJarFile = new TypeLiteral>() {}; bind(pluginJarFile).annotatedWith(PluginJarPaths.class).toInstance(agentOption.getPluginJars()); TypeLiteral> pluginJars = new TypeLiteral>() {}; bind(pluginJars).annotatedWith(PluginJars.class).toProvider(PluginJarsProvider.class).in(Scopes.SINGLETON); - bindBootstrapCoreInformation(); - - final ContainerResolver containerResolver = new ContainerResolver(); - final boolean isContainer = containerResolver.isContainer(); + bindBootstrapCoreInformation(agentOption); - bindAgentInformation(agentOption.getAgentId(), agentOption.getAgentName(), agentOption.getApplicationName(), isContainer); + bindAgentInformation(agentOption); bindShutdownHook(contextConfig); } - private void bindBootstrapCoreInformation() { - List bootstrapJarPaths = agentOption.getBootstrapJarPaths(); + private void bindBootstrapCoreInformation(AgentContextOption agentOption) { + List bootstrapJarPaths = agentOption.getBootstrapJarPaths(); - TypeLiteral> bootstrapJarFIle = new TypeLiteral>() {}; + TypeLiteral> bootstrapJarFIle = new TypeLiteral>() {}; bind(bootstrapJarFIle).annotatedWith(BootstrapJarPaths.class).toInstance(bootstrapJarPaths); BootstrapCore bootstrapCore = new BootstrapCore(bootstrapJarPaths); @@ -161,15 +159,20 @@ private void bindConstants(ContextConfig contextConfig) { bindConstant().annotatedWith(DeadlockMonitorInterval.class).to(contextConfig.getDeadlockMonitorInterval()); } - private void bindAgentInformation(String agentId, String agentName, String applicationName, boolean isContainer) { + private void bindAgentInformation(AgentContextOption agentOption) { - bind(String.class).annotatedWith(AgentId.class).toInstance(agentId); - bind(String.class).annotatedWith(AgentName.class).toInstance(agentName); - bind(String.class).annotatedWith(ApplicationName.class).toInstance(applicationName); + bind(String.class).annotatedWith(AgentId.class).toInstance(agentOption.getAgentId()); + bind(String.class).annotatedWith(AgentName.class).toInstance(agentOption.getAgentName()); + bind(String.class).annotatedWith(ApplicationName.class).toInstance(agentOption.getApplicationName()); + final ContainerResolver containerResolver = new ContainerResolver(); + final boolean isContainer = containerResolver.isContainer(); bind(Boolean.class).annotatedWith(Container.class).toInstance(isContainer); + bind(Long.class).annotatedWith(AgentStartTime.class).toProvider(AgentStartTimeProvider.class).in(Scopes.SINGLETON); bind(ServiceType.class).annotatedWith(ConfiguredApplicationType.class).toProvider(ConfiguredApplicationTypeProvider.class).in(Scopes.SINGLETON); + + bind(String.class).annotatedWith(ClusterNamespace.class).toProvider(ClusterNamespaceProvider.class).in(Scopes.SINGLETON); } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentInformationProvider.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentInformationProvider.java index 4aaf856b930b..71b784c18929 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentInformationProvider.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/AgentInformationProvider.java @@ -31,6 +31,7 @@ import com.navercorp.pinpoint.profiler.context.module.AgentStartTime; import com.navercorp.pinpoint.profiler.context.module.ApplicationName; import com.navercorp.pinpoint.profiler.context.module.ApplicationServerType; +import com.navercorp.pinpoint.profiler.context.module.ClusterNamespace; import com.navercorp.pinpoint.profiler.context.module.Container; import com.navercorp.pinpoint.profiler.util.RuntimeMXBeanUtils; import org.apache.logging.log4j.LogManager; @@ -52,9 +53,13 @@ public class AgentInformationProvider implements Provider { private final long agentStartTime; private final ServiceType serverType; + private final Provider clusterNamespaceProvider; + @Inject public AgentInformationProvider(@AgentId String agentId, @AgentName String agentName, @ApplicationName String applicationName, - @Container boolean isContainer, @AgentStartTime long agentStartTime, @ApplicationServerType ServiceType serverType) { + @Container boolean isContainer, @AgentStartTime long agentStartTime, + @ApplicationServerType ServiceType serverType, + @ClusterNamespace Provider clusterNamespaceProvider) { Objects.requireNonNull(agentId, "agentId"); Objects.requireNonNull(applicationName, "applicationName"); @@ -64,7 +69,7 @@ public AgentInformationProvider(@AgentId String agentId, @AgentName String agent this.isContainer = isContainer; this.agentStartTime = agentStartTime; this.serverType = Objects.requireNonNull(serverType, "serverType"); - + this.clusterNamespaceProvider = clusterNamespaceProvider; } public AgentInformation get() { @@ -78,7 +83,10 @@ public AgentInformation createAgentInformation() { final int pid = RuntimeMXBeanUtils.getPid(); final String jvmVersion = JvmUtils.getSystemProperty(SystemPropertyKey.JAVA_VERSION); - return new DefaultAgentInformation(agentId, agentName, applicationName, isContainer, agentStartTime, pid, machineName, hostIp, serverType, jvmVersion, Version.VERSION); + String clusterNamespace = this.clusterNamespaceProvider.get(); + return new DefaultAgentInformation(agentId, agentName, applicationName, isContainer, agentStartTime, pid, + machineName, hostIp, serverType, jvmVersion, Version.VERSION, + clusterNamespace); } private String getHostIp() { diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/TraceContextProvider.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/TraceContextProvider.java index 3d725949804d..3be71e3816a8 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/TraceContextProvider.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/TraceContextProvider.java @@ -37,6 +37,7 @@ */ public class TraceContextProvider implements Provider { private final ProfilerConfig profilerConfig; + private final Provider agentInformationProvider; private final TraceIdFactory traceIdFactory; @@ -76,6 +77,9 @@ public TraceContextProvider(ProfilerConfig profilerConfig, public TraceContext get() { AgentInformation agentInformation = this.agentInformationProvider.get(); return new DefaultTraceContext(profilerConfig, agentInformation, traceIdFactory, traceFactory, - serverMetaDataHolder, apiMetaDataService, stringMetaDataService, sqlMetaDataService, jdbcContext); + serverMetaDataHolder, apiMetaDataService, stringMetaDataService, sqlMetaDataService, + jdbcContext); } + + } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/plugin/PluginJarsProvider.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/plugin/PluginJarsProvider.java index c4f9190337b3..1faebab12316 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/plugin/PluginJarsProvider.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/plugin/PluginJarsProvider.java @@ -29,6 +29,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; @@ -44,7 +45,7 @@ public class PluginJarsProvider implements Provider> { private final List pluginJars; @Inject - public PluginJarsProvider(@PluginJarPaths List pluginJarPaths, PluginLoadingConfig pluginLoadingConfig) { + public PluginJarsProvider(@PluginJarPaths List pluginJarPaths, PluginLoadingConfig pluginLoadingConfig) { Objects.requireNonNull(pluginJarPaths, "pluginJarPaths"); Objects.requireNonNull(pluginLoadingConfig, "pluginLoadingConfig"); @@ -59,7 +60,7 @@ public List get() { return pluginJars; } - private List createPluginJars(final List pluginJarPaths, + private List createPluginJars(final List pluginJarPaths, final PluginFilter pluginFilter, final List orderedPluginIdList) { if (CollectionUtils.isEmpty(pluginJarPaths)) { @@ -95,9 +96,9 @@ private List sort(List pluginList, List orderedPlu return pluginJars; } - private List filter(List pluginJarPaths, PluginFilter pluginFilter) { + private List filter(List pluginJarPaths, PluginFilter pluginFilter) { List list = new ArrayList<>(); - for (String pluginJarPath : pluginJarPaths) { + for (Path pluginJarPath : pluginJarPaths) { PluginJar pluginJar = PluginJar.fromFilePath(pluginJarPath); if (!pluginFilter.accept(pluginJar)) { continue; diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/classloading/BootstrapCore.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/classloading/BootstrapCore.java index 231a5e3d4d7f..78008dc5b26c 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/classloading/BootstrapCore.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/classloading/BootstrapCore.java @@ -22,6 +22,7 @@ import org.apache.logging.log4j.Logger; import java.io.InputStream; +import java.nio.file.Path; import java.util.List; import java.util.Objects; @@ -37,7 +38,7 @@ public class BootstrapCore { private final ClassLoader bootstrapClassLoader = Object.class.getClassLoader(); - public BootstrapCore(List bootstrapJarPaths) { + public BootstrapCore(List bootstrapJarPaths) { Objects.requireNonNull(bootstrapJarPaths, "bootstrapJarPaths"); this.bootstrapRepository = new JarFileRepository(bootstrapJarPaths); diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/config/DefaultInstrumentConfig.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/config/DefaultInstrumentConfig.java index 09e6f546567a..69e5bcae2962 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/config/DefaultInstrumentConfig.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/config/DefaultInstrumentConfig.java @@ -16,7 +16,6 @@ package com.navercorp.pinpoint.profiler.instrument.config; -import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig; import com.navercorp.pinpoint.bootstrap.config.Filter; import com.navercorp.pinpoint.bootstrap.config.ProfilableClassFilter; import com.navercorp.pinpoint.bootstrap.config.SkipFilter; @@ -28,6 +27,8 @@ public class DefaultInstrumentConfig implements InstrumentConfig { + public static final String PROFILER_INTERCEPTOR_EXCEPTION_PROPAGATE = "profiler.interceptor.exception.propagate"; + public static final String INSTRUMENT_ENGINE_ASM = "ASM"; @Value("${profiler.instrument.engine}") @@ -58,7 +59,7 @@ public class DefaultInstrumentConfig implements InstrumentConfig { @Value("${profiler.applicationservertype}") private String applicationServerType = null; - @Value("${" + DefaultProfilerConfig.PROFILER_INTERCEPTOR_EXCEPTION_PROPAGATE + "}") + @Value("${" + PROFILER_INTERCEPTOR_EXCEPTION_PROPAGATE + "}") private boolean propagateInterceptorException = false; @Value("${profiler.lambda.expressions.support}") diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/lambda/LambdaBytecodeLogger.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/lambda/LambdaBytecodeLogger.java index 299f5cbcbccb..f2c4cc32c9e2 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/lambda/LambdaBytecodeLogger.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/lambda/LambdaBytecodeLogger.java @@ -16,8 +16,9 @@ package com.navercorp.pinpoint.profiler.instrument.lambda; -import com.navercorp.pinpoint.bootstrap.BootLogger; import com.navercorp.pinpoint.bootstrap.instrument.lambda.LambdaBytecodeHandler; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.Arrays; import java.util.Objects; @@ -26,12 +27,12 @@ * @author Woonduk Kang(emeroad) */ public class LambdaBytecodeLogger implements LambdaBytecodeHandler { - private final BootLogger logger; + private final Logger logger; private final LambdaBytecodeHandler delegate; public LambdaBytecodeLogger(LambdaBytecodeHandler delegate) { this.delegate = Objects.requireNonNull(delegate, "delegate"); - this.logger = BootLogger.getLogger(this.getClass()); + this.logger = LogManager.getLogger(this.getClass()); } @Override diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/scanner/JarFileRepository.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/scanner/JarFileRepository.java index f7eb4d7213f9..0e1013e2225d 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/scanner/JarFileRepository.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/instrument/scanner/JarFileRepository.java @@ -17,6 +17,7 @@ package com.navercorp.pinpoint.profiler.instrument.scanner; import java.io.InputStream; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -28,15 +29,15 @@ public class JarFileRepository { private final JarFileScanner[] scanners; - public JarFileRepository(List jarFilePathList) { + public JarFileRepository(List jarFilePathList) { this.scanners = newJarScanner(jarFilePathList); } - private JarFileScanner[] newJarScanner(List jarFilePathList) { + private JarFileScanner[] newJarScanner(List jarFilePathList) { Objects.requireNonNull(jarFilePathList, "jarFilePathList"); final List jarFileList = new ArrayList<>(jarFilePathList.size()); - for (String jarFilePath : jarFilePathList) { + for (Path jarFilePath : jarFilePathList) { JarFileScanner jarFileScanner = JarFileScanner.of(jarFilePath); jarFileList.add(jarFileScanner); } diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentIdResolver.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentIdResolver.java similarity index 76% rename from agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentIdResolver.java rename to agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentIdResolver.java index 43399982e952..31788a4a0c4f 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentIdResolver.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentIdResolver.java @@ -14,10 +14,12 @@ * limitations under the License. */ -package com.navercorp.pinpoint.bootstrap; +package com.navercorp.pinpoint.profiler.name; -import com.navercorp.pinpoint.bootstrap.util.StringUtils; -import com.navercorp.pinpoint.common.util.AgentUuidUtils; +import com.navercorp.pinpoint.common.profiler.name.AgentUuidUtils; +import com.navercorp.pinpoint.common.util.StringUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.List; import java.util.Objects; @@ -27,7 +29,7 @@ * @author Woonduk Kang(emeroad) */ public class AgentIdResolver { - private final BootLogger logger = BootLogger.getLogger(this.getClass()); + private final Logger logger = LogManager.getLogger(this.getClass()); private final List agentPropertyList; @@ -42,7 +44,7 @@ public AgentIds resolve() { if (StringUtils.isEmpty(agentId)) { logger.info("Failed to resolve AgentId(-Dpinpoint.agentId)"); agentId = newRandomAgentId(); - logger.info("Auto generate AgentId='" + agentId + "'"); + logger.info("Auto generate AgentId='{}'", agentId); } final String applicationName = getApplicationName(); @@ -66,12 +68,13 @@ private String newRandomAgentId() { private String getAgentId() { for (AgentProperties agentProperty : agentPropertyList) { - final String agentId = agentProperty.getAgentId(); + final AgentProperties.KeyValue keyValue = agentProperty.getAgentId(); + final String agentId = keyValue.getValue(); if (StringUtils.isEmpty(agentId)) { continue; } if (idValidator.validateAgentId(agentProperty.getType(), agentId)) { - logger.info(agentProperty.getType().getDesc() + " " + agentProperty.getAgentIdKey() + "=" + agentId); + logger.info(agentProperty.getType().getDesc() + " " + keyValue.getKey() + "=" + agentId); return agentId; } } @@ -80,9 +83,10 @@ private String getAgentId() { private String getAgentName() { for (AgentProperties agentProperty : agentPropertyList) { - final String agentName = agentProperty.getAgentName(); + final AgentProperties.KeyValue keyValue = agentProperty.getAgentName(); + final String agentName = keyValue.getValue(); if (idValidator.validateAgentName(agentProperty.getType(), agentName)) { - logger.info(agentProperty.getType().getDesc() + " " + agentProperty.getAgentNameKey() + "=" + agentName); + logger.info(agentProperty.getType().getDesc() + " " + keyValue.getKey() + "=" + agentName); return agentName; } } @@ -91,12 +95,13 @@ private String getAgentName() { private String getApplicationName() { for (AgentProperties agentProperty : agentPropertyList) { - final String applicationName = agentProperty.getApplicationName(); + final AgentProperties.KeyValue keyValue = agentProperty.getApplicationName(); + final String applicationName = keyValue.getValue(); if (StringUtils.isEmpty(applicationName)) { continue; } if (idValidator.validateApplicationName(agentProperty.getType(), applicationName)) { - logger.info(agentProperty.getType().getDesc() + " " + agentProperty.getApplicationNameKey() + "=" + applicationName); + logger.info(agentProperty.getType().getDesc() + " " + keyValue.getKey() + "=" + applicationName); return applicationName; } } diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentIdResolverBuilder.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentIdResolverBuilder.java similarity index 96% rename from agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentIdResolverBuilder.java rename to agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentIdResolverBuilder.java index b20d7e2f3b4e..1243b30a2a61 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentIdResolverBuilder.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentIdResolverBuilder.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.navercorp.pinpoint.bootstrap; +package com.navercorp.pinpoint.profiler.name; import java.util.ArrayList; import java.util.List; diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentIdSourceType.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentIdSourceType.java similarity index 97% rename from agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentIdSourceType.java rename to agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentIdSourceType.java index 87e6ecc05151..fca32d854ad9 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentIdSourceType.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentIdSourceType.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.navercorp.pinpoint.bootstrap; +package com.navercorp.pinpoint.profiler.name; import java.util.Objects; diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentIds.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentIds.java similarity index 96% rename from agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentIds.java rename to agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentIds.java index 032dbe46302f..239401ecfa10 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentIds.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentIds.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.navercorp.pinpoint.bootstrap; +package com.navercorp.pinpoint.profiler.name; import java.util.Objects; diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentProperties.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentProperties.java similarity index 52% rename from agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentProperties.java rename to agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentProperties.java index 284539b311bf..e42807039aee 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/AgentProperties.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentProperties.java @@ -14,7 +14,9 @@ * limitations under the License. */ -package com.navercorp.pinpoint.bootstrap; +package com.navercorp.pinpoint.profiler.name; + +import com.navercorp.pinpoint.common.util.StringUtils; import java.util.Objects; import java.util.function.Function; @@ -35,48 +37,53 @@ public AgentIdSourceType getType() { return type; } - public String getAgentId() { - return trimProperty(type.getAgentId()); + public KeyValue getAgentId() { + return keyValue(type.getAgentId()); } - public String getAgentIdKey() { - return type.getAgentId(); - } + public KeyValue getAgentName() { + return keyValue(type.getAgentName()); + } - public String getAgentName() { - return trimProperty(type.getAgentName()); + public KeyValue getApplicationName() { + return keyValue(type.getApplicationName()); } - public String getAgentNameKey() { - return type.getAgentName(); + private KeyValue keyValue(String key) { + String value = this.properties.apply(key); + value = StringUtils.trim(value); + return new KeyValue(key, value); } + public static class KeyValue { + private final String key; + private final String name; - public String getApplicationName() { - return trimProperty(type.getApplicationName()); - } + public KeyValue(String key, String name) { + this.key = Objects.requireNonNull(key, "key"); + this.name = name; + } - public String getApplicationNameKey() { - return type.getApplicationName(); - } + public String getKey() { + return key; + } - private String trimProperty(String key) { - return trim(this.properties.apply(key)); - } + public String getValue() { + return name; + } - private String trim(String string) { - if (string == null) { - return null; + @Override + public String toString() { + return key + "=" + name; } - return string.trim(); } @Override public String toString() { - return "AgentProperties{" + - "type=" + type + - ", properties=" + properties + + return "AgentProperties{" + + type + "=" + + properties + '}'; } } diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/IdValidator.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/IdValidator.java similarity index 90% rename from agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/IdValidator.java rename to agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/IdValidator.java index 01e270c6db6a..e4a12fec4198 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/IdValidator.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/IdValidator.java @@ -15,11 +15,13 @@ * */ -package com.navercorp.pinpoint.bootstrap; +package com.navercorp.pinpoint.profiler.name; -import com.navercorp.pinpoint.bootstrap.util.StringUtils; import com.navercorp.pinpoint.common.PinpointConstants; import com.navercorp.pinpoint.common.util.IdValidateUtils; +import com.navercorp.pinpoint.common.util.StringUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.Objects; @@ -28,7 +30,7 @@ */ public class IdValidator { - private final BootLogger logger = BootLogger.getLogger(this.getClass()); + private final Logger logger = LogManager.getLogger(this.getClass()); private static final int MAX_ID_LENGTH = PinpointConstants.AGENT_ID_MAX_LEN; diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/PluginJar.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/PluginJar.java index 15e8c6116bbb..036153bef9a5 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/PluginJar.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/plugin/PluginJar.java @@ -21,7 +21,6 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.List; import java.util.Objects; import java.util.jar.JarFile; @@ -41,15 +40,15 @@ public class PluginJar { private final JarFile jarFile; private final PluginManifest manifest; - private PluginJar(String filePath) { - Objects.requireNonNull(filePath, "filePath"); - Path path = Paths.get(filePath); + public PluginJar(Path path) { + Objects.requireNonNull(path, "path"); this.url = toURL(path); this.jarFile = createJarFile(path); this.manifest = PluginManifest.of(jarFile); } + private URL toURL(Path file) { try { return file.toUri().toURL(); @@ -67,8 +66,8 @@ private static JarFile createJarFile(Path pluginJar) { } } - public static PluginJar fromFilePath(String filePath) { - return new PluginJar(filePath); + public static PluginJar fromFilePath(Path path) { + return new PluginJar(path); } private static void verify(Path file) { diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/sender/grpc/metric/ChannelzScheduledReporterBuilder.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/sender/grpc/metric/ChannelzScheduledReporterBuilder.java index 0853785db7f3..a059c75da07a 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/sender/grpc/metric/ChannelzScheduledReporterBuilder.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/sender/grpc/metric/ChannelzScheduledReporterBuilder.java @@ -16,10 +16,11 @@ package com.navercorp.pinpoint.profiler.sender.grpc.metric; import com.google.common.annotations.VisibleForTesting; -import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.common.util.StringUtils; import java.time.Duration; import java.util.Objects; +import java.util.function.Function; /** * @author youngjin.kim2 @@ -28,8 +29,13 @@ public class ChannelzScheduledReporterBuilder { private Duration reportPeriod = Duration.ofSeconds(60); - public ChannelzScheduledReporterBuilder acceptConfig(ProfilerConfig config) { - String reportPeriodStr = Objects.requireNonNull(config, "config").getGrpcStatLoggingPeriod(); + public ChannelzScheduledReporterBuilder acceptConfig(Function config) { + Objects.requireNonNull(config, "config"); + + String reportPeriodStr = config.apply("profiler.transport.grpc.stats.logging.period"); + if (StringUtils.isEmpty(reportPeriodStr)) { + reportPeriodStr = "PT1M"; + } this.reportPeriod = Duration.parse(reportPeriodStr); return this; } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ContainerResolver.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ContainerResolver.java index faa48de94ff7..9d3824d5b75c 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ContainerResolver.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ContainerResolver.java @@ -16,8 +16,10 @@ package com.navercorp.pinpoint.profiler.util; -import com.navercorp.pinpoint.bootstrap.BootLogger; -import com.navercorp.pinpoint.bootstrap.util.StringUtils; + +import com.navercorp.pinpoint.common.util.StringUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.io.File; import java.util.Objects; @@ -32,7 +34,7 @@ public class ContainerResolver { public static final String KUBERNETES_SERVICE_HOST = "KUBERNETES_SERVICE_HOST"; public static final String DOT_DOCKER_ENV_FILEPATH = "/.dockerenv"; - private final BootLogger logger = BootLogger.getLogger(this.getClass()); + private final Logger logger = LogManager.getLogger(this.getClass()); private final Properties properties; @@ -60,11 +62,11 @@ public boolean isContainer() { private boolean readPropertyBool(String key) { String value = properties.getProperty(key); if (StringUtils.isEmpty(value)) { - logger.info("-D" + key + " found."); + logger.info("-D{} found", key); return true; } boolean boolValue = Boolean.parseBoolean(value); - logger.info("-D" + key + " found : " + value + ", resolved to " + boolValue); + logger.info("-D{} found : {}, resolved to {}", key, value, boolValue); return boolValue; } diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/config/LogDirCleanerTest.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/config/LogDirCleanerTest.java new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultTraceContextTest.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultTraceContextTest.java index 0af10b51c80a..fedd6521e468 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultTraceContextTest.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultTraceContextTest.java @@ -30,10 +30,8 @@ import com.navercorp.pinpoint.profiler.context.module.DefaultApplicationContext; import com.navercorp.pinpoint.profiler.context.provider.sampler.SamplerConfig; import com.navercorp.pinpoint.profiler.sampler.CountingSamplerFactory; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.AutoClose; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -45,8 +43,8 @@ * @author HyunGil Jeong */ public class DefaultTraceContextTest { - private final Logger logger = LogManager.getLogger(this.getClass()); + @AutoClose private DefaultApplicationContext applicationContext; @BeforeEach @@ -57,13 +55,6 @@ public void setUp() throws Exception { applicationContext.start(); } - @AfterEach - public void tearDown() throws Exception { - if (applicationContext != null) { - applicationContext.close(); - } - } - @Test public void parseTest() { String agent = "test"; @@ -72,13 +63,12 @@ public void parseTest() { TraceId traceId = new DefaultTraceId(agent, agentStartTime, agentTransactionCount); String id = traceId.getTransactionId(); - logger.debug("id={}", id); TransactionId transactionid = TransactionIdUtils.parseTransactionId(id); - Assertions.assertEquals(transactionid.getAgentId(), agent); - Assertions.assertEquals(transactionid.getAgentStartTime(), agentStartTime); - Assertions.assertEquals(transactionid.getTransactionSequence(), agentTransactionCount); + Assertions.assertEquals(agent, transactionid.getAgentId()); + Assertions.assertEquals(agentStartTime, transactionid.getAgentStartTime()); + Assertions.assertEquals(agentTransactionCount, transactionid.getTransactionSequence()); } @Test @@ -132,7 +122,8 @@ public void transactionCountTest() { final long newTransactionCount = 22L; - @SuppressWarnings("unused") final long expectedSampledNewCount = newTransactionCount / samplingRate + (newTransactionCount % samplingRate > 0 ? 1 : 0); + @SuppressWarnings("unused") + final long expectedSampledNewCount = newTransactionCount / samplingRate + (newTransactionCount % samplingRate > 0 ? 1 : 0); final long expectedUnsampledNewCount = newTransactionCount - expectedSampledNewCount; for (int i = 0; i < newTransactionCount; i++) { traceContext.newTraceObject(); @@ -163,8 +154,6 @@ public void transactionCountTest() { } public ProfilerConfig getProfilerConfig() { - ProfilerConfig profilerConfig = spy(new DefaultProfilerConfig()); - Mockito.when(profilerConfig.getStaticResourceCleanup()).thenReturn(true); - return profilerConfig; + return spy(new DefaultProfilerConfig()); } } diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/MockApplicationContextFactory.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/MockApplicationContextFactory.java index 52a37ad0aca2..db3f1e181983 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/MockApplicationContextFactory.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/MockApplicationContextFactory.java @@ -16,9 +16,10 @@ package com.navercorp.pinpoint.profiler.context; -import com.navercorp.pinpoint.bootstrap.AgentOption; -import com.navercorp.pinpoint.bootstrap.DefaultAgentOption; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; +import com.navercorp.pinpoint.profiler.AgentContextOption; +import com.navercorp.pinpoint.profiler.AgentContextOptionBuilder; +import com.navercorp.pinpoint.profiler.AgentOption; import com.navercorp.pinpoint.profiler.context.module.DefaultApplicationContext; import com.navercorp.pinpoint.profiler.context.module.ModuleFactory; import org.mockito.Mockito; @@ -40,9 +41,11 @@ public DefaultApplicationContext build(ProfilerConfig config, ModuleFactory modu String mockAgentId = "mockAgentId"; String mockAgentName = "mockAgentName"; String mockApplicationName = "mockApplicationName"; - AgentOption agentOption = new DefaultAgentOption(instrumentation, mockAgentId, mockAgentName, mockApplicationName, - config, Collections.emptyList(), Collections.emptyList()); - return new DefaultApplicationContext(agentOption, moduleFactory); + AgentOption agentOption = new AgentOption(instrumentation, config.getProperties(), + Collections.emptyMap(), null, + Collections.emptyList(), Collections.emptyList(), false); + AgentContextOption agentContextOption = AgentContextOptionBuilder.build(agentOption, mockAgentId, mockAgentName, mockApplicationName, config); + return new DefaultApplicationContext(agentContextOption, moduleFactory); } } diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/TestAgentInformation.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/TestAgentInformation.java index 672ce9ab59b0..8b35f8c1d482 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/TestAgentInformation.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/TestAgentInformation.java @@ -39,8 +39,11 @@ public class TestAgentInformation extends DefaultAgentInformation { private static final ServiceType SERVICE_TYPE = ServiceType.TEST_STAND_ALONE; private static final String JVM_VERSION = JvmUtils.getSystemProperty(SystemPropertyKey.JAVA_VERSION); private static final String AGENT_VERSION = Version.VERSION; + private static final String CLUSTER_NAMESPACE = "cluster-namespace"; public TestAgentInformation() { - super(AGENT_ID, AGENT_NAME, APPLICATION_NAME, IS_CONTAINER, System.currentTimeMillis(), PID, MACHINE_NAME, HOST_IP, SERVICE_TYPE, JVM_VERSION, AGENT_VERSION); + super(AGENT_ID, AGENT_NAME, APPLICATION_NAME, IS_CONTAINER, System.currentTimeMillis(), + PID, MACHINE_NAME, HOST_IP, SERVICE_TYPE, JVM_VERSION, AGENT_VERSION, + CLUSTER_NAMESPACE); } } diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/graph/DependencyGraph.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/graph/DependencyGraph.java index 2eeb9b622e34..04b94b24fa27 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/graph/DependencyGraph.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/graph/DependencyGraph.java @@ -21,11 +21,12 @@ import com.google.inject.Module; import com.google.inject.grapher.graphviz.GraphvizGrapher; import com.google.inject.grapher.graphviz.GraphvizModule; -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.util.CodeSourceUtils; +import com.navercorp.pinpoint.profiler.AgentContextOption; +import com.navercorp.pinpoint.profiler.AgentContextOptionBuilder; +import com.navercorp.pinpoint.profiler.AgentOption; import com.navercorp.pinpoint.profiler.context.module.DefaultApplicationContext; import com.navercorp.pinpoint.profiler.context.module.InterceptorRegistryModule; import com.navercorp.pinpoint.profiler.context.module.ModuleFactory; @@ -34,7 +35,6 @@ import com.navercorp.pinpoint.profiler.util.TestInterceptorRegistryBinder; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.mockito.Mockito; import java.io.IOException; import java.io.PrintWriter; @@ -44,7 +44,6 @@ import java.util.Collections; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; /** * @author Woonduk Kang(emeroad) @@ -78,26 +77,27 @@ public void dumpDependencyGraph() throws IOException { } private DefaultApplicationContext newApplicationContext() { - ProfilerConfig profilerConfig = spy(new DefaultProfilerConfig()); - Mockito.when(profilerConfig.getStaticResourceCleanup()).thenReturn(true); + ProfilerConfig profilerConfig = new DefaultProfilerConfig(); Instrumentation instrumentation = mock(Instrumentation.class); - AgentOption agentOption = new DefaultAgentOption(instrumentation, - "mockAgentId", "mockAgentName", "mockApplicationName", - profilerConfig, Collections.emptyList(), Collections.emptyList()); + + AgentOption agentOption = new AgentOption(instrumentation, + profilerConfig.getProperties(), Collections.emptyMap(), null, + Collections.emptyList(), Collections.emptyList(), false); InterceptorRegistryBinder interceptorRegistryBinder = new TestInterceptorRegistryBinder(); Module testInterceptorRegistryModule = InterceptorRegistryModule.wrap(interceptorRegistryBinder); ModuleFactory moduleFactory = new OverrideModuleFactory(testInterceptorRegistryModule); - return new DefaultApplicationContext(agentOption, moduleFactory); + AgentContextOption agentContextOption = AgentContextOptionBuilder.build(agentOption, + "mockAgentId", "mockAgentName", "mockApplicationName", profilerConfig); + return new DefaultApplicationContext(agentContextOption, moduleFactory); } private String currentWorkingDir() { URL location = CodeSourceUtils.getCodeLocation(Logger.class); - String dir = location.getPath(); - return dir; + return location.getPath(); } public static class Grapher { diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/module/DefaultApplicationContextTest.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/module/DefaultApplicationContextTest.java index f5b4696763aa..e152b4886897 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/module/DefaultApplicationContextTest.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/module/DefaultApplicationContextTest.java @@ -22,11 +22,12 @@ import com.google.inject.Module; import com.google.inject.Scopes; import com.google.inject.TypeLiteral; -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.profiler.AgentContextOption; +import com.navercorp.pinpoint.profiler.AgentContextOptionBuilder; import com.navercorp.pinpoint.profiler.AgentInfoSender; +import com.navercorp.pinpoint.profiler.AgentOption; import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder; import com.navercorp.pinpoint.profiler.util.TestInterceptorRegistryBinder; import org.junit.jupiter.api.Assertions; @@ -38,8 +39,6 @@ import java.util.Map; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; /** * @author Woonduk Kang(emeroad) @@ -50,8 +49,7 @@ public class DefaultApplicationContextTest { @Test public void test() { - DefaultApplicationContext applicationContext = newApplicationContext(); - try { + try (DefaultApplicationContext applicationContext = newApplicationContext()) { Injector injector = applicationContext.getInjector(); Map, Binding> bindings = injector.getBindings(); for (Map.Entry, Binding> e : bindings.entrySet()) { @@ -66,8 +64,6 @@ public void test() { AgentInfoSender instance1 = injector.getInstance(AgentInfoSender.class); AgentInfoSender instance2 = injector.getInstance(AgentInfoSender.class); Assertions.assertSame(instance1, instance2); - } finally { - applicationContext.close(); } } @@ -84,19 +80,21 @@ private boolean isPinpointBinding(Key key) { } private DefaultApplicationContext newApplicationContext() { - ProfilerConfig profilerConfig = spy(new DefaultProfilerConfig()); - when(profilerConfig.getStaticResourceCleanup()).thenReturn(true); -// when(profilerConfig.getTransportModule()).thenReturn("GRPC"); + ProfilerConfig profilerConfig = new DefaultProfilerConfig(); Instrumentation instrumentation = mock(Instrumentation.class); - AgentOption agentOption = new DefaultAgentOption(instrumentation, "mockAgentId", "mockAgentName", "mockApplicationName", - profilerConfig, Collections.emptyList(), Collections.emptyList()); + AgentOption agentOption = new AgentOption(instrumentation, + profilerConfig.getProperties(), Collections.emptyMap(), null, + Collections.emptyList(), Collections.emptyList(), false); InterceptorRegistryBinder interceptorRegistryBinder = new TestInterceptorRegistryBinder(); Module interceptorRegistryModule = InterceptorRegistryModule.wrap(interceptorRegistryBinder); ModuleFactory moduleFactory = new OverrideModuleFactory(interceptorRegistryModule); - return new DefaultApplicationContext(agentOption, moduleFactory); + AgentContextOption agentContextOption = AgentContextOptionBuilder.build(agentOption, + "mockAgentId", "mockAgentName", "mockApplicationName", + profilerConfig); + return new DefaultApplicationContext(agentContextOption, moduleFactory); } -} \ No newline at end of file +} diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/module/ModuleFactoryResolverTest.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/module/ModuleFactoryResolverTest.java index 90f38f30a7ce..d3cb676c5206 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/module/ModuleFactoryResolverTest.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/module/ModuleFactoryResolverTest.java @@ -17,7 +17,7 @@ package com.navercorp.pinpoint.profiler.context.module; import com.google.inject.Module; -import com.navercorp.pinpoint.bootstrap.AgentOption; +import com.navercorp.pinpoint.profiler.AgentContextOption; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -53,7 +53,7 @@ public void test3() { public static class TestModuleFactory implements ModuleFactory { @Override - public Module newModule(AgentOption agentOption) { + public Module newModule(AgentContextOption agentOption) { return null; } diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/module/OverrideModuleFactory.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/module/OverrideModuleFactory.java index 15da56102361..8c0dff7a2c47 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/module/OverrideModuleFactory.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/module/OverrideModuleFactory.java @@ -18,7 +18,7 @@ import com.google.inject.Module; import com.google.inject.util.Modules; -import com.navercorp.pinpoint.bootstrap.AgentOption; +import com.navercorp.pinpoint.profiler.AgentContextOption; import java.util.Objects; @@ -33,7 +33,7 @@ public OverrideModuleFactory(Module... with) { } @Override - public Module newModule(AgentOption agentOption) { + public Module newModule(AgentContextOption agentOption) { ModuleFactory moduleFactory = new ApplicationContextModuleFactory(); Module module = moduleFactory.newModule(agentOption); return Modules.override(module).with(with); diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/instrument/classloading/JarProfilerPluginClassInjectorTest.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/instrument/classloading/JarProfilerPluginClassInjectorTest.java index 4934b617e046..cdfbb22c6901 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/instrument/classloading/JarProfilerPluginClassInjectorTest.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/instrument/classloading/JarProfilerPluginClassInjectorTest.java @@ -32,7 +32,9 @@ import org.junit.jupiter.api.Test; import java.lang.reflect.Constructor; +import java.net.URI; import java.net.URL; +import java.nio.file.Paths; import java.util.Collections; import java.util.List; @@ -111,9 +113,9 @@ private Plugin getMockPlugin(String className) throws Exception { private Plugin getMockPlugin(Class clazz) throws Exception { final URL location = CodeSourceUtils.getCodeLocation(clazz); - + URI uri = location.toURI(); logger.debug("url:{}", location); - PluginJar pluginJar = PluginJar.fromFilePath(location.getFile()); + PluginJar pluginJar = PluginJar.fromFilePath(Paths.get(uri)); return new JarPlugin<>(pluginJar, Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); } diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/name/AgentIdResolverTest.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/name/AgentIdResolverTest.java new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/grpc/AgentGrpcDataSenderTestMain.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/grpc/AgentGrpcDataSenderTestMain.java index ac8ef9764f37..ac75de6a8310 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/grpc/AgentGrpcDataSenderTestMain.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/sender/grpc/AgentGrpcDataSenderTestMain.java @@ -40,12 +40,13 @@ import com.navercorp.pinpoint.profiler.monitor.metric.gc.JvmGcType; import io.grpc.NameResolverProvider; +import java.io.Closeable; import java.util.Collections; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -public class AgentGrpcDataSenderTestMain { +public class AgentGrpcDataSenderTestMain implements Closeable { private static final String AGENT_ID = "mockAgentId"; private static final String AGENT_NAME = "mockAgentName"; private static final String APPLICATION_NAME = "mockApplicationName"; @@ -82,24 +83,23 @@ public void request() throws Exception { } private AgentInfo newAgentInfo() { - AgentInformation agentInformation = new DefaultAgentInformation(AGENT_ID, AGENT_NAME, APPLICATION_NAME, true, START_TIME, 99, "", "", ServiceType.TEST_STAND_ALONE, "1.0", "1.0"); + AgentInformation agentInformation = new DefaultAgentInformation(AGENT_ID, AGENT_NAME, APPLICATION_NAME, true, START_TIME, + 99, "", "", ServiceType.TEST_STAND_ALONE, "1.0", "1.0", "cluster-namespace"); JvmInformation jvmInformation = new JvmInformation("1.0", JvmGcType.G1); ServerMetaData serverInfo = new DefaultServerMetaData("serverInfo", Collections.emptyList(), Collections.emptyMap(), Collections.emptyList()); return new AgentInfo(agentInformation, serverInfo, jvmInformation); } public static void main(String[] args) { - AgentGrpcDataSenderTestMain main = new AgentGrpcDataSenderTestMain(); - try { + try (AgentGrpcDataSenderTestMain main = new AgentGrpcDataSenderTestMain()) { main.request(); } catch (Exception e) { e.printStackTrace(); - } finally { - main.close(); } } - private void close() { + @Override + public void close() { this.scheduledExecutorService.shutdown(); } } \ No newline at end of file diff --git a/banner/src/main/resources/pinpoint-banner/banner.txt b/banner/src/main/resources/pinpoint-banner/banner.txt index be1d3166e91e..0987a5fa2f47 100644 --- a/banner/src/main/resources/pinpoint-banner/banner.txt +++ b/banner/src/main/resources/pinpoint-banner/banner.txt @@ -1,4 +1,5 @@ + 88888888ba 88 888b 88 88888888ba ,ad8888ba, 88 888b 88 888888888888 88 ,8P 88 88 `8b 88 88 ,8P d8' `8b 88 88 `8b 88 88 88aaaaaa8P' 88 88 `8b 88 88aaaaaa8P' 88 da 88 88 88 `8b 88 88 diff --git a/collector/pom.xml b/collector/pom.xml index 8c495f43450b..802fa1f5fe08 100644 --- a/collector/pom.xml +++ b/collector/pom.xml @@ -106,10 +106,6 @@ com.navercorp.pinpoint pinpoint-profiler - - com.navercorp.pinpoint - pinpoint-bootstrap - com.navercorp.pinpoint pinpoint-bootstrap-core diff --git a/commons/src/main/java/com/navercorp/pinpoint/common/util/AgentUuidUtils.java b/commons-profiler/src/main/java/com/navercorp/pinpoint/common/profiler/name/AgentUuidUtils.java similarity index 97% rename from commons/src/main/java/com/navercorp/pinpoint/common/util/AgentUuidUtils.java rename to commons-profiler/src/main/java/com/navercorp/pinpoint/common/profiler/name/AgentUuidUtils.java index 8adc4b3221b1..78ba2fa6e8cf 100644 --- a/commons/src/main/java/com/navercorp/pinpoint/common/util/AgentUuidUtils.java +++ b/commons-profiler/src/main/java/com/navercorp/pinpoint/common/profiler/name/AgentUuidUtils.java @@ -14,7 +14,9 @@ * limitations under the License. */ -package com.navercorp.pinpoint.common.util; +package com.navercorp.pinpoint.common.profiler.name; + +import com.navercorp.pinpoint.common.util.BytesUtils; import java.nio.charset.StandardCharsets; import java.util.Base64; diff --git a/commons/src/test/java/com/navercorp/pinpoint/common/util/AgentUuidUtilsTest.java b/commons-profiler/src/test/java/com/navercorp/pinpoint/common/profiler/name/AgentUuidUtilsTest.java similarity index 95% rename from commons/src/test/java/com/navercorp/pinpoint/common/util/AgentUuidUtilsTest.java rename to commons-profiler/src/test/java/com/navercorp/pinpoint/common/profiler/name/AgentUuidUtilsTest.java index 3c0499ed6c71..ee7f6b489196 100644 --- a/commons/src/test/java/com/navercorp/pinpoint/common/util/AgentUuidUtilsTest.java +++ b/commons-profiler/src/test/java/com/navercorp/pinpoint/common/profiler/name/AgentUuidUtilsTest.java @@ -14,9 +14,10 @@ * limitations under the License. */ -package com.navercorp.pinpoint.common.util; +package com.navercorp.pinpoint.common.profiler.name; import com.navercorp.pinpoint.common.PinpointConstants; +import com.navercorp.pinpoint.common.util.IdValidateUtils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test;