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.trace;
019
020import static org.apache.hadoop.hbase.trace.HBaseSemanticAttributes.DB_CONNECTION_STRING;
021import static org.apache.hadoop.hbase.trace.HBaseSemanticAttributes.DB_SYSTEM;
022import static org.apache.hadoop.hbase.trace.HBaseSemanticAttributes.DB_SYSTEM_VALUE;
023import static org.apache.hadoop.hbase.trace.HBaseSemanticAttributes.DB_USER;
024
025import io.opentelemetry.api.common.AttributeKey;
026import io.opentelemetry.api.trace.Span;
027import io.opentelemetry.api.trace.SpanBuilder;
028import io.opentelemetry.api.trace.SpanKind;
029import java.util.HashMap;
030import java.util.Map;
031import java.util.Optional;
032import java.util.function.Supplier;
033import org.apache.hadoop.hbase.client.AsyncConnectionImpl;
034import org.apache.hadoop.hbase.client.ClusterConnection;
035import org.apache.hadoop.hbase.security.User;
036import org.apache.hadoop.hbase.trace.TraceUtil;
037import org.apache.yetus.audience.InterfaceAudience;
038
039/**
040 * Construct {@link Span} instances originating from the client side of a connection.
041 */
042@InterfaceAudience.Private
043public class ConnectionSpanBuilder implements Supplier<Span> {
044
045  private String name;
046  private final Map<AttributeKey<?>, Object> attributes = new HashMap<>();
047
048  public ConnectionSpanBuilder(final AsyncConnectionImpl conn) {
049    populateConnectionAttributes(attributes, conn);
050  }
051
052  @Override
053  public Span get() {
054    return build();
055  }
056
057  public ConnectionSpanBuilder setName(final String name) {
058    this.name = name;
059    return this;
060  }
061
062  public <T> ConnectionSpanBuilder addAttribute(final AttributeKey<T> key, T value) {
063    attributes.put(key, value);
064    return this;
065  }
066
067  @SuppressWarnings("unchecked")
068  public Span build() {
069    final SpanBuilder builder =
070      TraceUtil.getGlobalTracer().spanBuilder(name).setSpanKind(SpanKind.INTERNAL);
071    attributes.forEach((k, v) -> builder.setAttribute((AttributeKey<? super Object>) k, v));
072    return builder.startSpan();
073  }
074
075  /**
076   * @see #populateConnectionAttributes(Map, AsyncConnectionImpl)
077   */
078  static void populateConnectionAttributes(final Map<AttributeKey<?>, Object> attributes,
079    final ClusterConnection conn) {
080    attributes.put(DB_SYSTEM, DB_SYSTEM_VALUE);
081    attributes.put(DB_CONNECTION_STRING, conn.getConnectionRegistry().getConnectionString());
082    attributes.put(DB_USER, Optional.ofNullable(conn.getUser()).map(Object::toString).orElse(null));
083  }
084
085  /**
086   * Static utility method that performs the primary logic of this builder. It is visible to other
087   * classes in this package so that other builders can use this functionality as a mix-in.
088   * @param attributes the attributes map to be populated.
089   * @param conn       the source of connection attribute values.
090   */
091  static void populateConnectionAttributes(final Map<AttributeKey<?>, Object> attributes,
092    final AsyncConnectionImpl conn) {
093    final Supplier<String> connStringSupplier =
094      () -> conn.getConnectionRegistry().getConnectionString();
095    populateConnectionAttributes(attributes, connStringSupplier, conn::getUser);
096  }
097
098  /**
099   * Static utility method that performs the primary logic of this builder. It is visible to other
100   * classes in this package so that other builders can use this functionality as a mix-in.
101   * @param attributes               the attributes map to be populated.
102   * @param connectionStringSupplier the source of the {@code db.connection_string} attribute value.
103   * @param userSupplier             the source of the {@code db.user} attribute value.
104   */
105  static void populateConnectionAttributes(final Map<AttributeKey<?>, Object> attributes,
106    final Supplier<String> connectionStringSupplier, final Supplier<User> userSupplier) {
107    attributes.put(DB_SYSTEM, DB_SYSTEM_VALUE);
108    attributes.put(DB_CONNECTION_STRING, connectionStringSupplier.get());
109    attributes.put(DB_USER,
110      Optional.ofNullable(userSupplier.get()).map(Object::toString).orElse(null));
111  }
112}