opentelemetry-java-instrumentation icon indicating copy to clipboard operation
opentelemetry-java-instrumentation copied to clipboard

Remove Attributes combination from onEnd

Open wgy035 opened this issue 1 year ago • 4 comments

Hopefully resolves https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/11887

Remove reduntant attributes combination in OperationListener.onEnd, move all attributes extract to AttributesExtractor.onEnd

wgy035 avatar Aug 07 '24 08:08 wgy035

Also see https://github.com/open-telemetry/opentelemetry-java-instrumentation/pull/11092

laurit avatar Aug 07 '24 12:08 laurit

Did you consider using something like

public final class CompositeAttributes implements Attributes {
  private final Attributes first;
  private final Attributes second;

  private CompositeAttributes(Attributes first, Attributes second) {
    this.first = first;
    this.second = second;
  }

  public static Attributes of(Attributes first, Attributes second) {
    return new CompositeAttributes(first, second);
  }

  @Nullable
  @Override
  public <T> T get(AttributeKey<T> attributeKey) {
    T value = first.get(attributeKey);
    if (value != null) {
      return value;
    }
    return second.get(attributeKey);
  }

  @Override
  public void forEach(BiConsumer<? super AttributeKey<?>, ? super Object> biConsumer) {
    first.forEach(biConsumer);
    second.forEach(biConsumer);
  }

  @Override
  public int size() {
    return first.size() + second.size();
  }

  @Override
  public boolean isEmpty() {
    return first.isEmpty() && second.isEmpty();
  }

  @Override
  public Map<AttributeKey<?>, Object> asMap() {
    Map<AttributeKey<?>, Object> map = new HashMap<>();
    map.putAll(first.asMap());
    map.putAll(second.asMap());
    return map;
  }

  @Override
  public AttributesBuilder toBuilder() {
    return first.toBuilder().putAll(second);
  }
}

laurit avatar Aug 08 '24 08:08 laurit

@wgy035 have you had a chance to check whether composing the start and end attributes as described above would help?

laurit avatar Aug 16 '24 06:08 laurit

@wgy035 have you had a chance to check whether composing the start and end attributes as described above would help?

@laurit yep, using such a composite structure will reduce traverse overhead and approximately a CompositeAttributesBuilder is also needed, now I'm testing the performance improvement and the code will be updated with a new commit.

wgy035 avatar Aug 17 '24 09:08 wgy035

This PR has been labeled as stale due to lack of activity. It will be automatically closed if there is no further activity over the next 14 days.

github-actions[bot] avatar Sep 29 '25 22:09 github-actions[bot]