Exemplo n.º 1
0
  public DefaultAgent(
      String agentPath,
      String agentArgs,
      Instrumentation instrumentation,
      ProfilerConfig profilerConfig) {
    if (agentPath == null) {
      throw new NullPointerException("agentPath must not be null");
    }
    if (instrumentation == null) {
      throw new NullPointerException("instrumentation must not be null");
    }
    if (profilerConfig == null) {
      throw new NullPointerException("profilerConfig must not be null");
    }

    this.binder = new Slf4jLoggerBinder();
    bindPLoggerFactory(this.binder);

    dumpSystemProperties();
    dumpConfig(profilerConfig);

    changeStatus(AgentStatus.INITIALIZING);

    this.agentPath = agentPath;
    this.profilerConfig = profilerConfig;

    final ApplicationServerTypeResolver typeResolver =
        new ApplicationServerTypeResolver(profilerConfig.getApplicationServerType());
    if (!typeResolver.resolve()) {
      throw new PinpointException("ApplicationServerType not found.");
    }
    this.byteCodeInstrumentor = new JavaAssistByteCodeInstrumentor(this);
    if (logger.isInfoEnabled()) {
      logger.info("DefaultAgent classLoader:{}", this.getClass().getClassLoader());
    }

    final AgentInformationFactory agentInformationFactory = new AgentInformationFactory();
    this.agentInformation =
        agentInformationFactory.createAgentInformation(typeResolver.getServerType());
    logger.info("agentInformation:{}", agentInformation);

    CommandDispatcher commandDispatcher = new CommandDispatcher();
    commandDispatcher.registerCommandService(new ThreadDumpService());
    commandDispatcher.registerCommandService(new EchoService());

    this.factory = createPinpointSocketFactory(commandDispatcher);
    this.socket =
        createPinpointSocket(
            this.profilerConfig.getCollectorTcpServerIp(),
            this.profilerConfig.getCollectorTcpServerPort(),
            factory);

    this.serverMetaDataHolder = createServerMetaDataHolder();

    this.tcpDataSender = createTcpDataSender(socket);

    this.spanDataSender =
        createUdpSpanDataSender(
            this.profilerConfig.getCollectorSpanServerPort(),
            "Pinpoint-UdpSpanDataExecutor",
            this.profilerConfig.getSpanDataSenderWriteQueueSize(),
            this.profilerConfig.getSpanDataSenderSocketTimeout(),
            this.profilerConfig.getSpanDataSenderSocketSendBufferSize());
    this.statDataSender =
        createUdpStatDataSender(
            this.profilerConfig.getCollectorStatServerPort(),
            "Pinpoint-UdpStatDataExecutor",
            this.profilerConfig.getStatDataSenderWriteQueueSize(),
            this.profilerConfig.getStatDataSenderSocketTimeout(),
            this.profilerConfig.getStatDataSenderSocketSendBufferSize());

    this.traceContext = createTraceContext(agentInformation.getServerType());

    this.agentInfoSender =
        new AgentInfoSender(
            tcpDataSender,
            profilerConfig.getAgentInfoSendRetryInterval(),
            this.agentInformation,
            this.serverMetaDataHolder);

    this.agentStatMonitor =
        new AgentStatMonitor(
            this.statDataSender,
            this.agentInformation.getAgentId(),
            this.agentInformation.getStartTime());

    ClassFileRetransformer retransformer = new ClassFileRetransformer(instrumentation);
    instrumentation.addTransformer(retransformer, true);
    this.classFileTransformer =
        new ClassFileTransformerDispatcher(this, byteCodeInstrumentor, retransformer);
    instrumentation.addTransformer(this.classFileTransformer);

    preLoadClass();

    /**
     * FIXME In case of Tomcat,
     * com.baidu.oped.apm.profiler.modifier.tomcat.interceptor.CatalinaAwaitInterceptor invokes
     * start() method before entering await() method of org.apache.catalina.startup.Catalina. But
     * for other applications, it must be invoked directly.
     */
    if (typeResolver.isManuallyStartupRequired()) {
      start();
    }
  }
