All Downloads are FREE. Search and download functionalities are using the official Maven repository.

zipkin2.internal.TracesAdapter Maven / Gradle / Ivy

There is a newer version: 3.4.2
Show newest version
/*
 * Copyright The OpenZipkin Authors
 * SPDX-License-Identifier: Apache-2.0
 */
package zipkin2.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import zipkin2.Call;
import zipkin2.Span;
import zipkin2.storage.SpanStore;
import zipkin2.storage.Traces;

public final class TracesAdapter implements Traces {
  final SpanStore delegate;

  public TracesAdapter(SpanStore spanStore) {
    this.delegate = spanStore;
  }

  @Override public Call> getTrace(String traceId) {
    return delegate.getTrace(traceId);
  }

  @Override public Call>> getTraces(Iterable traceIds) {
    if (traceIds == null) throw new NullPointerException("traceIds == null");

    List>> calls = new ArrayList<>();
    for (String traceId : traceIds) {
      calls.add(getTrace(Span.normalizeTraceId(traceId)));
    }

    if (calls.isEmpty()) return Call.emptyList();
    if (calls.size() == 1) return calls.get(0).map(ToListOfTraces.INSTANCE);
    return new ScatterGather(calls);
  }

  enum ToListOfTraces implements Call.Mapper, List>> {
    INSTANCE;

    @Override public List> map(List input) {
      return input.isEmpty() ? Collections.>emptyList()
        : Collections.singletonList(input);
    }

    @Override public String toString() {
      return "ToListOfTraces()";
    }
  }

  static final class ScatterGather extends AggregateCall, List>> {
    ScatterGather(List>> calls) {
      super(calls);
    }

    @Override protected List> newOutput() {
      return new ArrayList<>();
    }

    @Override protected void append(List input, List> output) {
      if (!input.isEmpty()) output.add(input);
    }

    @Override public ScatterGather clone() {
      return new ScatterGather(cloneCalls());
    }
  }

  @Override public String toString() {
    return "TracesAdapter{" + delegate + "}";
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy