Search in sources :

Example 1 with MetricGroup

use of org.apache.flink.metrics.MetricGroup 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 2 with MetricGroup

use of org.apache.flink.metrics.MetricGroup in project flink by apache.

the class RichAsyncFunctionTest method testRuntimeContext.

/**
	 * Test the set of runtime context methods in the context of a {@link RichAsyncFunction}.
	 */
@Test
public void testRuntimeContext() throws Exception {
    RichAsyncFunction<Integer, Integer> function = new RichAsyncFunction<Integer, Integer>() {

        private static final long serialVersionUID = 1707630162838967972L;

        @Override
        public void asyncInvoke(Integer input, AsyncCollector<Integer> collector) throws Exception {
        // no op
        }
    };
    final String taskName = "foobarTask";
    final MetricGroup metricGroup = mock(MetricGroup.class);
    final int numberOfParallelSubtasks = 42;
    final int indexOfSubtask = 43;
    final int attemptNumber = 1337;
    final String taskNameWithSubtask = "barfoo";
    final ExecutionConfig executionConfig = mock(ExecutionConfig.class);
    final ClassLoader userCodeClassLoader = mock(ClassLoader.class);
    RuntimeContext mockedRuntimeContext = mock(RuntimeContext.class);
    when(mockedRuntimeContext.getTaskName()).thenReturn(taskName);
    when(mockedRuntimeContext.getMetricGroup()).thenReturn(metricGroup);
    when(mockedRuntimeContext.getNumberOfParallelSubtasks()).thenReturn(numberOfParallelSubtasks);
    when(mockedRuntimeContext.getIndexOfThisSubtask()).thenReturn(indexOfSubtask);
    when(mockedRuntimeContext.getAttemptNumber()).thenReturn(attemptNumber);
    when(mockedRuntimeContext.getTaskNameWithSubtasks()).thenReturn(taskNameWithSubtask);
    when(mockedRuntimeContext.getExecutionConfig()).thenReturn(executionConfig);
    when(mockedRuntimeContext.getUserCodeClassLoader()).thenReturn(userCodeClassLoader);
    function.setRuntimeContext(mockedRuntimeContext);
    RuntimeContext runtimeContext = function.getRuntimeContext();
    assertEquals(taskName, runtimeContext.getTaskName());
    assertEquals(metricGroup, runtimeContext.getMetricGroup());
    assertEquals(numberOfParallelSubtasks, runtimeContext.getNumberOfParallelSubtasks());
    assertEquals(indexOfSubtask, runtimeContext.getIndexOfThisSubtask());
    assertEquals(attemptNumber, runtimeContext.getAttemptNumber());
    assertEquals(taskNameWithSubtask, runtimeContext.getTaskNameWithSubtasks());
    assertEquals(executionConfig, runtimeContext.getExecutionConfig());
    assertEquals(userCodeClassLoader, runtimeContext.getUserCodeClassLoader());
    try {
        runtimeContext.getDistributedCache();
        fail("Expected getDistributedCached to fail with unsupported operation exception.");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        runtimeContext.getState(new ValueStateDescriptor<>("foobar", Integer.class, 42));
        fail("Expected getState to fail with unsupported operation exception.");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        runtimeContext.getListState(new ListStateDescriptor<>("foobar", Integer.class));
        fail("Expected getListState to fail with unsupported operation exception.");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        runtimeContext.getReducingState(new ReducingStateDescriptor<>("foobar", new ReduceFunction<Integer>() {

            private static final long serialVersionUID = 2136425961884441050L;

            @Override
            public Integer reduce(Integer value1, Integer value2) throws Exception {
                return value1;
            }
        }, Integer.class));
        fail("Expected getReducingState to fail with unsupported operation exception.");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        runtimeContext.getFoldingState(new FoldingStateDescriptor<>("foobar", 0, new FoldFunction<Integer, Integer>() {

            @Override
            public Integer fold(Integer accumulator, Integer value) throws Exception {
                return accumulator;
            }
        }, Integer.class));
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        runtimeContext.getMapState(new MapStateDescriptor<>("foobar", Integer.class, String.class));
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        runtimeContext.addAccumulator("foobar", new Accumulator<Integer, Integer>() {

            private static final long serialVersionUID = -4673320336846482358L;

            @Override
            public void add(Integer value) {
            // no op
            }

            @Override
            public Integer getLocalValue() {
                return null;
            }

            @Override
            public void resetLocal() {
            }

            @Override
            public void merge(Accumulator<Integer, Integer> other) {
            }

            @Override
            public Accumulator<Integer, Integer> clone() {
                return null;
            }
        });
        fail("Expected addAccumulator to fail with unsupported operation exception.");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        runtimeContext.getAccumulator("foobar");
        fail("Expected getAccumulator to fail with unsupported operation exception.");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        runtimeContext.getAllAccumulators();
        fail("Expected getAllAccumulators to fail with unsupported operation exception.");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        runtimeContext.getIntCounter("foobar");
        fail("Expected getIntCounter to fail with unsupported operation exception.");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        runtimeContext.getLongCounter("foobar");
        fail("Expected getLongCounter to fail with unsupported operation exception.");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        runtimeContext.getDoubleCounter("foobar");
        fail("Expected getDoubleCounter to fail with unsupported operation exception.");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        runtimeContext.getHistogram("foobar");
        fail("Expected getHistogram to fail with unsupported operation exception.");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        runtimeContext.hasBroadcastVariable("foobar");
        fail("Expected hasBroadcastVariable to fail with unsupported operation exception.");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        runtimeContext.getBroadcastVariable("foobar");
        fail("Expected getBroadcastVariable to fail with unsupported operation exception.");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        runtimeContext.getBroadcastVariableWithInitializer("foobar", new BroadcastVariableInitializer<Object, Object>() {

            @Override
            public Object initializeBroadcastVariable(Iterable<Object> data) {
                return null;
            }
        });
        fail("Expected getBroadcastVariableWithInitializer to fail with unsupported operation exception.");
    } catch (UnsupportedOperationException e) {
    // expected
    }
}
Also used : Accumulator(org.apache.flink.api.common.accumulators.Accumulator) FoldFunction(org.apache.flink.api.common.functions.FoldFunction) MetricGroup(org.apache.flink.metrics.MetricGroup) ReduceFunction(org.apache.flink.api.common.functions.ReduceFunction) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) AsyncCollector(org.apache.flink.streaming.api.functions.async.collector.AsyncCollector) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) IterationRuntimeContext(org.apache.flink.api.common.functions.IterationRuntimeContext) Test(org.junit.Test)

