From 53b3b08546f28274e62f8c6e1f69fc2156e42cea Mon Sep 17 00:00:00 2001 From: KirillKurdyukov Date: Mon, 26 Oct 2026 16:37:46 +0300 Subject: [PATCH 1/2] Add endpoint and database attributes to Topic metrics --- .../tech/ydb/core/grpc/GrpcTransport.java | 8 ++ .../tech/ydb/core/impl/BaseGrpcTransport.java | 5 + .../main/java/tech/ydb/topic/TopicRpc.java | 7 ++ .../tech/ydb/topic/impl/GrpcTopicRpc.java | 9 ++ .../ydb/topic/read/impl/AsyncReaderImpl.java | 4 +- .../tech/ydb/topic/read/impl/ReadConfig.java | 9 +- .../ydb/topic/read/impl/ReaderMetrics.java | 6 +- .../ydb/topic/read/impl/SyncReaderImpl.java | 4 +- .../tech/ydb/topic/write/impl/WriterImpl.java | 4 +- .../ydb/topic/write/impl/WriterMetrics.java | 10 +- .../topic/read/impl/ReaderMetricsTest.java | 109 ++++++++++++++---- .../topic/write/impl/WriterMetricsTest.java | 95 ++++++++++++--- 12 files changed, 223 insertions(+), 47 deletions(-) diff --git a/core/src/main/java/tech/ydb/core/grpc/GrpcTransport.java b/core/src/main/java/tech/ydb/core/grpc/GrpcTransport.java index 96816601f..cb9c5c601 100644 --- a/core/src/main/java/tech/ydb/core/grpc/GrpcTransport.java +++ b/core/src/main/java/tech/ydb/core/grpc/GrpcTransport.java @@ -40,6 +40,14 @@ GrpcReadWriteStream readWriteStreamCall( String getDatabase(); + /** + * Returns the configured discovery endpoint as host:port, or an empty string if unavailable. + * @return configured discovery endpoint + */ + default String getEndpoint() { + return ""; + } + ScheduledExecutorService getScheduler(); default Tracer getTracer() { diff --git a/core/src/main/java/tech/ydb/core/impl/BaseGrpcTransport.java b/core/src/main/java/tech/ydb/core/impl/BaseGrpcTransport.java index e15eb5c5a..b8e624cbb 100644 --- a/core/src/main/java/tech/ydb/core/impl/BaseGrpcTransport.java +++ b/core/src/main/java/tech/ydb/core/impl/BaseGrpcTransport.java @@ -69,6 +69,11 @@ protected BaseGrpcTransport(EndpointRecord serverEndpoint, String buildInfo) { protected abstract AuthCallOptions getAuthCallOptions(); + @Override + public String getEndpoint() { + return serverEndpoint.getHostAndPort(); + } + protected abstract GrpcChannel getChannel(GrpcRequestSettings settings); protected void pessimizeEndpoint(EndpointRecord endpoint, String reason) { diff --git a/topic/src/main/java/tech/ydb/topic/TopicRpc.java b/topic/src/main/java/tech/ydb/topic/TopicRpc.java index f4bc48a37..bc4edfe0b 100644 --- a/topic/src/main/java/tech/ydb/topic/TopicRpc.java +++ b/topic/src/main/java/tech/ydb/topic/TopicRpc.java @@ -1,5 +1,7 @@ package tech.ydb.topic; +import java.util.Collections; +import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ScheduledExecutorService; @@ -7,6 +9,7 @@ import tech.ydb.core.Status; import tech.ydb.core.grpc.GrpcReadWriteStream; import tech.ydb.core.grpc.GrpcRequestSettings; +import tech.ydb.core.metrics.Attr; import tech.ydb.proto.topic.YdbTopic.AlterTopicRequest; import tech.ydb.proto.topic.YdbTopic.CommitOffsetRequest; import tech.ydb.proto.topic.YdbTopic.CreateTopicRequest; @@ -95,4 +98,8 @@ default GrpcReadWriteStream readSession(String traceId); ScheduledExecutorService getScheduler(); + + default List getMetricAttributes() { + return Collections.emptyList(); + } } diff --git a/topic/src/main/java/tech/ydb/topic/impl/GrpcTopicRpc.java b/topic/src/main/java/tech/ydb/topic/impl/GrpcTopicRpc.java index 7aa1f518f..3d5890c33 100644 --- a/topic/src/main/java/tech/ydb/topic/impl/GrpcTopicRpc.java +++ b/topic/src/main/java/tech/ydb/topic/impl/GrpcTopicRpc.java @@ -1,5 +1,7 @@ package tech.ydb.topic.impl; +import java.util.Arrays; +import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ScheduledExecutorService; @@ -11,6 +13,7 @@ import tech.ydb.core.grpc.GrpcReadWriteStream; import tech.ydb.core.grpc.GrpcRequestSettings; import tech.ydb.core.grpc.GrpcTransport; +import tech.ydb.core.metrics.Attr; import tech.ydb.core.operation.OperationBinder; import tech.ydb.proto.topic.YdbTopic; import tech.ydb.proto.topic.YdbTopic.AlterTopicRequest; @@ -130,4 +133,10 @@ public GrpcReadWriteStream getMetricAttributes() { + return Arrays.asList( + Attr.of("endpoint", transport.getEndpoint()), Attr.of("database", transport.getDatabase())); + } } diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/AsyncReaderImpl.java b/topic/src/main/java/tech/ydb/topic/read/impl/AsyncReaderImpl.java index 5da01583a..2faffbd18 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/AsyncReaderImpl.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/AsyncReaderImpl.java @@ -14,6 +14,7 @@ import tech.ydb.core.Issue; import tech.ydb.core.Status; import tech.ydb.core.StatusCode; +import tech.ydb.core.metrics.Attr; import tech.ydb.topic.TopicRpc; import tech.ydb.topic.description.CodecRegistry; import tech.ydb.topic.impl.DebugTools; @@ -60,7 +61,8 @@ public AsyncReaderImpl(TopicRpc topicRpc, this.decompressor = new LazyExecutor("reader[" + debugId + "]-decoder", settings.getDecompressionExecutor()); this.controlEventsExecutor = new SerialExecutor(processor); - this.config = new ReadConfig(codecRegistry, controlEventsExecutor, processor, decompressor, settings); + this.config = new ReadConfig(codecRegistry, controlEventsExecutor, processor, decompressor, settings, + topicRpc.getMetricAttributes().toArray(new Attr[0])); this.impl = new Impl(topicRpc, debugId, settings, config); String readerName = settings.getReaderName(); diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/ReadConfig.java b/topic/src/main/java/tech/ydb/topic/read/impl/ReadConfig.java index b01a84fbe..aa92a1b58 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/ReadConfig.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/ReadConfig.java @@ -2,6 +2,7 @@ import java.util.concurrent.Executor; +import tech.ydb.core.metrics.Attr; import tech.ydb.topic.description.CodecRegistry; import tech.ydb.topic.settings.ReaderSettings; @@ -21,6 +22,11 @@ public class ReadConfig { public ReadConfig(CodecRegistry codecRegistry, Executor control, Executor processor, Executor decompressor, ReaderSettings settings) { + this(codecRegistry, control, processor, decompressor, settings, new Attr[0]); + } + + public ReadConfig(CodecRegistry codecRegistry, Executor control, Executor processor, Executor decompressor, + ReaderSettings settings, Attr... attributes) { this.codecRegistry = codecRegistry; this.control = control; this.processor = processor; @@ -28,7 +34,8 @@ public ReadConfig(CodecRegistry codecRegistry, Executor control, Executor proces this.consumerName = settings.getConsumerName(); this.maxMemoryUsageBytes = settings.getMaxMemoryUsageBytes(); this.maxBatchSize = settings.getMaxBatchSize(); - this.metrics = new ReaderMetrics(settings.getMeter(), settings.getConsumerName(), settings.getReaderName()); + this.metrics = new ReaderMetrics(settings.getMeter(), settings.getConsumerName(), settings.getReaderName(), + attributes); } public CodecRegistry getCodecRegistry() { 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..1be338c1d 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 @@ -46,7 +46,7 @@ interface Source { private MetricRegistration bufferMessagesGauge = MetricRegistration.NOOP; private MetricRegistration ageGauge = MetricRegistration.NOOP; - ReaderMetrics(Meter meter, String consumer, String readerName) { + ReaderMetrics(Meter meter, String consumer, String readerName, Attr... attributes) { this.meter = meter; this.enabled = meter != Meter.NOOP; this.deliveredMessages = meter.createCounter( @@ -67,7 +67,9 @@ interface Source { "The time spent decompressing a message, excluding queueing."); this.processingDuration = meter.createHistogram("ydb.topic.reader.message.processing.duration", "s", "The time from receipt until a message is ready for delivery."); - this.commonAttributes = createCommonAttributes(consumer, readerName); + Attr[] readerAttributes = createCommonAttributes(consumer, readerName); + this.commonAttributes = Arrays.copyOf(attributes, attributes.length + readerAttributes.length); + System.arraycopy(readerAttributes, 0, commonAttributes, attributes.length, readerAttributes.length); } void addSource(Source source) { diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/SyncReaderImpl.java b/topic/src/main/java/tech/ydb/topic/read/impl/SyncReaderImpl.java index 984c997da..1dbc0e096 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/SyncReaderImpl.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/SyncReaderImpl.java @@ -21,6 +21,7 @@ import tech.ydb.core.Issue; import tech.ydb.core.Status; +import tech.ydb.core.metrics.Attr; import tech.ydb.topic.TopicRpc; import tech.ydb.topic.description.CodecRegistry; import tech.ydb.topic.description.OffsetsRange; @@ -65,7 +66,8 @@ public SyncReaderImpl(TopicRpc topicRpc, ReaderSettings settings, @Nonnull Codec this.debugId = DebugTools.createDebugId(settings.getLogPrefix()); this.decompressor = new LazyExecutor("reader[" + debugId + "]-decoder", settings.getDecompressionExecutor()); - this.config = new ReadConfig(codecRegistry, Runnable::run, Runnable::run, decompressor, settings); + this.config = new ReadConfig(codecRegistry, Runnable::run, Runnable::run, decompressor, settings, + topicRpc.getMetricAttributes().toArray(new Attr[0])); this.impl = new Impl(topicRpc, debugId, settings, config); String readerName = settings.getReaderName(); diff --git a/topic/src/main/java/tech/ydb/topic/write/impl/WriterImpl.java b/topic/src/main/java/tech/ydb/topic/write/impl/WriterImpl.java index 744e98a44..30c43e5a9 100644 --- a/topic/src/main/java/tech/ydb/topic/write/impl/WriterImpl.java +++ b/topic/src/main/java/tech/ydb/topic/write/impl/WriterImpl.java @@ -19,6 +19,7 @@ import tech.ydb.core.Status; import tech.ydb.core.UnexpectedResultException; import tech.ydb.core.impl.Observability; +import tech.ydb.core.metrics.Attr; import tech.ydb.topic.TopicRpc; import tech.ydb.topic.description.CodecRegistry; import tech.ydb.topic.impl.DebugTools; @@ -58,7 +59,8 @@ public WriterImpl(TopicRpc topicRpc, WriterSettings settings, Executor compressi public WriterImpl(TopicRpc topicRpc, WriteStreamFactory factory, WriterSettings settings, Executor compressionExecutor, @Nonnull CodecRegistry codecRegistry) { this.debugId = DebugTools.createDebugId(settings.getLogPrefix()); - this.metrics = new WriterMetrics(settings.getMeter(), settings.getTopicPath(), settings.getWriterName()); + this.metrics = new WriterMetrics(settings.getMeter(), settings.getTopicPath(), settings.getWriterName(), + topicRpc.getMetricAttributes().toArray(new Attr[0])); this.stream = new WriteSession(debugId, factory, settings, topicRpc.getScheduler(), new ListenerImpl()); this.writeQueue = new WriterQueue(debugId, settings, metrics, codecRegistry, compressionExecutor, sendTask); Observability.reportMetricsUsage(settings.getMeter()); diff --git a/topic/src/main/java/tech/ydb/topic/write/impl/WriterMetrics.java b/topic/src/main/java/tech/ydb/topic/write/impl/WriterMetrics.java index 755795868..2f251cddf 100644 --- a/topic/src/main/java/tech/ydb/topic/write/impl/WriterMetrics.java +++ b/topic/src/main/java/tech/ydb/topic/write/impl/WriterMetrics.java @@ -33,7 +33,7 @@ final class WriterMetrics { private MetricRegistration bufferLimitGauge = MetricRegistration.NOOP; private MetricRegistration oldestAgeGauge = MetricRegistration.NOOP; - WriterMetrics(Meter meter, String topic, String writerName) { + WriterMetrics(Meter meter, String topic, String writerName, Attr... attributes) { this.meter = meter; this.enabled = meter != Meter.NOOP; this.sendingMessages = meter.createCounter("ydb.topic.writer.sending.messages", MESSAGE_UNIT, @@ -52,9 +52,11 @@ final class WriterMetrics { "The time spent compressing a message, excluding queueing."); this.processingDuration = meter.createHistogram("ydb.topic.writer.message.processing.duration", "s", "The time from buffer acceptance until a message is ready for sending."); - this.commonAttributes = writerName == null - ? new Attr[0] - : new Attr[]{Attr.of("topic", topic), Attr.of("writer.name", writerName)}; + this.commonAttributes = Arrays.copyOf(attributes, attributes.length + (writerName == null ? 0 : 2)); + if (writerName != null) { + commonAttributes[attributes.length] = Attr.of("topic", topic); + commonAttributes[attributes.length + 1] = Attr.of("writer.name", writerName); + } } void register(LongSupplier bufferUsed, LongSupplier bufferLimit, LongSupplier oldestTimestamp) { 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..44be8c953 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 @@ -15,14 +15,17 @@ import tech.ydb.common.retry.RetryConfig; import tech.ydb.core.Status; import tech.ydb.core.StatusCode; +import tech.ydb.core.grpc.GrpcTransport; +import tech.ydb.core.impl.SingleChannelTransport; +import tech.ydb.proto.topic.v1.TopicServiceGrpc; import tech.ydb.topic.TopicClient; -import tech.ydb.topic.TopicRpc; import tech.ydb.topic.description.Codec; -import tech.ydb.topic.impl.TopicClientImpl; import tech.ydb.topic.read.DecompressionException; +import tech.ydb.topic.read.AsyncReader; import tech.ydb.topic.read.Message; import tech.ydb.topic.read.SyncReader; import tech.ydb.topic.settings.ReaderSettings; +import tech.ydb.topic.settings.ReadEventHandlersSettings; import tech.ydb.topic.utils.InMemoryMeter; import static tech.ydb.topic.utils.InMemoryMeter.attributes; @@ -31,17 +34,21 @@ public class ReaderMetricsTest { private static final String PREFIX = "ydb.topic.reader."; private static final byte[] MSG1 = new byte[] { 1, 2 }; private static final byte[] MSG2 = new byte[] { 3, 4, 5 }; - private static final Map COMMON = attributes("consumer", "consumer", "reader.name", "reader"); + private static final Map COMMON = attributes("endpoint", "reader.example:2135", + "database", "/reader-db", "consumer", "consumer", "reader.name", "reader"); private static final Map TOPIC = attributes( + "endpoint", "reader.example:2135", "database", "/reader-db", "consumer", "consumer", "reader.name", "reader", "topic", "/topic"); private static final Map OTHER = attributes( + "endpoint", "reader.example:2135", "database", "/reader-db", "consumer", "consumer", "reader.name", "reader", "topic", "/other-topic"); private final InMemoryMeter meter = new InMemoryMeter(); @Test public void readAndCommitMetrics() throws Exception { ReadStreamMock stream = new ReadStreamMock(true); - try (TopicClient client = TopicClientImpl.newClient(mockRpc(stream)).build()) { + try (GrpcTransport transport = mockTransport(stream); + TopicClient client = TopicClient.newClient(transport).build()) { SyncReader reader = client.createSyncReader(ReaderSettings.newBuilder() .addTopic("/topic").addTopic("/other-topic").setConsumerName("consumer") .setMaxMemoryUsageBytes(100).setDecompressionExecutor(Runnable::run) @@ -94,9 +101,11 @@ public void readAndCommitMetrics() throws Exception { meter.assertCounter(PREFIX + "commit.acknowledged", 1, OTHER); meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 0, COMMON); for (String name : Arrays.asList("compression.duration", "message.processing.duration")) { - meter.assertHistogram(PREFIX + name, "s", 2, attributes("consumer", "consumer", + meter.assertHistogram(PREFIX + name, "s", 2, attributes( + "endpoint", "reader.example:2135", "database", "/reader-db", "consumer", "consumer", "reader.name", "reader", "topic", "/topic", "codec", "gzip", "outcome", "ok")); - meter.assertHistogram(PREFIX + name, "s", 1, attributes("consumer", "consumer", + meter.assertHistogram(PREFIX + name, "s", 1, attributes( + "endpoint", "reader.example:2135", "database", "/reader-db", "consumer", "consumer", "reader.name", "reader", "topic", "/other-topic", "codec", "gzip", "outcome", "ok")); } meter.assertUnit(PREFIX + "received.bytes", "By"); @@ -114,9 +123,12 @@ public void readAndCommitMetrics() throws Exception { @Test public void readWithoutConsumerMetrics() throws Exception { ReadStreamMock stream = new ReadStreamMock(true); - Map common = attributes("reader.name", "without-consumer"); - Map topic = attributes("reader.name", "without-consumer", "topic", "/topic"); - try (TopicClient client = TopicClientImpl.newClient(mockRpc(stream)).build()) { + Map common = attributes("endpoint", "reader.example:2135", "database", "/reader-db", + "reader.name", "without-consumer"); + Map topic = attributes("endpoint", "reader.example:2135", "database", "/reader-db", + "reader.name", "without-consumer", "topic", "/topic"); + try (GrpcTransport transport = mockTransport(stream); + TopicClient client = TopicClient.newClient(transport).build()) { SyncReader reader = client.createSyncReader(ReaderSettings.newBuilder().addTopic("/topic") .withoutConsumer().setDecompressionExecutor(Runnable::run) .withMeter(meter, "without-consumer").build()); @@ -135,7 +147,8 @@ public void readWithoutConsumerMetrics() throws Exception { meter.assertLongGauge(PREFIX + "partition_session.count", 1, common); 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", + meter.assertHistogram(PREFIX + name, "s", 1, attributes( + "endpoint", "reader.example:2135", "database", "/reader-db", "reader.name", "without-consumer", "topic", "/topic", "codec", "gzip", "outcome", "ok")); } } finally { @@ -152,11 +165,14 @@ public void sessionErrorsFollowRetriesAndStop() throws Exception { ReadStreamMock second = new ReadStreamMock(true); ReadStreamMock third = new ReadStreamMock(true); RetryConfig retries = status -> (number, elapsed) -> number < 2 ? 0 : -1; - Map retry = attributes("consumer", "consumer", "reader.name", "reader", + Map retry = attributes("endpoint", "reader.example:2135", "database", "/reader-db", + "consumer", "consumer", "reader.name", "reader", "retry_decision", "retry", "status_code", "TRANSPORT_UNAVAILABLE", "error.type", "transport_error"); - Map stop = attributes("consumer", "consumer", "reader.name", "reader", + Map stop = attributes("endpoint", "reader.example:2135", "database", "/reader-db", + "consumer", "consumer", "reader.name", "reader", "retry_decision", "stop", "status_code", "TRANSPORT_UNAVAILABLE", "error.type", "transport_error"); - try (TopicClient client = TopicClientImpl.newClient(mockRpc(first, second, third)).build()) { + try (GrpcTransport transport = mockTransport(first, second, third); + TopicClient client = TopicClient.newClient(transport).build()) { SyncReader reader = client.createSyncReader(ReaderSettings.newBuilder().addTopic("/topic") .setConsumerName("consumer").setRetryConfig(retries).withMeter(meter, "reader").build()); try { @@ -185,7 +201,8 @@ public void decompressionFailureMetrics() throws Exception { Codec codec = Mockito.mock(Codec.class, Mockito.CALLS_REAL_METHODS); Mockito.when(codec.getId()).thenReturn(Codec.GZIP); Mockito.doThrow(problem).when(codec).decode(Mockito.any(InputStream.class)); - try (TopicClient client = TopicClientImpl.newClient(mockRpc(stream)).registerCodec(codec).build()) { + try (GrpcTransport transport = mockTransport(stream); + TopicClient client = TopicClient.newClient(transport).registerCodec(codec).build()) { SyncReader reader = client.createSyncReader(ReaderSettings.newBuilder().addTopic("/topic") .setConsumerName("consumer").setDecompressionExecutor(Runnable::run) .withMeter(meter, "reader").build()); @@ -204,10 +221,12 @@ public void decompressionFailureMetrics() throws Exception { meter.assertCounter(PREFIX + "delivered.messages", 2, TOPIC); meter.assertNoCounterSamples(PREFIX + "commit.acknowledged"); for (String name : Arrays.asList("compression.duration", "message.processing.duration")) { - meter.assertHistogram(PREFIX + name, "s", 2, attributes("consumer", "consumer", + meter.assertHistogram(PREFIX + name, "s", 2, attributes( + "endpoint", "reader.example:2135", "database", "/reader-db", "consumer", "consumer", "reader.name", "reader", "topic", "/topic", "codec", "gzip", "outcome", "error", "error.type", "java.io.IOException")); - meter.assertHistogram(PREFIX + name, "s", 0, attributes("consumer", "consumer", + meter.assertHistogram(PREFIX + name, "s", 0, attributes( + "endpoint", "reader.example:2135", "database", "/reader-db", "consumer", "consumer", "reader.name", "reader", "topic", "/topic", "codec", "gzip", "outcome", "ok")); } } finally { @@ -218,10 +237,58 @@ public void decompressionFailureMetrics() throws Exception { meter.assertNoGauges(); } - private static TopicRpc mockRpc(ReadStreamMock first, ReadStreamMock... rest) { - TopicRpc rpc = Mockito.mock(TopicRpc.class); - Mockito.when(rpc.getScheduler()).thenReturn(Mockito.mock(ScheduledExecutorService.class)); - Mockito.when(rpc.readSession(Mockito.anyString())).thenReturn(first, rest); - return rpc; + @Test + public void reusedSettingsKeepConnectionAttributes() throws Exception { + ReadStreamMock first = new ReadStreamMock(true); + ReadStreamMock second = new ReadStreamMock(true); + ReaderSettings settings = ReaderSettings.newBuilder().addTopic("/topic").setConsumerName("consumer") + .setDecompressionExecutor(Runnable::run).withMeter(meter, "reader").build(); + CompletableFuture delivered = new CompletableFuture<>(); + Map otherCommon = attributes("endpoint", "other-reader.example:2137", + "database", "/other-reader-db", "consumer", "consumer", "reader.name", "reader"); + try (GrpcTransport firstTransport = mockTransport(first); + GrpcTransport secondTransport = Mockito.spy(new SingleChannelTransport( + GrpcTransport.forHost("other-reader.example", 2137, "/other-reader-db") + .withScheduler(Mockito.mock(ScheduledExecutorService.class)))); + TopicClient firstClient = TopicClient.newClient(firstTransport).build(); + TopicClient secondClient = TopicClient.newClient(secondTransport).build()) { + Mockito.doReturn(second).when(secondTransport) + .readWriteStreamCall(Mockito.eq(TopicServiceGrpc.getStreamReadMethod()), Mockito.any()); + SyncReader sync = firstClient.createSyncReader(settings); + AsyncReader async = secondClient.createAsyncReader(settings, ReadEventHandlersSettings.newBuilder() + .setExecutor(Runnable::run).setEventHandler(event -> delivered.complete(event.getMessages().get(0))) + .build()); + try { + sync.init(); + async.init(); + first.responseInit("first"); + second.responseInit("second"); + first.responseStartPartition("/topic", 42, 0); + second.responseStartPartition("/topic", 43, 0); + first.responseData(12).partition(1, 0).batch(Codec.GZIP, MSG1).and().send(); + second.responseData(8).partition(1, 0).batch(Codec.GZIP, MSG2).and().send(); + Assert.assertArrayEquals(MSG1, sync.receive(0, TimeUnit.SECONDS).getData()); + Assert.assertArrayEquals(MSG2, delivered.get(1, TimeUnit.SECONDS).getData()); + meter.assertCounter(PREFIX + "received.bytes", 12, COMMON); + meter.assertCounter(PREFIX + "received.bytes", 8, otherCommon); + meter.assertLongGauge(PREFIX + "partition_session.count", 1, COMMON); + meter.assertLongGauge(PREFIX + "partition_session.count", 1, otherCommon); + meter.assertCounterSeries(PREFIX + "received.bytes", 2); + Assert.assertSame(meter, settings.getMeter()); + } finally { + sync.shutdown(); + async.shutdown().get(1, TimeUnit.SECONDS); + } + } + meter.assertNoGauges(); + } + + private static GrpcTransport mockTransport(ReadStreamMock first, ReadStreamMock... rest) { + GrpcTransport transport = Mockito.spy(new SingleChannelTransport( + GrpcTransport.forEndpoint("grpcs://reader.example:2135", "/reader-db") + .withScheduler(Mockito.mock(ScheduledExecutorService.class)))); + Mockito.doReturn(first, (Object[]) rest).when(transport) + .readWriteStreamCall(Mockito.eq(TopicServiceGrpc.getStreamReadMethod()), Mockito.any()); + return transport; } } diff --git a/topic/src/test/java/tech/ydb/topic/write/impl/WriterMetricsTest.java b/topic/src/test/java/tech/ydb/topic/write/impl/WriterMetricsTest.java index db38dfebc..054402fb5 100644 --- a/topic/src/test/java/tech/ydb/topic/write/impl/WriterMetricsTest.java +++ b/topic/src/test/java/tech/ydb/topic/write/impl/WriterMetricsTest.java @@ -16,10 +16,11 @@ import tech.ydb.common.retry.RetryConfig; import tech.ydb.core.Status; import tech.ydb.core.StatusCode; +import tech.ydb.core.grpc.GrpcTransport; +import tech.ydb.core.impl.SingleChannelTransport; +import tech.ydb.proto.topic.v1.TopicServiceGrpc; import tech.ydb.topic.TopicClient; -import tech.ydb.topic.TopicRpc; import tech.ydb.topic.description.Codec; -import tech.ydb.topic.impl.TopicClientImpl; import tech.ydb.topic.settings.WriterSettings; import tech.ydb.topic.utils.InMemoryMeter; import tech.ydb.topic.write.AsyncWriter; @@ -31,13 +32,15 @@ public class WriterMetricsTest { private static final String PREFIX = "ydb.topic.writer."; - private static final Map COMMON = attributes("topic", "/test/topic", "writer.name", "writer"); + private static final Map COMMON = attributes("endpoint", "writer.example:2136", + "database", "/writer-db", "topic", "/test/topic", "writer.name", "writer"); private final InMemoryMeter meter = new InMemoryMeter(); @Test public void sendAndAcknowledgeMetrics() throws Exception { WriteStreamMock stream = new WriteStreamMock(); - try (TopicClient client = TopicClientImpl.newClient(mockRpc(stream)) + try (GrpcTransport transport = mockTransport(stream); + TopicClient client = TopicClient.newClient(transport) .setCompressionExecutor(Runnable::run).build()) { AsyncWriter writer = client.createAsyncWriter(WriterSettings.newBuilder().setTopicPath("/test/topic") .setCodec(Codec.RAW).setMaxSendBufferMemorySize(100).withMeter(meter, "writer").build()); @@ -62,6 +65,7 @@ public void sendAndAcknowledgeMetrics() throws Exception { meter.assertDoubleGauge(PREFIX + "sending.oldest_age", 0, COMMON); meter.assertHistogram(PREFIX + "message.ack.duration", "s", 2, COMMON); meter.assertHistogram(PREFIX + "message.processing.duration", "s", 2, attributes( + "endpoint", "writer.example:2136", "database", "/writer-db", "topic", "/test/topic", "writer.name", "writer", "codec", "raw", "outcome", "ok")); meter.assertNoHistogramSamples(PREFIX + "buffer.wait.duration"); meter.assertNoHistogramSamples(PREFIX + "compression.duration"); @@ -84,7 +88,8 @@ public void compressedSyncSendMetrics() throws Exception { stream.close(Status.SUCCESS); return null; }).when(stream).close(); - try (TopicClient client = TopicClientImpl.newClient(mockRpc(stream)) + try (GrpcTransport transport = mockTransport(stream); + TopicClient client = TopicClient.newClient(transport) .setCompressionExecutor(Runnable::run).build()) { SyncWriter writer = client.createSyncWriter(WriterSettings.newBuilder().setTopicPath("/test/topic") .setCodec(Codec.GZIP).setMaxSendBufferMemorySize(100).withMeter(meter, "writer").build()); @@ -103,6 +108,7 @@ public void compressedSyncSendMetrics() throws Exception { meter.assertLongGauge(PREFIX + "buffer.used.bytes", 0, COMMON); for (String name : Arrays.asList("compression.duration", "message.processing.duration")) { meter.assertHistogram(PREFIX + name, "s", 2, attributes( + "endpoint", "writer.example:2136", "database", "/writer-db", "topic", "/test/topic", "writer.name", "writer", "codec", "gzip", "outcome", "ok")); } for (String name : Arrays.asList("buffer.wait.duration", "message.ack.duration")) { @@ -122,11 +128,14 @@ public void sessionErrorsFollowRetriesAndStop() throws Exception { WriteStreamMock second = new WriteStreamMock(); WriteStreamMock third = new WriteStreamMock(); RetryConfig retries = status -> (number, elapsed) -> number < 2 ? 0 : -1; - Map retry = attributes("topic", "/test/topic", "writer.name", "writer", + Map retry = attributes("endpoint", "writer.example:2136", "database", "/writer-db", + "topic", "/test/topic", "writer.name", "writer", "retry_decision", "retry", "status_code", "TRANSPORT_UNAVAILABLE", "error.type", "transport_error"); - Map stop = attributes("topic", "/test/topic", "writer.name", "writer", + Map stop = attributes("endpoint", "writer.example:2136", "database", "/writer-db", + "topic", "/test/topic", "writer.name", "writer", "retry_decision", "stop", "status_code", "TRANSPORT_UNAVAILABLE", "error.type", "transport_error"); - try (TopicClient client = TopicClientImpl.newClient(mockRpc(first, second, third)) + try (GrpcTransport transport = mockTransport(first, second, third); + TopicClient client = TopicClient.newClient(transport) .setCompressionExecutor(Runnable::run).build()) { AsyncWriter writer = client.createAsyncWriter(WriterSettings.newBuilder().setTopicPath("/test/topic") .setRetryConfig(retries).withMeter(meter, "writer").build()); @@ -159,7 +168,8 @@ public void compressionFailureMetrics() throws Exception { Codec codec = Mockito.mock(Codec.class, Mockito.CALLS_REAL_METHODS); Mockito.when(codec.getId()).thenReturn(Codec.GZIP); Mockito.doThrow(problem).when(codec).encode(Mockito.any(OutputStream.class)); - try (TopicClient client = TopicClientImpl.newClient(mockRpc(stream)).registerCodec(codec) + try (GrpcTransport transport = mockTransport(stream); + TopicClient client = TopicClient.newClient(transport).registerCodec(codec) .setCompressionExecutor(Runnable::run).build()) { AsyncWriter writer = client.createAsyncWriter(WriterSettings.newBuilder().setTopicPath("/test/topic") .setCodec(Codec.GZIP).withMeter(meter, "writer").build()); @@ -178,10 +188,12 @@ public void compressionFailureMetrics() throws Exception { meter.assertNoHistogramSamples(PREFIX + "message.ack.duration"); meter.assertLongGauge(PREFIX + "buffer.used.bytes", 0, COMMON); for (String name : Arrays.asList("compression.duration", "message.processing.duration")) { - meter.assertHistogram(PREFIX + name, "s", 2, attributes("topic", "/test/topic", + meter.assertHistogram(PREFIX + name, "s", 2, attributes( + "endpoint", "writer.example:2136", "database", "/writer-db", "topic", "/test/topic", "writer.name", "writer", "codec", "gzip", "outcome", "error", "error.type", "java.io.IOException")); - meter.assertHistogram(PREFIX + name, "s", 0, attributes("topic", "/test/topic", + meter.assertHistogram(PREFIX + name, "s", 0, attributes( + "endpoint", "writer.example:2136", "database", "/writer-db", "topic", "/test/topic", "writer.name", "writer", "codec", "gzip", "outcome", "ok")); } } finally { @@ -194,10 +206,61 @@ public void compressionFailureMetrics() throws Exception { meter.assertNoGauges(); } - private static TopicRpc mockRpc(WriteStreamMock first, WriteStreamMock... rest) { - TopicRpc rpc = Mockito.mock(TopicRpc.class); - Mockito.when(rpc.getScheduler()).thenReturn(Mockito.mock(ScheduledExecutorService.class)); - Mockito.when(rpc.writeSession(Mockito.anyString())).thenReturn(first, rest); - return rpc; + @Test + public void reusedSettingsKeepConnectionAttributes() throws Exception { + WriteStreamMock first = new WriteStreamMock(); + WriteStreamMock second = new WriteStreamMock(); + WriterSettings settings = WriterSettings.newBuilder().setTopicPath("/test/topic").setCodec(Codec.RAW) + .withMeter(meter, "writer").build(); + Map otherCommon = attributes("endpoint", "other-writer.example:2137", + "database", "/other-writer-db", "topic", "/test/topic", "writer.name", "writer"); + try (GrpcTransport firstTransport = mockTransport(first); + GrpcTransport secondTransport = Mockito.spy(new SingleChannelTransport( + GrpcTransport.forHost("other-writer.example", 2137, "/other-writer-db") + .withScheduler(Mockito.mock(ScheduledExecutorService.class)))); + TopicClient firstClient = TopicClient.newClient(firstTransport) + .setCompressionExecutor(Runnable::run).build(); + TopicClient secondClient = TopicClient.newClient(secondTransport) + .setCompressionExecutor(Runnable::run).build()) { + Mockito.doReturn(second).when(secondTransport) + .readWriteStreamCall(Mockito.eq(TopicServiceGrpc.getStreamWriteMethod()), Mockito.any()); + AsyncWriter firstWriter = firstClient.createAsyncWriter(settings); + AsyncWriter secondWriter = secondClient.createAsyncWriter(settings); + try { + firstWriter.init(); + secondWriter.init(); + first.sendInitResponse(0); + second.sendInitResponse(0); + CompletableFuture firstAck = firstWriter.send(Message.of(new byte[]{1, 2})); + CompletableFuture secondAck = secondWriter.send(Message.of(new byte[]{3, 4, 5})); + meter.assertCounter(PREFIX + "sending.bytes", 2, COMMON); + meter.assertCounter(PREFIX + "sending.bytes", 3, otherCommon); + meter.assertLongGauge(PREFIX + "buffer.used.bytes", 2, COMMON); + meter.assertLongGauge(PREFIX + "buffer.used.bytes", 3, otherCommon); + meter.assertCounterSeries(PREFIX + "sending.bytes", 2); + first.sendAckResponse(1, 10); + second.sendAckResponse(1, 20); + firstAck.get(1, TimeUnit.SECONDS); + secondAck.get(1, TimeUnit.SECONDS); + Assert.assertSame(meter, settings.getMeter()); + } finally { + CompletableFuture firstClosed = firstWriter.shutdown(); + CompletableFuture secondClosed = secondWriter.shutdown(); + first.close(Status.SUCCESS); + second.close(Status.SUCCESS); + firstClosed.get(1, TimeUnit.SECONDS); + secondClosed.get(1, TimeUnit.SECONDS); + } + } + meter.assertNoGauges(); + } + + private static GrpcTransport mockTransport(WriteStreamMock first, WriteStreamMock... rest) { + GrpcTransport transport = Mockito.spy(new SingleChannelTransport( + GrpcTransport.forEndpoint("grpc://writer.example:2136", "/writer-db") + .withScheduler(Mockito.mock(ScheduledExecutorService.class)))); + Mockito.doReturn(first, (Object[]) rest).when(transport) + .readWriteStreamCall(Mockito.eq(TopicServiceGrpc.getStreamWriteMethod()), Mockito.any()); + return transport; } } From e85d22f7c68c6035296c832983effd1d7f4a7aef Mon Sep 17 00:00:00 2001 From: KirillKurdyukov Date: Mon, 26 Oct 2026 17:36:31 +0300 Subject: [PATCH 2/2] Pass Topic metric endpoint and database directly --- .../main/java/tech/ydb/topic/TopicRpc.java | 11 +++++---- .../tech/ydb/topic/impl/GrpcTopicRpc.java | 13 +++++----- .../ydb/topic/read/impl/AsyncReaderImpl.java | 3 +-- .../tech/ydb/topic/read/impl/ReadConfig.java | 7 +++--- .../ydb/topic/read/impl/ReaderMetrics.java | 24 ++++++++++++------- .../ydb/topic/read/impl/SyncReaderImpl.java | 3 +-- .../tech/ydb/topic/write/impl/WriterImpl.java | 3 +-- .../ydb/topic/write/impl/WriterMetrics.java | 15 +++++++----- 8 files changed, 44 insertions(+), 35 deletions(-) diff --git a/topic/src/main/java/tech/ydb/topic/TopicRpc.java b/topic/src/main/java/tech/ydb/topic/TopicRpc.java index bc4edfe0b..2bfba6ef2 100644 --- a/topic/src/main/java/tech/ydb/topic/TopicRpc.java +++ b/topic/src/main/java/tech/ydb/topic/TopicRpc.java @@ -1,7 +1,5 @@ package tech.ydb.topic; -import java.util.Collections; -import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ScheduledExecutorService; @@ -9,7 +7,6 @@ import tech.ydb.core.Status; import tech.ydb.core.grpc.GrpcReadWriteStream; import tech.ydb.core.grpc.GrpcRequestSettings; -import tech.ydb.core.metrics.Attr; import tech.ydb.proto.topic.YdbTopic.AlterTopicRequest; import tech.ydb.proto.topic.YdbTopic.CommitOffsetRequest; import tech.ydb.proto.topic.YdbTopic.CreateTopicRequest; @@ -99,7 +96,11 @@ default GrpcReadWriteStream getMetricAttributes() { - return Collections.emptyList(); + default String getEndpoint() { + return ""; + } + + default String getDatabase() { + return ""; } } diff --git a/topic/src/main/java/tech/ydb/topic/impl/GrpcTopicRpc.java b/topic/src/main/java/tech/ydb/topic/impl/GrpcTopicRpc.java index 3d5890c33..88f009b0c 100644 --- a/topic/src/main/java/tech/ydb/topic/impl/GrpcTopicRpc.java +++ b/topic/src/main/java/tech/ydb/topic/impl/GrpcTopicRpc.java @@ -1,7 +1,5 @@ package tech.ydb.topic.impl; -import java.util.Arrays; -import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ScheduledExecutorService; @@ -13,7 +11,6 @@ import tech.ydb.core.grpc.GrpcReadWriteStream; import tech.ydb.core.grpc.GrpcRequestSettings; import tech.ydb.core.grpc.GrpcTransport; -import tech.ydb.core.metrics.Attr; import tech.ydb.core.operation.OperationBinder; import tech.ydb.proto.topic.YdbTopic; import tech.ydb.proto.topic.YdbTopic.AlterTopicRequest; @@ -135,8 +132,12 @@ public ScheduledExecutorService getScheduler() { } @Override - public List getMetricAttributes() { - return Arrays.asList( - Attr.of("endpoint", transport.getEndpoint()), Attr.of("database", transport.getDatabase())); + public String getEndpoint() { + return transport.getEndpoint(); + } + + @Override + public String getDatabase() { + return transport.getDatabase(); } } diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/AsyncReaderImpl.java b/topic/src/main/java/tech/ydb/topic/read/impl/AsyncReaderImpl.java index 2faffbd18..e8eddbd71 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/AsyncReaderImpl.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/AsyncReaderImpl.java @@ -14,7 +14,6 @@ import tech.ydb.core.Issue; import tech.ydb.core.Status; import tech.ydb.core.StatusCode; -import tech.ydb.core.metrics.Attr; import tech.ydb.topic.TopicRpc; import tech.ydb.topic.description.CodecRegistry; import tech.ydb.topic.impl.DebugTools; @@ -62,7 +61,7 @@ public AsyncReaderImpl(TopicRpc topicRpc, this.controlEventsExecutor = new SerialExecutor(processor); this.config = new ReadConfig(codecRegistry, controlEventsExecutor, processor, decompressor, settings, - topicRpc.getMetricAttributes().toArray(new Attr[0])); + topicRpc.getEndpoint(), topicRpc.getDatabase()); this.impl = new Impl(topicRpc, debugId, settings, config); String readerName = settings.getReaderName(); diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/ReadConfig.java b/topic/src/main/java/tech/ydb/topic/read/impl/ReadConfig.java index aa92a1b58..1aeecc0a1 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/ReadConfig.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/ReadConfig.java @@ -2,7 +2,6 @@ import java.util.concurrent.Executor; -import tech.ydb.core.metrics.Attr; import tech.ydb.topic.description.CodecRegistry; import tech.ydb.topic.settings.ReaderSettings; @@ -22,11 +21,11 @@ public class ReadConfig { public ReadConfig(CodecRegistry codecRegistry, Executor control, Executor processor, Executor decompressor, ReaderSettings settings) { - this(codecRegistry, control, processor, decompressor, settings, new Attr[0]); + this(codecRegistry, control, processor, decompressor, settings, "", ""); } public ReadConfig(CodecRegistry codecRegistry, Executor control, Executor processor, Executor decompressor, - ReaderSettings settings, Attr... attributes) { + ReaderSettings settings, String endpoint, String database) { this.codecRegistry = codecRegistry; this.control = control; this.processor = processor; @@ -35,7 +34,7 @@ public ReadConfig(CodecRegistry codecRegistry, Executor control, Executor proces this.maxMemoryUsageBytes = settings.getMaxMemoryUsageBytes(); this.maxBatchSize = settings.getMaxBatchSize(); this.metrics = new ReaderMetrics(settings.getMeter(), settings.getConsumerName(), settings.getReaderName(), - attributes); + endpoint, database); } public CodecRegistry getCodecRegistry() { 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 1be338c1d..3fd5b4776 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 @@ -46,7 +46,11 @@ interface Source { private MetricRegistration bufferMessagesGauge = MetricRegistration.NOOP; private MetricRegistration ageGauge = MetricRegistration.NOOP; - ReaderMetrics(Meter meter, String consumer, String readerName, Attr... attributes) { + ReaderMetrics(Meter meter, String consumer, String readerName) { + this(meter, consumer, readerName, "", ""); + } + + ReaderMetrics(Meter meter, String consumer, String readerName, String endpoint, String database) { this.meter = meter; this.enabled = meter != Meter.NOOP; this.deliveredMessages = meter.createCounter( @@ -67,9 +71,8 @@ interface Source { "The time spent decompressing a message, excluding queueing."); this.processingDuration = meter.createHistogram("ydb.topic.reader.message.processing.duration", "s", "The time from receipt until a message is ready for delivery."); - Attr[] readerAttributes = createCommonAttributes(consumer, readerName); - this.commonAttributes = Arrays.copyOf(attributes, attributes.length + readerAttributes.length); - System.arraycopy(readerAttributes, 0, commonAttributes, attributes.length, readerAttributes.length); + this.commonAttributes = enabled + ? createCommonAttributes(consumer, readerName, endpoint, database) : new Attr[0]; } void addSource(Source source) { @@ -230,15 +233,20 @@ private void record(LongCounter counter, long value, Attr... extraAttributes) { } } - private static Attr[] createCommonAttributes(String consumer, String readerName) { + private static Attr[] createCommonAttributes(String consumer, String readerName, String endpoint, String database) { + Attr endpointAttr = Attr.of("endpoint", endpoint); + Attr databaseAttr = Attr.of("database", database); boolean hasReaderName = readerName != null && !readerName.isEmpty(); if (consumer == null) { - return hasReaderName ? new Attr[]{Attr.of("reader.name", readerName)} : new Attr[0]; + return hasReaderName + ? new Attr[]{endpointAttr, databaseAttr, Attr.of("reader.name", readerName)} + : new Attr[]{endpointAttr, databaseAttr}; } if (!hasReaderName) { - return new Attr[]{Attr.of("consumer", consumer)}; + return new Attr[]{endpointAttr, databaseAttr, Attr.of("consumer", consumer)}; } - return new Attr[]{Attr.of("consumer", consumer), Attr.of("reader.name", readerName)}; + return new Attr[]{ + endpointAttr, databaseAttr, Attr.of("consumer", consumer), Attr.of("reader.name", readerName)}; } private static class CommitOffsets { diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/SyncReaderImpl.java b/topic/src/main/java/tech/ydb/topic/read/impl/SyncReaderImpl.java index 1dbc0e096..250fda529 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/SyncReaderImpl.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/SyncReaderImpl.java @@ -21,7 +21,6 @@ import tech.ydb.core.Issue; import tech.ydb.core.Status; -import tech.ydb.core.metrics.Attr; import tech.ydb.topic.TopicRpc; import tech.ydb.topic.description.CodecRegistry; import tech.ydb.topic.description.OffsetsRange; @@ -67,7 +66,7 @@ public SyncReaderImpl(TopicRpc topicRpc, ReaderSettings settings, @Nonnull Codec this.decompressor = new LazyExecutor("reader[" + debugId + "]-decoder", settings.getDecompressionExecutor()); this.config = new ReadConfig(codecRegistry, Runnable::run, Runnable::run, decompressor, settings, - topicRpc.getMetricAttributes().toArray(new Attr[0])); + topicRpc.getEndpoint(), topicRpc.getDatabase()); this.impl = new Impl(topicRpc, debugId, settings, config); String readerName = settings.getReaderName(); diff --git a/topic/src/main/java/tech/ydb/topic/write/impl/WriterImpl.java b/topic/src/main/java/tech/ydb/topic/write/impl/WriterImpl.java index 30c43e5a9..69887b68e 100644 --- a/topic/src/main/java/tech/ydb/topic/write/impl/WriterImpl.java +++ b/topic/src/main/java/tech/ydb/topic/write/impl/WriterImpl.java @@ -19,7 +19,6 @@ import tech.ydb.core.Status; import tech.ydb.core.UnexpectedResultException; import tech.ydb.core.impl.Observability; -import tech.ydb.core.metrics.Attr; import tech.ydb.topic.TopicRpc; import tech.ydb.topic.description.CodecRegistry; import tech.ydb.topic.impl.DebugTools; @@ -60,7 +59,7 @@ public WriterImpl(TopicRpc topicRpc, WriteStreamFactory factory, WriterSettings Executor compressionExecutor, @Nonnull CodecRegistry codecRegistry) { this.debugId = DebugTools.createDebugId(settings.getLogPrefix()); this.metrics = new WriterMetrics(settings.getMeter(), settings.getTopicPath(), settings.getWriterName(), - topicRpc.getMetricAttributes().toArray(new Attr[0])); + topicRpc.getEndpoint(), topicRpc.getDatabase()); this.stream = new WriteSession(debugId, factory, settings, topicRpc.getScheduler(), new ListenerImpl()); this.writeQueue = new WriterQueue(debugId, settings, metrics, codecRegistry, compressionExecutor, sendTask); Observability.reportMetricsUsage(settings.getMeter()); diff --git a/topic/src/main/java/tech/ydb/topic/write/impl/WriterMetrics.java b/topic/src/main/java/tech/ydb/topic/write/impl/WriterMetrics.java index 2f251cddf..5144e8ff8 100644 --- a/topic/src/main/java/tech/ydb/topic/write/impl/WriterMetrics.java +++ b/topic/src/main/java/tech/ydb/topic/write/impl/WriterMetrics.java @@ -33,7 +33,11 @@ final class WriterMetrics { private MetricRegistration bufferLimitGauge = MetricRegistration.NOOP; private MetricRegistration oldestAgeGauge = MetricRegistration.NOOP; - WriterMetrics(Meter meter, String topic, String writerName, Attr... attributes) { + WriterMetrics(Meter meter, String topic, String writerName) { + this(meter, topic, writerName, "", ""); + } + + WriterMetrics(Meter meter, String topic, String writerName, String endpoint, String database) { this.meter = meter; this.enabled = meter != Meter.NOOP; this.sendingMessages = meter.createCounter("ydb.topic.writer.sending.messages", MESSAGE_UNIT, @@ -52,11 +56,10 @@ final class WriterMetrics { "The time spent compressing a message, excluding queueing."); this.processingDuration = meter.createHistogram("ydb.topic.writer.message.processing.duration", "s", "The time from buffer acceptance until a message is ready for sending."); - this.commonAttributes = Arrays.copyOf(attributes, attributes.length + (writerName == null ? 0 : 2)); - if (writerName != null) { - commonAttributes[attributes.length] = Attr.of("topic", topic); - commonAttributes[attributes.length + 1] = Attr.of("writer.name", writerName); - } + this.commonAttributes = !enabled ? new Attr[0] : writerName == null + ? new Attr[]{Attr.of("endpoint", endpoint), Attr.of("database", database)} + : new Attr[]{Attr.of("endpoint", endpoint), Attr.of("database", database), + Attr.of("topic", topic), Attr.of("writer.name", writerName)}; } void register(LongSupplier bufferUsed, LongSupplier bufferLimit, LongSupplier oldestTimestamp) {