Search in sources :

Example 6 with IMetric

use of org.apache.storm.metric.api.IMetric in project storm by apache.

the class Executor method metricsTick.

public void metricsTick(Task taskData, TupleImpl tuple) {
    try {
        Integer interval = tuple.getInteger(0);
        int taskId = taskData.getTaskId();
        Map<Integer, Map<String, IMetric>> taskToMetricToRegistry = intervalToTaskToMetricToRegistry.get(interval);
        Map<String, IMetric> nameToRegistry = null;
        if (taskToMetricToRegistry != null) {
            nameToRegistry = taskToMetricToRegistry.get(taskId);
        }
        if (nameToRegistry != null) {
            IMetricsConsumer.TaskInfo taskInfo = new IMetricsConsumer.TaskInfo(hostname, workerTopologyContext.getThisWorkerPort(), componentId, taskId, Time.currentTimeSecs(), interval);
            List<IMetricsConsumer.DataPoint> dataPoints = new ArrayList<>();
            for (Map.Entry<String, IMetric> entry : nameToRegistry.entrySet()) {
                IMetric metric = entry.getValue();
                Object value = metric.getValueAndReset();
                if (value != null) {
                    IMetricsConsumer.DataPoint dataPoint = new IMetricsConsumer.DataPoint(entry.getKey(), value);
                    dataPoints.add(dataPoint);
                }
            }
            if (!dataPoints.isEmpty()) {
                sendUnanchored(taskData, Constants.METRICS_STREAM_ID, new Values(taskInfo, dataPoints), executorTransfer);
            }
        }
    } catch (Exception e) {
        throw Utils.wrapInRuntime(e);
    }
}
Also used : IMetric(org.apache.storm.metric.api.IMetric) ArrayList(java.util.ArrayList) Values(org.apache.storm.tuple.Values) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) IMetricsConsumer(org.apache.storm.metric.api.IMetricsConsumer) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with IMetric

use of org.apache.storm.metric.api.IMetric in project storm by apache.

the class SystemBolt method prepare.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void prepare(final Map topoConf, TopologyContext context, OutputCollector collector) {
    if (_prepareWasCalled && !"local".equals(topoConf.get(Config.STORM_CLUSTER_MODE))) {
        throw new RuntimeException("A single worker should have 1 SystemBolt instance.");
    }
    _prepareWasCalled = true;
    int bucketSize = Utils.getInt(topoConf.get(Config.TOPOLOGY_BUILTIN_METRICS_BUCKET_SIZE_SECS));
    final RuntimeMXBean jvmRT = ManagementFactory.getRuntimeMXBean();
    context.registerMetric("uptimeSecs", new IMetric() {

        @Override
        public Object getValueAndReset() {
            return jvmRT.getUptime() / 1000.0;
        }
    }, bucketSize);
    context.registerMetric("startTimeSecs", new IMetric() {

        @Override
        public Object getValueAndReset() {
            return jvmRT.getStartTime() / 1000.0;
        }
    }, bucketSize);
    context.registerMetric("newWorkerEvent", new IMetric() {

        boolean doEvent = true;

        @Override
        public Object getValueAndReset() {
            if (doEvent) {
                doEvent = false;
                return 1;
            } else
                return 0;
        }
    }, bucketSize);
    final MemoryMXBean jvmMemRT = ManagementFactory.getMemoryMXBean();
    context.registerMetric("memory/heap", new MemoryUsageMetric(jvmMemRT::getHeapMemoryUsage), bucketSize);
    context.registerMetric("memory/nonHeap", new MemoryUsageMetric(jvmMemRT::getNonHeapMemoryUsage), bucketSize);
    for (GarbageCollectorMXBean b : ManagementFactory.getGarbageCollectorMXBeans()) {
        context.registerMetric("GC/" + b.getName().replaceAll("\\W", ""), new GarbageCollectorMetric(b), bucketSize);
    }
    registerMetrics(context, (Map<String, String>) topoConf.get(Config.WORKER_METRICS), bucketSize, topoConf);
    registerMetrics(context, (Map<String, String>) topoConf.get(Config.TOPOLOGY_WORKER_METRICS), bucketSize, topoConf);
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) IMetric(org.apache.storm.metric.api.IMetric) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean)