Example 3 with MetricGroup

use of org.apache.flink.metrics.MetricGroup in project flink by apache.

the class ScheduledDropwizardReporterTest method testMetricCleanup.

/**
	 * This test verifies that metrics are properly added and removed to/from the ScheduledDropwizardReporter and
	 * the underlying Dropwizard MetricRegistry.
	 */
@Test
public void testMetricCleanup() {
    TestingScheduledDropwizardReporter rep = new TestingScheduledDropwizardReporter();
    MetricGroup mp = new UnregisteredMetricsGroup();
    Counter c = new SimpleCounter();
    Meter m = new Meter() {

        @Override
        public void markEvent() {
        }

        @Override
        public void markEvent(long n) {
        }

        @Override
        public double getRate() {
            return 0;
        }

        @Override
        public long getCount() {
            return 0;
        }
    };
    Histogram h = new Histogram() {

        @Override
        public void update(long value) {
        }

        @Override
        public long getCount() {
            return 0;
        }

        @Override
        public HistogramStatistics getStatistics() {
            return null;
        }
    };
    Gauge g = new Gauge() {

        @Override
        public Object getValue() {
            return null;
        }
    };
    rep.notifyOfAddedMetric(c, "counter", mp);
    assertEquals(1, rep.getCounters().size());
    assertEquals(1, rep.registry.getCounters().size());
    rep.notifyOfAddedMetric(m, "meter", mp);
    assertEquals(1, rep.getMeters().size());
    assertEquals(1, rep.registry.getMeters().size());
    rep.notifyOfAddedMetric(h, "histogram", mp);
    assertEquals(1, rep.getHistograms().size());
    assertEquals(1, rep.registry.getHistograms().size());
    rep.notifyOfAddedMetric(g, "gauge", mp);
    assertEquals(1, rep.getGauges().size());
    assertEquals(1, rep.registry.getGauges().size());
    rep.notifyOfRemovedMetric(c, "counter", mp);
    assertEquals(0, rep.getCounters().size());
    assertEquals(0, rep.registry.getCounters().size());
    rep.notifyOfRemovedMetric(m, "meter", mp);
    assertEquals(0, rep.getMeters().size());
    assertEquals(0, rep.registry.getMeters().size());
    rep.notifyOfRemovedMetric(h, "histogram", mp);
    assertEquals(0, rep.getHistograms().size());
    assertEquals(0, rep.registry.getHistograms().size());
    rep.notifyOfRemovedMetric(g, "gauge", mp);
    assertEquals(0, rep.getGauges().size());
    assertEquals(0, rep.registry.getGauges().size());
}
Also used : UnregisteredMetricsGroup(org.apache.flink.metrics.groups.UnregisteredMetricsGroup) Histogram(org.apache.flink.metrics.Histogram) SimpleCounter(org.apache.flink.metrics.SimpleCounter) Counter(org.apache.flink.metrics.Counter) SimpleCounter(org.apache.flink.metrics.SimpleCounter) Meter(org.apache.flink.metrics.Meter) TaskMetricGroup(org.apache.flink.runtime.metrics.groups.TaskMetricGroup) TaskManagerMetricGroup(org.apache.flink.runtime.metrics.groups.TaskManagerMetricGroup) TaskManagerJobMetricGroup(org.apache.flink.runtime.metrics.groups.TaskManagerJobMetricGroup) MetricGroup(org.apache.flink.metrics.MetricGroup) Gauge(org.apache.flink.metrics.Gauge) Test(org.junit.Test)

