org.apache.flink.metrics.Gauge Java Examples

The following examples show how to use org.apache.flink.metrics.Gauge. 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: MetricUtilsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that heap/non-heap metrics do not rely on a static MemoryUsage instance.
 *
 * <p>We can only check this easily for the currently used heap memory, so we use it this as a proxy for testing
 * the functionality in general.
 */
@Test
public void testHeapMetrics() throws Exception {
	final InterceptingOperatorMetricGroup heapMetrics = new InterceptingOperatorMetricGroup();

	MetricUtils.instantiateHeapMemoryMetrics(heapMetrics);

	@SuppressWarnings("unchecked")
	final Gauge<Long> used = (Gauge<Long>) heapMetrics.get(MetricNames.MEMORY_USED);

	final long usedHeapInitially = used.getValue();

	// check memory usage difference multiple times since other tests may affect memory usage as well
	for (int x = 0; x < 10; x++) {
		final byte[] array = new byte[1024 * 1024 * 8];
		final long usedHeapAfterAllocation = used.getValue();

		if (usedHeapInitially != usedHeapAfterAllocation) {
			return;
		}
		Thread.sleep(50);
	}
	Assert.fail("Heap usage metric never changed it's value.");
}
 
Example #2
Source File: PrometheusReporterTaskScopeTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void gaugesCanBeAddedSeveralTimesIfTheyDifferInLabels() throws UnirestException {
	Gauge<Integer> gauge1 = new Gauge<Integer>() {
		@Override
		public Integer getValue() {
			return 3;
		}
	};
	Gauge<Integer> gauge2 = new Gauge<Integer>() {
		@Override
		public Integer getValue() {
			return 4;
		}
	};

	taskMetricGroup1.gauge("my_gauge", gauge1);
	taskMetricGroup2.gauge("my_gauge", gauge2);

	assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_gauge", LABEL_NAMES, labelValues1),
		equalTo(3.));
	assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_gauge", LABEL_NAMES, labelValues2),
		equalTo(4.));
}
 
Example #3
Source File: AbstractReporter.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void notifyOfAddedMetric(Metric metric, String metricName, MetricGroup group) {
	final String name = group.getMetricIdentifier(metricName, this);

	synchronized (this) {
		if (metric instanceof Counter) {
			counters.put((Counter) metric, name);
		} else if (metric instanceof Gauge) {
			gauges.put((Gauge<?>) metric, name);
		} else if (metric instanceof Histogram) {
			histograms.put((Histogram) metric, name);
		} else if (metric instanceof Meter) {
			meters.put((Meter) metric, name);
		} else {
			log.warn("Cannot add unknown metric type {}. This indicates that the reporter " +
				"does not support this metric type.", metric.getClass().getName());
		}
	}
}
 
Example #4
Source File: MetricGroupTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void closedGroupDoesNotRegisterMetrics() {
	GenericMetricGroup group = new GenericMetricGroup(
			exceptionOnRegister, new DummyAbstractMetricGroup(exceptionOnRegister), "testgroup");
	assertFalse(group.isClosed());

	group.close();
	assertTrue(group.isClosed());

	// these will fail is the registration is propagated
	group.counter("testcounter");
	group.gauge("testgauge", new Gauge<Object>() {
		@Override
		public Object getValue() {
			return null;
		}
	});
}
 
Example #5
Source File: AbstractReporter.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void notifyOfRemovedMetric(Metric metric, String metricName, MetricGroup group) {
	synchronized (this) {
		if (metric instanceof Counter) {
			counters.remove(metric);
		} else if (metric instanceof Gauge) {
			gauges.remove(metric);
		} else if (metric instanceof Histogram) {
			histograms.remove(metric);
		} else if (metric instanceof Meter) {
			meters.remove(metric);
		} else {
			log.warn("Cannot remove unknown metric type {}. This indicates that the reporter " +
				"does not support this metric type.", metric.getClass().getName());
		}
	}
}
 
