Java Code Examples for org.apache.flink.metrics.Counter#inc()

The following examples show how to use org.apache.flink.metrics.Counter#inc() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnionWithTempOperator.java    From flink with Apache License 2.0 6 votes vote down vote up
@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);
	}
}
 
Example 2
Source File: InfluxdbReporterTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testMetricReporting() throws Exception {
	String retentionPolicy = "one_hour";
	MetricRegistryImpl metricRegistry = createMetricRegistry(retentionPolicy);
	try {
		String metricName = "TestCounter";
		Counter counter = registerTestMetric(metricName, metricRegistry);
		counter.inc(42);

		stubFor(post(urlPathEqualTo("/write"))
			.willReturn(aResponse()
				.withStatus(200)));

		InfluxdbReporter reporter = (InfluxdbReporter) metricRegistry.getReporters().get(0);
		reporter.report();

		verify(postRequestedFor(urlPathEqualTo("/write"))
			.withQueryParam("db", equalTo(TEST_INFLUXDB_DB))
			.withQueryParam("rp", equalTo(retentionPolicy))
			.withHeader("Content-Type", containing("text/plain"))
			.withRequestBody(containing("taskmanager_" + metricName + ",host=" + METRIC_HOSTNAME + ",tm_id=" + METRIC_TM_ID + " count=42i")));
	} finally {
		metricRegistry.shutdown().get();
	}
}
 
Example 3
Source File: PrometheusReporterTaskScopeTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void removingSingleInstanceOfMetricDoesNotBreakOtherInstances() throws UnirestException {
	Counter counter1 = new SimpleCounter();
	counter1.inc(1);
	Counter counter2 = new SimpleCounter();
	counter2.inc(2);

	taskMetricGroup1.counter("my_counter", counter1);
	taskMetricGroup2.counter("my_counter", counter2);

	assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_counter", LABEL_NAMES, labelValues1),
		equalTo(1.));
	assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_counter", LABEL_NAMES, labelValues2),
		equalTo(2.));

	taskMetricGroup2.close();
	assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_counter", LABEL_NAMES, labelValues1),
		equalTo(1.));

	taskMetricGroup1.close();
	assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_counter", LABEL_NAMES, labelValues1),
		nullValue());
}
 
Example 4
Source File: InfluxdbReporterTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testMetricReporting() throws Exception {
	MetricRegistryImpl metricRegistry = createMetricRegistry();
	try {
		String metricName = "TestCounter";
		Counter counter = registerTestMetric(metricName, metricRegistry);
		counter.inc(42);

		stubFor(post(urlPathEqualTo("/write"))
			.willReturn(aResponse()
				.withStatus(200)));

		InfluxdbReporter reporter = (InfluxdbReporter) metricRegistry.getReporters().get(0);
		reporter.report();

		verify(postRequestedFor(urlPathEqualTo("/write"))
			.withQueryParam("db", equalTo(TEST_INFLUXDB_DB))
			.withHeader("Content-Type", containing("text/plain"))
			.withRequestBody(containing("taskmanager_" + metricName + ",host=" + METRIC_HOSTNAME + ",tm_id=" + METRIC_TM_ID + " count=42i")));
	} finally {
		metricRegistry.shutdown().get();
	}
}
 
Example 5
Source File: UnionWithTempOperator.java    From flink with Apache License 2.0 6 votes vote down vote up
@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);
	}
}
 
Example 6
Source File: TaskIOMetricGroupTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testTaskIOMetricGroup() {
	TaskMetricGroup task = UnregisteredMetricGroups.createUnregisteredTaskMetricGroup();
	TaskIOMetricGroup taskIO = task.getIOMetricGroup();

	// test counter forwarding
	assertNotNull(taskIO.getNumRecordsInCounter());
	assertNotNull(taskIO.getNumRecordsOutCounter());

	Counter c1 = new SimpleCounter();
	c1.inc(32L);
	Counter c2 = new SimpleCounter();
	c2.inc(64L);

	taskIO.reuseRecordsInputCounter(c1);
	taskIO.reuseRecordsOutputCounter(c2);
	assertEquals(32L, taskIO.getNumRecordsInCounter().getCount());
	assertEquals(64L, taskIO.getNumRecordsOutCounter().getCount());

	// test IOMetrics instantiation
	taskIO.getNumBytesInLocalCounter().inc(100L);
	taskIO.getNumBytesInRemoteCounter().inc(150L);
	taskIO.getNumBytesOutCounter().inc(250L);
	taskIO.getNumBuffersInLocalCounter().inc(1L);
	taskIO.getNumBuffersInRemoteCounter().inc(2L);
	taskIO.getNumBuffersOutCounter().inc(3L);

	IOMetrics io = taskIO.createSnapshot();
	assertEquals(32L, io.getNumRecordsIn());
	assertEquals(64L, io.getNumRecordsOut());
	assertEquals(100L, io.getNumBytesInLocal());
	assertEquals(150L, io.getNumBytesInRemote());
	assertEquals(250L, io.getNumBytesOut());
	assertEquals(1L, taskIO.getNumBuffersInLocalCounter().getCount());
	assertEquals(2L, taskIO.getNumBuffersInRemoteCounter().getCount());
	assertEquals(3L, taskIO.getNumBuffersOutCounter().getCount());
}
 