Example 4 with MetricGroup

use of org.apache.flink.metrics.MetricGroup in project flink by apache.

the class TaskExecutorMetricsInitializer method instantiateGarbageCollectorMetrics.

private static void instantiateGarbageCollectorMetrics(MetricGroup metrics) {
    List<GarbageCollectorMXBean> garbageCollectors = ManagementFactory.getGarbageCollectorMXBeans();
    for (final GarbageCollectorMXBean garbageCollector : garbageCollectors) {
        MetricGroup gcGroup = metrics.addGroup(garbageCollector.getName());
        gcGroup.<Long, Gauge<Long>>gauge("Count", new Gauge<Long>() {

            @Override
            public Long getValue() {
                return garbageCollector.getCollectionCount();
            }
        });
        gcGroup.<Long, Gauge<Long>>gauge("Time", new Gauge<Long>() {

            @Override
            public Long getValue() {
                return garbageCollector.getCollectionTime();
            }
        });
    }
}
Also used : GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) MetricGroup(org.apache.flink.metrics.MetricGroup) Gauge(org.apache.flink.metrics.Gauge)

Example 5 with MetricGroup

use of org.apache.flink.metrics.MetricGroup in project flink by apache.

the class MetricGroupTest method sameGroupOnNameCollision.

@Test
public void sameGroupOnNameCollision() {
    GenericMetricGroup group = new GenericMetricGroup(registry, new DummyAbstractMetricGroup(registry), "somegroup");
    String groupName = "sometestname";
    MetricGroup subgroup1 = group.addGroup(groupName);
    MetricGroup subgroup2 = group.addGroup(groupName);
    assertNotNull(subgroup1);
    assertNotNull(subgroup2);
    assertTrue(subgroup1 == subgroup2);
}
Also used : MetricGroup(org.apache.flink.metrics.MetricGroup) Test(org.junit.Test)

Aggregations

MetricGroup (org.apache.flink.metrics.MetricGroup)23 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 Gauge (org.apache.flink.metrics.Gauge)5 UnregisteredMetricsGroup (org.apache.flink.metrics.groups.UnregisteredMetricsGroup)5 List (java.util.List)4 Map (java.util.Map)4 TaskInfo (org.apache.flink.api.common.TaskInfo)4 RuntimeUDFContext (org.apache.flink.api.common.functions.util.RuntimeUDFContext)4 Configuration (org.apache.flink.configuration.Configuration)4 HashMap (java.util.HashMap)3 InvalidProgramException (org.apache.flink.api.common.InvalidProgramException)3 MetricRegistry (org.apache.flink.runtime.metrics.MetricRegistry)3 MetricRegistryConfiguration (org.apache.flink.runtime.metrics.MetricRegistryConfiguration)3 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)2 MemoryMXBean (java.lang.management.MemoryMXBean)2 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)2 RuntimeContext (org.apache.flink.api.common.functions.RuntimeContext)2 Counter (org.apache.flink.metrics.Counter)2 Histogram (org.apache.flink.metrics.Histogram)2