001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import io.opentelemetry.api.trace.SpanKind;
023import io.opentelemetry.api.trace.StatusCode;
024import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
025import io.opentelemetry.sdk.trace.data.SpanData;
026import java.io.IOException;
027import java.util.concurrent.CompletableFuture;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.hbase.HBaseConfiguration;
030import org.apache.hadoop.hbase.ServerName;
031import org.apache.hadoop.hbase.Waiter;
032import org.apache.hadoop.hbase.security.User;
033import org.apache.hadoop.hbase.security.UserProvider;
034import org.apache.hadoop.hbase.testclassification.ClientTests;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.trace.HBaseSemanticAttributes;
037import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
038import org.junit.jupiter.api.AfterEach;
039import org.junit.jupiter.api.BeforeEach;
040import org.junit.jupiter.api.Tag;
041import org.junit.jupiter.api.Test;
042import org.junit.jupiter.api.extension.RegisterExtension;
043
044import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
045
046@Tag(ClientTests.TAG)
047@Tag(MediumTests.TAG)
048public class TestAsyncConnectionTracing {
049
050  private static Configuration CONF = HBaseConfiguration.create();
051
052  private ServerName masterServer =
053    ServerName.valueOf("localhost", 12345, EnvironmentEdgeManager.currentTime());
054
055  private AsyncConnection conn;
056
057  @RegisterExtension
058  public static final OpenTelemetryExtension traceRule = OpenTelemetryExtension.create();
059
060  @BeforeEach
061  public void setUp() throws IOException {
062    User user = UserProvider.instantiate(CONF).getCurrent();
063    ConnectionRegistry registry = new DoNothingConnectionRegistry(CONF, user) {
064
065      @Override
066      public CompletableFuture<ServerName> getActiveMaster() {
067        return CompletableFuture.completedFuture(masterServer);
068      }
069    };
070    conn = new AsyncConnectionImpl(CONF, registry, "test", null, user);
071  }
072
073  @AfterEach
074  public void tearDown() throws IOException {
075    Closeables.close(conn, true);
076  }
077
078  private void assertTrace(String methodName, ServerName serverName) {
079    Waiter.waitFor(CONF, 1000,
080      () -> traceRule.getSpans().stream()
081        .anyMatch(span -> span.getName().equals("AsyncConnection." + methodName)
082          && span.getKind() == SpanKind.INTERNAL && span.hasEnded()));
083    SpanData data = traceRule.getSpans().stream()
084      .filter(s -> s.getName().equals("AsyncConnection." + methodName)).findFirst().get();
085    assertEquals(StatusCode.OK, data.getStatus().getStatusCode());
086    if (serverName != null) {
087      assertEquals(serverName.getServerName(),
088        data.getAttributes().get(HBaseSemanticAttributes.SERVER_NAME_KEY));
089    }
090  }
091
092  @Test
093  public void testHbck() {
094    conn.getHbck().join();
095    assertTrace("getHbck", masterServer);
096  }
097
098  @Test
099  public void testHbckWithServerName() throws IOException {
100    ServerName serverName =
101      ServerName.valueOf("localhost", 23456, EnvironmentEdgeManager.currentTime());
102    conn.getHbck(serverName);
103    assertTrace("getHbck", serverName);
104  }
105
106  @Test
107  public void testClose() throws IOException {
108    conn.close();
109    assertTrace("close", null);
110  }
111}