Example #6
Source File: MetricMapperTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testMapGauge() {
	verifyPoint(
		MetricMapper.map(INFO, TIMESTAMP, (Gauge<Number>) () -> 42),
		"value=42");

	verifyPoint(
		MetricMapper.map(INFO, TIMESTAMP, (Gauge<Number>) () -> null),
		"value=null");

	verifyPoint(
		MetricMapper.map(INFO, TIMESTAMP, (Gauge<String>) () -> "hello"),
		"value=hello");

	verifyPoint(
		MetricMapper.map(INFO, TIMESTAMP, (Gauge<Long>) () -> 42L),
		"value=42");
}
 
Example #7
Source File: MetricUtilsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that heap/non-heap metrics do not rely on a static MemoryUsage instance.
 *
 * <p>We can only check this easily for the currently used heap memory, so we use it this as a proxy for testing
 * the functionality in general.
 */
@Test
public void testHeapMetrics() throws Exception {
	final InterceptingOperatorMetricGroup heapMetrics = new InterceptingOperatorMetricGroup();

	MetricUtils.instantiateHeapMemoryMetrics(heapMetrics);

	@SuppressWarnings("unchecked")
	final Gauge<Long> used = (Gauge<Long>) heapMetrics.get(MetricNames.MEMORY_USED);

	final long usedHeapInitially = used.getValue();

	// check memory usage difference multiple times since other tests may affect memory usage as well
	for (int x = 0; x < 10; x++) {
		final byte[] array = new byte[1024 * 1024 * 8];
		final long usedHeapAfterAllocation = used.getValue();

		if (usedHeapInitially != usedHeapAfterAllocation) {
			return;
		}
		Thread.sleep(50);
	}
	Assert.fail("Heap usage metric never changed it's value.");
}
 
Example #8
Source File: DatadogHttpReporter.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void notifyOfAddedMetric(Metric metric, String metricName, MetricGroup group) {
	final String name = group.getMetricIdentifier(metricName);

	List<String> tags = new ArrayList<>(configTags);
	tags.addAll(getTagsFromMetricGroup(group));
	String host = getHostFromMetricGroup(group);

	if (metric instanceof Counter) {
		Counter c = (Counter) metric;
		counters.put(c, new DCounter(c, name, host, tags));
	} else if (metric instanceof Gauge) {
		Gauge g = (Gauge) metric;
		gauges.put(g, new DGauge(g, name, host, tags));
	} else if (metric instanceof Meter) {
		Meter m = (Meter) metric;
		// Only consider rate
		meters.put(m, new DMeter(m, name, host, tags));
	} else if (metric instanceof Histogram) {
		LOGGER.warn("Cannot add {} because Datadog HTTP API doesn't support Histogram", metricName);
	} else {
		LOGGER.warn("Cannot add unknown metric type {}. This indicates that the reporter " +
			"does not support this metric type.", metric.getClass().getName());
	}
}
 
Example #9
Source File: DatadogHttpReporter.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void notifyOfAddedMetric(Metric metric, String metricName, MetricGroup group) {
	final String name = group.getMetricIdentifier(metricName);

	List<String> tags = new ArrayList<>(configTags);
	tags.addAll(getTagsFromMetricGroup(group));
	String host = getHostFromMetricGroup(group);

	if (metric instanceof Counter) {
		Counter c = (Counter) metric;
		counters.put(c, new DCounter(c, name, host, tags, clock));
	} else if (metric instanceof Gauge) {
		Gauge g = (Gauge) metric;
		gauges.put(g, new DGauge(g, name, host, tags, clock));
	} else if (metric instanceof Meter) {
		Meter m = (Meter) metric;
		// Only consider rate
		meters.put(m, new DMeter(m, name, host, tags, clock));
	} else if (metric instanceof Histogram) {
		LOGGER.warn("Cannot add {} because Datadog HTTP API doesn't support Histogram", metricName);
	} else {
		LOGGER.warn("Cannot add unknown metric type {}. This indicates that the reporter " +
			"does not support this metric type.", metric.getClass().getName());
	}
}
 
Example #10
Source File: PrometheusReporterTaskScopeTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void gaugesCanBeAddedSeveralTimesIfTheyDifferInLabels() throws UnirestException {
	Gauge<Integer> gauge1 = new Gauge<Integer>() {
		@Override
		public Integer getValue() {
			return 3;
		}
	};
	Gauge<Integer> gauge2 = new Gauge<Integer>() {
		@Override
		public Integer getValue() {
			return 4;
		}
	};

	taskMetricGroup1.gauge("my_gauge", gauge1);
	taskMetricGroup2.gauge("my_gauge", gauge2);

	assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_gauge", LABEL_NAMES, labelValues1),
		equalTo(3.));
	assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_gauge", LABEL_NAMES, labelValues2),
		equalTo(4.));
}
 
