org.apache.flink.runtime.rest.messages.job.metrics.AggregatedMetricsResponseBody Java Examples

The following examples show how to use org.apache.flink.runtime.rest.messages.job.metrics.AggregatedMetricsResponseBody. 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: AggregatingMetricsHandlerTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(1.0, aggregatedMetric.getMin(), 0.1);
	assertEquals(3.0, aggregatedMetric.getMax(), 0.1);
	assertEquals(2.0, aggregatedMetric.getAvg(), 0.1);
	assertEquals(4.0, aggregatedMetric.getSum(), 0.1);
}
 
Example #2
Source File: AggregatingMetricsHandlerTestBase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testListMetrics() throws Exception {
	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		Collections.emptyMap()
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	List<String> availableMetrics = response.getMetrics().stream()
		.map(AggregatedMetric::getId)
		.sorted()
		.collect(Collectors.toList());

	assertEquals(2, availableMetrics.size());
	assertEquals("abc.metric1", availableMetrics.get(0));
	assertEquals("abc.metric2", availableMetrics.get(1));
}
 
Example #3
Source File: AggregatingMetricsHandlerTestBase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(1.0, aggregatedMetric.getMin(), 0.1);
	assertEquals(3.0, aggregatedMetric.getMax(), 0.1);
	assertEquals(2.0, aggregatedMetric.getAvg(), 0.1);
	assertEquals(4.0, aggregatedMetric.getSum(), 0.1);
}
 
Example #4
Source File: AggregatingMetricsHandlerTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testListMetrics() throws Exception {
	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		Collections.emptyMap()
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	List<String> availableMetrics = response.getMetrics().stream()
		.map(AggregatedMetric::getId)
		.sorted()
		.collect(Collectors.toList());

	assertEquals(2, availableMetrics.size());
	assertEquals("abc.metric1", availableMetrics.get(0));
	assertEquals("abc.metric2", availableMetrics.get(1));
}
 
Example #5
Source File: AggregatingMetricsHandlerTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testListMetrics() throws Exception {
	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		Collections.emptyMap()
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	List<String> availableMetrics = response.getMetrics().stream()
		.map(AggregatedMetric::getId)
		.sorted()
		.collect(Collectors.toList());

	assertEquals(2, availableMetrics.size());
	assertEquals("abc.metric1", availableMetrics.get(0));
	assertEquals("abc.metric2", availableMetrics.get(1));
}
 
Example #6
Source File: AggregatingMetricsHandlerTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(1.0, aggregatedMetric.getMin(), 0.1);
	assertEquals(3.0, aggregatedMetric.getMax(), 0.1);
	assertEquals(2.0, aggregatedMetric.getAvg(), 0.1);
	assertEquals(4.0, aggregatedMetric.getSum(), 0.1);
}
 
