Search in sources :

Example 16 with MetricConfig

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

the class MetricRegistryImplTest method testReporterScheduling.

/**
 * Verifies that reporters implementing the Scheduled interface are regularly called to report
 * the metrics.
 */
@Test
public void testReporterScheduling() throws Exception {
    MetricConfig config = new MetricConfig();
    config.setProperty("arg1", "hello");
    config.setProperty(ConfigConstants.METRICS_REPORTER_INTERVAL_SUFFIX, "50 MILLISECONDS");
    MetricRegistryImpl registry = new MetricRegistryImpl(MetricRegistryTestUtils.defaultMetricRegistryConfiguration(), Collections.singletonList(ReporterSetup.forReporter("test", config, new TestReporter3())));
    long start = System.currentTimeMillis();
    // only start counting from now on
    TestReporter3.reportCount = 0;
    for (int x = 0; x < 10; x++) {
        Thread.sleep(100);
        int reportCount = TestReporter3.reportCount;
        long curT = System.currentTimeMillis();
        /**
         * Within a given time-frame T only T/500 reports may be triggered due to the interval
         * between reports. This value however does not not take the first triggered report into
         * account (=> +1). Furthermore we have to account for the mis-alignment between reports
         * being triggered and our time measurement (=> +1); for T=200 a total of 4-6 reports
         * may have been triggered depending on whether the end of the interval for the first
         * reports ends before or after T=50.
         */
        long maxAllowedReports = (curT - start) / 50 + 2;
        Assert.assertTrue("Too many reports were triggered.", maxAllowedReports >= reportCount);
    }
    Assert.assertTrue("No report was triggered.", TestReporter3.reportCount > 0);
    registry.shutdown().get();
}
Also used : MetricConfig(org.apache.flink.metrics.MetricConfig) MetricGroupTest(org.apache.flink.runtime.metrics.groups.MetricGroupTest) Test(org.junit.Test)

Example 17 with MetricConfig

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

the class MetricRegistryImplTest method testReporterIntervalParsingErrorFallsBackToDefaultValue.

@Test
public void testReporterIntervalParsingErrorFallsBackToDefaultValue() throws Exception {
    MetricConfig config = new MetricConfig();
    // in a prior implementation the time amount was applied even if the time unit was invalid
    // in this case this would imply using 1 SECOND as the interval (seconds is the default)
    config.setProperty(ConfigConstants.METRICS_REPORTER_INTERVAL_SUFFIX, "1 UNICORN");
    final ManuallyTriggeredScheduledExecutorService manuallyTriggeredScheduledExecutorService = new ManuallyTriggeredScheduledExecutorService();
    MetricRegistryImpl registry = new MetricRegistryImpl(MetricRegistryTestUtils.defaultMetricRegistryConfiguration(), Collections.singletonList(ReporterSetup.forReporter("test", config, new TestReporter3())), manuallyTriggeredScheduledExecutorService);
    try {
        Collection<ScheduledFuture<?>> scheduledTasks = manuallyTriggeredScheduledExecutorService.getActiveScheduledTasks();
        ScheduledFuture<?> reportTask = Iterators.getOnlyElement(scheduledTasks.iterator());
        Assert.assertEquals(MetricOptions.REPORTER_INTERVAL.defaultValue().getSeconds(), reportTask.getDelay(TimeUnit.SECONDS));
    } finally {
        registry.shutdown().get();
    }
}
Also used : MetricConfig(org.apache.flink.metrics.MetricConfig) ManuallyTriggeredScheduledExecutorService(org.apache.flink.runtime.concurrent.ManuallyTriggeredScheduledExecutorService) ScheduledFuture(java.util.concurrent.ScheduledFuture) MetricGroupTest(org.apache.flink.runtime.metrics.groups.MetricGroupTest) Test(org.junit.Test)

Example 18 with MetricConfig

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

the class MetricRegistryImplTest method testConfigurableDelimiterForReporters.

