Skywalking-08:OAL原理——如何动态生成Class类

OAL 如何动态生成 Class

代码入口

org.apache.skywalking.oal.rt.OALRuntime#start 方法

    public void start(ClassLoader currentClassLoader) throws ModuleStartException, OALCompileException {
        if (!IS_RT_TEMP_FOLDER_INIT_COMPLETED) {
            prepareRTTempFolder();
            IS_RT_TEMP_FOLDER_INIT_COMPLETED = true;
        }

        this.currentClassLoader = currentClassLoader;
        Reader read;

        try {
            read = ResourceUtils.read(oalDefine.getConfigFile());
        } catch (FileNotFoundException e) {
            throw new ModuleStartException("Can't locate " + oalDefine.getConfigFile(), e);
        }

        OALScripts oalScripts;
        try {
            ScriptParser scriptParser = ScriptParser.createFromFile(read, oalDefine.getSourcePackage());
            // 解析oal脚本,生成OALScripts对象
            oalScripts = scriptParser.parse();
        } catch (IOException e) {
            throw new ModuleStartException("OAL script parse analysis failure.", e);
        }
        // OALScripts对象动态生成需要的类
        this.generateClassAtRuntime(oalScripts);
    }

时序图

OALRuntime-generate-class-at-runtime.sdt 该文件可以在 IDEASequence Diagram 插件中打开

请添加图片描述

案例

启动 OAP 配置中,配置下环境变量 SW_OAL_ENGINE_DEBUG=Y,这样能在工作目录下的 oal-rt 目录下找到生成的 Class 文件。

通过如下目录结构,可以看出有三种 Class

  • dispatcher :调度器,将指标对象发送 MetricsStreamProcessor (指标处理器)
  • metrics :指标类,存储指标数据
  • StorageBuilder :存储构造器,实现类 StorageBuilder 接口,提供 mapStorageData 之间互转的方法
oal-rt
├── dispatcher
│   ├── ServiceInstanceJVMClassDispatcher.class
│   └── ServiceInstanceJVMThreadDispatcher.class
└── metrics
    ├── InstanceJvmClassLoadedClassCountMetrics.class
    ├── InstanceJvmClassTotalLoadedClassCountMetrics.class
    ├── InstanceJvmClassUnloadedClassCountMetrics.class
    ├── InstanceJvmThreadDaemonCountMetrics.class
    ├── InstanceJvmThreadDeadlockedMetrics.class
    ├── InstanceJvmThreadLiveCountMetrics.class
    └── builder
        ├── InstanceJvmClassLoadedClassCountMetricsBuilder.class
        ├── InstanceJvmClassTotalLoadedClassCountMetricsBuilder.class
        ├── InstanceJvmClassUnloadedClassCountMetricsBuilder.class
        ├── InstanceJvmThreadDaemonCountMetricsBuilder.class
        ├── InstanceJvmThreadDeadlockedMetricsBuilder.class
        └── InstanceJvmThreadLiveCountMetricsBuilder.class

指标类

package org.apache.skywalking.oap.server.core.source.oal.rt.metrics;

import org.apache.skywalking.oap.server.core.analysis.Stream;
import org.apache.skywalking.oap.server.core.analysis.metrics.LongAvgMetrics;
import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
import org.apache.skywalking.oap.server.core.analysis.metrics.MetricsMetaInfo;
import org.apache.skywalking.oap.server.core.analysis.metrics.WithMetadata;
import org.apache.skywalking.oap.server.core.analysis.worker.MetricsStreamProcessor;
import org.apache.skywalking.oap.server.core.remote.grpc.proto.RemoteData;
import org.apache.skywalking.oap.server.core.remote.grpc.proto.RemoteData.Builder;
import org.apache.skywalking.oap.server.core.source.oal.rt.metrics.builder.InstanceJvmClassLoadedClassCountMetricsBuilder;
import org.apache.skywalking.oap.server.core.storage.annotation.Column;