Example #7
Source File: AggregatingMetricsHandlerTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMaxAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));
	queryParams.put("agg", Collections.singletonList("max"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(3.0, aggregatedMetric.getMax(), 0.1);
	assertNull(aggregatedMetric.getMin());
	assertNull(aggregatedMetric.getSum());
	assertNull(aggregatedMetric.getAvg());
}
 
Example #8
Source File: AggregatingMetricsHandlerTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));
	queryParams.put("agg", Arrays.asList("min", "max", "avg"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(1.0, aggregatedMetric.getMin(), 0.1);
	assertEquals(3.0, aggregatedMetric.getMax(), 0.1);
	assertEquals(2.0, aggregatedMetric.getAvg(), 0.1);
	assertNull(aggregatedMetric.getSum());
}
 
Example #9
Source File: AggregatingMetricsHandlerTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testAvgAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));
	queryParams.put("agg", Collections.singletonList("avg"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(2.0, aggregatedMetric.getAvg(), 0.1);
	assertNull(aggregatedMetric.getMin());
	assertNull(aggregatedMetric.getMax());
	assertNull(aggregatedMetric.getSum());
}
 
Example #10
Source File: AggregatingMetricsHandlerTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSumAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));
	queryParams.put("agg", Collections.singletonList("sum"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(4.0, aggregatedMetric.getSum(), 0.1);
	assertNull(aggregatedMetric.getMin());
	assertNull(aggregatedMetric.getMax());
	assertNull(aggregatedMetric.getAvg());
}
 
Example #11
Source File: AggregatingMetricsHandlerTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMaxAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));
	queryParams.put("agg", Collections.singletonList("max"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(3.0, aggregatedMetric.getMax(), 0.1);
	assertNull(aggregatedMetric.getMin());
	assertNull(aggregatedMetric.getSum());
	assertNull(aggregatedMetric.getAvg());
}
 
Example #12
Source File: AggregatingMetricsHandlerTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMinAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));
	queryParams.put("agg", Collections.singletonList("min"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(1.0, aggregatedMetric.getMin(), 0.1);
	assertNull(aggregatedMetric.getMax());
	assertNull(aggregatedMetric.getSum());
	assertNull(aggregatedMetric.getAvg());
}
 
Example #13
Source File: AbstractAggregatingMetricsHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts and aggregates all requested metrics from the given metric stores, and maps the result to a JSON string.
 *
 * @param stores available metrics
 * @param requestedMetrics ids of requested metrics
 * @param requestedAggregationsFactories requested aggregations
 * @return JSON string containing the requested metrics
 */
private AggregatedMetricsResponseBody getAggregatedMetricValues(
		Collection<? extends MetricStore.ComponentMetricStore> stores,
		List<String> requestedMetrics,
		MetricAccumulatorFactory requestedAggregationsFactories) {

	Collection<AggregatedMetric> aggregatedMetrics = new ArrayList<>(requestedMetrics.size());
	for (String requestedMetric : requestedMetrics) {
		final Collection<Double> values = new ArrayList<>(stores.size());
		try {
			for (MetricStore.ComponentMetricStore store : stores) {
				String stringValue = store.metrics.get(requestedMetric);
				if (stringValue != null) {
					values.add(Double.valueOf(stringValue));
				}
			}
		} catch (NumberFormatException nfe) {
			log.warn("The metric {} is not numeric and can't be aggregated.", requestedMetric, nfe);
			// metric is not numeric so we can't perform aggregations => ignore it
			continue;
		}
		if (!values.isEmpty()) {

			Iterator<Double> valuesIterator = values.iterator();
			MetricAccumulator acc = requestedAggregationsFactories.get(requestedMetric, valuesIterator.next());
			valuesIterator.forEachRemaining(acc::add);

			aggregatedMetrics.add(acc.get());
		} else {
			return new AggregatedMetricsResponseBody(Collections.emptyList());
		}
	}
	return new AggregatedMetricsResponseBody(aggregatedMetrics);
}
 
Example #14
Source File: AggregatingMetricsHandlerTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));
	queryParams.put("agg", Arrays.asList("min", "max", "avg"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(1.0, aggregatedMetric.getMin(), 0.1);
	assertEquals(3.0, aggregatedMetric.getMax(), 0.1);
	assertEquals(2.0, aggregatedMetric.getAvg(), 0.1);
	assertNull(aggregatedMetric.getSum());
}
 
Example #15
Source File: AggregatingMetricsHandlerTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testAvgAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));
	queryParams.put("agg", Collections.singletonList("avg"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(2.0, aggregatedMetric.getAvg(), 0.1);
	assertNull(aggregatedMetric.getMin());
	assertNull(aggregatedMetric.getMax());
	assertNull(aggregatedMetric.getSum());
}
 
Example #16
Source File: AggregatingMetricsHandlerTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSumAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));
	queryParams.put("agg", Collections.singletonList("sum"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(4.0, aggregatedMetric.getSum(), 0.1);
	assertNull(aggregatedMetric.getMin());
	assertNull(aggregatedMetric.getMax());
	assertNull(aggregatedMetric.getAvg());
}
 
Example #17
Source File: AggregatingMetricsHandlerTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMinAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));
	queryParams.put("agg", Collections.singletonList("min"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(1.0, aggregatedMetric.getMin(), 0.1);
	assertNull(aggregatedMetric.getMax());
	assertNull(aggregatedMetric.getSum());
	assertNull(aggregatedMetric.getAvg());
}
 
Example #18
Source File: AbstractAggregatingMetricsHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts and aggregates all requested metrics from the given metric stores, and maps the result to a JSON string.
 *
 * @param stores available metrics
 * @param requestedMetrics ids of requested metrics
 * @param requestedAggregationsFactories requested aggregations
 * @return JSON string containing the requested metrics
 */
private AggregatedMetricsResponseBody getAggregatedMetricValues(
		Collection<? extends MetricStore.ComponentMetricStore> stores,
		List<String> requestedMetrics,
		MetricAccumulatorFactory requestedAggregationsFactories) {

	Collection<AggregatedMetric> aggregatedMetrics = new ArrayList<>(requestedMetrics.size());
	for (String requestedMetric : requestedMetrics) {
		final Collection<Double> values = new ArrayList<>(stores.size());
		try {
			for (MetricStore.ComponentMetricStore store : stores) {
				String stringValue = store.metrics.get(requestedMetric);
				if (stringValue != null) {
					values.add(Double.valueOf(stringValue));
				}
			}
		} catch (NumberFormatException nfe) {
			log.warn("The metric {} is not numeric and can't be aggregated.", requestedMetric, nfe);
			// metric is not numeric so we can't perform aggregations => ignore it
			continue;
		}
		if (!values.isEmpty()) {

			Iterator<Double> valuesIterator = values.iterator();
			MetricAccumulator acc = requestedAggregationsFactories.get(requestedMetric, valuesIterator.next());
			valuesIterator.forEachRemaining(acc::add);

			aggregatedMetrics.add(acc.get());
		} else {
			return new AggregatedMetricsResponseBody(Collections.emptyList());
		}
	}
	return new AggregatedMetricsResponseBody(aggregatedMetrics);
}
 
Example #19
Source File: AggregatingMetricsHandlerTestBase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));
	queryParams.put("agg", Arrays.asList("min", "max", "avg"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(1.0, aggregatedMetric.getMin(), 0.1);
	assertEquals(3.0, aggregatedMetric.getMax(), 0.1);
	assertEquals(2.0, aggregatedMetric.getAvg(), 0.1);
	assertNull(aggregatedMetric.getSum());
}
 
Example #20
Source File: AggregatingMetricsHandlerTestBase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testAvgAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));
	queryParams.put("agg", Collections.singletonList("avg"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(2.0, aggregatedMetric.getAvg(), 0.1);
	assertNull(aggregatedMetric.getMin());
	assertNull(aggregatedMetric.getMax());
	assertNull(aggregatedMetric.getSum());
}
 
Example #21
Source File: AggregatingMetricsHandlerTestBase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testSumAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));
	queryParams.put("agg", Collections.singletonList("sum"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(4.0, aggregatedMetric.getSum(), 0.1);
	assertNull(aggregatedMetric.getMin());
	assertNull(aggregatedMetric.getMax());
	assertNull(aggregatedMetric.getAvg());
}
 
Example #22
Source File: AggregatingMetricsHandlerTestBase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testMaxAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));
	queryParams.put("agg", Collections.singletonList("max"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(3.0, aggregatedMetric.getMax(), 0.1);
	assertNull(aggregatedMetric.getMin());
	assertNull(aggregatedMetric.getSum());
	assertNull(aggregatedMetric.getAvg());
}
 
Example #23
Source File: AggregatingMetricsHandlerTestBase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testMinAggregation() throws Exception {
	Map<String, List<String>> queryParams = new HashMap<>(4);
	queryParams.put("get", Collections.singletonList("abc.metric1"));
	queryParams.put("agg", Collections.singletonList("min"));

	HandlerRequest<EmptyRequestBody, P> request = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		handler.getMessageHeaders().getUnresolvedMessageParameters(),
		pathParameters,
		queryParams
	);

	AggregatedMetricsResponseBody response = handler.handleRequest(request, MOCK_DISPATCHER_GATEWAY)
		.get();

	Collection<AggregatedMetric> aggregatedMetrics = response.getMetrics();

	assertEquals(1, aggregatedMetrics.size());
	AggregatedMetric aggregatedMetric = aggregatedMetrics.iterator().next();

	assertEquals("abc.metric1", aggregatedMetric.getId());
	assertEquals(1.0, aggregatedMetric.getMin(), 0.1);
	assertNull(aggregatedMetric.getMax());
	assertNull(aggregatedMetric.getSum());
	assertNull(aggregatedMetric.getAvg());
}
 
