Search in sources :

Example 21 with MetricConfig

use of org.apache.kafka.common.metrics.MetricConfig in project apache-kafka-on-k8s by banzaicloud.

the class SenderTest method testSenderMetricsTemplates.

@Test
public void testSenderMetricsTemplates() throws Exception {
    metrics.close();
    Map<String, String> clientTags = Collections.singletonMap("client-id", "clientA");
    metrics = new Metrics(new MetricConfig().tags(clientTags));
    SenderMetricsRegistry metricsRegistry = new SenderMetricsRegistry(metrics);
    Sender sender = new Sender(logContext, client, metadata, this.accumulator, false, MAX_REQUEST_SIZE, ACKS_ALL, 1, metricsRegistry, time, REQUEST_TIMEOUT, 50, null, apiVersions);
    // Append a message so that topic metrics are created
    accumulator.append(tp0, 0L, "key".getBytes(), "value".getBytes(), null, null, MAX_BLOCK_TIMEOUT);
    // connect
    sender.run(time.milliseconds());
    // send produce request
    sender.run(time.milliseconds());
    client.respond(produceResponse(tp0, 0, Errors.NONE, 0));
    sender.run(time.milliseconds());
    // Create throttle time metrics
    Sender.throttleTimeSensor(metricsRegistry);
    // Verify that all metrics except metrics-count have registered templates
    Set<MetricNameTemplate> allMetrics = new HashSet<>();
    for (MetricName n : metrics.metrics().keySet()) {
        if (!n.group().equals("kafka-metrics-count"))
            allMetrics.add(new MetricNameTemplate(n.name(), n.group(), "", n.tags().keySet()));
    }
    TestUtils.checkEquals(allMetrics, new HashSet<>(metricsRegistry.allTemplates()), "metrics", "templates");
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) Metrics(org.apache.kafka.common.metrics.Metrics) MetricNameTemplate(org.apache.kafka.common.MetricNameTemplate) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 22 with MetricConfig

use of org.apache.kafka.common.metrics.MetricConfig in project apache-kafka-on-k8s by banzaicloud.

the class TransactionManagerTest method setup.

@Before
public void setup() {
    Map<String, String> metricTags = new LinkedHashMap<>();
    metricTags.put("client-id", CLIENT_ID);
    int batchSize = 16 * 1024;
    MetricConfig metricConfig = new MetricConfig().tags(metricTags);
    this.brokerNode = new Node(0, "localhost", 2211);
    this.transactionManager = new TransactionManager(logContext, transactionalId, transactionTimeoutMs, DEFAULT_RETRY_BACKOFF_MS);
    Metrics metrics = new Metrics(metricConfig, time);
    SenderMetricsRegistry senderMetrics = new SenderMetricsRegistry(metrics);
    this.accumulator = new RecordAccumulator(logContext, batchSize, 1024 * 1024, CompressionType.NONE, 0L, 0L, metrics, time, apiVersions, transactionManager);
    this.sender = new Sender(logContext, this.client, this.metadata, this.accumulator, true, MAX_REQUEST_SIZE, ACKS_ALL, MAX_RETRIES, senderMetrics, this.time, REQUEST_TIMEOUT, 50, transactionManager, apiVersions);
    this.metadata.update(this.cluster, Collections.<String>emptySet(), time.milliseconds());
    client.setNode(brokerNode);
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) Metrics(org.apache.kafka.common.metrics.Metrics) Node(org.apache.kafka.common.Node) LinkedHashMap(java.util.LinkedHashMap) Before(org.junit.Before)

Example 23 with MetricConfig

use of org.apache.kafka.common.metrics.MetricConfig in project apache-kafka-on-k8s by banzaicloud.

the class MeterTest method testMeter.