Example 7
Source File: PrometheusReporterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * {@link io.prometheus.client.Counter} may not decrease, so report {@link Counter} as {@link io.prometheus.client.Gauge}.
 *
 * @throws UnirestException Might be thrown on HTTP problems.
 */
@Test
public void counterIsReportedAsPrometheusGauge() throws UnirestException {
	Counter testCounter = new SimpleCounter();
	testCounter.inc(7);

	assertThatGaugeIsExported(testCounter, "testCounter", "7.0");
}
 
Example 8
Source File: PrometheusReporterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void registeringSameMetricTwiceDoesNotThrowException() {
	Counter counter = new SimpleCounter();
	counter.inc();
	String counterName = "testCounter";

	reporter.notifyOfAddedMetric(counter, counterName, metricGroup);
	reporter.notifyOfAddedMetric(counter, counterName, metricGroup);
}
 
Example 9
Source File: PrometheusReporterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * {@link io.prometheus.client.Counter} may not decrease, so report {@link Counter} as {@link io.prometheus.client.Gauge}.
 *
 * @throws UnirestException Might be thrown on HTTP problems.
 */
@Test
public void counterIsReportedAsPrometheusGauge() throws UnirestException {
	Counter testCounter = new SimpleCounter();
	testCounter.inc(7);

	assertThatGaugeIsExported(testCounter, "testCounter", "7.0");
}
 
Example 10
Source File: PrometheusReporterTaskScopeTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void countersCanBeAddedSeveralTimesIfTheyDifferInLabels() throws UnirestException {
	Counter counter1 = new SimpleCounter();
	counter1.inc(1);
	Counter counter2 = new SimpleCounter();
	counter2.inc(2);

	taskMetricGroup1.counter("my_counter", counter1);
	taskMetricGroup2.counter("my_counter", counter2);

	assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_counter", LABEL_NAMES, labelValues1),
		equalTo(1.));
	assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_counter", LABEL_NAMES, labelValues2),
		equalTo(2.));
}
 
Example 11
Source File: FlinkMetricContainer.java    From beam with Apache License 2.0 5 votes vote down vote up
private void updateCounters(Iterable<MetricResult<Long>> counters) {
  for (MetricResult<Long> metricResult : counters) {
    String flinkMetricName = getFlinkMetricNameString(metricResult.getKey());

    Long update = metricResult.getAttempted();

    // update flink metric
    Counter counter =
        flinkCounterCache.computeIfAbsent(
            flinkMetricName, n -> runtimeContext.getMetricGroup().counter(n));
    // Beam counters are already pre-aggregated, just update with the current value here
    counter.inc(update - counter.getCount());
  }
}
 
Example 12
Source File: TaskIOMetricGroupTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testTaskIOMetricGroup() {
	TaskMetricGroup task = UnregisteredMetricGroups.createUnregisteredTaskMetricGroup();
	TaskIOMetricGroup taskIO = task.getIOMetricGroup();

	// test counter forwarding
	assertNotNull(taskIO.getNumRecordsInCounter());
	assertNotNull(taskIO.getNumRecordsOutCounter());

	Counter c1 = new SimpleCounter();
	c1.inc(32L);
	Counter c2 = new SimpleCounter();
	c2.inc(64L);

	taskIO.reuseRecordsInputCounter(c1);
	taskIO.reuseRecordsOutputCounter(c2);
	assertEquals(32L, taskIO.getNumRecordsInCounter().getCount());
	assertEquals(64L, taskIO.getNumRecordsOutCounter().getCount());

	// test IOMetrics instantiation
	taskIO.getNumBytesInCounter().inc(100L);
	taskIO.getNumBytesOutCounter().inc(250L);
	taskIO.getNumBuffersOutCounter().inc(3L);
	taskIO.getIdleTimeMsPerSecond().markEvent(2L);

	IOMetrics io = taskIO.createSnapshot();
	assertEquals(32L, io.getNumRecordsIn());
	assertEquals(64L, io.getNumRecordsOut());
	assertEquals(100L, io.getNumBytesIn());
	assertEquals(250L, io.getNumBytesOut());
	assertEquals(3L, taskIO.getNumBuffersOutCounter().getCount());
	assertEquals(2L, taskIO.getIdleTimeMsPerSecond().getCount());
}
 
