Search in sources :

Example 11 with MetricConfig

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

the class StreamsMetricsImplTest method shouldAddNewStoreLevelMutableMetric.

@Test
public void shouldAddNewStoreLevelMutableMetric() {
    final Metrics metrics = mock(Metrics.class);
    final MetricName metricName = new MetricName(METRIC_NAME1, STATE_STORE_LEVEL_GROUP, DESCRIPTION1, STORE_LEVEL_TAG_MAP);
    final MetricConfig metricConfig = new MetricConfig().recordLevel(INFO_RECORDING_LEVEL);
    expect(metrics.metricName(METRIC_NAME1, STATE_STORE_LEVEL_GROUP, DESCRIPTION1, STORE_LEVEL_TAG_MAP)).andReturn(metricName);
    expect(metrics.metric(metricName)).andReturn(null);
    metrics.addMetric(eq(metricName), eqMetricConfig(metricConfig), eq(VALUE_PROVIDER));
    replay(metrics);
    final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, CLIENT_ID, VERSION, time);
    streamsMetrics.addStoreLevelMutableMetric(TASK_ID1, SCOPE_NAME, STORE_NAME1, METRIC_NAME1, DESCRIPTION1, INFO_RECORDING_LEVEL, VALUE_PROVIDER);
    verify(metrics);
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) Metrics(org.apache.kafka.common.metrics.Metrics) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with MetricConfig

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

the class StreamsMetricsImplTest method testTotalMetricDoesntDecrease.

@Test
public void testTotalMetricDoesntDecrease() {
    final MockTime time = new MockTime(1);
    final MetricConfig config = new MetricConfig().timeWindow(1, TimeUnit.MILLISECONDS);
    final Metrics metrics = new Metrics(config, time);
    final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, "", VERSION, time);
    final String scope = "scope";
    final String entity = "entity";
    final String operation = "op";
    final Sensor sensor = streamsMetrics.addLatencyRateTotalSensor(scope, entity, operation, RecordingLevel.INFO);
    final double latency = 100.0;
    final MetricName totalMetricName = metrics.metricName("op-total", "stream-scope-metrics", "", "thread-id", Thread.currentThread().getName(), "scope-id", "entity");
    final KafkaMetric totalMetric = metrics.metric(totalMetricName);
    for (int i = 0; i < 10; i++) {
        assertEquals(i, Math.round(totalMetric.measurable().measure(config, time.milliseconds())));
        sensor.record(latency, time.milliseconds());
    }
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) Metrics(org.apache.kafka.common.metrics.Metrics) EasyMock.anyString(org.easymock.EasyMock.anyString) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) MockTime(org.apache.kafka.common.utils.MockTime) Sensor(org.apache.kafka.common.metrics.Sensor) StreamsMetricsImpl.addInvocationRateAndCountToSensor(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.addInvocationRateAndCountToSensor) StreamsMetricsImpl.addAvgAndMaxLatencyToSensor(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.addAvgAndMaxLatencyToSensor) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with MetricConfig

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

the class StreamsMetricsImplTest method shouldAddAmountRateAndSum.

