Search in sources :

Example 16 with Counter

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

the class UnionWithTempOperator method run.

@Override
public void run() throws Exception {
    final Counter numRecordsIn = this.taskContext.getMetricGroup().getIOMetricGroup().getNumRecordsInCounter();
    final Counter numRecordsOut = this.taskContext.getMetricGroup().getIOMetricGroup().getNumRecordsOutCounter();
    final Collector<T> output = new CountingCollector<>(this.taskContext.getOutputCollector(), numRecordsOut);
    T reuse = this.taskContext.<T>getInputSerializer(STREAMED_INPUT).getSerializer().createInstance();
    T record;
    final MutableObjectIterator<T> input = this.taskContext.getInput(STREAMED_INPUT);
    while (this.running && ((record = input.next(reuse)) != null)) {
        numRecordsIn.inc();
        output.collect(record);
    }
    final MutableObjectIterator<T> cache = this.taskContext.getInput(CACHED_INPUT);
    while (this.running && ((record = cache.next(reuse)) != null)) {
        numRecordsIn.inc();
        output.collect(record);
    }
}
Also used : CountingCollector(org.apache.flink.runtime.operators.util.metrics.CountingCollector) Counter(org.apache.flink.metrics.Counter)

Example 17 with Counter

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

the class StatsDReporterTest method testAddingMetrics.

/**
	 * Tests that the registered metrics' names don't contain invalid characters.
	 */
@Test
public void testAddingMetrics() throws NoSuchFieldException, IllegalAccessException {
    Configuration configuration = new Configuration();
    String taskName = "testTask";
    String jobName = "testJob:-!ax..?";
    String hostname = "local::host:";
    String taskManagerId = "tas:kMana::ger";
    String counterName = "testCounter";
    configuration.setString(ConfigConstants.METRICS_REPORTERS_LIST, "test");
    configuration.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, "org.apache.flink.metrics.statsd.StatsDReporterTest$TestingStatsDReporter");
    configuration.setString(ConfigConstants.METRICS_SCOPE_NAMING_TASK, "<host>.<tm_id>.<job_name>");
    configuration.setString(ConfigConstants.METRICS_SCOPE_DELIMITER, "_");
    MetricRegistry metricRegistry = new MetricRegistry(MetricRegistryConfiguration.fromConfiguration(configuration));
    char delimiter = metricRegistry.getDelimiter();
    TaskManagerMetricGroup tmMetricGroup = new TaskManagerMetricGroup(metricRegistry, hostname, taskManagerId);
    TaskManagerJobMetricGroup tmJobMetricGroup = new TaskManagerJobMetricGroup(metricRegistry, tmMetricGroup, new JobID(), jobName);
    TaskMetricGroup taskMetricGroup = new TaskMetricGroup(metricRegistry, tmJobMetricGroup, new AbstractID(), new AbstractID(), taskName, 0, 0);
    SimpleCounter myCounter = new SimpleCounter();
    taskMetricGroup.counter(counterName, myCounter);
    List<MetricReporter> reporters = metricRegistry.getReporters();
    assertTrue(reporters.size() == 1);
    MetricReporter metricReporter = reporters.get(0);
    assertTrue("Reporter should be of type StatsDReporter", metricReporter instanceof StatsDReporter);
    TestingStatsDReporter reporter = (TestingStatsDReporter) metricReporter;
    Map<Counter, String> counters = reporter.getCounters();
    assertTrue(counters.containsKey(myCounter));
    String expectedCounterName = reporter.filterCharacters(hostname) + delimiter + reporter.filterCharacters(taskManagerId) + delimiter + reporter.filterCharacters(jobName) + delimiter + reporter.filterCharacters(counterName);
    assertEquals(expectedCounterName, counters.get(myCounter));
    metricRegistry.shutdown();
}
Also used : MetricRegistryConfiguration(org.apache.flink.runtime.metrics.MetricRegistryConfiguration) Configuration(org.apache.flink.configuration.Configuration) TaskMetricGroup(org.apache.flink.runtime.metrics.groups.TaskMetricGroup) MetricRegistry(org.apache.flink.runtime.metrics.MetricRegistry) TaskManagerMetricGroup(org.apache.flink.runtime.metrics.groups.TaskManagerMetricGroup) TaskManagerJobMetricGroup(org.apache.flink.runtime.metrics.groups.TaskManagerJobMetricGroup) MetricReporter(org.apache.flink.metrics.reporter.MetricReporter) SimpleCounter(org.apache.flink.metrics.SimpleCounter) Counter(org.apache.flink.metrics.Counter) SimpleCounter(org.apache.flink.metrics.SimpleCounter) AbstractID(org.apache.flink.util.AbstractID) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 18 with Counter

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

the class ScheduledDropwizardReporterTest method testAddingMetrics.

/**
	 * Tests that the registered metrics' names don't contain invalid characters.
	 */