Example 13
Source File: InputFormatSourceFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void run(SourceContext<OUT> ctx) throws Exception {
	try {

		Counter completedSplitsCounter = getRuntimeContext().getMetricGroup().counter("numSplitsProcessed");
		if (isRunning && format instanceof RichInputFormat) {
			((RichInputFormat) format).openInputFormat();
		}

		OUT nextElement = serializer.createInstance();
		while (isRunning) {
			format.open(splitIterator.next());

			// for each element we also check if cancel
			// was called by checking the isRunning flag

			while (isRunning && !format.reachedEnd()) {
				nextElement = format.nextRecord(nextElement);
				if (nextElement != null) {
					ctx.collect(nextElement);
				} else {
					break;
				}
			}
			format.close();
			completedSplitsCounter.inc();

			if (isRunning) {
				isRunning = splitIterator.hasNext();
			}
		}
	} finally {
		format.close();
		if (format instanceof RichInputFormat) {
			((RichInputFormat) format).closeInputFormat();
		}
		isRunning = false;
	}
}
 
Example 14
Source File: MetricMapperTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMapCounter() {
	Counter counter = new SimpleCounter();
	counter.inc(42L);

	verifyPoint(
		MetricMapper.map(INFO, TIMESTAMP, counter),
		"count=42");
}
 
Example 15
Source File: PrometheusReporterTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * {@link io.prometheus.client.Counter} may not decrease, so report {@link Counter} as {@link io.prometheus.client.Gauge}.
 *
 * @throws UnirestException Might be thrown on HTTP problems.
 */
@Test
public void counterIsReportedAsPrometheusGauge() throws UnirestException {
	Counter testCounter = new SimpleCounter();
	testCounter.inc(7);

	assertThatGaugeIsExported(testCounter, "testCounter", "7.0");
}
 
Example 16
Source File: PrometheusReporterTaskScopeTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void countersCanBeAddedSeveralTimesIfTheyDifferInLabels() throws UnirestException {
	Counter counter1 = new SimpleCounter();
	counter1.inc(1);
	Counter counter2 = new SimpleCounter();
	counter2.inc(2);

	taskMetricGroup1.counter("my_counter", counter1);
	taskMetricGroup2.counter("my_counter", counter2);

	assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_counter", LABEL_NAMES, labelValues1),
		equalTo(1.));
	assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_counter", LABEL_NAMES, labelValues2),
		equalTo(2.));
}
 
Example 17
Source File: InputFormatSourceFunction.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void run(SourceContext<OUT> ctx) throws Exception {
	try {

		Counter completedSplitsCounter = getRuntimeContext().getMetricGroup().counter("numSplitsProcessed");
		if (isRunning && format instanceof RichInputFormat) {
			((RichInputFormat) format).openInputFormat();
		}

		OUT nextElement = serializer.createInstance();
		while (isRunning) {
			format.open(splitIterator.next());

			// for each element we also check if cancel
			// was called by checking the isRunning flag

			while (isRunning && !format.reachedEnd()) {
				nextElement = format.nextRecord(nextElement);
				if (nextElement != null) {
					ctx.collect(nextElement);
				} else {
					break;
				}
			}
			format.close();
			completedSplitsCounter.inc();

			if (isRunning) {
				isRunning = splitIterator.hasNext();
			}
		}
	} finally {
		format.close();
		if (format instanceof RichInputFormat) {
			((RichInputFormat) format).closeInputFormat();
		}
		isRunning = false;
	}
}
 
Example 18
Source File: InputChannelMetrics.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void inc() {
	for (Counter c : counters) {
		c.inc();
	}
}
 
Example 19
Source File: InputChannelMetrics.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void inc() {
	for (Counter c : counters) {
		c.inc();
	}
}
 
Example 20
Source File: StreamTwoInputProcessor.java    From flink with Apache License 2.0 4 votes vote down vote up
private void postProcessRecord(Counter numRecordsIn) {
	numRecordsIn.inc();
	inputSelectionHandler.nextSelection();
}