Search in sources :

Example 36 with MetricConfig

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

the class StreamsMetricsImpl method addClientLevelImmutableMetric.

public <T> void addClientLevelImmutableMetric(final String name, final String description, final RecordingLevel recordingLevel, final T value) {
    final MetricName metricName = metrics.metricName(name, CLIENT_LEVEL_GROUP, description, clientLevelTagMap());
    final MetricConfig metricConfig = new MetricConfig().recordLevel(recordingLevel);
    synchronized (clientLevelMetrics) {
        metrics.addMetric(metricName, metricConfig, new ImmutableMetricValue<>(value));
        clientLevelMetrics.push(metricName);
    }
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName)

Example 37 with MetricConfig

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

the class StreamsMetricsImplTest method shouldAddClientLevelMutableMetric.

@Test
public void shouldAddClientLevelMutableMetric() {
    final Metrics metrics = mock(Metrics.class);
    final RecordingLevel recordingLevel = RecordingLevel.INFO;
    final MetricConfig metricConfig = new MetricConfig().recordLevel(recordingLevel);
    final Gauge<String> valueProvider = (config, now) -> "mutable-value";
    expect(metrics.metricName(METRIC_NAME1, CLIENT_LEVEL_GROUP, DESCRIPTION1, clientLevelTags)).andReturn(metricName1);
    metrics.addMetric(EasyMock.eq(metricName1), eqMetricConfig(metricConfig), eq(valueProvider));
    replay(metrics);
    final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, CLIENT_ID, VERSION, time);
    streamsMetrics.addClientLevelMutableMetric(METRIC_NAME1, DESCRIPTION1, recordingLevel, valueProvider);
    verify(metrics);
}
Also used : RecordingLevel(org.apache.kafka.common.metrics.Sensor.RecordingLevel) MetricConfig(org.apache.kafka.common.metrics.MetricConfig) CoreMatchers.is(org.hamcrest.CoreMatchers.is) RecordingLevel(org.apache.kafka.common.metrics.Sensor.RecordingLevel) MockTime(org.apache.kafka.common.utils.MockTime) Arrays(java.util.Arrays) Rate(org.apache.kafka.common.metrics.stats.Rate) EasyMock.capture(org.easymock.EasyMock.capture) THREAD_LEVEL_GROUP(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.THREAD_LEVEL_GROUP) ImmutableMetricValue(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.ImmutableMetricValue) RATE_SUFFIX(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.RATE_SUFFIX) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) Utils.mkMap(org.apache.kafka.common.utils.Utils.mkMap) IArgumentMatcher(org.easymock.IArgumentMatcher) ROLLUP_VALUE(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.ROLLUP_VALUE) Duration(java.time.Duration) Map(java.util.Map) AVG_SUFFIX(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.AVG_SUFFIX) MetricName(org.apache.kafka.common.MetricName) EasyMock.eq(org.easymock.EasyMock.eq) Sensor(org.apache.kafka.common.metrics.Sensor) TOTAL_SUFFIX(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.TOTAL_SUFFIX) Time(org.apache.kafka.common.utils.Time) EasyMock.newCapture(org.easymock.EasyMock.newCapture) MetricConfig(org.apache.kafka.common.metrics.MetricConfig) EasyMock.resetToDefault(org.easymock.EasyMock.resetToDefault) List(java.util.List) Metrics(org.apache.kafka.common.metrics.Metrics) Utils.mkEntry(org.apache.kafka.common.utils.Utils.mkEntry) Matchers.equalTo(org.hamcrest.Matchers.equalTo) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) CoreMatchers.equalToObject(org.hamcrest.CoreMatchers.equalToObject) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) CLIENT_LEVEL_GROUP(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.CLIENT_LEVEL_GROUP) PowerMock.createMock(org.powermock.api.easymock.PowerMock.createMock) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) CoreMatchers.not(org.hamcrest.CoreMatchers.not) CLIENT_ID_TAG(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.CLIENT_ID_TAG) EasyMock.mock(org.easymock.EasyMock.mock) 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) EasyMock.replay(org.easymock.EasyMock.replay) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) EasyMock.anyObject(org.easymock.EasyMock.anyObject) Capture(org.easymock.Capture) EasyMock.anyString(org.easymock.EasyMock.anyString) MAX_SUFFIX(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.MAX_SUFFIX) EasyMock.niceMock(org.easymock.EasyMock.niceMock) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) LATENCY_SUFFIX(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.LATENCY_SUFFIX) Version(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.Version) EasyMock(org.easymock.EasyMock) EasyMock.expect(org.easymock.EasyMock.expect) TimeUnit(java.util.concurrent.TimeUnit) STATE_STORE_LEVEL_GROUP(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.STATE_STORE_LEVEL_GROUP) CaptureType(org.easymock.CaptureType) Gauge(org.apache.kafka.common.metrics.Gauge) StreamsTestUtils(org.apache.kafka.test.StreamsTestUtils) EasyMock.verify(org.easymock.EasyMock.verify) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) PROCESSOR_NODE_LEVEL_GROUP(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.PROCESSOR_NODE_LEVEL_GROUP) Metrics(org.apache.kafka.common.metrics.Metrics) EasyMock.anyString(org.easymock.EasyMock.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 38 with MetricConfig

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

the class StreamsMetricsImplTest method verifyMetric.

private void verifyMetric(final String name, final String description, final double valueToRecord1, final double valueToRecord2, final double expectedMetricValue) {
    final KafkaMetric metric = metrics.metric(new MetricName(name, group, description, tags));
    assertThat(metric, is(notNullValue()));
    assertThat(metric.metricName().description(), equalTo(description));
    sensor.record(valueToRecord1, time.milliseconds());
    sensor.record(valueToRecord2, time.milliseconds());
    assertThat(metric.measurable().measure(new MetricConfig(), time.milliseconds()), equalTo(expectedMetricValue));
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric)

Example 39 with MetricConfig

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

the class RocksDBStoreTest method shouldVerifyThatPropertyBasedMetricsUseValidPropertyName.

@Test
public void shouldVerifyThatPropertyBasedMetricsUseValidPropertyName() {
    final TaskId taskId = new TaskId(0, 0);
    final Metrics metrics = new Metrics(new MetricConfig().recordLevel(RecordingLevel.INFO));
    final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, "test-application", StreamsConfig.METRICS_LATEST, time);
    final Properties props = StreamsTestUtils.getStreamsConfig();
    context = EasyMock.niceMock(InternalMockProcessorContext.class);
    EasyMock.expect(context.metrics()).andStubReturn(streamsMetrics);
    EasyMock.expect(context.taskId()).andStubReturn(taskId);
    EasyMock.expect(context.appConfigs()).andStubReturn(new StreamsConfig(props).originals());
    EasyMock.expect(context.stateDir()).andStubReturn(dir);
    EasyMock.replay(context);
    rocksDBStore.init((StateStoreContext) context, rocksDBStore);
    final List<String> propertyNames = Arrays.asList("num-entries-active-mem-table", "num-deletes-active-mem-table", "num-entries-imm-mem-tables", "num-deletes-imm-mem-tables", "num-immutable-mem-table", "cur-size-active-mem-table", "cur-size-all-mem-tables", "size-all-mem-tables", "mem-table-flush-pending", "num-running-flushes", "compaction-pending", "num-running-compactions", "estimate-pending-compaction-bytes", "total-sst-files-size", "live-sst-files-size", "num-live-versions", "block-cache-capacity", "block-cache-usage", "block-cache-pinned-usage", "estimate-num-keys", "estimate-table-readers-mem", "background-errors");
    for (final String propertyname : propertyNames) {
        final Metric metric = metrics.metric(new MetricName(propertyname, StreamsMetricsImpl.STATE_STORE_LEVEL_GROUP, "description is not verified", streamsMetrics.storeLevelTagMap(taskId.toString(), METRICS_SCOPE, DB_NAME)));
        assertThat("Metric " + propertyname + " not found!", metric, notNullValue());
        metric.metricValue();
    }
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) Metrics(org.apache.kafka.common.metrics.Metrics) TaskId(org.apache.kafka.streams.processor.TaskId) Metric(org.apache.kafka.common.Metric) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl) Properties(java.util.Properties) InternalMockProcessorContext(org.apache.kafka.test.InternalMockProcessorContext) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 40 with MetricConfig

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

