Search in sources :

Example 6 with MetricsRegistry

use of org.apache.samza.metrics.MetricsRegistry 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 7 with MetricsRegistry

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

the class TestLocalTableRead method setUp.

@Before
public void setUp() {
    keys = Arrays.asList("k1", "k2", "k3");
    values = new HashMap<>();
    values.put("k1", "v1");
    values.put("k2", "v2");
    values.put("k3", null);
    kvStore = mock(KeyValueStore.class);
    when(kvStore.get("k1")).thenReturn("v1");
    when(kvStore.get("k2")).thenReturn("v2");
    when(kvStore.getAll(keys)).thenReturn(values);
    getNs = new Timer("");
    getAllNs = new Timer("");
    numGets = new Counter("");
    numGetAlls = new Counter("");
    getCallbackNs = new Timer("");
    numMissedLookups = new Counter("");
    metricsRegistry = mock(MetricsRegistry.class);
    String groupName = LocalTable.class.getSimpleName();
    when(metricsRegistry.newCounter(groupName, TABLE_ID + "-num-gets")).thenReturn(numGets);
    when(metricsRegistry.newCounter(groupName, TABLE_ID + "-num-getAlls")).thenReturn(numGetAlls);
    when(metricsRegistry.newCounter(groupName, TABLE_ID + "-num-missed-lookups")).thenReturn(numMissedLookups);
    when(metricsRegistry.newTimer(groupName, TABLE_ID + "-get-ns")).thenReturn(getNs);
    when(metricsRegistry.newTimer(groupName, TABLE_ID + "-getAll-ns")).thenReturn(getAllNs);
    when(metricsRegistry.newTimer(groupName, TABLE_ID + "-get-callback-ns")).thenReturn(getCallbackNs);
}
Also used : MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) Counter(org.apache.samza.metrics.Counter) Timer(org.apache.samza.metrics.Timer) Before(org.junit.Before)

Example 8 with MetricsRegistry

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

the class StreamAppender method setupSystem.

/**
 * This should only be called after verifying that the {@link LoggingContextHolder} has the config.
 */
protected void setupSystem() {
    config = getConfig();
    Log4jSystemConfig log4jSystemConfig = new Log4jSystemConfig(config);
    if (streamName == null) {
        streamName = getStreamName(log4jSystemConfig.getJobName(), log4jSystemConfig.getJobId());
    }
    // TODO we need the ACTUAL metrics registry, or the metrics won't get reported by the metric reporters!
    MetricsRegistry metricsRegistry = new MetricsRegistryMap();
    metrics = new StreamAppenderMetrics("stream-appender", metricsRegistry);
    String systemName = log4jSystemConfig.getSystemName();
    String systemFactoryName = log4jSystemConfig.getSystemFactory(systemName).orElseThrow(() -> new SamzaException("Could not figure out \"" + systemName + "\" system factory for log4j StreamAppender to use"));
    SystemFactory systemFactory = ReflectionUtil.getObj(systemFactoryName, SystemFactory.class);
    setSerde(log4jSystemConfig, systemName, streamName);
    if (config.getBoolean(CREATE_STREAM_ENABLED, false)) {
        int streamPartitionCount = getPartitionCount();
        System.out.println("[StreamAppender] creating stream " + streamName + " with partition count " + streamPartitionCount);
        StreamSpec streamSpec = StreamSpec.createStreamAppenderStreamSpec(streamName, systemName, streamPartitionCount);
        // SystemAdmin only needed for stream creation here.
        SystemAdmin systemAdmin = systemFactory.getAdmin(systemName, config);
        systemAdmin.start();
        systemAdmin.createStream(streamSpec);
        systemAdmin.stop();
    }
    systemProducer = systemFactory.getProducer(systemName, config, metricsRegistry, this.getClass().getSimpleName());
    systemStream = new SystemStream(systemName, streamName);
    systemProducer.register(SOURCE);
    systemProducer.start();
    log.info(SOURCE + " has been registered in " + systemName + ". So all the logs will be sent to " + streamName + " in " + systemName + ". Logs are partitioned by " + key);
    startTransferThread();
}
Also used : StreamSpec(org.apache.samza.system.StreamSpec) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) SystemFactory(org.apache.samza.system.SystemFactory) SystemStream(org.apache.samza.system.SystemStream) SystemAdmin(org.apache.samza.system.SystemAdmin) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Log4jSystemConfig(org.apache.samza.config.Log4jSystemConfig) SamzaException(org.apache.samza.SamzaException)

Example 9 with MetricsRegistry

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

the class TestMonitorService method testShouldNotFailWhenTheMonitorFactoryClassIsNotDefined.