Example #24
Source File: AbstractAggregatingMetricsHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts and aggregates all requested metrics from the given metric stores, and maps the result to a JSON string.
 *
 * @param stores available metrics
 * @param requestedMetrics ids of requested metrics
 * @param requestedAggregationsFactories requested aggregations
 * @return JSON string containing the requested metrics
 */
private AggregatedMetricsResponseBody getAggregatedMetricValues(
		Collection<? extends MetricStore.ComponentMetricStore> stores,
		List<String> requestedMetrics,
		MetricAccumulatorFactory requestedAggregationsFactories) {

	Collection<AggregatedMetric> aggregatedMetrics = new ArrayList<>(requestedMetrics.size());
	for (String requestedMetric : requestedMetrics) {
		final Collection<Double> values = new ArrayList<>(stores.size());
		try {
			for (MetricStore.ComponentMetricStore store : stores) {
				String stringValue = store.metrics.get(requestedMetric);
				if (stringValue != null) {
					values.add(Double.valueOf(stringValue));
				}
			}
		} catch (NumberFormatException nfe) {
			log.warn("The metric {} is not numeric and can't be aggregated.", requestedMetric, nfe);
			// metric is not numeric so we can't perform aggregations => ignore it
			continue;
		}
		if (!values.isEmpty()) {

			Iterator<Double> valuesIterator = values.iterator();
			MetricAccumulator acc = requestedAggregationsFactories.get(requestedMetric, valuesIterator.next());
			valuesIterator.forEachRemaining(acc::add);

			aggregatedMetrics.add(acc.get());
		} else {
			return new AggregatedMetricsResponseBody(Collections.emptyList());
		}
	}
	return new AggregatedMetricsResponseBody(aggregatedMetrics);
}
 
