/*
 * 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.hadoop.hbase.client;

import static org.junit.jupiter.api.Assertions.assertEquals;

import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
import io.opentelemetry.sdk.trace.data.SpanData;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.Waiter;
import org.apache.hadoop.hbase.security.UserProvider;
import org.apache.hadoop.hbase.testclassification.ClientTests;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.trace.HBaseSemanticAttributes;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import org.apache.hbase.thirdparty.com.google.common.io.Closeables;

@Tag(ClientTests.TAG)
@Tag(MediumTests.TAG)
public class TestAsyncConnectionTracing {

  private static Configuration CONF = HBaseConfiguration.create();

  private ServerName masterServer =
    ServerName.valueOf("localhost", 12345, System.currentTimeMillis());

  private AsyncConnection conn;

  @RegisterExtension
  public static final OpenTelemetryExtension traceRule = OpenTelemetryExtension.create();

  @BeforeEach
  public void setUp() throws IOException {
    ConnectionRegistry registry = new DoNothingConnectionRegistry(CONF) {

      @Override
      public CompletableFuture<ServerName> getActiveMaster() {
        return CompletableFuture.completedFuture(masterServer);
      }
    };
    conn =
      new AsyncConnectionImpl(CONF, registry, "test", UserProvider.instantiate(CONF).getCurrent());
  }

  @AfterEach
  public void tearDown() throws IOException {
    Closeables.close(conn, true);
  }

  private void assertTrace(String methodName, ServerName serverName) {
    Waiter.waitFor(CONF, 1000,
      () -> traceRule.getSpans().stream()
        .anyMatch(span -> span.getName().equals("AsyncConnection." + methodName)
          && span.getKind() == SpanKind.INTERNAL && span.hasEnded()));
    SpanData data = traceRule.getSpans().stream()
      .filter(s -> s.getName().equals("AsyncConnection." + methodName)).findFirst().get();
    assertEquals(StatusCode.OK, data.getStatus().getStatusCode());
    if (serverName != null) {
      assertEquals(serverName.getServerName(),
        data.getAttributes().get(HBaseSemanticAttributes.SERVER_NAME_KEY));
    }
  }

  @Test
  public void testHbck() {
    conn.getHbck().join();
    assertTrace("getHbck", masterServer);
  }

  @Test
  public void testHbckWithServerName() throws IOException {
    ServerName serverName = ServerName.valueOf("localhost", 23456, System.currentTimeMillis());
    conn.getHbck(serverName);
    assertTrace("getHbck", serverName);
  }

  @Test
  public void testClose() throws IOException {
    conn.close();
    assertTrace("close", null);
  }
}