@Test
public void testShouldNotFailWhenTheMonitorFactoryClassIsNotDefined() throws Exception {
    // Test that when MonitorFactoryClass is not defined in the config, monitor service
    // should not fail.
    Map<String, String> configMap = ImmutableMap.of("monitor.monitor1.config.key1", "configValue1", "monitor.monitor1.config.key2", "configValue2", String.format("monitor.MOCK_MONITOR.%s", CONFIG_MONITOR_FACTORY_CLASS), MockMonitorFactory.class.getCanonicalName());
    SamzaRestConfig config = new SamzaRestConfig(new MapConfig(configMap));
    class SamzaMonitorServiceTest extends SamzaMonitorService {

        MetricsRegistry metricsRegistry;

        public SamzaMonitorServiceTest(SamzaRestConfig config, MetricsRegistry metricsRegistry) {
            super(config, metricsRegistry);
            this.metricsRegistry = metricsRegistry;
        }

        @Override
        public void createSchedulerAndScheduleMonitor(String monitorName, MonitorConfig monitorConfig, long schedulingIntervalInMs) {
            try {
                // immediately run monitor, without scheduling
                instantiateMonitor(monitorName, monitorConfig, metricsRegistry).monitor();
            } catch (Exception e) {
                fail();
            }
        }
    }
    SamzaMonitorService monitorService = new SamzaMonitorServiceTest(config, METRICS_REGISTRY);
    try {
        monitorService.start();
    } catch (Exception e) {
        fail();
    }
    Mockito.verify(MockMonitorFactory.MOCK_MONITOR, Mockito.times(1)).monitor();
}
Also used : MockMonitorFactory(org.apache.samza.monitor.mock.MockMonitorFactory) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) NoOpMetricsRegistry(org.apache.samza.util.NoOpMetricsRegistry) SamzaRestConfig(org.apache.samza.rest.SamzaRestConfig) MapConfig(org.apache.samza.config.MapConfig) SamzaException(org.apache.samza.SamzaException) Test(org.junit.Test)

Example 10 with MetricsRegistry

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

the class TestInputRegexMonitor method setUp.

@Before
public void setUp() {
    inputStreamsDiscovered = new HashSet<>();
    Map<String, Pattern> patternMap = new HashMap<>();
    patternMap.put(systemName, Pattern.compile("test-.*"));
    StreamMetadataCache mockStreamMetadataCache = new MockStreamMetadataCache(null, 1, null);
    MetricsRegistry metrics = Mockito.mock(MetricsRegistry.class);
    this.callbackCount = new CountDownLatch(expectedNumberOfCallbacks);
    // Creating an streamRegexMonitor with empty-input set and test-.* regex input
    this.streamRegexMonitor = new StreamRegexMonitor(new HashSet<>(), patternMap, mockStreamMetadataCache, metrics, inputRegexMs, new StreamRegexMonitor.Callback() {

        @Override
        public void onInputStreamsChanged(Set<SystemStream> initialInputSet, Set<SystemStream> newInputStreams, Map<String, Pattern> regexesMonitored) {
            callbackCount.countDown();
            inputStreamsDiscovered.addAll(newInputStreams);
            // Check that the newInputStream discovered is "kafka" "Test-1"
            Assert.assertTrue(inputStreamsDiscovered.size() == 1);
            Assert.assertTrue(inputStreamsDiscovered.contains(sampleStream));
        }
    });
}
Also used : StreamMetadataCache(org.apache.samza.system.StreamMetadataCache) Pattern(java.util.regex.Pattern) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Before(org.junit.Before)

Aggregations

MetricsRegistry (org.apache.samza.metrics.MetricsRegistry)18 HashMap (java.util.HashMap)7 Counter (org.apache.samza.metrics.Counter)7 Timer (org.apache.samza.metrics.Timer)7 MapConfig (org.apache.samza.config.MapConfig)6 Map (java.util.Map)5 Config (org.apache.samza.config.Config)5 Context (org.apache.samza.context.Context)5 TaskModel (org.apache.samza.job.model.TaskModel)4 MetricsRegistryMap (org.apache.samza.metrics.MetricsRegistryMap)4 Before (org.junit.Before)4 File (java.io.File)3 Set (java.util.Set)3 SamzaException (org.apache.samza.SamzaException)3 TaskName (org.apache.samza.container.TaskName)3 ContainerContext (org.apache.samza.context.ContainerContext)3 ContainerModel (org.apache.samza.job.model.ContainerModel)3 JobModel (org.apache.samza.job.model.JobModel)3 Gauge (org.apache.samza.metrics.Gauge)3 Test (org.junit.Test)3