Example #25
Source File: AbstractAggregatingMetricsHandler.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
protected CompletableFuture<AggregatedMetricsResponseBody> handleRequest(@Nonnull HandlerRequest<EmptyRequestBody, P> request, @Nonnull RestfulGateway gateway) throws RestHandlerException {
	return CompletableFuture.supplyAsync(
		() -> {
			try {
				fetcher.update();
				List<String> requestedMetrics = request.getQueryParameter(MetricsFilterParameter.class);
				List<MetricsAggregationParameter.AggregationMode> requestedAggregations = request.getQueryParameter(MetricsAggregationParameter.class);
				MetricStore store = fetcher.getMetricStore();

				Collection<? extends MetricStore.ComponentMetricStore> stores = getStores(store, request);

				if (requestedMetrics.isEmpty()) {
					Collection<String> list = getAvailableMetrics(stores);
					return new AggregatedMetricsResponseBody(
						list.stream()
							.map(AggregatedMetric::new)
							.collect(Collectors.toList())
					);
				}

				DoubleAccumulator.DoubleMinimumFactory minimumFactory = null;
				DoubleAccumulator.DoubleMaximumFactory maximumFactory = null;
				DoubleAccumulator.DoubleAverageFactory averageFactory = null;
				DoubleAccumulator.DoubleSumFactory sumFactory = null;
				// by default we return all aggregations
				if (requestedAggregations.isEmpty()) {
					minimumFactory = DoubleAccumulator.DoubleMinimumFactory.get();
					maximumFactory = DoubleAccumulator.DoubleMaximumFactory.get();
					averageFactory = DoubleAccumulator.DoubleAverageFactory.get();
					sumFactory = DoubleAccumulator.DoubleSumFactory.get();
				} else {
					for (MetricsAggregationParameter.AggregationMode aggregation : requestedAggregations) {
						switch (aggregation) {
							case MIN:
								minimumFactory = DoubleAccumulator.DoubleMinimumFactory.get();
								break;
							case MAX:
								maximumFactory = DoubleAccumulator.DoubleMaximumFactory.get();
								break;
							case AVG:
								averageFactory = DoubleAccumulator.DoubleAverageFactory.get();
								break;
							case SUM:
								sumFactory = DoubleAccumulator.DoubleSumFactory.get();
								break;
							default:
								log.warn("Unsupported aggregation specified: {}", aggregation);
						}
					}
				}
				MetricAccumulatorFactory metricAccumulatorFactory = new MetricAccumulatorFactory(minimumFactory, maximumFactory, averageFactory, sumFactory);

				return getAggregatedMetricValues(stores, requestedMetrics, metricAccumulatorFactory);
			} catch (Exception e) {
				log.warn("Could not retrieve metrics.", e);
				throw new CompletionException(new RestHandlerException("Could not retrieve metrics.", HttpResponseStatus.INTERNAL_SERVER_ERROR));
			}
		},
		executor);
}
 
