public static void main(String[] args) { Map<String, String> metricTags = Collections.singletonMap("client-id", "client-id"); MetricConfig metricConfig = new MetricConfig().tags(metricTags); Metrics metrics = new Metrics(metricConfig); ProducerMetrics metricsRegistry = new ProducerMetrics(metrics); System.out.println(Metrics.toHtmlTable("kafka.producer", metricsRegistry.getAllTemplates())); }
public static Selector createSelector(ChannelBuilder channelBuilder, Time time) { return new Selector(5000, new Metrics(), time, "MetricGroup", channelBuilder, new LogContext()); }
static KafkaAdminClient createInternal(AdminClientConfig config, AdminMetadataManager metadataManager, KafkaClient client, Time time) { Metrics metrics = null; String clientId = generateClientId(config); try { metrics = new Metrics(new MetricConfig(), new LinkedList<>(), time); LogContext logContext = createLogContext(clientId); return new KafkaAdminClient(config, clientId, time, metadataManager, metrics, client, null, logContext); } catch (Throwable exc) { closeQuietly(metrics, "Metrics"); throw new KafkaException("Failed to create new KafkaAdminClient", exc); } }
@Before public void setup() { this.metrics = new Metrics(config, Arrays.asList((MetricsReporter) new JmxReporter()), time, true); }
private Selector createSelector(Map<String, Object> sslClientConfigs, final Integer netReadBufSize, final Integer netWriteBufSize, final Integer appBufSize) { TestSslChannelBuilder channelBuilder = new TestSslChannelBuilder(Mode.CLIENT); channelBuilder.configureBufferSizes(netReadBufSize, netWriteBufSize, appBufSize); this.channelBuilder = channelBuilder; this.channelBuilder.configure(sslClientConfigs); this.selector = new Selector(5000, new Metrics(), time, "MetricGroup", channelBuilder, new LogContext()); return selector; }
@Test public void testIncludeInternalTopicsConfigOption() { coordinator = buildCoordinator(new Metrics(), assignors, false, false, true); subscriptions.subscribe(Pattern.compile(".*"), rebalanceListener); client.updateMetadata(TestUtils.metadataUpdateWith(1, singletonMap(Topic.GROUP_METADATA_TOPIC_NAME, 2))); assertTrue(subscriptions.subscription().contains(Topic.GROUP_METADATA_TOPIC_NAME)); }
@Test public void testRemoveChildSensor() { final Metrics metrics = new Metrics(); final Sensor parent = metrics.sensor("parent"); final Sensor child = metrics.sensor("child", parent); assertEquals(singletonList(child), metrics.childrenSensors().get(parent)); metrics.removeSensor("child"); assertEquals(emptyList(), metrics.childrenSensors().get(parent)); }
private void setupWithTransactionState(TransactionManager transactionManager, boolean guaranteeOrder, BufferPool customPool) { long deliveryTimeoutMs = 1500L; long totalSize = 1024 * 1024; String metricGrpName = "producer-metrics"; MetricConfig metricConfig = new MetricConfig().tags(Collections.singletonMap("client-id", CLIENT_ID)); this.metrics = new Metrics(metricConfig, time); BufferPool pool = (customPool == null) ? new BufferPool(totalSize, batchSize, metrics, time, metricGrpName) : customPool; this.accumulator = new RecordAccumulator(logContext, batchSize, CompressionType.NONE, 0L, 0L, deliveryTimeoutMs, metrics, metricGrpName, time, apiVersions, transactionManager, pool); this.senderMetricsRegistry = new SenderMetricsRegistry(this.metrics); this.sender = new Sender(logContext, this.client, this.metadata, this.accumulator, guaranteeOrder, MAX_REQUEST_SIZE, ACKS_ALL, Integer.MAX_VALUE, this.senderMetricsRegistry, this.time, REQUEST_TIMEOUT, 50, transactionManager, apiVersions); this.client.updateMetadata(TestUtils.metadataUpdateWith(1, Collections.singletonMap("test", 2))); }
@Before public void setup() throws Exception { // Create certificates for use by client and server. Add server cert to client truststore and vice versa. serverCertStores = new CertStores(true, "server", "localhost"); clientCertStores = new CertStores(false, "client", "localhost"); sslServerConfigs = serverCertStores.getTrustingConfig(clientCertStores); sslClientConfigs = clientCertStores.getTrustingConfig(serverCertStores); this.channelBuilder = new SslChannelBuilder(Mode.CLIENT, null, false); this.channelBuilder.configure(sslClientConfigs); this.selector = new Selector(5000, new Metrics(), time, "MetricGroup", channelBuilder, new LogContext()); }
@Test public void testCleanupMemoryAvailabilityOnMetricsException() throws Exception { BufferPool bufferPool = spy(new BufferPool(2, 1, new Metrics(), time, metricGroup)); doThrow(new OutOfMemoryError()).when(bufferPool).recordWaitTime(anyLong()); bufferPool.allocate(1, 0); try { bufferPool.allocate(2, 1000); fail("Expected oom."); } catch (OutOfMemoryError expected) { } assertEquals(1, bufferPool.availableMemory()); assertEquals(0, bufferPool.queued()); assertEquals(1, bufferPool.unallocatedMemory()); //This shouldn't timeout bufferPool.allocate(1, 0); verify(bufferPool).recordWaitTime(anyLong()); }
@Before public void setup() { config = new MetricConfig().eventWindow(50).samples(2); time = new MockTime(); metrics = new Metrics(config, Arrays.asList((MetricsReporter) new JmxReporter()), time, true); }
@Test public void testAutoCommitDynamicAssignment() { final String consumerId = "consumer"; ConsumerCoordinator coordinator = buildCoordinator(new Metrics(), assignors, ConsumerConfig.DEFAULT_EXCLUDE_INTERNAL_TOPICS, true, true); subscriptions.subscribe(singleton(topic1), rebalanceListener); joinAsFollowerAndReceiveAssignment(consumerId, coordinator, singletonList(t1p)); subscriptions.seek(t1p, 100); prepareOffsetCommitRequest(singletonMap(t1p, 100L), Errors.NONE); time.sleep(autoCommitIntervalMs); coordinator.poll(time.timer(Long.MAX_VALUE)); assertFalse(client.hasPendingResponses()); }
@Before public void setUp() throws Exception { Map<String, Object> configs = new HashMap<>(); this.server = new EchoServer(SecurityProtocol.PLAINTEXT, configs); this.server.start(); this.time = new MockTime(); this.channelBuilder = new PlaintextChannelBuilder(ListenerName.forSecurityProtocol(SecurityProtocol.PLAINTEXT)); this.channelBuilder.configure(configs); this.metrics = new Metrics(); this.selector = new Selector(5000, this.metrics, time, "MetricGroup", channelBuilder, new LogContext()); }
@Test public void testAutoCommitManualAssignment() { ConsumerCoordinator coordinator = buildCoordinator(new Metrics(), assignors, ConsumerConfig.DEFAULT_EXCLUDE_INTERNAL_TOPICS, true, true); subscriptions.assignFromUser(singleton(t1p)); subscriptions.seek(t1p, 100); client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE)); coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE)); prepareOffsetCommitRequest(singletonMap(t1p, 100L), Errors.NONE); time.sleep(autoCommitIntervalMs); coordinator.poll(time.timer(Long.MAX_VALUE)); assertFalse(client.hasPendingResponses()); }
@Before public void setUp() throws Exception { File trustStoreFile = File.createTempFile("truststore", ".jks"); Map<String, Object> sslServerConfigs = TestSslUtils.createSslConfig(false, true, Mode.SERVER, trustStoreFile, "server"); this.server = new EchoServer(SecurityProtocol.SSL, sslServerConfigs); this.server.start(); this.time = new MockTime(); sslClientConfigs = TestSslUtils.createSslConfig(false, false, Mode.CLIENT, trustStoreFile, "client"); this.channelBuilder = new SslChannelBuilder(Mode.CLIENT, null, false); this.channelBuilder.configure(sslClientConfigs); this.metrics = new Metrics(); this.selector = new Selector(5000, metrics, time, "MetricGroup", channelBuilder, new LogContext()); }
@Test public void testImmediatelyConnectedCleaned() throws Exception { Metrics metrics = new Metrics(); // new metrics object to avoid metric registration conflicts Selector selector = new ImmediatelyConnectingSelector(5000, metrics, time, "MetricGroup", channelBuilder, new LogContext()); try { testImmediatelyConnectedCleaned(selector, true); testImmediatelyConnectedCleaned(selector, false); } finally { selector.close(); metrics.close(); } }
private void setupCoordinator(int retryBackoffMs, int rebalanceTimeoutMs) { this.mockTime = new MockTime(); Metadata metadata = new Metadata(retryBackoffMs, 60 * 60 * 1000L, true); this.mockClient = new MockClient(mockTime, metadata); this.consumerClient = new ConsumerNetworkClient(new LogContext(), mockClient, metadata, mockTime, retryBackoffMs, REQUEST_TIMEOUT_MS, HEARTBEAT_INTERVAL_MS); Metrics metrics = new Metrics(); mockClient.updateMetadata(TestUtils.metadataUpdateWith(1, emptyMap())); this.node = metadata.fetch().nodes().get(0); this.coordinatorNode = new Node(Integer.MAX_VALUE - node.id(), node.host(), node.port()); this.coordinator = new DummyCoordinator(consumerClient, metrics, mockTime, rebalanceTimeoutMs, retryBackoffMs); }
@Test public void testAutoCommitAfterCoordinatorBackToService() { ConsumerCoordinator coordinator = buildCoordinator(new Metrics(), assignors, ConsumerConfig.DEFAULT_EXCLUDE_INTERNAL_TOPICS, true, true); subscriptions.assignFromUser(Collections.singleton(t1p)); subscriptions.seek(t1p, 100L); coordinator.markCoordinatorUnknown(); assertTrue(coordinator.coordinatorUnknown()); client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE)); prepareOffsetCommitRequest(singletonMap(t1p, 100L), Errors.NONE); // async commit offset should find coordinator time.sleep(autoCommitIntervalMs); // sleep for a while to ensure auto commit does happen coordinator.maybeAutoCommitOffsetsAsync(time.milliseconds()); assertFalse(coordinator.coordinatorUnknown()); assertEquals(100L, subscriptions.position(t1p).longValue()); }
@Before public void setup() throws Exception { metrics = new Metrics(); metrics.addReporter(new JmxReporter()); sensor = metrics.sensor("kafka.requests"); countMetricName = metrics.metricName("pack.bean1.count", "grp1"); sensor.add(countMetricName, new Count()); sumMetricName = metrics.metricName("pack.bean1.sum", "grp1"); sensor.add(sumMetricName, new Sum()); }
@Before public void setup() { this.subscriptions = new SubscriptionState(OffsetResetStrategy.EARLIEST); this.metadata = new Metadata(0, Long.MAX_VALUE, true); this.client = new MockClient(time, metadata); this.client.updateMetadata(metadataResponse); this.consumerClient = new ConsumerNetworkClient(new LogContext(), client, metadata, time, 100, requestTimeoutMs, Integer.MAX_VALUE); this.metrics = new Metrics(time); this.rebalanceListener = new MockRebalanceListener(); this.mockOffsetCommitCallback = new MockCommitCallback(); this.partitionAssignor.clear(); this.coordinator = buildCoordinator(metrics, assignors, ConsumerConfig.DEFAULT_EXCLUDE_INTERNAL_TOPICS, false, true); }