Example #11
Source File: DatadogHttpReporter.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void notifyOfAddedMetric(Metric metric, String metricName, MetricGroup group) {
	final String name = group.getMetricIdentifier(metricName);

	List<String> tags = new ArrayList<>(configTags);
	tags.addAll(getTagsFromMetricGroup(group));
	String host = getHostFromMetricGroup(group);

	if (metric instanceof Counter) {
		Counter c = (Counter) metric;
		counters.put(c, new DCounter(c, name, host, tags));
	} else if (metric instanceof Gauge) {
		Gauge g = (Gauge) metric;
		gauges.put(g, new DGauge(g, name, host, tags));
	} else if (metric instanceof Meter) {
		Meter m = (Meter) metric;
		// Only consider rate
		meters.put(m, new DMeter(m, name, host, tags));
	} else if (metric instanceof Histogram) {
		LOGGER.warn("Cannot add {} because Datadog HTTP API doesn't support Histogram", metricName);
	} else {
		LOGGER.warn("Cannot add unknown metric type {}. This indicates that the reporter " +
			"does not support this metric type.", metric.getClass().getName());
	}
}
 
Example #12
Source File: AbstractReporter.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void notifyOfAddedMetric(Metric metric, String metricName, MetricGroup group) {
	final MetricInfo metricInfo = metricInfoProvider.getMetricInfo(metricName, group);
	synchronized (this) {
		if (metric instanceof Counter) {
			counters.put((Counter) metric, metricInfo);
		} else if (metric instanceof Gauge) {
			gauges.put((Gauge<?>) metric, metricInfo);
		} else if (metric instanceof Histogram) {
			histograms.put((Histogram) metric, metricInfo);
		} else if (metric instanceof Meter) {
			meters.put((Meter) metric, metricInfo);
		} else {
			log.warn("Cannot add unknown metric type {}. This indicates that the reporter " +
				"does not support this metric type.", metric.getClass().getName());
		}
	}
}
 
Example #13
Source File: AbstractReporter.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void notifyOfAddedMetric(Metric metric, String metricName, MetricGroup group) {
	final MetricInfo metricInfo = metricInfoProvider.getMetricInfo(metricName, group);
	synchronized (this) {
		if (metric instanceof Counter) {
			counters.put((Counter) metric, metricInfo);
		} else if (metric instanceof Gauge) {
			gauges.put((Gauge<?>) metric, metricInfo);
		} else if (metric instanceof Histogram) {
			histograms.put((Histogram) metric, metricInfo);
		} else if (metric instanceof Meter) {
			meters.put((Meter) metric, metricInfo);
		} else {
			log.warn("Cannot add unknown metric type {}. This indicates that the reporter " +
				"does not support this metric type.", metric.getClass().getName());
		}
	}
}
 
Example #14
Source File: ScheduledDropwizardReporter.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void notifyOfRemovedMetric(Metric metric, String metricName, MetricGroup group) {
	synchronized (this) {
		String fullName;

		if (metric instanceof Counter) {
			fullName = counters.remove(metric);
		} else if (metric instanceof Gauge) {
			fullName = gauges.remove(metric);
		} else if (metric instanceof Histogram) {
			fullName = histograms.remove(metric);
		} else if (metric instanceof Meter) {
			fullName = meters.remove(metric);
		} else {
			fullName = null;
		}

		if (fullName != null) {
			registry.remove(fullName);
		}
	}
}
 
Example #15
Source File: AbstractPrometheusReporter.java    From flink with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
io.prometheus.client.Gauge.Child gaugeFrom(Gauge gauge) {
	return new io.prometheus.client.Gauge.Child() {
		@Override
		public double get() {
			final Object value = gauge.getValue();
			if (value == null) {
				log.debug("Gauge {} is null-valued, defaulting to 0.", gauge);
				return 0;
			}
			if (value instanceof Double) {
				return (double) value;
			}
			if (value instanceof Number) {
				return ((Number) value).doubleValue();
			}
			if (value instanceof Boolean) {
				return ((Boolean) value) ? 1 : 0;
			}
			log.debug("Invalid type for Gauge {}: {}, only number types and booleans are supported by this reporter.",
				gauge, value.getClass().getName());
			return 0;
		}
	};
}
 
