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")));
}
Aggregations