use of co.cask.cdap.api.metrics.MetricTimeSeries in project cdap by caskdata.
the class MetricsProcessorServiceTest method testMetricsProcessor.
@Test
public void testMetricsProcessor() throws Exception {
injector.getInstance(TransactionManager.class).startAndWait();
injector.getInstance(DatasetOpExecutor.class).startAndWait();
injector.getInstance(DatasetService.class).startAndWait();
zkServer = InMemoryZKServer.builder().build();
zkServer.startAndWait();
Properties kafkaConfig = generateKafkaConfig(tmpFolder1);
EmbeddedKafkaServer kafkaServer = new EmbeddedKafkaServer(kafkaConfig);
kafkaServer.startAndWait();
ZKClientService zkClient = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
zkClient.startAndWait();
KafkaClientService kafkaClient = new ZKKafkaClientService(zkClient);
kafkaClient.startAndWait();
final MetricStore metricStore = injector.getInstance(MetricStore.class);
Set<Integer> partitions = new HashSet<>();
for (int i = 0; i < PARTITION_SIZE; i++) {
partitions.add(i);
}
KafkaPublisher publisher = kafkaClient.getPublisher(KafkaPublisher.Ack.FIRE_AND_FORGET, Compression.SNAPPY);
final KafkaPublisher.Preparer preparer = publisher.prepare(TOPIC_PREFIX);
// Wait for metrics to be successfully published to Kafka. Retry if publishing fails.
Tasks.waitFor(true, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return publishKafkaMetrics(METRICS_CONTEXT, expected, preparer);
}
}, 15, TimeUnit.SECONDS, "Failed to publish correct number of metrics to Kafka");
// Start KafkaMetricsProcessorService after metrics are published to Kafka
KafkaMetricsProcessorService kafkaMetricsProcessorService = new KafkaMetricsProcessorService(kafkaClient, injector.getInstance(MetricDatasetFactory.class), new MetricsMessageCallbackFactory(injector.getInstance(SchemaGenerator.class), injector.getInstance(DatumReaderFactory.class), metricStore, 4), TOPIC_PREFIX, partitions, new NoopMetricsContext());
kafkaMetricsProcessorService.startAndWait();
// Intentionally set queue size to a small value, so that MessagingMetricsProcessorService
// internally can persist metrics when more messages are to be fetched
MessagingMetricsProcessorService messagingMetricsProcessorService = new MessagingMetricsProcessorService(injector.getInstance(MetricDatasetFactory.class), TOPIC_PREFIX, messagingService, injector.getInstance(SchemaGenerator.class), injector.getInstance(DatumReaderFactory.class), metricStore, 1000L, 5, partitions, new NoopMetricsContext(), 50, 0);
messagingMetricsProcessorService.startAndWait();
long startTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
// Publish metrics with messaging service and record expected metrics
for (int i = 10; i < 20; i++) {
publishMessagingMetrics(i, startTime, METRICS_CONTEXT, expected, SYSTEM_METRIC_PREFIX, MetricType.COUNTER);
}
Thread.sleep(500);
// Stop and restart messagingMetricsProcessorService
messagingMetricsProcessorService.stopAndWait();
// Intentionally set queue size to a large value, so that MessagingMetricsProcessorService
// internally only persists metrics during terminating.
messagingMetricsProcessorService = new MessagingMetricsProcessorService(injector.getInstance(MetricDatasetFactory.class), TOPIC_PREFIX, messagingService, injector.getInstance(SchemaGenerator.class), injector.getInstance(DatumReaderFactory.class), metricStore, 500L, 100, partitions, new NoopMetricsContext(), 50, 0);
messagingMetricsProcessorService.startAndWait();
// Publish metrics after MessagingMetricsProcessorService restarts and record expected metrics
for (int i = 20; i < 30; i++) {
publishMessagingMetrics(i, startTime, METRICS_CONTEXT, expected, SYSTEM_METRIC_PREFIX, MetricType.GAUGE);
}
final List<String> missingMetricNames = new ArrayList<>();
// are retrieved when timeout occurs, print out the missing metrics
try {
Tasks.waitFor(true, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return canQueryAllMetrics(metricStore, METRICS_CONTEXT, expected, missingMetricNames);
}
}, 10000, TimeUnit.MILLISECONDS, "Failed to get all metrics");
} catch (TimeoutException e) {
Assert.fail(String.format("Metrics: [%s] cannot be found in the metrics store.", Joiner.on(", ").join(missingMetricNames)));
}
// Query metrics from the metricStore and compare them with the expected ones
assertMetricsResult(metricStore, METRICS_CONTEXT, expected);
// Query for the 5 counter metrics published with messaging between time 5 - 14
Collection<MetricTimeSeries> queryResult = metricStore.query(new MetricDataQuery(5, 14, 1, Integer.MAX_VALUE, ImmutableMap.of(SYSTEM_METRIC_PREFIX + COUNTER_METRIC_NAME, AggregationFunction.SUM), METRICS_CONTEXT, ImmutableList.<String>of(), null));
MetricTimeSeries timeSeries = Iterables.getOnlyElement(queryResult);
Assert.assertEquals(5, timeSeries.getTimeValues().size());
for (TimeValue timeValue : timeSeries.getTimeValues()) {
Assert.assertEquals(1L, timeValue.getValue());
}
// Stop services and servers
kafkaMetricsProcessorService.stopAndWait();
messagingMetricsProcessorService.stopAndWait();
kafkaServer.stopAndWait();
zkServer.stopAndWait();
// Delete all metrics
metricStore.deleteAll();
}
use of co.cask.cdap.api.metrics.MetricTimeSeries in project cdap by caskdata.
the class MetricsQueryHelper method decorate.
private MetricQueryResult decorate(Collection<MetricTimeSeries> series, long startTs, long endTs, int resolution) {
MetricQueryResult.TimeSeries[] serieses = new MetricQueryResult.TimeSeries[series.size()];
int i = 0;
for (MetricTimeSeries timeSeries : series) {
MetricQueryResult.TimeValue[] timeValues = decorate(timeSeries.getTimeValues());
serieses[i++] = new MetricQueryResult.TimeSeries(timeSeries.getMetricName(), tagNamesToHuman(timeSeries.getTagValues()), timeValues);
}
return new MetricQueryResult(startTs, endTs, serieses, resolution);
}
use of co.cask.cdap.api.metrics.MetricTimeSeries in project cdap by caskdata.
the class MetricsQueryHelper method executeQuery.
private MetricQueryResult executeQuery(MetricQueryRequest queryRequest) throws Exception {
if (queryRequest.getMetrics().size() == 0) {
throw new IllegalArgumentException("Missing metrics parameter in the query");
}
Map<String, String> tagsSliceBy = humanToTagNames(transformTagMap(queryRequest.getTags()));
MetricQueryRequest.TimeRange timeRange = queryRequest.getTimeRange();
MetricDataQuery query = new MetricDataQuery(timeRange.getStart(), timeRange.getEnd(), timeRange.getResolutionInSeconds(), timeRange.getCount(), toMetrics(queryRequest.getMetrics()), tagsSliceBy, transformGroupByTags(queryRequest.getGroupBy()), timeRange.getInterpolate());
Collection<MetricTimeSeries> queryResult = metricStore.query(query);
long endTime = timeRange.getEnd();
if (timeRange.getResolutionInSeconds() == Integer.MAX_VALUE && endTime == 0) {
// for aggregate query, we set the end time to be query time (current time)
endTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
}
return decorate(queryResult, timeRange.getStart(), endTime, timeRange.getResolutionInSeconds());
}
use of co.cask.cdap.api.metrics.MetricTimeSeries in project cdap by caskdata.
the class DefaultMetricStore method query.
@Override
public Collection<MetricTimeSeries> query(MetricDataQuery query) {
Collection<TimeSeries> cubeResult = cube.get().query(buildCubeQuery(query));
List<MetricTimeSeries> result = Lists.newArrayList();
for (TimeSeries timeSeries : cubeResult) {
result.add(new MetricTimeSeries(timeSeries.getMeasureName(), timeSeries.getDimensionValues(), timeSeries.getTimeValues()));
}
return result;
}
Aggregations