Example #26
Source File: AbstractAggregatingMetricsHandler.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected CompletableFuture<AggregatedMetricsResponseBody> handleRequest(@Nonnull HandlerRequest<EmptyRequestBody, P> request, @Nonnull RestfulGateway gateway) throws RestHandlerException {
	return CompletableFuture.supplyAsync(
		() -> {
			try {
				fetcher.update();
				List<String> requestedMetrics = request.getQueryParameter(MetricsFilterParameter.class);
				List<MetricsAggregationParameter.AggregationMode> requestedAggregations = request.getQueryParameter(MetricsAggregationParameter.class);
				MetricStore store = fetcher.getMetricStore();

				Collection<? extends MetricStore.ComponentMetricStore> stores = getStores(store, request);

				if (requestedMetrics.isEmpty()) {
					Collection<String> list = getAvailableMetrics(stores);
					return new AggregatedMetricsResponseBody(
						list.stream()
							.map(AggregatedMetric::new)
							.collect(Collectors.toList())
					);
				}

				DoubleAccumulator.DoubleMinimumFactory minimumFactory = null;
				DoubleAccumulator.DoubleMaximumFactory maximumFactory = null;
				DoubleAccumulator.DoubleAverageFactory averageFactory = null;
				DoubleAccumulator.DoubleSumFactory sumFactory = null;
				// by default we return all aggregations
				if (requestedAggregations.isEmpty()) {
					minimumFactory = DoubleAccumulator.DoubleMinimumFactory.get();
					maximumFactory = DoubleAccumulator.DoubleMaximumFactory.get();
					averageFactory = DoubleAccumulator.DoubleAverageFactory.get();
					sumFactory = DoubleAccumulator.DoubleSumFactory.get();
				} else {
					for (MetricsAggregationParameter.AggregationMode aggregation : requestedAggregations) {
						switch (aggregation) {
							case MIN:
								minimumFactory = DoubleAccumulator.DoubleMinimumFactory.get();
								break;
							case MAX:
								maximumFactory = DoubleAccumulator.DoubleMaximumFactory.get();
								break;
							case AVG:
								averageFactory = DoubleAccumulator.DoubleAverageFactory.get();
								break;
							case SUM:
								sumFactory = DoubleAccumulator.DoubleSumFactory.get();
								break;
							default:
								log.warn("Unsupported aggregation specified: {}", aggregation);
						}
					}
				}
				MetricAccumulatorFactory metricAccumulatorFactory = new MetricAccumulatorFactory(minimumFactory, maximumFactory, averageFactory, sumFactory);

				return getAggregatedMetricValues(stores, requestedMetrics, metricAccumulatorFactory);
			} catch (Exception e) {
				log.warn("Could not retrieve metrics.", e);
				throw new CompletionException(new RestHandlerException("Could not retrieve metrics.", HttpResponseStatus.INTERNAL_SERVER_ERROR));
			}
		},
		executor);
}
 
