apoorvmittal10 commented on code in PR #14724:
URL: https://github.com/apache/kafka/pull/14724#discussion_r1412729783


##########
clients/src/test/java/org/apache/kafka/common/telemetry/internals/ClientTelemetryReporterTest.java:
##########
@@ -0,0 +1,573 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.common.telemetry.internals;
+
+import io.opentelemetry.proto.common.v1.KeyValue;
+
+import org.apache.kafka.clients.CommonClientConfigs;
+import org.apache.kafka.clients.consumer.ConsumerConfig;
+import org.apache.kafka.clients.producer.ProducerConfig;
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.message.GetTelemetrySubscriptionsRequestData;
+import org.apache.kafka.common.message.GetTelemetrySubscriptionsResponseData;
+import org.apache.kafka.common.message.PushTelemetryRequestData;
+import org.apache.kafka.common.message.PushTelemetryResponseData;
+import org.apache.kafka.common.metrics.KafkaMetricsContext;
+import org.apache.kafka.common.metrics.MetricsContext;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.record.CompressionType;
+import org.apache.kafka.common.requests.AbstractRequest;
+import org.apache.kafka.common.requests.GetTelemetrySubscriptionsRequest;
+import org.apache.kafka.common.requests.GetTelemetrySubscriptionsResponse;
+import org.apache.kafka.common.requests.PushTelemetryRequest;
+import org.apache.kafka.common.requests.PushTelemetryResponse;
+import org.apache.kafka.common.telemetry.ClientTelemetryState;
+import org.apache.kafka.common.utils.MockTime;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class ClientTelemetryReporterTest {
+
+    private MockTime time;
+    private ClientTelemetryReporter clientTelemetryReporter;
+    private Map<String, Object> configs;
+    private MetricsContext metricsContext;
+    private Uuid uuid;
+    private ClientTelemetryReporter.ClientTelemetrySubscription subscription;
+
+    @BeforeEach
+    public void setUp() {
+        time = new MockTime();
+        clientTelemetryReporter = new ClientTelemetryReporter(time);
+        configs = new HashMap<>();
+        metricsContext = new KafkaMetricsContext("test");
+        uuid = Uuid.randomUuid();
+        subscription = new 
ClientTelemetryReporter.ClientTelemetrySubscription(uuid, 1234, 20000,
+            Collections.emptyList(), true, null);
+    }
+
+    @Test
+    public void testInitTelemetryReporter() {
+        configs.put(CommonClientConfigs.CLIENT_ID_CONFIG, "test-client");
+        configs.put(CommonClientConfigs.CLIENT_RACK_CONFIG, "rack");
+
+        clientTelemetryReporter.configure(configs);
+        clientTelemetryReporter.contextChange(metricsContext);
+        assertNotNull(clientTelemetryReporter.metricsCollector());
+        assertNotNull(clientTelemetryReporter.telemetryProvider().resource());
+        assertEquals(1, 
clientTelemetryReporter.telemetryProvider().resource().getAttributesCount());
+        assertEquals(
+            ClientTelemetryProvider.CLIENT_RACK, 
clientTelemetryReporter.telemetryProvider().resource().getAttributes(0).getKey());
+        assertEquals("rack", 
clientTelemetryReporter.telemetryProvider().resource().getAttributes(0).getValue().getStringValue());
+    }
+
+    @Test
+    public void testInitTelemetryReporterNoCollector() {
+        // Remove namespace config which skips the collector initialization.
+        MetricsContext metricsContext = Collections::emptyMap;
+
+        clientTelemetryReporter.configure(configs);
+        clientTelemetryReporter.contextChange(metricsContext);
+        assertNull(clientTelemetryReporter.metricsCollector());
+    }
+
+    @Test
+    public void testProducerLabels() {
+        configs.put(CommonClientConfigs.CLIENT_ID_CONFIG, "test-client");
+        configs.put(ConsumerConfig.GROUP_ID_CONFIG, "group-id");
+        configs.put(ConsumerConfig.GROUP_INSTANCE_ID_CONFIG, 
"group-instance-id");
+        configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "transaction-id");
+        configs.put(CommonClientConfigs.CLIENT_RACK_CONFIG, "rack");
+
+        clientTelemetryReporter.configure(configs);
+        clientTelemetryReporter.contextChange(new 
KafkaMetricsContext("kafka.producer"));
+        assertNotNull(clientTelemetryReporter.metricsCollector());
+        assertNotNull(clientTelemetryReporter.telemetryProvider().resource());
+
+        List<KeyValue> attributes = 
clientTelemetryReporter.telemetryProvider().resource().getAttributesList();
+        assertEquals(2, attributes.size());
+        attributes.forEach(attribute -> {
+            if 
(attribute.getKey().equals(ClientTelemetryProvider.CLIENT_RACK)) {
+                assertEquals("rack", attribute.getValue().getStringValue());
+            } else if 
(attribute.getKey().equals(ClientTelemetryProvider.TRANSACTIONAL_ID)) {
+                assertEquals("transaction-id", 
attribute.getValue().getStringValue());
+            }
+        });
+    }
+
+    @Test
+    public void testConsumerLabels() {
+        configs.put(CommonClientConfigs.CLIENT_ID_CONFIG, "test-client");
+        configs.put(ConsumerConfig.GROUP_ID_CONFIG, "group-id");
+        configs.put(ConsumerConfig.GROUP_INSTANCE_ID_CONFIG, 
"group-instance-id");
+        configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "transaction-id");
+        configs.put(CommonClientConfigs.CLIENT_RACK_CONFIG, "rack");
+
+        clientTelemetryReporter.configure(configs);
+        clientTelemetryReporter.contextChange(new 
KafkaMetricsContext("kafka.consumer"));
+        assertNotNull(clientTelemetryReporter.metricsCollector());
+        assertNotNull(clientTelemetryReporter.telemetryProvider().resource());
+
+        List<KeyValue> attributes = 
clientTelemetryReporter.telemetryProvider().resource().getAttributesList();
+        assertEquals(3, attributes.size());
+        attributes.forEach(attribute -> {
+            if 
(attribute.getKey().equals(ClientTelemetryProvider.CLIENT_RACK)) {
+                assertEquals("rack", attribute.getValue().getStringValue());
+            } else if 
(attribute.getKey().equals(ClientTelemetryProvider.GROUP_ID)) {
+                assertEquals("group-id", 
attribute.getValue().getStringValue());
+            } else if 
(attribute.getKey().equals(ClientTelemetryProvider.GROUP_INSTANCE_ID)) {
+                assertEquals("group-instance-id", 
attribute.getValue().getStringValue());
+            }
+        });
+    }
+
+    @Test
+    public void testTelemetryReporterClose() {
+        clientTelemetryReporter.close();
+        assertEquals(ClientTelemetryState.TERMINATED, 
((ClientTelemetryReporter.DefaultClientTelemetrySender) clientTelemetryReporter
+            .telemetrySender()).state());
+    }
+
+    @Test
+    public void testTelemetryReporterCloseMultipleTimesNoException() {
+        clientTelemetryReporter.close();
+        clientTelemetryReporter.close();
+        assertEquals(ClientTelemetryState.TERMINATED, 
((ClientTelemetryReporter.DefaultClientTelemetrySender) clientTelemetryReporter
+            .telemetrySender()).state());
+    }
+
+    @Test
+    public void testTelemetrySenderTimeToNextUpdate() {
+        ClientTelemetryReporter.DefaultClientTelemetrySender telemetrySender = 
(ClientTelemetryReporter.DefaultClientTelemetrySender) 
clientTelemetryReporter.telemetrySender();
+
+        assertEquals(ClientTelemetryState.SUBSCRIPTION_NEEDED, 
telemetrySender.state());
+        assertEquals(0, telemetrySender.timeToNextUpdate(100));
+
+        telemetrySender.updateSubscriptionResult(subscription, 
time.milliseconds());
+        assertEquals(20000, telemetrySender.timeToNextUpdate(100), 200);
+
+        
assertTrue(telemetrySender.maybeSetState(ClientTelemetryState.SUBSCRIPTION_IN_PROGRESS));
+        assertEquals(100, telemetrySender.timeToNextUpdate(100));
+
+        
assertTrue(telemetrySender.maybeSetState(ClientTelemetryState.PUSH_NEEDED));
+        long time = telemetrySender.timeToNextUpdate(100);
+        assertTrue(time > 0 && time >= 0.5 * time && time <= 1.5 * time);
+
+        
assertTrue(telemetrySender.maybeSetState(ClientTelemetryState.PUSH_IN_PROGRESS));
+        assertEquals(100, telemetrySender.timeToNextUpdate(100));
+
+        
assertTrue(telemetrySender.maybeSetState(ClientTelemetryState.TERMINATING_PUSH_NEEDED));
+        assertEquals(0, telemetrySender.timeToNextUpdate(100));
+
+        
assertTrue(telemetrySender.maybeSetState(ClientTelemetryState.TERMINATING_PUSH_IN_PROGRESS));
+        assertEquals(Long.MAX_VALUE, telemetrySender.timeToNextUpdate(100));
+
+        
assertTrue(telemetrySender.maybeSetState(ClientTelemetryState.TERMINATED));
+        assertThrows(IllegalStateException.class, () -> 
telemetrySender.timeToNextUpdate(100));
+    }
+
+    @Test
+    public void testCreateRequestSubscriptionNeeded() {
+        ClientTelemetryReporter.DefaultClientTelemetrySender telemetrySender = 
(ClientTelemetryReporter.DefaultClientTelemetrySender) 
clientTelemetryReporter.telemetrySender();
+        assertEquals(ClientTelemetryState.SUBSCRIPTION_NEEDED, 
telemetrySender.state());
+
+        Optional<AbstractRequest.Builder<?>> requestOptional = 
telemetrySender.createRequest();
+        assertNotNull(requestOptional);
+        assertTrue(requestOptional.isPresent());
+        assertTrue(requestOptional.get().build() instanceof 
GetTelemetrySubscriptionsRequest);
+        GetTelemetrySubscriptionsRequest request = 
(GetTelemetrySubscriptionsRequest) requestOptional.get().build();
+
+        GetTelemetrySubscriptionsRequest expectedResult = new 
GetTelemetrySubscriptionsRequest.Builder(

Review Comment:
   Filed: https://issues.apache.org/jira/browse/KAFKA-15958



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to