Search in sources :

Example 1 with MockMetricsReporter

use of org.apache.kafka.connect.runtime.MockConnectMetrics.MockMetricsReporter in project kafka by apache.

the class WorkerTest method testWorkerMetrics.

@Test
public void testWorkerMetrics() throws Exception {
    expectConverters();
    expectStartStorage();
    expectFileConfigProvider();
    // Create
    EasyMock.expect(plugins.currentThreadLoader()).andReturn(delegatingLoader).times(2);
    EasyMock.expect(plugins.newConnector(WorkerTestConnector.class.getName())).andReturn(sourceConnector);
    EasyMock.expect(sourceConnector.version()).andReturn("1.0");
    Map<String, String> props = new HashMap<>();
    props.put(SinkConnectorConfig.TOPICS_CONFIG, "foo,bar");
    props.put(ConnectorConfig.TASKS_MAX_CONFIG, "1");
    props.put(ConnectorConfig.NAME_CONFIG, CONNECTOR_ID);
    props.put(ConnectorConfig.CONNECTOR_CLASS_CONFIG, WorkerTestConnector.class.getName());
    EasyMock.expect(sourceConnector.version()).andReturn("1.0");
    EasyMock.expect(plugins.compareAndSwapLoaders(sourceConnector)).andReturn(delegatingLoader).times(2);
    sourceConnector.initialize(anyObject(ConnectorContext.class));
    EasyMock.expectLastCall();
    sourceConnector.start(props);
    EasyMock.expectLastCall();
    EasyMock.expect(Plugins.compareAndSwapLoaders(delegatingLoader)).andReturn(pluginLoader).times(2);
    connectorStatusListener.onStartup(CONNECTOR_ID);
    EasyMock.expectLastCall();
    // Remove
    sourceConnector.stop();
    EasyMock.expectLastCall();
    connectorStatusListener.onShutdown(CONNECTOR_ID);
    EasyMock.expectLastCall();
    expectStopStorage();
    expectClusterId();
    PowerMock.replayAll();
    Worker worker = new Worker("worker-1", Time.SYSTEM, plugins, config, offsetBackingStore, noneConnectorClientConfigOverridePolicy);
    MetricName name = worker.metrics().metrics().metricName("test.avg", "grp1");
    worker.metrics().metrics().addMetric(name, new Avg());
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectInstance> ret = server.queryMBeans(null, null);
    List<MetricsReporter> list = worker.metrics().metrics().reporters();
    for (MetricsReporter reporter : list) {
        if (reporter instanceof MockMetricsReporter) {
            MockMetricsReporter mockMetricsReporter = (MockMetricsReporter) reporter;
            // verify connect cluster is set in MetricsContext
            assertEquals(CLUSTER_ID, mockMetricsReporter.getMetricsContext().contextLabels().get(WorkerConfig.CONNECT_KAFKA_CLUSTER_ID));
        }
    }
    // verify metric is created with correct jmx prefix
    assertNotNull(server.getObjectInstance(new ObjectName("kafka.connect:type=grp1")));
}
Also used : MockMetricsReporter(org.apache.kafka.connect.runtime.MockConnectMetrics.MockMetricsReporter) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName) MetricName(org.apache.kafka.common.MetricName) Avg(org.apache.kafka.common.metrics.stats.Avg) MockMetricsReporter(org.apache.kafka.connect.runtime.MockConnectMetrics.MockMetricsReporter) MetricsReporter(org.apache.kafka.common.metrics.MetricsReporter) ConnectorContext(org.apache.kafka.connect.connector.ConnectorContext) MBeanServer(javax.management.MBeanServer) ThreadedTest(org.apache.kafka.connect.util.ThreadedTest) Test(org.junit.Test) ParameterizedTest(org.apache.kafka.connect.util.ParameterizedTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 MBeanServer (javax.management.MBeanServer)1 ObjectInstance (javax.management.ObjectInstance)1 ObjectName (javax.management.ObjectName)1 MetricName (org.apache.kafka.common.MetricName)1 MetricsReporter (org.apache.kafka.common.metrics.MetricsReporter)1 Avg (org.apache.kafka.common.metrics.stats.Avg)1 ConnectorContext (org.apache.kafka.connect.connector.ConnectorContext)1 MockMetricsReporter (org.apache.kafka.connect.runtime.MockConnectMetrics.MockMetricsReporter)1 ParameterizedTest (org.apache.kafka.connect.util.ParameterizedTest)1 ThreadedTest (org.apache.kafka.connect.util.ThreadedTest)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1