JStorm自定义Metric的简单步骤和原理

简单介绍

可以在JStorm的控制台看见。
在这里插入图片描述
和常见的java metrics系统一样,支持

  • Counter:累计值
  • Gauge:瞬时值
  • Histogram:瞬时值的分布

使用

public class MyBolt extends BaseBasicBolt {

    private MetricClient metricClient;
    private AsmCounter myCounter;

    @Override
    public void prepare(Map stormConf, TopologyContext context) {
        metricClient = new MetricClient(context);
        myCounter = metricClient.registerCounter("myCounter");
    }

    public void execute(Tuple input, BasicOutputCollector collector) {
        Object value = input.getValueByField("value");
        myCounter.inc();
        if(value instanceof UmetripEvent){
            System.out.println("RECEIVE");
            UmetripEvent event = (UmetripEvent)value;

        }
       //throw new FailedException("manual fail");


    }

    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("EVENT"));
    }


}

原理

首先要介绍Topology Master。
在这里插入图片描述
在没有Topology Master的情况下,所有的Task汇报心跳、发送Metrics的时候都需要写ZK,并且Storm是把Metrics直接ZK里面,这样ZK的压力更大。有了Topology Master之后,Topology Master是Task的总节点,Task汇报心跳的时候只需要向Topology Master汇报心跳,Topology Master汇总后再发送给Nimbus或者ZK,这样会使ZK的压力缩到拓扑的量级。

    //JStormMetricsReporter。当工作在Worker中时,发送给topo master。topo master会在聚合后向nimbus发送metrics数据
    //当在Nimbus/Supervisor工作时,直接传给Nimbus。
    public void uploadMetricData(WorkerUploadMetrics metrics) {
        if (inTopology) {
            //in Worker, we upload data via netty transport
            if (boltOutput != null) {
                LOG.debug("emit metrics through bolt collector.");
                ((BoltCollector) boltOutput.getDelegate()).emitCtrl(Common.TOPOLOGY_MASTER_METRICS_STREAM_ID, null,
                        new Values(JStormServerUtils.getName(host, port), metrics));
            } else if (spoutOutput != null) {
                LOG.debug("emit metrics through spout collector.");
                ((SpoutCollector) spoutOutput.getDelegate()).emitCtrl(Common.TOPOLOGY_MASTER_METRICS_STREAM_ID,
                        new Values(JStormServerUtils.getName(host, port), metrics), null);
            } else {
                LOG.warn("topology:{}, both spout/bolt collectors are null, don't know what to do...", topologyId);
            }
        } else {
            // in supervisor or nimbus, we upload metric data via thrift
            LOG.debug("emit metrics through nimbus client.");
            TopologyMetric tpMetric = MetricUtils.mkTopologyMetric();
            tpMetric.set_workerMetric(metrics.get_allMetrics());

            //UpdateEvent.pushEvent(topologyId, tpMetric);
            try {
                // push metrics via nimbus client
                if (client == null) {
                    LOG.warn("nimbus client is null...");
                    client = new NimbusClientWrapper();
                    client.init(conf);
                }
                client.getClient().uploadTopologyMetrics(topologyId, tpMetric);
            } catch (Throwable ex) {
                LOG.error("upload metrics error:", ex);
                if (client != null) {
                    client.cleanup();
                    client = null;
                }
            }
        }
        //MetricUtils.logMetrics(metrics.get_allMetrics());
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值