@Test
public void testConfigurableDelimiterForReporters() throws Exception {
    MetricConfig config1 = new MetricConfig();
    config1.setProperty(ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "_");
    MetricConfig config2 = new MetricConfig();
    config2.setProperty(ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "-");
    MetricConfig config3 = new MetricConfig();
    config3.setProperty(ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "AA");
    MetricRegistryImpl registry = new MetricRegistryImpl(MetricRegistryTestUtils.defaultMetricRegistryConfiguration(), Arrays.asList(ReporterSetup.forReporter("test1", config1, new TestReporter()), ReporterSetup.forReporter("test2", config2, new TestReporter()), ReporterSetup.forReporter("test3", config3, new TestReporter())));
    assertEquals(GLOBAL_DEFAULT_DELIMITER, registry.getDelimiter());
    assertEquals('_', registry.getDelimiter(0));
    assertEquals('-', registry.getDelimiter(1));
    assertEquals(GLOBAL_DEFAULT_DELIMITER, registry.getDelimiter(2));
    assertEquals(GLOBAL_DEFAULT_DELIMITER, registry.getDelimiter(3));
    assertEquals(GLOBAL_DEFAULT_DELIMITER, registry.getDelimiter(-1));
    registry.shutdown().get();
}
Also used : MetricConfig(org.apache.flink.metrics.MetricConfig) TestReporter(org.apache.flink.runtime.metrics.util.TestReporter) MetricGroupTest(org.apache.flink.runtime.metrics.groups.MetricGroupTest) Test(org.junit.Test)

Example 19 with MetricConfig

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

the class AbstractMetricGroupTest method testScopeCachingForMultipleReporters.

