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