|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.broker.stats; |
| 20 | + |
| 21 | +import static org.apache.pulsar.broker.stats.BrokerOpenTelemetryTestUtil.assertMetricLongSumValue; |
| 22 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | +import lombok.Cleanup; |
| 25 | +import org.apache.pulsar.broker.BrokerTestUtil; |
| 26 | +import org.apache.pulsar.broker.service.BrokerTestBase; |
| 27 | +import org.apache.pulsar.broker.testcontext.PulsarTestContext; |
| 28 | +import org.apache.pulsar.client.api.PulsarClient; |
| 29 | +import org.apache.pulsar.client.api.PulsarClientException; |
| 30 | +import org.apache.pulsar.opentelemetry.OpenTelemetryAttributes; |
| 31 | +import org.apache.pulsar.opentelemetry.OpenTelemetryAttributes.ConnectionCreateStatus; |
| 32 | +import org.testng.annotations.AfterMethod; |
| 33 | +import org.testng.annotations.BeforeMethod; |
| 34 | +import org.testng.annotations.Test; |
| 35 | + |
| 36 | +public class OpenTelemetryBrokerOperabilityStatsTest extends BrokerTestBase { |
| 37 | + |
| 38 | + @BeforeMethod(alwaysRun = true) |
| 39 | + @Override |
| 40 | + protected void setup() throws Exception { |
| 41 | + super.baseSetup(); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + protected void customizeMainPulsarTestContextBuilder(PulsarTestContext.Builder pulsarTestContextBuilder) { |
| 46 | + super.customizeMainPulsarTestContextBuilder(pulsarTestContextBuilder); |
| 47 | + pulsarTestContextBuilder.enableOpenTelemetry(true); |
| 48 | + } |
| 49 | + |
| 50 | + @AfterMethod(alwaysRun = true) |
| 51 | + @Override |
| 52 | + protected void cleanup() throws Exception { |
| 53 | + super.internalCleanup(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testBrokerConnection() throws Exception { |
| 58 | + var topicName = BrokerTestUtil.newUniqueName("persistent://my-namespace/use/my-ns/testBrokerConnection"); |
| 59 | + |
| 60 | + @Cleanup |
| 61 | + var producer = pulsarClient.newProducer().topic(topicName).create(); |
| 62 | + |
| 63 | + var metrics = pulsarTestContext.getOpenTelemetryMetricReader().collectAllMetrics(); |
| 64 | + assertMetricLongSumValue(metrics, BrokerOperabilityMetrics.CONNECTION_COUNTER_METRIC_NAME, |
| 65 | + OpenTelemetryAttributes.ConnectionStatus.OPEN.attributes, 1); |
| 66 | + assertMetricLongSumValue(metrics, BrokerOperabilityMetrics.CONNECTION_COUNTER_METRIC_NAME, |
| 67 | + OpenTelemetryAttributes.ConnectionStatus.CLOSE.attributes, 0); |
| 68 | + assertMetricLongSumValue(metrics, BrokerOperabilityMetrics.CONNECTION_COUNTER_METRIC_NAME, |
| 69 | + OpenTelemetryAttributes.ConnectionStatus.ACTIVE.attributes, 1); |
| 70 | + |
| 71 | + assertMetricLongSumValue(metrics, BrokerOperabilityMetrics.CONNECTION_CREATE_COUNTER_METRIC_NAME, |
| 72 | + ConnectionCreateStatus.SUCCESS.attributes, 1); |
| 73 | + assertMetricLongSumValue(metrics, BrokerOperabilityMetrics.CONNECTION_CREATE_COUNTER_METRIC_NAME, |
| 74 | + ConnectionCreateStatus.FAILURE.attributes, 0); |
| 75 | + |
| 76 | + pulsarClient.close(); |
| 77 | + |
| 78 | + metrics = pulsarTestContext.getOpenTelemetryMetricReader().collectAllMetrics(); |
| 79 | + assertMetricLongSumValue(metrics, BrokerOperabilityMetrics.CONNECTION_COUNTER_METRIC_NAME, |
| 80 | + OpenTelemetryAttributes.ConnectionStatus.CLOSE.attributes, 1); |
| 81 | + |
| 82 | + pulsar.getConfiguration().setAuthenticationEnabled(true); |
| 83 | + |
| 84 | + replacePulsarClient(PulsarClient.builder() |
| 85 | + .serviceUrl(lookupUrl.toString()) |
| 86 | + .operationTimeout(1, TimeUnit.MILLISECONDS)); |
| 87 | + assertThatThrownBy(() -> pulsarClient.newProducer().topic(topicName).create()) |
| 88 | + .isInstanceOf(PulsarClientException.AuthenticationException.class); |
| 89 | + pulsarClient.close(); |
| 90 | + |
| 91 | + metrics = pulsarTestContext.getOpenTelemetryMetricReader().collectAllMetrics(); |
| 92 | + assertMetricLongSumValue(metrics, BrokerOperabilityMetrics.CONNECTION_COUNTER_METRIC_NAME, |
| 93 | + OpenTelemetryAttributes.ConnectionStatus.OPEN.attributes, 2); |
| 94 | + assertMetricLongSumValue(metrics, BrokerOperabilityMetrics.CONNECTION_COUNTER_METRIC_NAME, |
| 95 | + OpenTelemetryAttributes.ConnectionStatus.CLOSE.attributes, 2); |
| 96 | + assertMetricLongSumValue(metrics, BrokerOperabilityMetrics.CONNECTION_COUNTER_METRIC_NAME, |
| 97 | + OpenTelemetryAttributes.ConnectionStatus.ACTIVE.attributes, 0); |
| 98 | + |
| 99 | + assertMetricLongSumValue(metrics, BrokerOperabilityMetrics.CONNECTION_CREATE_COUNTER_METRIC_NAME, |
| 100 | + ConnectionCreateStatus.SUCCESS.attributes, 1); |
| 101 | + assertMetricLongSumValue(metrics, BrokerOperabilityMetrics.CONNECTION_CREATE_COUNTER_METRIC_NAME, |
| 102 | + ConnectionCreateStatus.FAILURE.attributes, 1); |
| 103 | + } |
| 104 | +} |
0 commit comments