@Test
public void testMeter() {
    Map<String, String> emptyTags = Collections.emptyMap();
    MetricName rateMetricName = new MetricName("rate", "test", "", emptyTags);
    MetricName totalMetricName = new MetricName("total", "test", "", emptyTags);
    Meter meter = new Meter(rateMetricName, totalMetricName);
    List<NamedMeasurable> stats = meter.stats();
    assertEquals(2, stats.size());
    NamedMeasurable total = stats.get(0);
    NamedMeasurable rate = stats.get(1);
    assertEquals(rateMetricName, rate.name());
    assertEquals(totalMetricName, total.name());
    Rate rateStat = (Rate) rate.stat();
    Total totalStat = (Total) total.stat();
    MetricConfig config = new MetricConfig();
    double nextValue = 0.0;
    double expectedTotal = 0.0;
    long now = 0;
    double intervalMs = 100;
    double delta = 5.0;
    // for time windows and that the total is cumulative.
    for (int i = 1; i <= 100; i++) {
        for (; now < i * 1000; now += intervalMs, nextValue += delta) {
            expectedTotal += nextValue;
            meter.record(config, nextValue, now);
        }
        assertEquals(expectedTotal, totalStat.measure(config, now), EPS);
        long windowSizeMs = rateStat.windowSize(config, now);
        long windowStartMs = Math.max(now - windowSizeMs, 0);
        double sampledTotal = 0.0;
        double prevValue = nextValue - delta;
        for (long timeMs = now - 100; timeMs >= windowStartMs; timeMs -= intervalMs, prevValue -= delta) sampledTotal += prevValue;
        assertEquals(sampledTotal * 1000 / windowSizeMs, rateStat.measure(config, now), EPS);
    }
}
Also used : NamedMeasurable(org.apache.kafka.common.metrics.CompoundStat.NamedMeasurable) MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) Test(org.junit.Test)

Example 24 with MetricConfig

use of org.apache.kafka.common.metrics.MetricConfig in project kafka by apache.

the class IntGaugeSuiteTest method createIntGaugeSuite.

private static IntGaugeSuite<String> createIntGaugeSuite() {
    MetricConfig config = new MetricConfig();
    Metrics metrics = new Metrics(config);
    IntGaugeSuite<String> suite = new IntGaugeSuite<>(log, "mySuite", metrics, name -> new MetricName(name, "group", "myMetric", Collections.emptyMap()), 3);
    return suite;
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) Metrics(org.apache.kafka.common.metrics.Metrics)

Example 25 with MetricConfig

use of org.apache.kafka.common.metrics.MetricConfig in project kafka by apache.

the class FrequenciesTest method setup.

@BeforeEach
public void setup() {
    config = new MetricConfig().eventWindow(50).samples(2);
    time = new MockTime();
    metrics = new Metrics(config, Arrays.asList(new JmxReporter()), time, true);
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) Metrics(org.apache.kafka.common.metrics.Metrics) JmxReporter(org.apache.kafka.common.metrics.JmxReporter) MockTime(org.apache.kafka.common.utils.MockTime) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

MetricConfig (org.apache.kafka.common.metrics.MetricConfig)44 Metrics (org.apache.kafka.common.metrics.Metrics)32 MetricName (org.apache.kafka.common.MetricName)23 Test (org.junit.Test)14 KafkaMetric (org.apache.kafka.common.metrics.KafkaMetric)8 Sensor (org.apache.kafka.common.metrics.Sensor)8 MockTime (org.apache.kafka.common.utils.MockTime)8 HashSet (java.util.HashSet)7 LinkedHashMap (java.util.LinkedHashMap)7 MetricNameTemplate (org.apache.kafka.common.MetricNameTemplate)7 JmxReporter (org.apache.kafka.common.metrics.JmxReporter)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 KafkaException (org.apache.kafka.common.KafkaException)6 Collections (java.util.Collections)5 Map (java.util.Map)5 TimeUnit (java.util.concurrent.TimeUnit)5 Metric (org.apache.kafka.common.Metric)5 TopicPartition (org.apache.kafka.common.TopicPartition)5