Search in sources :

Example 6 with MetricReporter

use of org.apache.flink.metrics.reporter.MetricReporter in project flink by apache.

the class DropwizardFlinkHistogramWrapperTest method testDropwizardHistogramWrapperReporting.

/**
	 * Tests that the DropwizardHistogramWrapper reports correct dropwizard snapshots to the
	 * ScheduledReporter.
	 */
@Test
public void testDropwizardHistogramWrapperReporting() throws Exception {
    long reportingInterval = 1000;
    long timeout = 30000;
    int size = 10;
    String histogramMetricName = "histogram";
    Configuration config = new Configuration();
    config.setString(ConfigConstants.METRICS_REPORTERS_LIST, "my_reporter");
    config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "my_reporter." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestingReporter.class.getName());
    config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "my_reporter." + ConfigConstants.METRICS_REPORTER_INTERVAL_SUFFIX, reportingInterval + " MILLISECONDS");
    MetricRegistry registry = null;
    MetricRegistryConfiguration metricRegistryConfiguration = MetricRegistryConfiguration.fromConfiguration(config);
    try {
        registry = new MetricRegistry(metricRegistryConfiguration);
        DropwizardHistogramWrapper histogramWrapper = new DropwizardHistogramWrapper(new com.codahale.metrics.Histogram(new SlidingWindowReservoir(size)));
        TaskManagerMetricGroup metricGroup = new TaskManagerMetricGroup(registry, "localhost", "tmId");
        metricGroup.histogram(histogramMetricName, histogramWrapper);
        String fullMetricName = metricGroup.getMetricIdentifier(histogramMetricName);
        assertTrue(registry.getReporters().size() == 1);
        MetricReporter reporter = registry.getReporters().get(0);
        assertTrue(reporter instanceof TestingReporter);
        TestingReporter testingReporter = (TestingReporter) reporter;
        TestingScheduledReporter scheduledReporter = testingReporter.scheduledReporter;
        // check that the metric has been registered
        assertEquals(1, testingReporter.getMetrics().size());
        for (int i = 0; i < size; i++) {
            histogramWrapper.update(i);
        }
        Future<Snapshot> snapshotFuture = scheduledReporter.getNextHistogramSnapshot(fullMetricName);
        Snapshot snapshot = snapshotFuture.get(timeout, TimeUnit.MILLISECONDS);
        assertEquals(0, snapshot.getMin());
        assertEquals((size - 1) / 2.0, snapshot.getMedian(), 0.001);
        assertEquals(size - 1, snapshot.getMax());
        assertEquals(size, snapshot.size());
        registry.unregister(histogramWrapper, "histogram", metricGroup);
        // check that the metric has been de-registered
        assertEquals(0, testingReporter.getMetrics().size());
    } finally {
        if (registry != null) {
            registry.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) MetricRegistryConfiguration(org.apache.flink.runtime.metrics.MetricRegistryConfiguration) MetricReporter(org.apache.flink.metrics.reporter.MetricReporter) Snapshot(com.codahale.metrics.Snapshot) SlidingWindowReservoir(com.codahale.metrics.SlidingWindowReservoir) Test(org.junit.Test)

Example 7 with MetricReporter

use of org.apache.flink.metrics.reporter.MetricReporter in project flink by apache.

the class MetricRegistryTest method testConfigurableDelimiterForReportersInGroup.

@Test
public void testConfigurableDelimiterForReportersInGroup() {
    Configuration config = new Configuration();
    config.setString(ConfigConstants.METRICS_REPORTERS_LIST, "test1,test2,test3,test4");
    config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "_");
    config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter8.class.getName());
    config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "-");
    config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter8.class.getName());
    config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test3." + ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "AA");
    config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test3." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter8.class.getName());
    config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test4." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter8.class.getName());
    config.setString(ConfigConstants.METRICS_SCOPE_NAMING_TM, "A.B");
    MetricRegistry registry = new MetricRegistry(MetricRegistryConfiguration.fromConfiguration(config));
    List<MetricReporter> reporters = registry.getReporters();
    //test1  reporter
    ((TestReporter8) reporters.get(0)).expectedDelimiter = '_';
    //test2 reporter
    ((TestReporter8) reporters.get(1)).expectedDelimiter = '-';
    //test3 reporter, because 'AA' - not correct delimiter
    ((TestReporter8) reporters.get(2)).expectedDelimiter = GLOBAL_DEFAULT_DELIMITER;
    //for test4 reporter use global delimiter
    ((TestReporter8) reporters.get(3)).expectedDelimiter = GLOBAL_DEFAULT_DELIMITER;
    TaskManagerMetricGroup group = new TaskManagerMetricGroup(registry, "host", "id");
    group.counter("C");
    group.close();
    registry.shutdown();
    assertEquals(4, TestReporter8.numCorrectDelimitersForRegister);
    assertEquals(4, TestReporter8.numCorrectDelimitersForUnregister);
}
Also used : Configuration(org.apache.flink.configuration.Configuration) TaskManagerMetricGroup(org.apache.flink.runtime.metrics.groups.TaskManagerMetricGroup) MetricReporter(org.apache.flink.metrics.reporter.MetricReporter) Test(org.junit.Test)

Example 8 with MetricReporter

use of org.apache.flink.metrics.reporter.MetricReporter in project flink by apache.

the class AbstractMetricGroupTest method testScopeCachingForMultipleReporters.