Example 8 with IMetric

use of org.apache.storm.metric.api.IMetric in project storm by apache.

the class GlobalCountBolt method prepare.

@Override
public void prepare(Map config, TopologyContext context) {
    globalCount = 0;
    globalCountDiff = 0;
    lastMetricsTime = System.nanoTime();
    context.registerMetric("GlobalMessageCount", new IMetric() {

        @Override
        public Object getValueAndReset() {
            long now = System.nanoTime();
            long millis = (now - lastMetricsTime) / 1000000;
            throughput = globalCountDiff / millis * 1000;
            Map values = new HashMap();
            values.put("global_count", globalCount);
            values.put("throughput", throughput);
            lastMetricsTime = now;
            globalCountDiff = 0;
            return values;
        }
    }, (Integer) config.get(Config.TOPOLOGY_BUILTIN_METRICS_BUCKET_SIZE_SECS));
}
Also used : HashMap(java.util.HashMap) IMetric(org.apache.storm.metric.api.IMetric) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with IMetric

use of org.apache.storm.metric.api.IMetric in project storm by apache.

the class ShellBolt method handleMetrics.

private void handleMetrics(ShellMsg shellMsg) {
    //get metric name
    String name = shellMsg.getMetricName();
    if (name.isEmpty()) {
        throw new RuntimeException("Receive Metrics name is empty");
    }
    //get metric by name
    IMetric iMetric = _context.getRegisteredMetricByName(name);
    if (iMetric == null) {
        throw new RuntimeException("Could not find metric by name[" + name + "] ");
    }
    if (!(iMetric instanceof IShellMetric)) {
        throw new RuntimeException("Metric[" + name + "] is not IShellMetric, can not call by RPC");
    }
    IShellMetric iShellMetric = (IShellMetric) iMetric;
    //call updateMetricFromRPC with params
    Object paramsObj = shellMsg.getMetricParams();
    try {
        iShellMetric.updateMetricFromRPC(paramsObj);
    } catch (RuntimeException re) {
        throw re;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : IMetric(org.apache.storm.metric.api.IMetric) IShellMetric(org.apache.storm.metric.api.rpc.IShellMetric) ReportedFailedException(org.apache.storm.topology.ReportedFailedException)

Example 10 with IMetric

use of org.apache.storm.metric.api.IMetric in project storm by apache.

the class BuiltinMetricsUtil method registerQueueMetrics.

public static void registerQueueMetrics(Map queues, Map stormConf, TopologyContext context) {
    for (Object o : queues.entrySet()) {
        Map.Entry entry = (Map.Entry) o;
        String name = "__" + entry.getKey();
        IMetric metric = new StateMetric((IStatefulObject) entry.getValue());
        registerMetric(name, metric, stormConf, context);
    }
}
Also used : StateMetric(org.apache.storm.metric.api.StateMetric) IMetric(org.apache.storm.metric.api.IMetric) IStatefulObject(org.apache.storm.metric.api.IStatefulObject) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

IMetric (org.apache.storm.metric.api.IMetric)11 HashMap (java.util.HashMap)7 Map (java.util.Map)7 IStatefulObject (org.apache.storm.metric.api.IStatefulObject)2 IShellMetric (org.apache.storm.metric.api.rpc.IShellMetric)2 IOException (java.io.IOException)1 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)1 MemoryMXBean (java.lang.management.MemoryMXBean)1 RuntimeMXBean (java.lang.management.RuntimeMXBean)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 IMetricsConsumer (org.apache.storm.metric.api.IMetricsConsumer)1 StateMetric (org.apache.storm.metric.api.StateMetric)1 MultiCountStatAndMetric (org.apache.storm.metric.internal.MultiCountStatAndMetric)1 MultiLatencyStatAndMetric (org.apache.storm.metric.internal.MultiLatencyStatAndMetric)1 ReportedFailedException (org.apache.storm.topology.ReportedFailedException)1 Values (org.apache.storm.tuple.Values)1