Example #16
Source File: AbstractReporter.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void notifyOfAddedMetric(Metric metric, String metricName, MetricGroup group) {
	final String name = group.getMetricIdentifier(metricName, this);

	synchronized (this) {
		if (metric instanceof Counter) {
			counters.put((Counter) metric, name);
		} else if (metric instanceof Gauge) {
			gauges.put((Gauge<?>) metric, name);
		} else if (metric instanceof Histogram) {
			histograms.put((Histogram) metric, name);
		} else if (metric instanceof Meter) {
			meters.put((Meter) metric, name);
		} else {
			log.warn("Cannot add unknown metric type {}. This indicates that the reporter " +
				"does not support this metric type.", metric.getClass().getName());
		}
	}
}
 
Example #17
Source File: DatadogHttpClientTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void serializeGaugeWithoutHost() throws JsonProcessingException {

	DGauge g = new DGauge(new Gauge<Number>() {
		@Override
		public Number getValue() {
			return 1;
		}
	}, "testCounter", null, tags);

	assertEquals(
		"{\"metric\":\"testCounter\",\"type\":\"gauge\",\"tags\":[\"tag1\",\"tag2\"],\"points\":[[123,1]]}",
		DatadogHttpClient.serialize(g));
}
 
Example #18
Source File: InputGateMetrics.java    From flink with Apache License 2.0 5 votes vote down vote up
private Gauge<Float> getAvgQueueLenGauge() {
	return new Gauge<Float>() {
		@Override
		public Float getValue() {
			return refreshAndGetAvg();
		}
	};
}
 
Example #19
Source File: ScheduledDropwizardReporter.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void report() {
	// we do not need to lock here, because the dropwizard registry is
	// internally a concurrent map
	@SuppressWarnings("rawtypes")
	final SortedMap<String, com.codahale.metrics.Gauge> gauges = registry.getGauges();
	final SortedMap<String, com.codahale.metrics.Counter> counters = registry.getCounters();
	final SortedMap<String, com.codahale.metrics.Histogram> histograms = registry.getHistograms();
	final SortedMap<String, com.codahale.metrics.Meter> meters = registry.getMeters();
	final SortedMap<String, com.codahale.metrics.Timer> timers = registry.getTimers();

	this.reporter.report(gauges, counters, histograms, meters, timers);
}
 
Example #20
Source File: StatsDReporter.java    From flink with Apache License 2.0 5 votes vote down vote up
private void reportGauge(final String name, final Gauge<?> gauge) {
	Object value = gauge.getValue();
	if (value == null) {
		return;
	}

	if (value instanceof Number) {
		send(numberIsNegative((Number) value), name, value.toString());
	}

	send(name, value.toString());
}
 
Example #21
Source File: PrometheusReporterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void booleanGaugeIsConvertedCorrectly() {
	assertThat(reporter.gaugeFrom(new Gauge<Boolean>() {
		@Override
		public Boolean getValue() {
			return true;
		}
	}).get(), equalTo(1.));
}
 
Example #22
Source File: ResultPartitionMetrics.java    From flink with Apache License 2.0 5 votes vote down vote up
private Gauge<Float> getAvgQueueLenGauge() {
	return new Gauge<Float>() {
		@Override
		public Float getValue() {
			return refreshAndGetAvg();
		}
	};
}
 
Example #23
Source File: DatadogHttpClientTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void serializeGauge() throws JsonProcessingException {

	DGauge g = new DGauge(new Gauge<Number>() {
		@Override
		public Number getValue() {
			return 1;
		}
	}, "testCounter", "localhost", tags);

	assertEquals(
		"{\"metric\":\"testCounter\",\"type\":\"gauge\",\"host\":\"localhost\",\"tags\":[\"tag1\",\"tag2\"],\"points\":[[123,1]]}",
		DatadogHttpClient.serialize(g));
}
 
