Search in sources :

Example 6 with Timer

use of org.apache.samza.metrics.Timer in project samza by apache.

the class TestNoOpMetricsRegistry method testNoOpMetricsHappyPath.

@Test
public void testNoOpMetricsHappyPath() {
    NoOpMetricsRegistry registry = new NoOpMetricsRegistry();
    Counter counter1 = registry.newCounter("testc", "a");
    Counter counter2 = registry.newCounter("testc", "b");
    Counter counter3 = registry.newCounter("testc2", "c");
    Gauge<String> gauge1 = registry.newGauge("testg", "a", "1");
    Gauge<String> gauge2 = registry.newGauge("testg", "b", "2");
    Gauge<String> gauge3 = registry.newGauge("testg", "c", "3");
    Gauge<String> gauge4 = registry.newGauge("testg2", "d", "4");
    Timer timer1 = registry.newTimer("testt", "a");
    Timer timer2 = registry.newTimer("testt", "b");
    Timer timer3 = registry.newTimer("testt2", "c");
    counter1.inc();
    counter2.inc(2);
    counter3.inc(4);
    gauge1.set("5");
    gauge2.set("6");
    gauge3.set("7");
    gauge4.set("8");
    timer1.update(1L);
    timer2.update(2L);
    timer3.update(3L);
    assertEquals(1, counter1.getCount());
    assertEquals(2, counter2.getCount());
    assertEquals(4, counter3.getCount());
    assertEquals("5", gauge1.getValue());
    assertEquals("6", gauge2.getValue());
    assertEquals("7", gauge3.getValue());
    assertEquals("8", gauge4.getValue());
    assertEquals(1, timer1.getSnapshot().getAverage(), 0);
    assertEquals(2, timer2.getSnapshot().getAverage(), 0);
    assertEquals(3, timer3.getSnapshot().getAverage(), 0);
}
Also used : Counter(org.apache.samza.metrics.Counter) Timer(org.apache.samza.metrics.Timer) Test(org.junit.Test)

Example 7 with Timer

use of org.apache.samza.metrics.Timer in project samza by apache.

the class TestRemoteTable method getMockContext.

public static Context getMockContext() {
    Context context = new MockContext();
    MetricsRegistry metricsRegistry = mock(MetricsRegistry.class);
    doAnswer(args -> new Timer((String) args.getArguments()[0])).when(metricsRegistry).newTimer(anyString(), anyString());
    doAnswer(args -> new Counter((String) args.getArguments()[0])).when(metricsRegistry).newCounter(anyString(), anyString());
    doAnswer(args -> new Gauge((String) args.getArguments()[0], 0)).when(metricsRegistry).newGauge(anyString(), any());
    doReturn(metricsRegistry).when(context.getContainerContext()).getContainerMetricsRegistry();
    return context;
}
Also used : MockContext(org.apache.samza.context.MockContext) Context(org.apache.samza.context.Context) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) MockContext(org.apache.samza.context.MockContext) Counter(org.apache.samza.metrics.Counter) Timer(org.apache.samza.metrics.Timer) Gauge(org.apache.samza.metrics.Gauge)

Example 8 with Timer

use of org.apache.samza.metrics.Timer in project samza by apache.

the class MetricsSnapshotReporter method innerRun.

public void innerRun() {
    LOG.debug("Begin flushing metrics.");
    for (MetricsRegistryWithSource metricsRegistryWithSource : this.registries) {
        String source = metricsRegistryWithSource.getSource();
        ReadableMetricsRegistry registry = metricsRegistryWithSource.getRegistry();
        LOG.debug("Flushing metrics for {}.", source);
        Map<String, Map<String, Object>> metricsMsg = new HashMap<>();
        // metrics
        registry.getGroups().forEach(group -> {
            Map<String, Object> groupMsg = new HashMap<>();
            registry.getGroup(group).forEach((name, metric) -> {
                if (!shouldIgnore(group, name)) {
                    metric.visit(new MetricsVisitor() {

                        @Override
                        public void counter(Counter counter) {
                            groupMsg.put(name, counter.getCount());
                        }

                        @Override
                        public <T> void gauge(Gauge<T> gauge) {
                            groupMsg.put(name, gauge.getValue());
                        }

                        @Override
                        public void timer(Timer timer) {
                            groupMsg.put(name, timer.getSnapshot().getAverage());
                        }
                    });
                }
            });
            // dont emit empty groups
            if (!groupMsg.isEmpty()) {
                metricsMsg.put(group, groupMsg);
            }
        });
        // publish to Kafka only if the metricsMsg carries any metrics
        if (!metricsMsg.isEmpty()) {
            MetricsHeader header = new MetricsHeader(this.jobName, this.jobId, this.containerName, this.executionEnvContainerId, Optional.of(this.samzaEpochId), source, this.version, this.samzaVersion, this.host, this.clock.currentTimeMillis(), this.resetTime);
            Metrics metrics = new Metrics(metricsMsg);
            LOG.debug("Flushing metrics for {} to {} with header and map: header={}, map={}.", source, out, header.getAsMap(), metrics.getAsMap());
            MetricsSnapshot metricsSnapshot = new MetricsSnapshot(header, metrics);
            Object maybeSerialized = (this.serializer != null) ? this.serializer.toBytes(metricsSnapshot) : metricsSnapshot;
            try {
                this.producer.send(source, new OutgoingMessageEnvelope(this.out, this.host, null, maybeSerialized));
                // Always flush, since we don't want metrics to get batched up.
                this.producer.flush(source);
            } catch (Exception e) {
                LOG.error(String.format("Exception when flushing metrics for source %s", source), e);
            }
        }
    }
    LOG.debug("Finished flushing metrics.");
}
Also used : ReadableMetricsRegistry(org.apache.samza.metrics.ReadableMetricsRegistry) HashMap(java.util.HashMap) MetricsVisitor(org.apache.samza.metrics.MetricsVisitor) SamzaException(org.apache.samza.SamzaException) Counter(org.apache.samza.metrics.Counter) Timer(org.apache.samza.metrics.Timer) MetricsRegistryWithSource(org.apache.samza.metrics.MetricsRegistryWithSource) HashMap(java.util.HashMap) Map(java.util.Map) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope)