@Test
public void testAddingMetrics() throws NoSuchFieldException, IllegalAccessException {
    Configuration configuration = new Configuration();
    String taskName = "test\"Ta\"..sk";
    String jobName = "testJ\"ob:-!ax..?";
    String hostname = "loc<>al\"::host\".:";
    String taskManagerId = "tas:kMana::ger";
    String counterName = "testCounter";
    configuration.setString(ConfigConstants.METRICS_REPORTERS_LIST, "test");
    configuration.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "test." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, "org.apache.flink.dropwizard.ScheduledDropwizardReporterTest$TestingScheduledDropwizardReporter");
    configuration.setString(ConfigConstants.METRICS_SCOPE_NAMING_TASK, "<host>.<tm_id>.<job_name>");
    configuration.setString(ConfigConstants.METRICS_SCOPE_DELIMITER, "_");
    MetricRegistryConfiguration metricRegistryConfiguration = MetricRegistryConfiguration.fromConfiguration(configuration);
    MetricRegistry metricRegistry = new MetricRegistry(metricRegistryConfiguration);
    char delimiter = metricRegistry.getDelimiter();
    TaskManagerMetricGroup tmMetricGroup = new TaskManagerMetricGroup(metricRegistry, hostname, taskManagerId);
    TaskManagerJobMetricGroup tmJobMetricGroup = new TaskManagerJobMetricGroup(metricRegistry, tmMetricGroup, new JobID(), jobName);
    TaskMetricGroup taskMetricGroup = new TaskMetricGroup(metricRegistry, tmJobMetricGroup, new AbstractID(), new AbstractID(), taskName, 0, 0);
    SimpleCounter myCounter = new SimpleCounter();
    com.codahale.metrics.Meter dropwizardMeter = new com.codahale.metrics.Meter();
    DropwizardMeterWrapper meterWrapper = new DropwizardMeterWrapper(dropwizardMeter);
    taskMetricGroup.counter(counterName, myCounter);
    taskMetricGroup.meter("meter", meterWrapper);
    List<MetricReporter> reporters = metricRegistry.getReporters();
    assertTrue(reporters.size() == 1);
    MetricReporter metricReporter = reporters.get(0);
    assertTrue("Reporter should be of type ScheduledDropwizardReporter", metricReporter instanceof ScheduledDropwizardReporter);
    TestingScheduledDropwizardReporter reporter = (TestingScheduledDropwizardReporter) metricReporter;
    Map<Counter, String> counters = reporter.getCounters();
    assertTrue(counters.containsKey(myCounter));
    Map<Meter, String> meters = reporter.getMeters();
    assertTrue(meters.containsKey(meterWrapper));
    String expectedCounterName = reporter.filterCharacters(hostname) + delimiter + reporter.filterCharacters(taskManagerId) + delimiter + reporter.filterCharacters(jobName) + delimiter + reporter.filterCharacters(counterName);
    assertEquals(expectedCounterName, counters.get(myCounter));
    metricRegistry.shutdown();
}
Also used : MetricRegistryConfiguration(org.apache.flink.runtime.metrics.MetricRegistryConfiguration) Configuration(org.apache.flink.configuration.Configuration) Meter(org.apache.flink.metrics.Meter) TaskMetricGroup(org.apache.flink.runtime.metrics.groups.TaskMetricGroup) MetricRegistry(org.apache.flink.runtime.metrics.MetricRegistry) TaskManagerMetricGroup(org.apache.flink.runtime.metrics.groups.TaskManagerMetricGroup) TaskManagerJobMetricGroup(org.apache.flink.runtime.metrics.groups.TaskManagerJobMetricGroup) MetricRegistryConfiguration(org.apache.flink.runtime.metrics.MetricRegistryConfiguration) MetricReporter(org.apache.flink.metrics.reporter.MetricReporter) DropwizardMeterWrapper(org.apache.flink.dropwizard.metrics.DropwizardMeterWrapper) SimpleCounter(org.apache.flink.metrics.SimpleCounter) Counter(org.apache.flink.metrics.Counter) SimpleCounter(org.apache.flink.metrics.SimpleCounter) AbstractID(org.apache.flink.util.AbstractID) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 19 with Counter

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

the class JMXReporter method notifyOfAddedMetric.

// ------------------------------------------------------------------------
//  adding / removing metrics
// ------------------------------------------------------------------------
@Override
public void notifyOfAddedMetric(Metric metric, String metricName, MetricGroup group) {
    final String domain = generateJmxDomain(metricName, group);
    final Hashtable<String, String> table = generateJmxTable(group.getAllVariables());
    AbstractBean jmxMetric;
    ObjectName jmxName;
    try {
        jmxName = new ObjectName(domain, table);
    } catch (MalformedObjectNameException e) {
        /**
			 * There is an implementation error on our side if this occurs. Either the domain was modified and no longer
			 * conforms to the JMX domain rules or the table wasn't properly generated.
			 */
        LOG.debug("Implementation error. The domain or table does not conform to JMX rules.", e);
        return;
    }
    if (metric instanceof Gauge) {
        jmxMetric = new JmxGauge((Gauge<?>) metric);
    } else if (metric instanceof Counter) {
        jmxMetric = new JmxCounter((Counter) metric);
    } else if (metric instanceof Histogram) {
        jmxMetric = new JmxHistogram((Histogram) metric);
    } else if (metric instanceof Meter) {
        jmxMetric = new JmxMeter((Meter) metric);
    } else {
        LOG.error("Cannot add unknown metric type: {}. This indicates that the metric type " + "is not supported by this reporter.", metric.getClass().getName());
        return;
    }
    try {
        synchronized (this) {
            mBeanServer.registerMBean(jmxMetric, jmxName);
            registeredMetrics.put(metric, jmxName);
        }
    } catch (NotCompliantMBeanException e) {
        // implementation error on our side
        LOG.debug("Metric did not comply with JMX MBean rules.", e);
    } catch (InstanceAlreadyExistsException e) {
        LOG.warn("A metric with the name " + jmxName + " was already registered.", e);
    } catch (Throwable t) {
        LOG.warn("Failed to register metric", t);
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) Histogram(org.apache.flink.metrics.Histogram) Meter(org.apache.flink.metrics.Meter) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) ObjectName(javax.management.ObjectName) Gauge(org.apache.flink.metrics.Gauge) Counter(org.apache.flink.metrics.Counter)

