Repository: open-telemetry/opentelemetry-java
File: sdk/common/src/main/java/io/opentelemetry/sdk/internal/AttributesMap.java (Java)

Code before:
 * at any time.
 */
public final class AttributesMap implements Attributes {

  private final HashMap<AttributeKey<?>, Object> delegate = new HashMap<>();
  private final long capacity;
  private final int lengthLimit;
  private int totalAddedValues = 0;

  private AttributesMap(long capacity, int lengthLimit) {
    this.capacity = capacity;
    this.lengthLimit = lengthLimit;
  }

  /**
   * Create an instance.
   *
   * @param capacity the max number of attribute entries
   * @param lengthLimit the maximum length of string attributes
   */
  public static AttributesMap create(long capacity, int lengthLimit) {
    return new AttributesMap(capacity, lengthLimit);
  }

  /** Add the attribute key value pair, applying capacity and length limits. */
  public <T> void put(AttributeKey<T> key, T value) {
    totalAddedValues++;
    if (size() >= capacity && !delegate.containsKey(key)) {
      return;
    }
    delegate.put(key, AttributeUtil.applyAttributeLengthLimit(value, lengthLimit));
  }

  /** Get the total number of attributes added, including those dropped for capcity limits. */
  public int getTotalAddedValues() {
    return totalAddedValues;
  }

  @SuppressWarnings("unchecked")
  @Override
  @Nullable
  public <T> T get(AttributeKey<T> key) {
    return (T) delegate.get(key);
  }

  @Override
  public Map<AttributeKey<?>, Object> asMap() {
    // Because Attributes is marked Immutable, IDEs may recognize this as redundant usage. However,
    // this class is private and is actually mutable, so we need to wrap with unmodifiableMap
    // anyways. We implement the immutable Attributes for this class to support the
    // Attributes.builder().putAll usage - it is tricky but an implementation detail of this private

Review comment (suggestion):
Because `AttributeMap extends HashMap`, a caller was accidentally bypassing this method and calling `Map#put(T, V)` directly, which was preventing the application of attribute limits.

We protect against this by delegating to `HashMap` instead of extending. 

Code after:
 * would be more appropriate to delegate. The problem with extending is that we don't enforce that
 * all {@link HashMap} methods for reading / writing data conform to the configured attribute
 * limits. Therefore, it's easy to accidentally call something like {@link Map#putAll(Map)} or
 * {@link Map#put(Object, Object)} and bypass the restrictions (see <a
 * href="https://github.com/open-telemetry/opentelemetry-java/issues/7135">#7135</a>). Callers MUST
 * take care to only call methods from {@link AttributesMap}, and not {@link HashMap}.
 *
 * <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
 * at any time.
 */
public final class AttributesMap extends HashMap<AttributeKey<?>, Object> implements Attributes {

  private static final long serialVersionUID = -5072696312123632376L;

  private final long capacity;
  private final int lengthLimit;
  private int totalAddedValues = 0;

  private AttributesMap(long capacity, int lengthLimit) {
    this.capacity = capacity;
    this.lengthLimit = lengthLimit;
  }

  /**
   * Create an instance.
   *
   * @param capacity the max number of attribute entries
   * @param lengthLimit the maximum length of string attributes
   */
  public static AttributesMap create(long capacity, int lengthLimit) {
    return new AttributesMap(capacity, lengthLimit);
  }

  /**
   * Add the attribute key value pair, applying capacity and length limits. Callers MUST ensure the
   * {@code value} type matches the type required by {@code key}.
   */
  @Override
  @Nullable
  public Object put(AttributeKey<?> key, Object value) {
    totalAddedValues++;
    if (size() >= capacity && !containsKey(key)) {
      return null;
    }
    return super.put(key, AttributeUtil.applyAttributeLengthLimit(value, lengthLimit));
  }

  /** Get the total number of attributes added, including those dropped for capcity limits. */
  public int getTotalAddedValues() {
    return totalAddedValues;
  }