@Test
public void shouldAddAmountRateAndSum() {
    StreamsMetricsImpl.addRateOfSumAndSumMetricsToSensor(sensor, group, tags, metricNamePrefix, DESCRIPTION1, DESCRIPTION2);
    final double valueToRecord1 = 18.0;
    final double valueToRecord2 = 72.0;
    final long defaultWindowSizeInSeconds = Duration.ofMillis(new MetricConfig().timeWindowMs()).getSeconds();
    final double expectedRateMetricValue = (valueToRecord1 + valueToRecord2) / defaultWindowSizeInSeconds;
    verifyMetric(metricNamePrefix + "-rate", DESCRIPTION1, valueToRecord1, valueToRecord2, expectedRateMetricValue);
    // values are recorded once for each metric verification
    final double expectedSumMetricValue = 2 * valueToRecord1 + 2 * valueToRecord2;
    verifyMetric(metricNamePrefix + "-total", DESCRIPTION2, valueToRecord1, valueToRecord2, expectedSumMetricValue);
    // one metric is added automatically in the constructor of Metrics
    assertThat(metrics.metrics().size(), equalTo(2 + 1));
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 14 with MetricConfig

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

the class StreamsMetricsImpl method addStoreLevelMutableMetric.

public <T> void addStoreLevelMutableMetric(final String taskId, final String metricsScope, final String storeName, final String name, final String description, final RecordingLevel recordingLevel, final Gauge<T> valueProvider) {
    final MetricName metricName = metrics.metricName(name, STATE_STORE_LEVEL_GROUP, description, storeLevelTagMap(taskId, metricsScope, storeName));
    if (metrics.metric(metricName) == null) {
        final MetricConfig metricConfig = new MetricConfig().recordLevel(recordingLevel);
        final String key = storeSensorPrefix(Thread.currentThread().getName(), taskId, storeName);
        metrics.addMetric(metricName, metricConfig, valueProvider);
        storeLevelMetrics.computeIfAbsent(key, ignored -> new LinkedList<>()).push(metricName);
    }
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) Max(org.apache.kafka.common.metrics.stats.Max) RecordingLevel(org.apache.kafka.common.metrics.Sensor.RecordingLevel) Rate(org.apache.kafka.common.metrics.stats.Rate) RocksDBMetricsRecordingTrigger(org.apache.kafka.streams.state.internals.metrics.RocksDBMetricsRecordingTrigger) HashMap(java.util.HashMap) CumulativeCount(org.apache.kafka.common.metrics.stats.CumulativeCount) Deque(java.util.Deque) Supplier(java.util.function.Supplier) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Metric(org.apache.kafka.common.Metric) MetricName(org.apache.kafka.common.MetricName) WindowedSum(org.apache.kafka.common.metrics.stats.WindowedSum) LinkedList(java.util.LinkedList) Value(org.apache.kafka.common.metrics.stats.Value) Sensor(org.apache.kafka.common.metrics.Sensor) Time(org.apache.kafka.common.utils.Time) MetricConfig(org.apache.kafka.common.metrics.MetricConfig) CumulativeSum(org.apache.kafka.common.metrics.stats.CumulativeSum) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Metrics(org.apache.kafka.common.metrics.Metrics) WindowedCount(org.apache.kafka.common.metrics.stats.WindowedCount) Avg(org.apache.kafka.common.metrics.stats.Avg) Min(org.apache.kafka.common.metrics.stats.Min) Gauge(org.apache.kafka.common.metrics.Gauge) StreamsMetrics(org.apache.kafka.streams.StreamsMetrics) Collections(java.util.Collections) MetricName(org.apache.kafka.common.MetricName) LinkedList(java.util.LinkedList)

Example 15 with MetricConfig

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

the class TopologyTestDriver method setupMetrics.

private StreamsMetricsImpl setupMetrics(final StreamsConfig streamsConfig) {
    final String threadId = Thread.currentThread().getName();
    final MetricConfig metricConfig = new MetricConfig().samples(streamsConfig.getInt(StreamsConfig.METRICS_NUM_SAMPLES_CONFIG)).recordLevel(Sensor.RecordingLevel.forName(streamsConfig.getString(StreamsConfig.METRICS_RECORDING_LEVEL_CONFIG))).timeWindow(streamsConfig.getLong(StreamsConfig.METRICS_SAMPLE_WINDOW_MS_CONFIG), TimeUnit.MILLISECONDS);
    metrics = new Metrics(metricConfig, mockWallClockTime);
    final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, "test-client", streamsConfig.getString(StreamsConfig.BUILT_IN_METRICS_VERSION_CONFIG), mockWallClockTime);
    TaskMetrics.droppedRecordsSensor(threadId, TASK_ID.toString(), streamsMetrics);
    return streamsMetrics;
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) Metrics(org.apache.kafka.common.metrics.Metrics) TaskMetrics(org.apache.kafka.streams.processor.internals.metrics.TaskMetrics) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl)

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