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);
}
}
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);
}
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));
}
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);
}
}
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);
}
}
Aggregations