@Test
public void testScopeCachingForMultipleReporters() throws Exception {
    String counterName = "1";
    Configuration config = new Configuration();
    config.setString(MetricOptions.SCOPE_NAMING_TM, "A.B.C.D");
    MetricConfig metricConfig1 = new MetricConfig();
    metricConfig1.setProperty(ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "-");
    MetricConfig metricConfig2 = new MetricConfig();
    metricConfig2.setProperty(ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "!");
    config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test1." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, CollectingMetricsReporter.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, CollectingMetricsReporter.class.getName());
    config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test2." + ConfigConstants.METRICS_REPORTER_SCOPE_DELIMITER, "!");
    CollectingMetricsReporter reporter1 = new CollectingMetricsReporter(FILTER_B);
    CollectingMetricsReporter reporter2 = new CollectingMetricsReporter(FILTER_C);
    MetricRegistryImpl testRegistry = new MetricRegistryImpl(MetricRegistryTestUtils.fromConfiguration(config), Arrays.asList(ReporterSetup.forReporter("test1", metricConfig1, reporter1), ReporterSetup.forReporter("test2", metricConfig2, reporter2)));
    try {
        MetricGroup tmGroup = TaskManagerMetricGroup.createTaskManagerMetricGroup(testRegistry, "host", new ResourceID("id"));
        tmGroup.counter(counterName);
        assertEquals("Reporters were not properly instantiated", 2, testRegistry.getReporters().size());
        {
            // verify reporter1
            MetricGroupAndName nameAndGroup = reporter1.getAddedMetrics().stream().filter(nag -> nag.name.equals(counterName)).findAny().get();
            String metricName = nameAndGroup.name;
            MetricGroup group = nameAndGroup.group;
            // the first call determines which filter is applied to all future
            // calls; in
            // this case
            // no filter is used at all
            assertEquals("A-B-C-D-1", group.getMetricIdentifier(metricName));
            // from now on the scope string is cached and should not be reliant
            // on the
            // given filter
            assertEquals("A-B-C-D-1", group.getMetricIdentifier(metricName, FILTER_C));
            assertEquals("A-B-C-D-1", group.getMetricIdentifier(metricName, reporter1));
            // the metric name however is still affected by the filter as it is
            // not
            // cached
            assertEquals("A-B-C-D-4", group.getMetricIdentifier(metricName, input -> input.replace("B", "X").replace(counterName, "4")));
        }
        {
            // verify reporter2
            MetricGroupAndName nameAndGroup = reporter2.getAddedMetrics().stream().filter(nag -> nag.name.equals(counterName)).findAny().get();
            String metricName = nameAndGroup.name;
            MetricGroup group = nameAndGroup.group;
            // the first call determines which filter is applied to all future calls
            assertEquals("A!B!X!D!1", group.getMetricIdentifier(metricName, reporter2));
            // from now on the scope string is cached and should not be reliant on the given
            // filter
            assertEquals("A!B!X!D!1", group.getMetricIdentifier(metricName));
            assertEquals("A!B!X!D!1", group.getMetricIdentifier(metricName, FILTER_C));
            // the metric name however is still affected by the filter as it is not cached
            assertEquals("A!B!X!D!3", group.getMetricIdentifier(metricName, input -> input.replace("A", "X").replace(counterName, "3")));
        }
    } finally {
        testRegistry.shutdown().get();
    }
}
Also used : MetricConfig(org.apache.flink.metrics.MetricConfig) Arrays(java.util.Arrays) MetricConfig(org.apache.flink.metrics.MetricConfig) MetricRegistryImpl(org.apache.flink.runtime.metrics.MetricRegistryImpl) CharacterFilter(org.apache.flink.metrics.CharacterFilter) AtomicReference(java.util.concurrent.atomic.AtomicReference) ReporterSetup(org.apache.flink.runtime.metrics.ReporterSetup) BlockerSync(org.apache.flink.core.testutils.BlockerSync) TestingMetricRegistry(org.apache.flink.runtime.metrics.util.TestingMetricRegistry) MetricRegistry(org.apache.flink.runtime.metrics.MetricRegistry) ScopeFormat(org.apache.flink.runtime.metrics.scope.ScopeFormat) Map(java.util.Map) ConfigConstants(org.apache.flink.configuration.ConfigConstants) TestLogger(org.apache.flink.util.TestLogger) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ResourceID(org.apache.flink.runtime.clusterframework.types.ResourceID) QueryScopeInfo(org.apache.flink.runtime.metrics.dump.QueryScopeInfo) IsNot.not(org.hamcrest.core.IsNot.not) CollectingMetricsReporter(org.apache.flink.runtime.metrics.CollectingMetricsReporter) MetricRegistryTestUtils(org.apache.flink.runtime.metrics.MetricRegistryTestUtils) MetricGroupAndName(org.apache.flink.runtime.metrics.CollectingMetricsReporter.MetricGroupAndName) Configuration(org.apache.flink.configuration.Configuration) NoOpMetricRegistry(org.apache.flink.runtime.metrics.NoOpMetricRegistry) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) MetricOptions(org.apache.flink.configuration.MetricOptions) IsMapContaining(org.hamcrest.collection.IsMapContaining) MetricGroup(org.apache.flink.metrics.MetricGroup) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Configuration(org.apache.flink.configuration.Configuration) ResourceID(org.apache.flink.runtime.clusterframework.types.ResourceID) MetricGroupAndName(org.apache.flink.runtime.metrics.CollectingMetricsReporter.MetricGroupAndName) MetricGroup(org.apache.flink.metrics.MetricGroup) MetricRegistryImpl(org.apache.flink.runtime.metrics.MetricRegistryImpl) CollectingMetricsReporter(org.apache.flink.runtime.metrics.CollectingMetricsReporter) Test(org.junit.Test)

Aggregations

MetricConfig (org.apache.flink.metrics.MetricConfig)19 Test (org.junit.jupiter.api.Test)6 Test (org.junit.Test)5 MetricGroup (org.apache.flink.metrics.MetricGroup)4 MetricGroupTest (org.apache.flink.runtime.metrics.groups.MetricGroupTest)4 Map (java.util.Map)3 ConfigConstants (org.apache.flink.configuration.ConfigConstants)3 Configuration (org.apache.flink.configuration.Configuration)3 TestMetricGroup (org.apache.flink.metrics.util.TestMetricGroup)3 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 MetricOptions (org.apache.flink.configuration.MetricOptions)2 UnregisteredMetricsGroup (org.apache.flink.metrics.groups.UnregisteredMetricsGroup)2 ResourceID (org.apache.flink.runtime.clusterframework.types.ResourceID)2 MetricGroupAndName (org.apache.flink.runtime.metrics.CollectingMetricsReporter.MetricGroupAndName)2 SlidingWindowReservoir (com.codahale.metrics.SlidingWindowReservoir)1 Snapshot (com.codahale.metrics.Snapshot)1 File (java.io.File)1 IOException (java.io.IOException)1