the class RocksDBStoreTest method shouldVerifyThatMetricsRecordedFromStatisticsGetMeasurementsFromRocksDB.

@Test
public void shouldVerifyThatMetricsRecordedFromStatisticsGetMeasurementsFromRocksDB() {
    final TaskId taskId = new TaskId(0, 0);
    final Metrics metrics = new Metrics(new MetricConfig().recordLevel(RecordingLevel.DEBUG));
    final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, "test-application", StreamsConfig.METRICS_LATEST, time);
    context = EasyMock.niceMock(InternalMockProcessorContext.class);
    EasyMock.expect(context.metrics()).andStubReturn(streamsMetrics);
    EasyMock.expect(context.taskId()).andStubReturn(taskId);
    EasyMock.expect(context.appConfigs()).andStubReturn(new StreamsConfig(StreamsTestUtils.getStreamsConfig()).originals());
    EasyMock.expect(context.stateDir()).andStubReturn(dir);
    final MonotonicProcessorRecordContext processorRecordContext = new MonotonicProcessorRecordContext("test", 0);
    EasyMock.expect(context.recordMetadata()).andStubReturn(Optional.of(processorRecordContext));
    EasyMock.replay(context);
    rocksDBStore.init((StateStoreContext) context, rocksDBStore);
    final byte[] key = "hello".getBytes();
    final byte[] value = "world".getBytes();
    rocksDBStore.put(Bytes.wrap(key), value);
    streamsMetrics.rocksDBMetricsRecordingTrigger().run();
    final Metric bytesWrittenTotal = metrics.metric(new MetricName("bytes-written-total", StreamsMetricsImpl.STATE_STORE_LEVEL_GROUP, "description is not verified", streamsMetrics.storeLevelTagMap(taskId.toString(), METRICS_SCOPE, DB_NAME)));
    assertThat((double) bytesWrittenTotal.metricValue(), greaterThan(0d));
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) MetricName(org.apache.kafka.common.MetricName) Metrics(org.apache.kafka.common.metrics.Metrics) TaskId(org.apache.kafka.streams.processor.TaskId) Metric(org.apache.kafka.common.Metric) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl) InternalMockProcessorContext(org.apache.kafka.test.InternalMockProcessorContext) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

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