Example 20 with Counter

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

the class MetricFetcherTest method createRequestDumpAnswer.

private static MetricDumpSerialization.MetricSerializationResult createRequestDumpAnswer(InstanceID tmID, JobID jobID) throws IOException {
    Map<Counter, Tuple2<QueryScopeInfo, String>> counters = new HashMap<>();
    Map<Gauge<?>, Tuple2<QueryScopeInfo, String>> gauges = new HashMap<>();
    Map<Histogram, Tuple2<QueryScopeInfo, String>> histograms = new HashMap<>();
    Map<Meter, Tuple2<QueryScopeInfo, String>> meters = new HashMap<>();
    SimpleCounter c1 = new SimpleCounter();
    SimpleCounter c2 = new SimpleCounter();
    c1.inc(1);
    c2.inc(2);
    counters.put(c1, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.OperatorQueryScopeInfo(jobID.toString(), "taskid", 2, "opname", "abc"), "oc"));
    counters.put(c2, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.TaskQueryScopeInfo(jobID.toString(), "taskid", 2, "abc"), "tc"));
    meters.put(new Meter() {

        @Override
        public void markEvent() {
        }

        @Override
        public void markEvent(long n) {
        }

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

        @Override
        public long getCount() {
            return 10;
        }
    }, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.JobQueryScopeInfo(jobID.toString(), "abc"), "jc"));
    gauges.put(new Gauge<String>() {

        @Override
        public String getValue() {
            return "x";
        }
    }, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.TaskManagerQueryScopeInfo(tmID.toString(), "abc"), "gauge"));
    histograms.put(new TestingHistogram(), new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.JobManagerQueryScopeInfo("abc"), "hist"));
    MetricDumpSerialization.MetricDumpSerializer serializer = new MetricDumpSerialization.MetricDumpSerializer();
    MetricDumpSerialization.MetricSerializationResult dump = serializer.serialize(counters, gauges, histograms, meters);
    serializer.close();
    return dump;
}
Also used : TestingHistogram(org.apache.flink.runtime.metrics.util.TestingHistogram) Histogram(org.apache.flink.metrics.Histogram) HashMap(java.util.HashMap) Meter(org.apache.flink.metrics.Meter) QueryScopeInfo(org.apache.flink.runtime.metrics.dump.QueryScopeInfo) Gauge(org.apache.flink.metrics.Gauge) SimpleCounter(org.apache.flink.metrics.SimpleCounter) Counter(org.apache.flink.metrics.Counter) SimpleCounter(org.apache.flink.metrics.SimpleCounter) MetricDumpSerialization(org.apache.flink.runtime.metrics.dump.MetricDumpSerialization) Tuple2(org.apache.flink.api.java.tuple.Tuple2) TestingHistogram(org.apache.flink.runtime.metrics.util.TestingHistogram)

Aggregations

Counter (org.apache.flink.metrics.Counter)39 CountingCollector (org.apache.flink.runtime.operators.util.metrics.CountingCollector)18 CountingMutableObjectIterator (org.apache.flink.runtime.operators.util.metrics.CountingMutableObjectIterator)10 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)8 Histogram (org.apache.flink.metrics.Histogram)8 Meter (org.apache.flink.metrics.Meter)8 Gauge (org.apache.flink.metrics.Gauge)7 SimpleCounter (org.apache.flink.metrics.SimpleCounter)7 Test (org.junit.Test)7 TaskConfig (org.apache.flink.runtime.operators.util.TaskConfig)5 Configuration (org.apache.flink.configuration.Configuration)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 SpillingResettableMutableObjectIterator (org.apache.flink.runtime.operators.resettable.SpillingResettableMutableObjectIterator)4 IOException (java.io.IOException)3 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)3 TaskManagerJobMetricGroup (org.apache.flink.runtime.metrics.groups.TaskManagerJobMetricGroup)3 TaskMetricGroup (org.apache.flink.runtime.metrics.groups.TaskMetricGroup)3 TestingHistogram (org.apache.flink.runtime.metrics.util.TestingHistogram)3