Skip to content

Commit f26f720

Browse files
llinderAdrian Cole
authored and
Adrian Cole
committed
Initial pass for adding multiple trace lookup
1 parent 35f23f3 commit f26f720

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

zipkin2/src/main/java/zipkin2/storage/InMemoryStorage.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,30 @@ public synchronized Call<List<Span>> getTrace(String traceId) {
307307
return Call.create(filtered);
308308
}
309309

310+
@Override
311+
public synchronized Call<List<List<Span>>> getTraces(List<String> traceIds) {
312+
List<List<Span>> traces = new ArrayList<>();
313+
for (String traceId : traceIds) {
314+
traceId = Span.normalizeTraceId(traceId);
315+
List<Span> spans = spansByTraceId(lowTraceId(traceId));
316+
if (spans == null || spans.isEmpty()) continue;
317+
if (!strictTraceId) {
318+
traces.add(spans);
319+
} else {
320+
List<Span> filtered = new ArrayList<>(spans);
321+
Iterator<Span> iterator = filtered.iterator();
322+
while (iterator.hasNext()) {
323+
if (!iterator.next().traceId().equals(traceId)) {
324+
iterator.remove();
325+
}
326+
}
327+
traces.add(filtered);
328+
}
329+
}
330+
331+
return Call.create(traces);
332+
}
333+
310334
@Override
311335
public synchronized Call<List<String>> getServiceNames() {
312336
if (!searchEnabled) return Call.emptyList();

zipkin2/src/main/java/zipkin2/storage/SpanStore.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ public interface SpanStore {
4949
*/
5050
Call<List<Span>> getTrace(String traceId);
5151

52+
/**
53+
* Retrieves list of spans that share a 128-bit trace id with no ordering expectation or empty if none are
54+
* found.
55+
*
56+
* <p>When strict trace ID is disabled, spans with the same right-most 16 characters are returned
57+
* even if the characters to the left are not.
58+
*
59+
* <p>Implementations should use {@link Span#normalizeTraceId(String)} to ensure consistency.
60+
*
61+
* @param traceIds a list of {@link Span#traceId() trace ID}
62+
*/
63+
Call<List<List<Span>>> getTraces(List<String> traceIds);
64+
5265
/**
5366
* Retrieves all {@link Span#localEndpoint() local} and {@link Span#remoteEndpoint() remote}
5467
* {@link Endpoint#serviceName service names}, sorted lexicographically.

zipkin2/src/test/java/zipkin2/storage/InMemoryStorageTest.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
package zipkin2.storage;
1515

1616
import java.io.IOException;
17-
import java.util.Collections;
18-
import java.util.List;
19-
import java.util.Map;
17+
import java.util.*;
2018
import java.util.stream.Collectors;
2119
import java.util.stream.IntStream;
2220
import org.junit.Test;
@@ -122,4 +120,32 @@ public class InMemoryStorageTest {
122120
"root"
123121
);
124122
}
123+
124+
@Test public void getTraces_byTraceIds() throws IOException {
125+
Span trace1Span1 = Span.newBuilder().traceId("1").id("1").name("root")
126+
.localEndpoint(Endpoint.newBuilder().serviceName("app").build())
127+
.timestamp(TODAY * 1000)
128+
.build();
129+
Span trace1Span2 = Span.newBuilder().traceId("1").parentId("1").id("2")
130+
.localEndpoint(Endpoint.newBuilder().serviceName("app").build())
131+
.timestamp(TODAY * 1000)
132+
.build();
133+
134+
Span trace2Span1 = Span.newBuilder().traceId("2").id("1").name("root")
135+
.localEndpoint(Endpoint.newBuilder().serviceName("app").build())
136+
.timestamp(TODAY * 1000)
137+
.build();
138+
Span trace2Span2 = Span.newBuilder().traceId("2").parentId("1").id("2")
139+
.localEndpoint(Endpoint.newBuilder().serviceName("app").build())
140+
.timestamp(TODAY * 1000)
141+
.build();
142+
143+
storage.accept(asList(trace1Span1, trace1Span2, trace2Span1, trace2Span2));
144+
145+
146+
assertThat(storage.getTraces(asList("1", "2")).execute()).contains(
147+
asList(trace1Span1, trace1Span2),
148+
asList(trace2Span1, trace2Span2)
149+
);
150+
}
125151
}

0 commit comments

Comments
 (0)