merlimat commented on code in PR #22179:
URL: https://github.com/apache/pulsar/pull/22179#discussion_r1517202529


##########
pulsar-broker/src/test/java/org/apache/pulsar/client/metrics/ClientMetricsTest.java:
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.pulsar.client.metrics;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+import io.opentelemetry.api.OpenTelemetry;
+import io.opentelemetry.api.common.Attributes;
+import io.opentelemetry.sdk.OpenTelemetrySdk;
+import io.opentelemetry.sdk.metrics.SdkMeterProvider;
+import io.opentelemetry.sdk.metrics.data.MetricData;
+import io.opentelemetry.sdk.metrics.data.MetricDataType;
+import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.concurrent.TimeUnit;
+import lombok.Cleanup;
+import org.apache.pulsar.client.api.Consumer;
+import org.apache.pulsar.client.api.Message;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.ProducerConsumerBase;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.SubscriptionType;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+@Test(groups = "broker-api")
+public class ClientMetricsTest extends ProducerConsumerBase {
+
+    InMemoryMetricReader reader;
+    OpenTelemetry otel;
+
+    @BeforeMethod
+    @Override
+    protected void setup() throws Exception {
+        super.internalSetup();
+        super.producerBaseSetup();
+
+        this.reader = InMemoryMetricReader.create();
+        SdkMeterProvider sdkMeterProvider = SdkMeterProvider.builder()
+                .registerMetricReader(reader)
+                .build();
+        this.otel = 
OpenTelemetrySdk.builder().setMeterProvider(sdkMeterProvider).build();
+    }
+
+    @AfterMethod(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    private Map<String, MetricData> collectMetrics() {
+        Map<String, MetricData> metrics = new TreeMap<>();
+        for (MetricData md : reader.collectAllMetrics()) {
+            metrics.put(md.getName(), md);
+        }
+        return metrics;
+    }
+
+    private void assertCounterValue(Map<String, MetricData> metrics, String 
name, long expectedValue,
+                                    Attributes expectedAttributes) {
+        assertEquals(getCounterValue(metrics, name, expectedAttributes), 
expectedValue);
+    }
+
+    private long getCounterValue(Map<String, MetricData> metrics, String name,
+                                    Attributes expectedAttributes) {
+        MetricData md = metrics.get(name);
+        assertNotNull(md, "metric not found: " + name);
+        assertEquals(md.getType(), MetricDataType.LONG_SUM);
+
+        for (var ex : md.getLongSumData().getPoints()) {
+            if (ex.getAttributes().equals(expectedAttributes)) {
+                return ex.getValue();
+            }
+        }
+
+        fail("metric attributes not found: " + expectedAttributes);
+        return -1;
+    }
+
+    private void assertHistoCountValue(Map<String, MetricData> metrics, String 
name, long expectedCount,
+                                       Attributes expectedAttributes) {
+        assertEquals(getHistoCountValue(metrics, name, expectedAttributes), 
expectedCount);
+    }
+
+    private long getHistoCountValue(Map<String, MetricData> metrics, String 
name,
+                                    Attributes expectedAttributes) {
+        MetricData md = metrics.get(name);
+        assertNotNull(md, "metric not found: " + name);
+        assertEquals(md.getType(), MetricDataType.HISTOGRAM);
+
+        for (var ex : md.getHistogramData().getPoints()) {
+            if (ex.getAttributes().equals(expectedAttributes)) {
+                return ex.getCount();
+            }
+        }
+
+        fail("metric attributes not found: " + expectedAttributes);
+        return -1;
+    }
+
+    @Test
+    public void testProducerMetrics() throws Exception {
+        String topic = newTopicName();
+
+        PulsarClient client = PulsarClient.builder()
+                .serviceUrl(pulsar.getBrokerServiceUrl())
+                .openTelemetry(otel)
+                .build();
+
+        Producer<String> producer = client.newProducer(Schema.STRING)
+                .topic(topic)
+                .create();
+
+        for (int i = 0; i < 5; i++) {
+            producer.send("Hello");
+        }
+
+        Attributes nsAttrs = Attributes.builder()
+                .put("pulsar.tenant", "my-property")
+                .put("pulsar.namespace", "my-property/my-ns")
+                .build();
+        Attributes nsAttrsSuccess = nsAttrs.toBuilder()
+                .put("success", true)
+                .build();
+
+        var metrics = collectMetrics();
+        System.err.println("All metrics: " + metrics.keySet());
+
+        assertCounterValue(metrics, "pulsar.client.connections.opened", 1, 
Attributes.empty());
+        assertCounterValue(metrics, 
"pulsar.client.producer.message.pending.count", 0, nsAttrs);

Review Comment:
   The metric is collected as a `LONG_SUM` type which is the same as a counter, 
so the same validation works for both.



-- 
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: commits-unsubscr...@pulsar.apache.org

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

Reply via email to