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_NAME;
021import static org.apache.hadoop.hbase.trace.HBaseSemanticAttributes.TABLE_KEY;
022
023import io.opentelemetry.api.common.AttributeKey;
024import io.opentelemetry.api.trace.Span;
025import io.opentelemetry.api.trace.SpanBuilder;
026import io.opentelemetry.api.trace.SpanKind;
027import java.util.HashMap;
028import java.util.Map;
029import java.util.function.Supplier;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.AsyncConnectionImpl;
032import org.apache.hadoop.hbase.trace.TraceUtil;
033import org.apache.yetus.audience.InterfaceAudience;
034
035/**
036 * Construct {@link Span} instances involving data tables.
037 */
038@InterfaceAudience.Private
039public class TableSpanBuilder implements Supplier<Span> {
040
041  private String name;
042  private final Map<AttributeKey<?>, Object> attributes = new HashMap<>();
043
044  public TableSpanBuilder(AsyncConnectionImpl conn) {
045    ConnectionSpanBuilder.populateConnectionAttributes(attributes, conn);
046  }
047
048  @Override
049  public Span get() {
050    return build();
051  }
052
053  public TableSpanBuilder setName(final String name) {
054    this.name = name;
055    return this;
056  }
057
058  public TableSpanBuilder setTableName(final TableName tableName) {
059    populateTableNameAttributes(attributes, tableName);
060    return this;
061  }
062
063  @SuppressWarnings("unchecked")
064  public Span build() {
065    final SpanBuilder builder =
066      TraceUtil.getGlobalTracer().spanBuilder(name).setSpanKind(SpanKind.INTERNAL);
067    attributes.forEach((k, v) -> builder.setAttribute((AttributeKey<? super Object>) k, v));
068    return builder.startSpan();
069  }
070
071  /**
072   * Static utility method that performs the primary logic of this builder. It is visible to other
073   * classes in this package so that other builders can use this functionality as a mix-in.
074   * @param attributes the attributes map to be populated.
075   * @param tableName  the source of attribute values.
076   */
077  static void populateTableNameAttributes(final Map<AttributeKey<?>, Object> attributes,
078    final TableName tableName) {
079    attributes.put(DB_NAME, tableName.getNamespaceAsString());
080    attributes.put(TABLE_KEY, tableName.getNameAsString());
081  }
082}