use of org.apache.flink.metrics.Gauge in project flink by apache.
the class TaskExecutorMetricsInitializer method instantiateClassLoaderMetrics.
private static void instantiateClassLoaderMetrics(MetricGroup metrics) {
final ClassLoadingMXBean mxBean = ManagementFactory.getClassLoadingMXBean();
metrics.<Long, Gauge<Long>>gauge("ClassesLoaded", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getTotalLoadedClassCount();
}
});
metrics.<Long, Gauge<Long>>gauge("ClassesUnloaded", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getUnloadedClassCount();
}
});
}
use of org.apache.flink.metrics.Gauge in project flink by apache.
the class TaskExecutorMetricsInitializer method instantiateMemoryMetrics.
private static void instantiateMemoryMetrics(MetricGroup metrics) {
final MemoryMXBean mxBean = ManagementFactory.getMemoryMXBean();
MetricGroup heap = metrics.addGroup("Heap");
heap.<Long, Gauge<Long>>gauge("Used", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getUsed();
}
});
heap.<Long, Gauge<Long>>gauge("Committed", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getCommitted();
}
});
heap.<Long, Gauge<Long>>gauge("Max", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getMax();
}
});
MetricGroup nonHeap = metrics.addGroup("NonHeap");
nonHeap.<Long, Gauge<Long>>gauge("Used", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getNonHeapMemoryUsage().getUsed();
}
});
nonHeap.<Long, Gauge<Long>>gauge("Committed", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getNonHeapMemoryUsage().getCommitted();
}
});
nonHeap.<Long, Gauge<Long>>gauge("Max", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getNonHeapMemoryUsage().getMax();
}
});
final MBeanServer con = ManagementFactory.getPlatformMBeanServer();
final String directBufferPoolName = "java.nio:type=BufferPool,name=direct";
try {
final ObjectName directObjectName = new ObjectName(directBufferPoolName);
MetricGroup direct = metrics.addGroup("Direct");
direct.<Long, Gauge<Long>>gauge("Count", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, directObjectName, "Count", -1L));
direct.<Long, Gauge<Long>>gauge("MemoryUsed", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, directObjectName, "MemoryUsed", -1L));
direct.<Long, Gauge<Long>>gauge("TotalCapacity", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, directObjectName, "TotalCapacity", -1L));
} catch (MalformedObjectNameException e) {
LOG.warn("Could not create object name {}.", directBufferPoolName, e);
}
final String mappedBufferPoolName = "java.nio:type=BufferPool,name=mapped";
try {
final ObjectName mappedObjectName = new ObjectName(mappedBufferPoolName);
MetricGroup mapped = metrics.addGroup("Mapped");
mapped.<Long, Gauge<Long>>gauge("Count", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, mappedObjectName, "Count", -1L));
mapped.<Long, Gauge<Long>>gauge("MemoryUsed", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, mappedObjectName, "MemoryUsed", -1L));
mapped.<Long, Gauge<Long>>gauge("TotalCapacity", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, mappedObjectName, "TotalCapacity", -1L));
} catch (MalformedObjectNameException e) {
LOG.warn("Could not create object name {}.", mappedBufferPoolName, e);
}
}
use of org.apache.flink.metrics.Gauge in project flink by apache.
the class JMXReporterTest method testJMXAvailability.
/**
* Verifies that we can connect to multiple JMXReporters running on the same machine.
*
* @throws Exception
*/
@Test
public void testJMXAvailability() throws Exception {
Configuration cfg = new Configuration();
cfg.setString(ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter.class.getName());
cfg.setString(ConfigConstants.METRICS_REPORTERS_LIST, "test1,test2");
cfg.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, JMXReporter.class.getName());
cfg.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1.port", "9040-9055");
cfg.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, JMXReporter.class.getName());
cfg.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2.port", "9040-9055");
MetricRegistry reg = new MetricRegistry(MetricRegistryConfiguration.fromConfiguration(cfg));
TaskManagerMetricGroup mg = new TaskManagerMetricGroup(reg, "host", "tm");
List<MetricReporter> reporters = reg.getReporters();
assertTrue(reporters.size() == 2);
MetricReporter rep1 = reporters.get(0);
MetricReporter rep2 = reporters.get(1);
Gauge<Integer> g1 = new Gauge<Integer>() {
@Override
public Integer getValue() {
return 1;
}
};
Gauge<Integer> g2 = new Gauge<Integer>() {
@Override
public Integer getValue() {
return 2;
}
};
rep1.notifyOfAddedMetric(g1, "rep1", new FrontMetricGroup<>(0, new TaskManagerMetricGroup(reg, "host", "tm")));
rep2.notifyOfAddedMetric(g2, "rep2", new FrontMetricGroup<>(1, new TaskManagerMetricGroup(reg, "host", "tm")));
ObjectName objectName1 = new ObjectName(JMX_DOMAIN_PREFIX + "taskmanager.rep1", JMXReporter.generateJmxTable(mg.getAllVariables()));
ObjectName objectName2 = new ObjectName(JMX_DOMAIN_PREFIX + "taskmanager.rep2", JMXReporter.generateJmxTable(mg.getAllVariables()));
JMXServiceURL url1 = new JMXServiceURL("service:jmx:rmi://localhost:" + ((JMXReporter) rep1).getPort() + "/jndi/rmi://localhost:" + ((JMXReporter) rep1).getPort() + "/jmxrmi");
JMXConnector jmxCon1 = JMXConnectorFactory.connect(url1);
MBeanServerConnection mCon1 = jmxCon1.getMBeanServerConnection();
assertEquals(1, mCon1.getAttribute(objectName1, "Value"));
assertEquals(2, mCon1.getAttribute(objectName2, "Value"));
jmxCon1.close();
JMXServiceURL url2 = new JMXServiceURL("service:jmx:rmi://localhost:" + ((JMXReporter) rep2).getPort() + "/jndi/rmi://localhost:" + ((JMXReporter) rep2).getPort() + "/jmxrmi");
JMXConnector jmxCon2 = JMXConnectorFactory.connect(url2);
MBeanServerConnection mCon2 = jmxCon2.getMBeanServerConnection();
assertEquals(1, mCon2.getAttribute(objectName1, "Value"));
assertEquals(2, mCon2.getAttribute(objectName2, "Value"));
rep1.notifyOfRemovedMetric(g1, "rep1", null);
rep1.notifyOfRemovedMetric(g2, "rep2", null);
jmxCon2.close();
rep1.close();
rep2.close();
mg.close();
reg.shutdown();
}
use of org.apache.flink.metrics.Gauge in project flink by apache.
the class JMXReporterTest method testPortConflictHandling.
/**
* Verifies that multiple JMXReporters can be started on the same machine and register metrics at the MBeanServer.
*
* @throws Exception if the attribute/mbean could not be found or the test is broken
*/
@Test
public void testPortConflictHandling() throws Exception {
Configuration cfg = new Configuration();
cfg.setString(ConfigConstants.METRICS_REPORTERS_LIST, "test1,test2");
cfg.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, JMXReporter.class.getName());
cfg.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1.port", "9020-9035");
cfg.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, JMXReporter.class.getName());
cfg.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2.port", "9020-9035");
MetricRegistry reg = new MetricRegistry(MetricRegistryConfiguration.fromConfiguration(cfg));
TaskManagerMetricGroup mg = new TaskManagerMetricGroup(reg, "host", "tm");
List<MetricReporter> reporters = reg.getReporters();
assertTrue(reporters.size() == 2);
MetricReporter rep1 = reporters.get(0);
MetricReporter rep2 = reporters.get(1);
Gauge<Integer> g1 = new Gauge<Integer>() {
@Override
public Integer getValue() {
return 1;
}
};
Gauge<Integer> g2 = new Gauge<Integer>() {
@Override
public Integer getValue() {
return 2;
}
};
rep1.notifyOfAddedMetric(g1, "rep1", new FrontMetricGroup<>(0, new TaskManagerMetricGroup(reg, "host", "tm")));
rep2.notifyOfAddedMetric(g2, "rep2", new FrontMetricGroup<>(0, new TaskManagerMetricGroup(reg, "host", "tm")));
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName1 = new ObjectName(JMX_DOMAIN_PREFIX + "taskmanager.rep1", JMXReporter.generateJmxTable(mg.getAllVariables()));
ObjectName objectName2 = new ObjectName(JMX_DOMAIN_PREFIX + "taskmanager.rep2", JMXReporter.generateJmxTable(mg.getAllVariables()));
assertEquals(1, mBeanServer.getAttribute(objectName1, "Value"));
assertEquals(2, mBeanServer.getAttribute(objectName2, "Value"));
rep1.notifyOfRemovedMetric(g1, "rep1", null);
rep1.notifyOfRemovedMetric(g2, "rep2", null);
mg.close();
reg.shutdown();
}
use of org.apache.flink.metrics.Gauge in project flink by apache.
the class ScheduledDropwizardReporter method notifyOfAddedMetric.
// ------------------------------------------------------------------------
// adding / removing metrics
// ------------------------------------------------------------------------
@Override
public void notifyOfAddedMetric(Metric metric, String metricName, MetricGroup group) {
final String fullName = group.getMetricIdentifier(metricName, this);
synchronized (this) {
if (metric instanceof Counter) {
counters.put((Counter) metric, fullName);
registry.register(fullName, new FlinkCounterWrapper((Counter) metric));
} else if (metric instanceof Gauge) {
gauges.put((Gauge<?>) metric, fullName);
registry.register(fullName, FlinkGaugeWrapper.fromGauge((Gauge<?>) metric));
} else if (metric instanceof Histogram) {
Histogram histogram = (Histogram) metric;
histograms.put(histogram, fullName);
if (histogram instanceof DropwizardHistogramWrapper) {
registry.register(fullName, ((DropwizardHistogramWrapper) histogram).getDropwizardHistogram());
} else {
registry.register(fullName, new FlinkHistogramWrapper(histogram));
}
} else if (metric instanceof Meter) {
Meter meter = (Meter) metric;
meters.put(meter, fullName);
if (meter instanceof DropwizardMeterWrapper) {
registry.register(fullName, ((DropwizardMeterWrapper) meter).getDropwizardMeter());
} else {
registry.register(fullName, new FlinkMeterWrapper(meter));
}
} else {
log.warn("Cannot add metric of type {}. This indicates that the reporter " + "does not support this metric type.", metric.getClass().getName());
}
}
}
Aggregations