diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/ReadSession.java b/topic/src/main/java/tech/ydb/topic/read/impl/ReadSession.java index 4c53d2e94..80656609c 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/ReadSession.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/ReadSession.java @@ -224,7 +224,7 @@ public void onStartPartition(YdbTopic.StreamReadMessage.StartPartitionSessionReq return; } - config.getMetrics().addPartition(psid, committed); + config.getMetrics().addPartition(psid, committed, ps.getPath()); partition.start(committed, offsets); TopicPartition lockKey = new TopicPartition(ps); if (locks.computeIfPresent(lockKey, (key, lock) -> lock.bind(partition)) != null) { diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/ReaderMetrics.java b/topic/src/main/java/tech/ydb/topic/read/impl/ReaderMetrics.java index 72e9ac192..d4e9f1edf 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/ReaderMetrics.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/ReaderMetrics.java @@ -6,6 +6,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; import java.util.function.LongSupplier; +import java.util.stream.Collectors; import tech.ydb.core.Status; import tech.ydb.core.StatusCode; @@ -78,8 +79,8 @@ void removeSource(Source source) { sources.remove(source); } - void addPartition(long id, long committedOffset) { - partitions.put(id, new CommitOffsets(committedOffset)); + void addPartition(long id, long committedOffset, String topicPath) { + partitions.put(id, new CommitOffsets(committedOffset, topicPath)); } void removePartition(long id) { @@ -93,9 +94,10 @@ void updateCommittedOffset(long id, long offset) { } } - private long getCommitOffsetLagMax() { - return partitions.values().stream() - .mapToLong(p -> Math.max(0, p.requested.get() - p.committed.get())).max().orElse(0); + private Attr[] topicAttributes(String topic) { + Attr[] attributes = Arrays.copyOf(commonAttributes, commonAttributes.length + 1); + attributes[commonAttributes.length] = Attr.of("topic", topic); + return attributes; } private long getLocalBufferMessages() { @@ -117,13 +119,18 @@ void register(LongSupplier bufferBudget) { partitionsGauge = meter.registerLongGauge( "ydb.topic.reader.partition_session.count", "{session}", "The number of partition sessions currently in the reader session processing lifecycle.", - m -> m.record(partitions.size(), commonAttributes)); + m -> partitions.values().stream().collect(Collectors.groupingBy( + offsets -> offsets.topicPath, Collectors.counting())) + .forEach((topic, count) -> m.record(count, topicAttributes(topic)))); creditGauge = meter.registerLongGauge("ydb.topic.reader.credit_balance_bytes", "By", "The protocol credit granted to the server and not yet consumed by read responses.", m -> m.record(bufferBudget.getAsLong(), commonAttributes)); commitLagGauge = meter.registerLongGauge("ydb.topic.reader.commit_offset.lag.max", MESSAGE_UNIT, "The maximum gap between requested and acknowledged commit offsets.", - m -> m.record(getCommitOffsetLagMax(), commonAttributes)); + m -> partitions.values().stream().collect(Collectors.toMap( + offsets -> offsets.topicPath, + offsets -> Math.max(0, offsets.requested.get() - offsets.committed.get()), Math::max)) + .forEach((topic, lag) -> m.record(lag, topicAttributes(topic)))); bufferMessagesGauge = meter.registerLongGauge("ydb.topic.reader.local_buffer.messages", MESSAGE_UNIT, "The number of messages currently buffered by the reader.", m -> m.record(getLocalBufferMessages(), commonAttributes)); @@ -240,10 +247,12 @@ private static Attr[] createCommonAttributes(String consumer, String readerName) } private static class CommitOffsets { + private final String topicPath; private final AtomicLong requested; private final AtomicLong committed; - CommitOffsets(long offset) { + CommitOffsets(long offset, String topicPath) { + this.topicPath = topicPath; this.requested = new AtomicLong(offset); this.committed = new AtomicLong(offset); } diff --git a/topic/src/test/java/tech/ydb/topic/read/impl/ReaderMetricsTest.java b/topic/src/test/java/tech/ydb/topic/read/impl/ReaderMetricsTest.java index a70f57ee8..99a27b725 100644 --- a/topic/src/test/java/tech/ydb/topic/read/impl/ReaderMetricsTest.java +++ b/topic/src/test/java/tech/ydb/topic/read/impl/ReaderMetricsTest.java @@ -50,9 +50,15 @@ public void readAndCommitMetrics() throws Exception { reader.init(); stream.assertLastMessage().isInitRequest("consumer", "/topic", "/other-topic").hasReaderName("reader"); stream.responseInit("read-session"); + meter.assertLongGaugeSeries(PREFIX + "partition_session.count", 0); + meter.assertLongGaugeSeries(PREFIX + "commit_offset.lag.max", 0); + meter.assertLongGauge(PREFIX + "local_buffer.messages", 0, COMMON); + meter.assertDoubleGauge(PREFIX + "local_buffer.message_age.max", 0, COMMON); stream.responseStartPartition("/topic", 42, 0); stream.responseStartPartition("/other-topic", 43, 0); - meter.assertLongGauge(PREFIX + "partition_session.count", 2, COMMON); + stream.responseStartPartition("/topic", 44, 0); + meter.assertLongGauge(PREFIX + "partition_session.count", 2, TOPIC); + meter.assertLongGauge(PREFIX + "partition_session.count", 1, OTHER); meter.assertLongGauge(PREFIX + "credit_balance_bytes", 100, COMMON); stream.responseData(12).partition(1, 0).batch(Codec.GZIP, MSG1, MSG2).and().send(); meter.assertCounter(PREFIX + "received.messages", 2, TOPIC); @@ -61,13 +67,17 @@ public void readAndCommitMetrics() throws Exception { meter.assertCounter(PREFIX + "received.messages", 2, TOPIC); meter.assertCounter(PREFIX + "received.messages", 1, OTHER); meter.assertCounter(PREFIX + "received.bytes", 20, COMMON); + meter.assertCounterSeries(PREFIX + "received.bytes", 1); meter.assertLongGauge(PREFIX + "credit_balance_bytes", 80, COMMON); meter.assertCounter(PREFIX + "delivered.messages", 0, TOPIC); meter.assertCounter(PREFIX + "delivered.messages", 0, OTHER); meter.assertLongGauge(PREFIX + "local_buffer.messages", 3, COMMON); meter.assertPositiveDoubleGauge(PREFIX + "local_buffer.message_age.max", COMMON); Message first = reader.receive(0, TimeUnit.SECONDS); + meter.assertLongGauge(PREFIX + "local_buffer.messages", 2, COMMON); Message second = reader.receive(0, TimeUnit.SECONDS); + meter.assertLongGauge(PREFIX + "local_buffer.messages", 1, COMMON); + meter.assertPositiveDoubleGauge(PREFIX + "local_buffer.message_age.max", COMMON); Message third = reader.receive(0, TimeUnit.SECONDS); Assert.assertNotNull(first); Assert.assertNotNull(second); @@ -81,18 +91,28 @@ public void readAndCommitMetrics() throws Exception { meter.assertDoubleGauge(PREFIX + "local_buffer.message_age.max", 0, COMMON); // The final 8 released bytes stay below the 10-byte credit-request threshold. meter.assertLongGauge(PREFIX + "credit_balance_bytes", 92, COMMON); + first.getCommitter().commitRanges(Arrays.asList( + first.getRangeToCommit(), second.getRangeToCommit())); + meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 2, TOPIC); + meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 0, OTHER); CompletableFuture committed = CompletableFuture.allOf( first.commit(), second.commit(), third.commit()); Assert.assertFalse(committed.isDone()); - meter.assertCounter(PREFIX + "commit.queued", 2, TOPIC); + meter.assertCounter(PREFIX + "commit.queued", 4, TOPIC); meter.assertCounter(PREFIX + "commit.queued", 1, OTHER); meter.assertNoCounterSamples(PREFIX + "commit.acknowledged"); - meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 2, COMMON); - stream.responseCommitAck().partition(1, 2).partition(2, 1).send(); + meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 2, TOPIC); + meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 1, OTHER); + stream.responseCommitAck().partition(1, 2).send(); + Assert.assertFalse(committed.isDone()); + meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 0, TOPIC); + meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 1, OTHER); + stream.responseCommitAck().partition(2, 1).send(); committed.get(1, TimeUnit.SECONDS); meter.assertCounter(PREFIX + "commit.acknowledged", 2, TOPIC); meter.assertCounter(PREFIX + "commit.acknowledged", 1, OTHER); - meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 0, COMMON); + meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 0, TOPIC); + meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 0, OTHER); for (String name : Arrays.asList("compression.duration", "message.processing.duration")) { meter.assertHistogram(PREFIX + name, "s", 2, attributes("consumer", "consumer", "reader.name", "reader", "topic", "/topic", "codec", "gzip", "outcome", "ok")); @@ -101,8 +121,12 @@ public void readAndCommitMetrics() throws Exception { } meter.assertUnit(PREFIX + "received.bytes", "By"); stream.responseStopPartition(1, true); + meter.assertLongGauge(PREFIX + "partition_session.count", 1, TOPIC); + meter.assertLongGauge(PREFIX + "partition_session.count", 1, OTHER); stream.responseStopPartition(2, true); - meter.assertLongGauge(PREFIX + "partition_session.count", 0, COMMON); + stream.responseStopPartition(3, true); + meter.assertLongGaugeSeries(PREFIX + "partition_session.count", 0); + meter.assertLongGaugeSeries(PREFIX + "commit_offset.lag.max", 0); } finally { reader.shutdown(); } @@ -132,7 +156,7 @@ public void readWithoutConsumerMetrics() throws Exception { Assert.assertNotNull(message); Assert.assertArrayEquals(MSG1, message.getData()); meter.assertCounter(PREFIX + "delivered.messages", 1, topic); - meter.assertLongGauge(PREFIX + "partition_session.count", 1, common); + meter.assertLongGauge(PREFIX + "partition_session.count", 1, topic); meter.assertLongGauge(PREFIX + "local_buffer.messages", 0, common); for (String name : Arrays.asList("compression.duration", "message.processing.duration")) { meter.assertHistogram(PREFIX + name, "s", 1, attributes("reader.name", "without-consumer", diff --git a/topic/src/test/java/tech/ydb/topic/utils/InMemoryMeter.java b/topic/src/test/java/tech/ydb/topic/utils/InMemoryMeter.java index 8cbbfea14..435cab90e 100644 --- a/topic/src/test/java/tech/ydb/topic/utils/InMemoryMeter.java +++ b/topic/src/test/java/tech/ydb/topic/utils/InMemoryMeter.java @@ -103,6 +103,17 @@ public void assertLongGauge(String name, long expected, Map attr Assert.assertEquals(name + " " + attributes, expected, values.get(attributes).longValue()); } + public void assertLongGaugeSeries(String name, int expected) { + List> callbacks = longGauges.get(name); + Assert.assertNotNull(name, callbacks); + Map, Long> values = new HashMap<>(); + for (Consumer callback : callbacks) { + callback.accept((value, attrs) -> Assert.assertNull(name + " duplicate series", + values.put(key(attrs), value))); + } + Assert.assertEquals(name, expected, values.size()); + } + public void assertDoubleGauge(String name, double expected, Map attributes) { Assert.assertEquals(name + " " + attributes, expected, doubleGaugeValue(name, attributes), 0); }