@Stream(
    name = "instance_jvm_class_loaded_class_count",
    scopeId = 11000,
    builder = InstanceJvmClassLoadedClassCountMetricsBuilder.class,
    processor = MetricsStreamProcessor.class
)
public class InstanceJvmClassLoadedClassCountMetrics extends LongAvgMetrics implements WithMetadata {
    @Column(
        columnName = "entity_id",
        length = 512
    )
    private String entityId;
    @Column(
        columnName = "service_id",
        length = 256
    )
    private String serviceId;

    public InstanceJvmClassLoadedClassCountMetrics() {
    }

    public String getEntityId() {
        return this.entityId;
    }

    public void setEntityId(String var1) {
        this.entityId = var1;
    }

    public String getServiceId() {
        return this.serviceId;
    }

    public void setServiceId(String var1) {
        this.serviceId = var1;
    }

    public String id() {
        String var1 = String.valueOf(this.getTimeBucket());
        var1 = String.valueOf(var1).concat(String.valueOf("_" + this.entityId));
        return var1;
    }

    public int hashCode() {
        byte var1 = 17;
        int var2 = 31 * var1 + this.entityId.hashCode();
        var2 = 31 * var2 + (int)this.getTimeBucket();
        return var2;
    }

    public int remoteHashCode() {
        byte var1 = 17;
        int var2 = 31 * var1 + this.entityId.hashCode();
        return var2;
    }

    public boolean equals(Object var1) {
        if (this == var1) {
            return true;
        } else if (var1 == null) {
            return false;
        } else if (this.getClass() != var1.getClass()) {
            return false;
        } else {
            InstanceJvmClassLoadedClassCountMetrics var2 = (InstanceJvmClassLoadedClassCountMetrics)var1;
            if (!this.entityId.equals(var2.entityId)) {
                return false;
            } else {
                return this.getTimeBucket() == var2.getTimeBucket();
            }
        }
    }

    public Builder serialize() {
        Builder var1 = RemoteData.newBuilder();
        var1.addDataStrings(this.getEntityId());
        var1.addDataStrings(this.getServiceId());
        var1.addDataLongs(this.getSummation());
        var1.addDataLongs(this.getCount());
        var1.addDataLongs(this.getValue());
        var1.addDataLongs(this.getTimeBucket());
        return var1;
    }

    public void deserialize(RemoteData var1) {
        this.setEntityId(var1.getDataStrings(0));
        this.setServiceId(var1.getDataStrings(1));
        this.setSummation(var1.getDataLongs(0));
        this.setCount(var1.getDataLongs(1));
        this.setValue(var1.getDataLongs(2));
        this.setTimeBucket(var1.getDataLongs(3));
    }

    public MetricsMetaInfo getMeta() {
        return new MetricsMetaInfo("instance_jvm_class_loaded_class_count", 11000, this.entityId);
    }

    public Metrics toHour() {
        InstanceJvmClassLoadedClassCountMetrics var1 = new InstanceJvmClassLoadedClassCountMetrics();
        var1.setEntityId(this.getEntityId());
        var1.setServiceId(this.getServiceId());
        var1.setSummation(this.getSummation());
        var1.setCount(this.getCount());
        var1.setValue(this.getValue());
        var1.setTimeBucket(this.toTimeBucketInHour());
        return var1;
    }

    public Metrics toDay() {
        InstanceJvmClassLoadedClassCountMetrics var1 = new InstanceJvmClassLoadedClassCountMetrics();
        var1.setEntityId(this.getEntityId());
        var1.setServiceId(this.getServiceId());
        var1.setSummation(this.getSummation());
        var1.setCount(this.getCount());
        var1.setValue(this.getValue());
        var1.setTimeBucket(this.toTimeBucketInDay());
        return var1;
    }
}

存储构造器

package org.apache.skywalking.oap.server.core.source.oal.rt.metrics.builder;

import java.util.HashMap;
import java.util.Map;
import org.apache.skywalking.oap.server.core.source.oal.rt.metrics.InstanceJvmClassLoadedClassCountMetrics;
import org.apache.skywalking.oap.server.core.storage.StorageBuilder;
import org.apache.skywalking.oap.server.core.storage.StorageData;

public class InstanceJvmClassLoadedClassCountMetricsBuilder implements StorageBuilder {
    public InstanceJvmClassLoadedClassCountMetricsBuilder() {
    }

