mjsax commented on code in PR #14620: URL: https://github.com/apache/kafka/pull/14620#discussion_r1396369888
########## clients/src/test/java/org/apache/kafka/common/telemetry/internals/SinglePointMetricTest.java: ########## @@ -0,0 +1,162 @@ +/* + * 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.AnyValue; +import io.opentelemetry.proto.common.v1.KeyValue; +import io.opentelemetry.proto.metrics.v1.AggregationTemporality; +import io.opentelemetry.proto.metrics.v1.Metric; +import io.opentelemetry.proto.metrics.v1.NumberDataPoint; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.time.Instant; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.Collections; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SinglePointMetricTest { + + private MetricKey metricKey; + private Instant now; + + /* + Test compares the metric representation from returned builder to ensure that the metric is + constructed correctly. + + For example: Gauge metric with name "name" and double value 1.0 at certain time is represented as: + + name: "name" + gauge { + data_points { + time_unix_nano: 1698063981021420000 + as_double: 1.0 + } + } + */ + + @BeforeEach + public void setUp() { + metricKey = new MetricKey("name", Collections.emptyMap()); + now = Instant.now(); + } + + @Test + public void testGaugeWithNumberValue() { + SinglePointMetric gaugeNumber = SinglePointMetric.gauge(metricKey, Long.valueOf(1), now); + assertEquals(getIntGaugeMetric("name", 1, now), + gaugeNumber.builder().build()); + } + + @Test + public void testGaugeWithDoubleValue() { + SinglePointMetric gaugeNumber = SinglePointMetric.gauge(metricKey, 1.0, now); + assertEquals(getDoubleGaugeMetric("name", 1.0, now), + gaugeNumber.builder().build()); + } + + @Test + public void testGaugeWithMetricTags() { + MetricKey metricKey = new MetricKey("name", Collections.singletonMap("tag", "value")); + SinglePointMetric gaugeNumber = SinglePointMetric.gauge(metricKey, 1.0, now); + assertEquals(getDoubleGaugeMetric("name", 1.0, now, + Collections.singletonMap("tag", "value")), gaugeNumber.builder().build()); + } + + @Test + public void testSum() { + SinglePointMetric sum = SinglePointMetric.sum(metricKey, 1.0, false, now); + assertEquals(getSumMetric("name", 1.0, false, now), sum.builder().build()); + } + + @Test + public void testSumWithStartTimeAndTags() { + MetricKey metricKey = new MetricKey("name", Collections.singletonMap("tag", "value")); + SinglePointMetric sum = SinglePointMetric.sum(metricKey, 1.0, true, now, now); + assertEquals(getSumMetric("name", 1.0, true, now, now, + Collections.singletonMap("tag", "value"), false), sum.builder().build()); + } + + @Test + public void testDeltaSum() { + SinglePointMetric sum = SinglePointMetric.deltaSum(metricKey, 1.0, true, now, now); + assertEquals(getSumMetric("name", 1.0, true, now, now, Collections.emptyMap(), + true), sum.builder().build()); + } + + private Metric getDoubleGaugeMetric(String name, Double value, Instant time) { + return getDoubleGaugeMetric(name, value, time, Collections.emptyMap()); + } + + private Metric getDoubleGaugeMetric(String name, Double value, Instant time, Map<String, String> tags) { + NumberDataPoint.Builder point = NumberDataPoint.newBuilder() Review Comment: Seems like this code is the same as the actually production code? Is there no simpler way to create an expected result? Or should we change the test condition `assertEquals` to check if the fields of the computed `SinglePointMetric` are set correctly? Atm, the test is like this( hope this illustrates what I mean): ``` // production code int add(int a, int b) { return a + b; } // test void shouldAddNumbers() { int sum = add(5, 6); assertEquals(addNumbers(5,6), sum); // <- this should be assert(11, sum); instead of duplicating production code inside `addNumbers` ? } int addNumbers(int a, int b) { return a + b; } ``` -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
