Search in sources :

Example 1 with Gauge

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();
        }
    });
}
Also used : ClassLoadingMXBean(java.lang.management.ClassLoadingMXBean) Gauge(org.apache.flink.metrics.Gauge)

Example 2 with Gauge

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);
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) MemoryMXBean(java.lang.management.MemoryMXBean) MetricGroup(org.apache.flink.metrics.MetricGroup) Gauge(org.apache.flink.metrics.Gauge) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 3 with Gauge

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();
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MetricRegistryConfiguration(org.apache.flink.runtime.metrics.MetricRegistryConfiguration) Configuration(org.apache.flink.configuration.Configuration) MetricRegistry(org.apache.flink.runtime.metrics.MetricRegistry) TaskManagerMetricGroup(org.apache.flink.runtime.metrics.groups.TaskManagerMetricGroup) MetricReporter(org.apache.flink.metrics.reporter.MetricReporter) Gauge(org.apache.flink.metrics.Gauge) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) TestReporter(org.apache.flink.runtime.metrics.util.TestReporter) MBeanServerConnection(javax.management.MBeanServerConnection) Test(org.junit.Test)

Example 4 with Gauge

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();
}
Also used : MetricRegistryConfiguration(org.apache.flink.runtime.metrics.MetricRegistryConfiguration) Configuration(org.apache.flink.configuration.Configuration) MetricRegistry(org.apache.flink.runtime.metrics.MetricRegistry) TaskManagerMetricGroup(org.apache.flink.runtime.metrics.groups.TaskManagerMetricGroup) MetricReporter(org.apache.flink.metrics.reporter.MetricReporter) Gauge(org.apache.flink.metrics.Gauge) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 5 with Gauge

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());
        }
    }
}
Also used : FlinkHistogramWrapper(org.apache.flink.dropwizard.metrics.FlinkHistogramWrapper) DropwizardHistogramWrapper(org.apache.flink.dropwizard.metrics.DropwizardHistogramWrapper) Histogram(org.apache.flink.metrics.Histogram) Counter(org.apache.flink.metrics.Counter) DropwizardMeterWrapper(org.apache.flink.dropwizard.metrics.DropwizardMeterWrapper) Meter(org.apache.flink.metrics.Meter) FlinkCounterWrapper(org.apache.flink.dropwizard.metrics.FlinkCounterWrapper) FlinkMeterWrapper(org.apache.flink.dropwizard.metrics.FlinkMeterWrapper) Gauge(org.apache.flink.metrics.Gauge)

Aggregations

Gauge (org.apache.flink.metrics.Gauge)17 Counter (org.apache.flink.metrics.Counter)7 Histogram (org.apache.flink.metrics.Histogram)7 Meter (org.apache.flink.metrics.Meter)7 Test (org.junit.Test)7 MetricGroup (org.apache.flink.metrics.MetricGroup)5 ObjectName (javax.management.ObjectName)4 Configuration (org.apache.flink.configuration.Configuration)4 SimpleCounter (org.apache.flink.metrics.SimpleCounter)4 MetricRegistry (org.apache.flink.runtime.metrics.MetricRegistry)4 MetricRegistryConfiguration (org.apache.flink.runtime.metrics.MetricRegistryConfiguration)4 TaskManagerMetricGroup (org.apache.flink.runtime.metrics.groups.TaskManagerMetricGroup)4 MalformedObjectNameException (javax.management.MalformedObjectNameException)3 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)3 MetricReporter (org.apache.flink.metrics.reporter.MetricReporter)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 MBeanServer (javax.management.MBeanServer)2 Metric (org.apache.flink.metrics.Metric)2 TestingHistogram (org.apache.flink.runtime.metrics.util.TestingHistogram)2