Exemplo n.º 2
0
  public DefaultAgent(
      AgentOption agentOption, final InterceptorRegistryBinder interceptorRegistryBinder) {
    if (agentOption == null) {
      throw new NullPointerException("agentOption must not be null");
    }
    if (agentOption.getInstrumentation() == null) {
      throw new NullPointerException("instrumentation must not be null");
    }
    if (agentOption.getProfilerConfig() == null) {
      throw new NullPointerException("profilerConfig must not be null");
    }
    if (agentOption.getServiceTypeRegistryService() == null) {
      throw new NullPointerException("serviceTypeRegistryService must not be null");
    }

    if (interceptorRegistryBinder == null) {
      throw new NullPointerException("interceptorRegistryBinder must not be null");
    }
    logger.info("AgentOption:{}", agentOption);

    this.binder = new Slf4jLoggerBinder();
    bindPLoggerFactory(this.binder);

    this.interceptorRegistryBinder = interceptorRegistryBinder;
    interceptorRegistryBinder.bind();
    this.serviceTypeRegistryService = agentOption.getServiceTypeRegistryService();

    dumpSystemProperties();
    dumpConfig(agentOption.getProfilerConfig());

    changeStatus(AgentStatus.INITIALIZING);

    this.profilerConfig = agentOption.getProfilerConfig();
    this.instrumentation = agentOption.getInstrumentation();
    this.agentOption = agentOption;
    this.classPool =
        new JavassistClassPool(interceptorRegistryBinder, agentOption.getBootStrapCoreJarPath());

    if (logger.isInfoEnabled()) {
      logger.info("DefaultAgent classLoader:{}", this.getClass().getClassLoader());
    }

    pluginContexts = loadPlugins(agentOption);

    this.classFileTransformer = new ClassFileTransformerDispatcher(this, pluginContexts);
    this.dynamicTransformService =
        new DynamicTransformService(instrumentation, classFileTransformer);

    instrumentation.addTransformer(this.classFileTransformer, true);

    String applicationServerTypeString = profilerConfig.getApplicationServerType();
    ServiceType applicationServerType =
        this.serviceTypeRegistryService.findServiceTypeByName(applicationServerTypeString);

    final ApplicationServerTypeResolver typeResolver =
        new ApplicationServerTypeResolver(
            pluginContexts, applicationServerType, profilerConfig.getApplicationTypeDetectOrder());

    final AgentInformationFactory agentInformationFactory = new AgentInformationFactory();
    this.agentInformation = agentInformationFactory.createAgentInformation(typeResolver.resolve());
    logger.info("agentInformation:{}", agentInformation);

    CommandDispatcher commandDispatcher = new CommandDispatcher();

    this.tcpDataSender = createTcpDataSender(commandDispatcher);

    this.serverMetaDataHolder = createServerMetaDataHolder();

    this.spanDataSender =
        createUdpSpanDataSender(
            this.profilerConfig.getCollectorSpanServerPort(),
            "Pinpoint-UdpSpanDataExecutor",
            this.profilerConfig.getSpanDataSenderWriteQueueSize(),
            this.profilerConfig.getSpanDataSenderSocketTimeout(),
            this.profilerConfig.getSpanDataSenderSocketSendBufferSize());
    this.statDataSender =
        createUdpStatDataSender(
            this.profilerConfig.getCollectorStatServerPort(),
            "Pinpoint-UdpStatDataExecutor",
            this.profilerConfig.getStatDataSenderWriteQueueSize(),
            this.profilerConfig.getStatDataSenderSocketTimeout(),
            this.profilerConfig.getStatDataSenderSocketSendBufferSize());

    this.traceContext = createTraceContext();

    addCommandService(commandDispatcher, traceContext);

    this.agentInfoSender =
        new AgentInfoSender.Builder(tcpDataSender, this.agentInformation)
            .sendInterval(profilerConfig.getAgentInfoSendRetryInterval())
            .build();
    this.serverMetaDataHolder.addListener(this.agentInfoSender);

    AgentStatCollectorFactory agentStatCollectorFactory =
        new AgentStatCollectorFactory(
            this.getTransactionCounter(this.traceContext), this.profilerConfig);
    this.agentStatMonitor =
        new AgentStatMonitor(
            this.statDataSender,
            this.agentInformation.getAgentId(),
            this.agentInformation.getStartTime(),
            agentStatCollectorFactory);

    InterceptorInvokerHelper.setPropagateException(
        profilerConfig.isPropagateInterceptorException());
  }