Example 9 with Timer

use of org.apache.samza.metrics.Timer in project samza by apache.

the class TestOperatorImpl method testOnMessageUpdatesMetrics.

@Test
public void testOnMessageUpdatesMetrics() {
    ReadableMetricsRegistry mockMetricsRegistry = mock(ReadableMetricsRegistry.class);
    when(this.context.getContainerContext().getContainerMetricsRegistry()).thenReturn(mockMetricsRegistry);
    Counter mockCounter = mock(Counter.class);
    Timer mockTimer = mock(Timer.class);
    when(mockMetricsRegistry.newCounter(anyString(), anyString())).thenReturn(mockCounter);
    when(mockMetricsRegistry.newTimer(anyString(), anyString())).thenReturn(mockTimer);
    Object mockTestOpImplOutput = mock(Object.class);
    OperatorImpl<Object, Object> opImpl = new TestOpImpl(mockTestOpImplOutput);
    opImpl.init(this.internalTaskContext);
    // send a message to this operator
    MessageCollector mockCollector = mock(MessageCollector.class);
    TaskCoordinator mockCoordinator = mock(TaskCoordinator.class);
    opImpl.onMessage(mock(Object.class), mockCollector, mockCoordinator);
    // verify that it updates message count and timer metrics
    verify(mockCounter, times(1)).inc();
    verify(mockTimer, times(1)).update(anyLong());
}
Also used : ReadableMetricsRegistry(org.apache.samza.metrics.ReadableMetricsRegistry) Counter(org.apache.samza.metrics.Counter) Timer(org.apache.samza.metrics.Timer) MessageCollector(org.apache.samza.task.MessageCollector) Matchers.anyObject(org.mockito.Matchers.anyObject) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Test(org.junit.Test)

Example 10 with Timer

use of org.apache.samza.metrics.Timer in project samza by apache.

the class TestOperatorImpl method testOnTimerUpdatesMetrics.

@Test
public void testOnTimerUpdatesMetrics() {
    ReadableMetricsRegistry mockMetricsRegistry = mock(ReadableMetricsRegistry.class);
    when(this.context.getContainerContext().getContainerMetricsRegistry()).thenReturn(mockMetricsRegistry);
    Counter mockMessageCounter = mock(Counter.class);
    Timer mockTimer = mock(Timer.class);
    when(mockMetricsRegistry.newCounter(anyString(), anyString())).thenReturn(mockMessageCounter);
    when(mockMetricsRegistry.newTimer(anyString(), anyString())).thenReturn(mockTimer);
    Object mockTestOpImplOutput = mock(Object.class);
    OperatorImpl<Object, Object> opImpl = new TestOpImpl(mockTestOpImplOutput);
    opImpl.init(this.internalTaskContext);
    // send a message to this operator
    MessageCollector mockCollector = mock(MessageCollector.class);
    TaskCoordinator mockCoordinator = mock(TaskCoordinator.class);
    opImpl.onTimer(mockCollector, mockCoordinator);
    // verify that it updates metrics
    verify(mockMessageCounter, times(0)).inc();
    verify(mockTimer, times(1)).update(anyLong());
}
Also used : ReadableMetricsRegistry(org.apache.samza.metrics.ReadableMetricsRegistry) Counter(org.apache.samza.metrics.Counter) Timer(org.apache.samza.metrics.Timer) MessageCollector(org.apache.samza.task.MessageCollector) Matchers.anyObject(org.mockito.Matchers.anyObject) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Test(org.junit.Test)

Aggregations

Timer (org.apache.samza.metrics.Timer)24 Test (org.junit.Test)18 TaskName (org.apache.samza.container.TaskName)14 TaskInstanceMetrics (org.apache.samza.container.TaskInstanceMetrics)13 HashMap (java.util.HashMap)12 MapConfig (org.apache.samza.config.MapConfig)12 Partition (org.apache.samza.Partition)11 CheckpointId (org.apache.samza.checkpoint.CheckpointId)11 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)11 ImmutableMap (com.google.common.collect.ImmutableMap)10 File (java.io.File)10 Map (java.util.Map)10 CheckpointManager (org.apache.samza.checkpoint.CheckpointManager)10 Checkpoint (org.apache.samza.checkpoint.Checkpoint)9 Counter (org.apache.samza.metrics.Counter)9 Path (java.nio.file.Path)8 SamzaException (org.apache.samza.SamzaException)6 CheckpointV1 (org.apache.samza.checkpoint.CheckpointV1)6 CheckpointV2 (org.apache.samza.checkpoint.CheckpointV2)6 SystemStream (org.apache.samza.system.SystemStream)6