Example #27
Source File: AbstractAggregatingMetricsHandler.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected CompletableFuture<AggregatedMetricsResponseBody> handleRequest(@Nonnull HandlerRequest<EmptyRequestBody, P> request, @Nonnull RestfulGateway gateway) throws RestHandlerException {
	return CompletableFuture.supplyAsync(
		() -> {
			try {
				fetcher.update();
				List<String> requestedMetrics = request.getQueryParameter(MetricsFilterParameter.class);
				List<MetricsAggregationParameter.AggregationMode> requestedAggregations = request.getQueryParameter(MetricsAggregationParameter.class);
				MetricStore store = fetcher.getMetricStore();

				Collection<? extends MetricStore.ComponentMetricStore> stores = getStores(store, request);

				if (requestedMetrics.isEmpty()) {
					Collection<String> list = getAvailableMetrics(stores);
					return new AggregatedMetricsResponseBody(
						list.stream()
							.map(AggregatedMetric::new)
							.collect(Collectors.toList())
					);
				}

				DoubleAccumulator.DoubleMinimumFactory minimumFactory = null;
				DoubleAccumulator.DoubleMaximumFactory maximumFactory = null;
				DoubleAccumulator.DoubleAverageFactory averageFactory = null;
				DoubleAccumulator.DoubleSumFactory sumFactory = null;
				// by default we return all aggregations
				if (requestedAggregations.isEmpty()) {
					minimumFactory = DoubleAccumulator.DoubleMinimumFactory.get();
					maximumFactory = DoubleAccumulator.DoubleMaximumFactory.get();
					averageFactory = DoubleAccumulator.DoubleAverageFactory.get();
					sumFactory = DoubleAccumulator.DoubleSumFactory.get();
				} else {
					for (MetricsAggregationParameter.AggregationMode aggregation : requestedAggregations) {
						switch (aggregation) {
							case MIN:
								minimumFactory = DoubleAccumulator.DoubleMinimumFactory.get();
								break;
							case MAX:
								maximumFactory = DoubleAccumulator.DoubleMaximumFactory.get();
								break;
							case AVG:
								averageFactory = DoubleAccumulator.DoubleAverageFactory.get();
								break;
							case SUM:
								sumFactory = DoubleAccumulator.DoubleSumFactory.get();
								break;
							default:
								log.warn("Unsupported aggregation specified: {}", aggregation);
						}
					}
				}
				MetricAccumulatorFactory metricAccumulatorFactory = new MetricAccumulatorFactory(minimumFactory, maximumFactory, averageFactory, sumFactory);

				return getAggregatedMetricValues(stores, requestedMetrics, metricAccumulatorFactory);
			} catch (Exception e) {
				log.warn("Could not retrieve metrics.", e);
				throw new CompletionException(new RestHandlerException("Could not retrieve metrics.", HttpResponseStatus.INTERNAL_SERVER_ERROR));
			}
		},
		executor);
}