Example #24
Source File: DatadogHttpReporter.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void notifyOfRemovedMetric(Metric metric, String metricName, MetricGroup group) {
	if (metric instanceof Counter) {
		counters.remove(metric);
	} else if (metric instanceof Gauge) {
		gauges.remove(metric);
	} else if (metric instanceof Meter) {
		meters.remove(metric);
	} else if (metric instanceof Histogram) {
		// No Histogram is registered
	} else {
		LOGGER.warn("Cannot remove unknown metric type {}. This indicates that the reporter " +
			"does not support this metric type.", metric.getClass().getName());
	}
}
 
Example #25
Source File: AbstractPrometheusReporter.java    From flink with Apache License 2.0 5 votes vote down vote up
private void addMetric(Metric metric, List<String> dimensionValues, Collector collector) {
	if (metric instanceof Gauge) {
		((io.prometheus.client.Gauge) collector).setChild(gaugeFrom((Gauge) metric), toArray(dimensionValues));
	} else if (metric instanceof Counter) {
		((io.prometheus.client.Gauge) collector).setChild(gaugeFrom((Counter) metric), toArray(dimensionValues));
	} else if (metric instanceof Meter) {
		((io.prometheus.client.Gauge) collector).setChild(gaugeFrom((Meter) metric), toArray(dimensionValues));
	} else if (metric instanceof Histogram) {
		((HistogramSummaryProxy) collector).addChild((Histogram) metric, dimensionValues);
	} else {
		log.warn("Cannot add unknown metric type: {}. This indicates that the metric type is not supported by this reporter.",
			metric.getClass().getName());
	}
}
 
Example #26
Source File: MetricQueryService.java    From flink with Apache License 2.0 5 votes vote down vote up
public void addMetric(String metricName, Metric metric, AbstractMetricGroup group) {
	runAsync(() -> {
		QueryScopeInfo info = group.getQueryServiceMetricInfo(FILTER);

		if (metric instanceof Counter) {
			counters.put((Counter) metric, new Tuple2<>(info, FILTER.filterCharacters(metricName)));
		} else if (metric instanceof Gauge) {
			gauges.put((Gauge<?>) metric, new Tuple2<>(info, FILTER.filterCharacters(metricName)));
		} else if (metric instanceof Histogram) {
			histograms.put((Histogram) metric, new Tuple2<>(info, FILTER.filterCharacters(metricName)));
		} else if (metric instanceof Meter) {
			meters.put((Meter) metric, new Tuple2<>(info, FILTER.filterCharacters(metricName)));
		}
	});
}
 
Example #27
Source File: PrometheusReporterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void gaugeIsReportedAsPrometheusGauge() throws UnirestException {
	Gauge<Integer> testGauge = new Gauge<Integer>() {
		@Override
		public Integer getValue() {
			return 1;
		}
	};

	assertThatGaugeIsExported(testGauge, "testGauge", "1.0");
}
 
Example #28
Source File: MetricDumpSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testJavaSerialization() throws IOException {
	MetricDumpSerialization.MetricDumpSerializer serializer = new MetricDumpSerialization.MetricDumpSerializer();

	final ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
	final ObjectOutputStream oos = new ObjectOutputStream(bos);

	oos.writeObject(serializer.serialize(
		new HashMap<Counter, Tuple2<QueryScopeInfo, String>>(),
		new HashMap<Gauge<?>, Tuple2<QueryScopeInfo, String>>(),
		new HashMap<Histogram, Tuple2<QueryScopeInfo, String>>(),
		new HashMap<Meter, Tuple2<QueryScopeInfo, String>>()));
}
 
Example #29
Source File: PrometheusReporterTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void shortGaugeIsConvertedCorrectly() {
	assertThat(reporter.gaugeFrom(new Gauge<Short>() {
		@Override
		public Short getValue() {
			return 13;
		}
	}).get(), equalTo(13.));
}
 
Example #30
Source File: ResultPartitionMetrics.java    From flink with Apache License 2.0 5 votes vote down vote up
private Gauge<Integer> getMaxQueueLenGauge() {
	return new Gauge<Integer>() {
		@Override
		public Integer getValue() {
			return refreshAndGetMax();
		}
	};
}