    public Map data2Map(StorageData var1) {
        InstanceJvmClassLoadedClassCountMetrics var2 = (InstanceJvmClassLoadedClassCountMetrics)var1;
        HashMap var3 = new HashMap();
        var3.put((Object)"entity_id", var2.getEntityId());
        var3.put((Object)"service_id", var2.getServiceId());
        var3.put((Object)"summation", new Long(var2.getSummation()));
        var3.put((Object)"count", new Long(var2.getCount()));
        var3.put((Object)"value", new Long(var2.getValue()));
        var3.put((Object)"time_bucket", new Long(var2.getTimeBucket()));
        return var3;
    }

    public StorageData map2Data(Map var1) {
        InstanceJvmClassLoadedClassCountMetrics var2 = new InstanceJvmClassLoadedClassCountMetrics();
        var2.setEntityId((String)var1.get("entity_id"));
        var2.setServiceId((String)var1.get("service_id"));
        var2.setSummation(((Number)var1.get("summation")).longValue());
        var2.setCount(((Number)var1.get("count")).longValue());
        var2.setValue(((Number)var1.get("value")).longValue());
        var2.setTimeBucket(((Number)var1.get("time_bucket")).longValue());
        return var2;
    }
}

调度器

package org.apache.skywalking.oap.server.core.source.oal.rt.dispatcher;

import org.apache.skywalking.oap.server.core.analysis.SourceDispatcher;
import org.apache.skywalking.oap.server.core.analysis.worker.MetricsStreamProcessor;
import org.apache.skywalking.oap.server.core.source.ServiceInstanceJVMClass;
import org.apache.skywalking.oap.server.core.source.Source;
import org.apache.skywalking.oap.server.core.source.oal.rt.metrics.InstanceJvmClassLoadedClassCountMetrics;
import org.apache.skywalking.oap.server.core.source.oal.rt.metrics.InstanceJvmClassTotalLoadedClassCountMetrics;
import org.apache.skywalking.oap.server.core.source.oal.rt.metrics.InstanceJvmClassUnloadedClassCountMetrics;

public class ServiceInstanceJVMClassDispatcher implements SourceDispatcher<ServiceInstanceJVMClass> {
    private void doInstanceJvmClassLoadedClassCount(ServiceInstanceJVMClass var1) {
        InstanceJvmClassLoadedClassCountMetrics var2 = new InstanceJvmClassLoadedClassCountMetrics();
        var2.setTimeBucket(var1.getTimeBucket());
        var2.setEntityId(var1.getEntityId());
        var2.setServiceId(var1.getServiceId());
        var2.combine(var1.getLoadedClassCount(), (long)1);
        MetricsStreamProcessor.getInstance().in(var2);
    }

    private void doInstanceJvmClassUnloadedClassCount(ServiceInstanceJVMClass var1) {
        InstanceJvmClassUnloadedClassCountMetrics var2 = new InstanceJvmClassUnloadedClassCountMetrics();
        var2.setTimeBucket(var1.getTimeBucket());
        var2.setEntityId(var1.getEntityId());
        var2.setServiceId(var1.getServiceId());
        var2.combine(var1.getUnloadedClassCount(), (long)1);
        MetricsStreamProcessor.getInstance().in(var2);
    }

    private void doInstanceJvmClassTotalLoadedClassCount(ServiceInstanceJVMClass var1) {
        InstanceJvmClassTotalLoadedClassCountMetrics var2 = new InstanceJvmClassTotalLoadedClassCountMetrics();
        var2.setTimeBucket(var1.getTimeBucket());
        var2.setEntityId(var1.getEntityId());
        var2.setServiceId(var1.getServiceId());
        var2.combine(var1.getTotalLoadedClassCount(), (long)1);
        MetricsStreamProcessor.getInstance().in(var2);
    }

    public void dispatch(Source var1) {
        ServiceInstanceJVMClass var2 = (ServiceInstanceJVMClass)var1;
        this.doInstanceJvmClassLoadedClassCount(var2);
        this.doInstanceJvmClassUnloadedClassCount(var2);
        this.doInstanceJvmClassTotalLoadedClassCount(var2);
    }

    public ServiceInstanceJVMClassDispatcher() {
    }
}

分享并记录所学所见

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容