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

zipkin2.storage.GroupByTraceId 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.storage;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import zipkin2.Call;
import zipkin2.Span;

import static zipkin2.storage.StrictTraceId.lowerTraceId;

/**
 * A mapper that groups unorganized input spans by trace ID. Useful when preparing a result for
 * {@link SpanStore#getTraces(QueryRequest)}.
 */
public final class GroupByTraceId implements Call.Mapper, List>> {
  public static Call.Mapper, List>> create(boolean strictTraceId) {
    return new GroupByTraceId(strictTraceId);
  }

  final boolean strictTraceId;

  GroupByTraceId(boolean strictTraceId) {
    this.strictTraceId = strictTraceId;
  }

  @SuppressWarnings("MixedMutabilityReturnType")
  @Override public List> map(List input) {
    if (input.isEmpty()) return Collections.emptyList();

    Map> groupedByTraceId = new LinkedHashMap<>();
    for (Span span : input) {
      String traceId = span.traceId();
      if (!strictTraceId) traceId = lowerTraceId(traceId);
      if (!groupedByTraceId.containsKey(traceId)) {
        groupedByTraceId.put(traceId, new ArrayList<>());
      }
      groupedByTraceId.get(traceId).add(span);
    }
    // Modifiable so that StrictTraceId can filter without allocating a new list
    return new ArrayList<>(groupedByTraceId.values());
  }

  @Override public String toString() {
    return "GroupByTraceId{strictTraceId=" + strictTraceId + "}";
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy