Search in sources :

Example 6 with MetricsWriter

use of io.cdap.cdap.api.metrics.MetricsWriter in project cdap by cdapio.

the class MetricsWritersMetricsPublisherTest method testPublishFlow.

@Test
public void testPublishFlow() throws Exception {
    int numWriters = 5;
    Map<String, MetricsWriter> writers = getMockWriters(numWriters);
    when(provider.loadMetricsWriters()).thenReturn(writers);
    CConfiguration cConf = CConfiguration.create();
    MetricsWritersMetricsPublisher publisher = new MetricsWritersMetricsPublisher(provider, cConf);
    publisher.initialize();
    // Ensure all writers are initialized
    for (Map.Entry<String, MetricsWriter> entry : writers.entrySet()) {
        MetricsWriter writer = entry.getValue();
        verify(writer, times(1)).initialize(any());
        verify(writer, times(0)).write(any());
    }
    Collection<MetricValues> metricValues = getMockMetricValuesArray(5);
    publisher.publish(metricValues);
    // Ensure all writers receive the same metrics
    for (Map.Entry<String, MetricsWriter> entry : writers.entrySet()) {
        MetricsWriter writer = entry.getValue();
        verify(writer, times(1)).write(metricValues);
    }
    // Ensure if one writer throws exception on write(), rest still get metrics
    MetricsWriter faultyWriter = writers.get(getWriterName(2));
    doThrow(IOException.class).when(faultyWriter).write(metricValues);
    publisher.publish(metricValues);
    for (Map.Entry<String, MetricsWriter> entry : writers.entrySet()) {
        MetricsWriter writer = entry.getValue();
        verify(writer, times(2)).write(metricValues);
    }
    publisher.close();
    // Ensure all writers are closed
    for (Map.Entry<String, MetricsWriter> entry : writers.entrySet()) {
        MetricsWriter writer = entry.getValue();
        verify(writer, times(1)).close();
    }
}
Also used : MetricsWriter(io.cdap.cdap.api.metrics.MetricsWriter) MetricValues(io.cdap.cdap.api.metrics.MetricValues) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) TreeMap(java.util.TreeMap) Map(java.util.Map) Test(org.junit.Test)

Example 7 with MetricsWriter

use of io.cdap.cdap.api.metrics.MetricsWriter in project cdap by cdapio.

the class MetricsWritersMetricsPublisherTest method getMockWriters.

private Map<String, MetricsWriter> getMockWriters(int count) {
    Map<String, MetricsWriter> writers = new TreeMap<>();
    for (int i = 0; i < count; ++i) {
        MetricsWriter writer = mock(MetricsWriter.class);
        String name = getWriterName(i);
        when(writer.getID()).thenReturn(name);
        writers.put(name, writer);
    }
    return writers;
}
Also used : MetricsWriter(io.cdap.cdap.api.metrics.MetricsWriter) TreeMap(java.util.TreeMap)

Example 8 with MetricsWriter

use of io.cdap.cdap.api.metrics.MetricsWriter in project cdap by cdapio.

the class MessagingMetricsProcessorManagerService method startUp.

@Override
protected void startUp() throws Exception {
    MetricStoreMetricsWriter metricsWriter = new MetricStoreMetricsWriter(metricStore);
    DefaultMetricsWriterContext context = new DefaultMetricsWriterContext(metricsContext, cConf, metricsWriter.getID());
    metricsWriter.initialize(context);
    this.metricsWriters.add(metricsWriter);
    for (Map.Entry<String, MetricsWriter> metricsWriterEntry : metricsWriterProvider.loadMetricsWriters().entrySet()) {
        MetricsWriter writer = metricsWriterEntry.getValue();
        this.metricsWriters.add(writer);
        DefaultMetricsWriterContext metricsWriterContext = new DefaultMetricsWriterContext(metricsContext, cConf, writer.getID());
        writer.initialize(metricsWriterContext);
    }
    String processorKey = String.format("metrics.processor.%s", instanceId);
    for (MetricsWriter metricsExtension : this.metricsWriters) {
        MetricsMetaKeyProvider topicIdMetricsKeyProvider = getKeyProvider(metricsExtension, cConf);
        metricsProcessorServices.add(new MessagingMetricsProcessorService(cConf, metricDatasetFactory, messagingService, schemaGenerator, readerFactory, metricsExtension, topicNumbers, metricsContext, metricsProcessIntervalMillis, instanceId, new DefaultMetadataHandler(processorKey, topicIdMetricsKeyProvider), topicIdMetricsKeyProvider));
    }
    for (MessagingMetricsProcessorService processorService : metricsProcessorServices) {
        processorService.startAndWait();
    }
}
Also used : MetricsWriter(io.cdap.cdap.api.metrics.MetricsWriter) Map(java.util.Map)

Example 9 with MetricsWriter

use of io.cdap.cdap.api.metrics.MetricsWriter in project cdap by caskdata.

the class MetricsWritersMetricsPublisherTest method testPublishingWithoutInitialization.

@Test(expected = IllegalStateException.class)
public void testPublishingWithoutInitialization() throws Exception {
    int numWriters = 1;
    Map<String, MetricsWriter> writers = getMockWriters(numWriters);
    when(provider.loadMetricsWriters()).thenReturn(writers);
    CConfiguration cConf = CConfiguration.create();
    MetricsWritersMetricsPublisher publisher = new MetricsWritersMetricsPublisher(provider, cConf);
    publisher.publish(getMockMetricArray(2), new TreeMap<>());
}
Also used : MetricsWriter(io.cdap.cdap.api.metrics.MetricsWriter) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) Test(org.junit.Test)

Example 10 with MetricsWriter

use of io.cdap.cdap.api.metrics.MetricsWriter in project cdap by caskdata.

the class MetricsWritersMetricsPublisherTest method getMockWriters.

private Map<String, MetricsWriter> getMockWriters(int count) {
    Map<String, MetricsWriter> writers = new TreeMap<>();
    for (int i = 0; i < count; ++i) {
        MetricsWriter writer = mock(MetricsWriter.class);
        String name = getWriterName(i);
        when(writer.getID()).thenReturn(name);
        writers.put(name, writer);
    }
    return writers;
}
Also used : MetricsWriter(io.cdap.cdap.api.metrics.MetricsWriter) TreeMap(java.util.TreeMap)

Aggregations

MetricsWriter (io.cdap.cdap.api.metrics.MetricsWriter)12 Map (java.util.Map)8 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)4 TreeMap (java.util.TreeMap)4 Test (org.junit.Test)4 MetricValues (io.cdap.cdap.api.metrics.MetricValues)2 NoopMetricsContext (io.cdap.cdap.api.metrics.NoopMetricsContext)2 DefaultMetricsWriterContext (io.cdap.cdap.metrics.process.DefaultMetricsWriterContext)2 IOException (java.io.IOException)2