@Test
public void testScopeCachingForMultipleReporters() throws Exception {
    Configuration config = new Configuration();
    config.setString(ConfigConstants.METRICS_SCOPE_NAMING_TM, "A.B.C.D");
    config.setString(ConfigConstants.METRICS_REPORTERS_LIST, "test1,test2");
    config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter1.class.getName());
    config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "-");
    config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, TestReporter2.class.getName());
    config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "!");
    MetricRegistry testRegistry = new MetricRegistry(MetricRegistryConfiguration.fromConfiguration(config));
    try {
        MetricGroup tmGroup = new TaskManagerMetricGroup(testRegistry, "host", "id");
        tmGroup.counter("1");
        assertEquals("Reporters were not properly instantiated", 2, testRegistry.getReporters().size());
        for (MetricReporter reporter : testRegistry.getReporters()) {
            ScopeCheckingTestReporter typedReporter = (ScopeCheckingTestReporter) reporter;
            if (typedReporter.failureCause != null) {
                throw typedReporter.failureCause;
            }
        }
    } finally {
        testRegistry.shutdown();
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) MetricRegistryConfiguration(org.apache.flink.runtime.metrics.MetricRegistryConfiguration) MetricRegistry(org.apache.flink.runtime.metrics.MetricRegistry) MetricGroup(org.apache.flink.metrics.MetricGroup) MetricReporter(org.apache.flink.metrics.reporter.MetricReporter) Test(org.junit.Test)

Example 9 with MetricReporter

use of org.apache.flink.metrics.reporter.MetricReporter in project flink by apache.

the class MetricRegistry method register.

// ------------------------------------------------------------------------
//  Metrics (de)registration
// ------------------------------------------------------------------------
/**
	 * Registers a new {@link Metric} with this registry.
	 *
	 * @param metric      the metric that was added
	 * @param metricName  the name of the metric
	 * @param group       the group that contains the metric
	 */
public void register(Metric metric, String metricName, AbstractMetricGroup group) {
    try {
        if (reporters != null) {
            for (int i = 0; i < reporters.size(); i++) {
                MetricReporter reporter = reporters.get(i);
                if (reporter != null) {
                    FrontMetricGroup front = new FrontMetricGroup<AbstractMetricGroup<?>>(i, group);
                    reporter.notifyOfAddedMetric(metric, metricName, front);
                }
            }
        }
        if (queryService != null) {
            MetricQueryService.notifyOfAddedMetric(queryService, metric, metricName, group);
        }
        if (metric instanceof View) {
            if (viewUpdater == null) {
                viewUpdater = new ViewUpdater(executor);
            }
            viewUpdater.notifyOfAddedView((View) metric);
        }
    } catch (Exception e) {
        LOG.error("Error while registering metric.", e);
    }
}
Also used : FrontMetricGroup(org.apache.flink.runtime.metrics.groups.FrontMetricGroup) MetricReporter(org.apache.flink.metrics.reporter.MetricReporter) View(org.apache.flink.metrics.View)

Example 10 with MetricReporter

use of org.apache.flink.metrics.reporter.MetricReporter in project flink by apache.

the class MetricRegistry method unregister.

/**
	 * Un-registers the given {@link Metric} with this registry.
	 *
	 * @param metric      the metric that should be removed
	 * @param metricName  the name of the metric
	 * @param group       the group that contains the metric
	 */
public void unregister(Metric metric, String metricName, AbstractMetricGroup group) {
    try {
        if (reporters != null) {
            for (int i = 0; i < reporters.size(); i++) {
                MetricReporter reporter = reporters.get(i);
                if (reporter != null) {
                    FrontMetricGroup front = new FrontMetricGroup<AbstractMetricGroup<?>>(i, group);
                    reporter.notifyOfRemovedMetric(metric, metricName, front);
                }
            }
        }
        if (queryService != null) {
            MetricQueryService.notifyOfRemovedMetric(queryService, metric);
        }
        if (metric instanceof View) {
            if (viewUpdater != null) {
                viewUpdater.notifyOfRemovedView((View) metric);
            }
        }
    } catch (Exception e) {
        LOG.error("Error while registering metric.", e);
    }
}
Also used : FrontMetricGroup(org.apache.flink.runtime.metrics.groups.FrontMetricGroup) MetricReporter(org.apache.flink.metrics.reporter.MetricReporter) View(org.apache.flink.metrics.View)

Aggregations

MetricReporter (org.apache.flink.metrics.reporter.MetricReporter)10 Configuration (org.apache.flink.configuration.Configuration)8 Test (org.junit.Test)8 MetricRegistry (org.apache.flink.runtime.metrics.MetricRegistry)7 MetricRegistryConfiguration (org.apache.flink.runtime.metrics.MetricRegistryConfiguration)7 TaskManagerMetricGroup (org.apache.flink.runtime.metrics.groups.TaskManagerMetricGroup)6 Gauge (org.apache.flink.metrics.Gauge)3 ObjectName (javax.management.ObjectName)2 JobID (org.apache.flink.api.common.JobID)2 Counter (org.apache.flink.metrics.Counter)2 MetricGroup (org.apache.flink.metrics.MetricGroup)2 SimpleCounter (org.apache.flink.metrics.SimpleCounter)2 View (org.apache.flink.metrics.View)2 FrontMetricGroup (org.apache.flink.runtime.metrics.groups.FrontMetricGroup)2 TaskManagerJobMetricGroup (org.apache.flink.runtime.metrics.groups.TaskManagerJobMetricGroup)2 TaskMetricGroup (org.apache.flink.runtime.metrics.groups.TaskMetricGroup)2 AbstractID (org.apache.flink.util.AbstractID)2 SlidingWindowReservoir (com.codahale.metrics.SlidingWindowReservoir)1 Snapshot (com.codahale.metrics.Snapshot)1 IOException (java.io.IOException)1