Skywalking Agent源码解读 - 从premain方法开始

文章详细介绍了SkyWalkingAgent的初始化过程,包括从MANIFEST.MF中获取入口,初始化配置,加载插件,构建类增强器以及启动服务管理。核心步骤涉及配置文件读取,插件定义加载,以及使用ByteBuddy进行字节码操作。服务管理模块通过SPI机制加载并启动多个BootService实现。

 

源代码解读的版本为8.7.0。

入口

从MANIFEST.MF文件中找到,

Premain-Class: org.apache.skywalking.apm.agent.SkyWalkingAgent

SkyWalkingAgent

premain方法

主要做了这些逻辑:

//SkyWalking agent initialized

SnifferConfigInitializer.initializeCoreConfig(agentArgs);

//SkyWalking agent initialized

pluginFinder = new PluginFinder(new PluginBootstrap().loadPlugins());

//SkyWalking agent inject bootstrap instrumentation

agentBuilder = BootstrapInstrumentBoost.inject(pluginFinder, instrumentation, agentBuilder, edgeClasses);

//SkyWalking agent open read edge in JDK 9+

agentBuilder = agentBuilder.with(new CacheableTransformerDecorator(Config.Agent.CLASS_CACHE_MODE));

// SkyWalking agent active class cache

if (Config.Agent.IS_CACHE_ENHANCED_CLASS) {

agentBuilder = agentBuilder.with(new CacheableTransformerDecorator(Config.Agent.CLASS_CACHE_MODE));

}

//Skywalking agent boot

ServiceManager.INSTANCE.boot();

1. SnifferConfigInitializer.initializeCoreConfig(agentArgs);

Java
 public static void initializeCoreConfig(String agentOptions) {
 // AGENT_SETTINGS 为Properties类变量
    AGENT_SETTINGS = new Properties();
    // 加载默认配置:config/agent.config,解析读入至AGENT_SETTINGS变量中
    try (final InputStreamReader configFileStream = loadConfig()) {
        AGENT_SETTINGS.load(configFileStream);
        for (String key : AGENT_SETTINGS.stringPropertyNames()) {
            String value = (String) AGENT_SETTINGS.get(key);
            AGENT_SETTINGS.put(key, PropertyPlaceholderHelper.INSTANCE.replacePlaceholders(value, AGENT_SETTINGS));
        }

    } catch (Exception e) {
        LOGGER.error(e, "Failed to read the config file, skywalking is going to run in default config.");
    }
// 读取可能存在的system properties,覆盖默认配置
    try {
        overrideConfigBySystemProp();
    } catch (Exception e) {
        LOGGER.error(e, "Failed to read the system properties.");
    }
// 读取agentArgs可能传入的参数,覆盖默认配置
    agentOptions = StringUtil.trim(agentOptions, ',');
    if (!StringUtil.isEmpty(agentOptions)) {
        try {
            agentOptions = agentOptions.trim();
            LOGGER.info("Agent options is {}.", agentOptions);

            overrideConfigByAgentOptions(agentOptions);
        } catch (Exception e) {
            LOGGER.error(e, "Failed to parse the agent options, val is {}.", agentOptions);
        }
    }

    initializeConfig(Config.class);
    // reconfigure logger after config initialization
    configureLogger();
    LOGGER = LogManager.getLogger(SnifferConfigInitializer.class);

    if (StringUtil.isEmpty(Config.Agent.SERVICE_NAME)) {
        throw new ExceptionInInitializerError("`agent.service_name` is missing.");
    }
    if (StringUtil.isEmpty(Config.Collector.BACKEND_SERVICE)) {
        throw new ExceptionInInitializerError("`collector.backend_service` is missing.");
    }
    if (Config.Plugin.PEER_MAX_LENGTH <= 3) {
        LOGGER.warn(
            "PEER_MAX_LENGTH configuration:{} error, the default value of 200 will be used.",
            Config.Plugin.PEER_MAX_LENGTH
        );
        Config.Plugin.PEER_MAX_LENGTH = 200;
    }

    IS_INIT_COMPLETED = true;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值