Skip to content

Commit 013a907

Browse files
zeagordAdrian Cole
authored and
Adrian Cole
committed
Adds getByTraceIds to elasticsearch
1 parent ce8033a commit 013a907

File tree

5 files changed

+136
-61
lines changed

5 files changed

+136
-61
lines changed

zipkin-storage/elasticsearch/src/main/java/zipkin2/elasticsearch/ElasticsearchSpanStore.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
*/
1414
package zipkin2.elasticsearch;
1515

16+
import java.util.LinkedHashSet;
1617
import java.util.List;
1718
import java.util.Locale;
1819
import java.util.Map;
20+
import java.util.Set;
1921
import zipkin2.Call;
2022
import zipkin2.DependencyLink;
2123
import zipkin2.Span;
@@ -121,6 +123,22 @@ public Call<List<Span>> getTrace(String traceId) {
121123
return search.newCall(request, BodyConverters.SPANS);
122124
}
123125

126+
@Override public Call<List<List<Span>>> getTraces(List<String> traceIds) {
127+
Set<String> normalizedTraceIds = new LinkedHashSet<>();
128+
for (String traceId : traceIds) {
129+
// make sure we have a 16 or 32 character trace ID
130+
traceId = Span.normalizeTraceId(traceId);
131+
132+
// Unless we are strict, truncate the trace ID to 64bit (encoded as 16 characters)
133+
if (!strictTraceId && traceId.length() == 32) traceId = traceId.substring(16);
134+
135+
normalizedTraceIds.add(traceId);
136+
}
137+
SearchRequest request =
138+
SearchRequest.create(asList(allSpanIndices)).terms("traceId", normalizedTraceIds);
139+
return search.newCall(request, BodyConverters.SPANS).map(groupByTraceId);
140+
}
141+
124142
@Override
125143
public Call<List<String>> getServiceNames() {
126144
if (!searchEnabled) return Call.emptyList();

zipkin-storage/elasticsearch/src/main/java/zipkin2/elasticsearch/internal/client/SearchRequest.java

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -63,42 +63,6 @@ public Filters addTerm(String field, String value) {
6363
add(new Term(field, value));
6464
return this;
6565
}
66-
67-
public Filters addNestedTerms(Collection<String> nestedFields, String value) {
68-
add(_nestedTermsEqual(nestedFields, value));
69-
return this;
70-
}
71-
72-
public Filters addNestedTerms(Map<String, String>... nestedTerms) {
73-
if (nestedTerms.length == 1) {
74-
add(mustMatchAllNestedTerms(nestedTerms[0]));
75-
return this;
76-
}
77-
List<NestedBoolQuery> nestedBoolQueries = new ArrayList<>(nestedTerms.length);
78-
for (Map<String, String> next : nestedTerms) {
79-
nestedBoolQueries.add(mustMatchAllNestedTerms(next));
80-
}
81-
add(new SearchRequest.BoolQuery("should", nestedBoolQueries));
82-
return this;
83-
}
84-
85-
static SearchRequest.BoolQuery _nestedTermsEqual(Collection<String> nestedFields, String value) {
86-
List<SearchRequest.NestedBoolQuery> conditions = new ArrayList<>();
87-
for (String nestedField : nestedFields) {
88-
conditions.add(new NestedBoolQuery(nestedField.substring(0, nestedField.indexOf('.')), "must",
89-
new SearchRequest.Term(nestedField, value)));
90-
}
91-
return new SearchRequest.BoolQuery("should", conditions);
92-
}
93-
94-
static NestedBoolQuery mustMatchAllNestedTerms(Map<String, String> next) {
95-
List<Term> terms = new ArrayList<>();
96-
String field = null;
97-
for (Map.Entry<String, String> nestedTerm : next.entrySet()) {
98-
terms.add(new Term(field = nestedTerm.getKey(), nestedTerm.getValue()));
99-
}
100-
return new NestedBoolQuery(field.substring(0, field.indexOf('.')), "must", terms);
101-
}
10266
}
10367

10468
public SearchRequest filters(Filters filters) {
@@ -109,7 +73,7 @@ public SearchRequest term(String field, String value) {
10973
return query(new Term(field, value));
11074
}
11175

112-
public SearchRequest terms(String field, List<String> values) {
76+
public SearchRequest terms(String field, Collection<String> values) {
11377
return query(new Terms(field, values));
11478
}
11579

@@ -138,14 +102,6 @@ static class Term {
138102
}
139103
}
140104

141-
static class Exists {
142-
final Map<String, String> exists;
143-
144-
Exists(String field) {
145-
exists = Collections.singletonMap("field", field);
146-
}
147-
}
148-
149105
static class Terms {
150106
final Map<String, Collection<String>> terms;
151107

@@ -181,20 +137,4 @@ static class BoolQuery {
181137
bool = Collections.singletonMap(op, clause);
182138
}
183139
}
184-
185-
static class NestedBoolQuery {
186-
final Map<String, Object> nested;
187-
188-
NestedBoolQuery(String path, String condition, List<Term> terms) {
189-
nested = new LinkedHashMap<>(2);
190-
nested.put("path", path);
191-
nested.put("query", new BoolQuery(condition, terms));
192-
}
193-
194-
NestedBoolQuery(String path, String condition, Term term) {
195-
nested = new LinkedHashMap<>(2);
196-
nested.put("path", path);
197-
nested.put("query", new BoolQuery(condition, term));
198-
}
199-
}
200140
}

zipkin-storage/elasticsearch/src/test/java/zipkin2/elasticsearch/integration/ITElasticsearchStorageV2.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,43 @@ public static class ITSearchEnabledFalse extends zipkin2.storage.ITSearchEnabled
5252
storage.clear();
5353
}
5454
}
55+
56+
public static class ITStrictTraceIdFalse extends zipkin2.storage.ITStrictTraceIdFalse {
57+
@ClassRule public static ElasticsearchStorageRule backend = classRule();
58+
@Rule public TestName testName = new TestName();
59+
60+
ElasticsearchStorage storage;
61+
62+
@Before public void connect() {
63+
storage = backend.computeStorageBuilder().index(index(testName))
64+
.strictTraceId(false).build();
65+
}
66+
67+
@Override protected StorageComponent storage() {
68+
return storage;
69+
}
70+
71+
@Before @Override public void clear() throws IOException {
72+
storage.clear();
73+
}
74+
}
75+
76+
public static class ITSpanStore extends zipkin2.storage.ITSpanStore {
77+
@ClassRule public static ElasticsearchStorageRule backend = classRule();
78+
@Rule public TestName testName = new TestName();
79+
80+
ElasticsearchStorage storage;
81+
82+
@Before public void connect() {
83+
storage = backend.computeStorageBuilder().index(index(testName)).build();
84+
}
85+
86+
@Override protected StorageComponent storage() {
87+
return storage;
88+
}
89+
90+
@Before @Override public void clear() throws IOException {
91+
storage.clear();
92+
}
93+
}
5594
}

zipkin-storage/elasticsearch/src/test/java/zipkin2/elasticsearch/integration/ITElasticsearchStorageV5.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,43 @@ public static class ITSearchEnabledFalse extends zipkin2.storage.ITSearchEnabled
5252
storage.clear();
5353
}
5454
}
55+
56+
public static class ITStrictTraceIdFalse extends zipkin2.storage.ITStrictTraceIdFalse {
57+
@ClassRule public static ElasticsearchStorageRule backend = classRule();
58+
@Rule public TestName testName = new TestName();
59+
60+
ElasticsearchStorage storage;
61+
62+
@Before public void connect() {
63+
storage = backend.computeStorageBuilder().index(index(testName))
64+
.strictTraceId(false).build();
65+
}
66+
67+
@Override protected StorageComponent storage() {
68+
return storage;
69+
}
70+
71+
@Before @Override public void clear() throws IOException {
72+
storage.clear();
73+
}
74+
}
75+
76+
public static class ITSpanStore extends zipkin2.storage.ITSpanStore {
77+
@ClassRule public static ElasticsearchStorageRule backend = classRule();
78+
@Rule public TestName testName = new TestName();
79+
80+
ElasticsearchStorage storage;
81+
82+
@Before public void connect() {
83+
storage = backend.computeStorageBuilder().index(index(testName)).build();
84+
}
85+
86+
@Override protected StorageComponent storage() {
87+
return storage;
88+
}
89+
90+
@Before @Override public void clear() throws IOException {
91+
storage.clear();
92+
}
93+
}
5594
}

zipkin-storage/elasticsearch/src/test/java/zipkin2/elasticsearch/integration/ITElasticsearchStorageV6.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,43 @@ public static class ITSearchEnabledFalse extends zipkin2.storage.ITSearchEnabled
5252
storage.clear();
5353
}
5454
}
55+
56+
public static class ITStrictTraceIdFalse extends zipkin2.storage.ITStrictTraceIdFalse {
57+
@ClassRule public static ElasticsearchStorageRule backend = classRule();
58+
@Rule public TestName testName = new TestName();
59+
60+
ElasticsearchStorage storage;
61+
62+
@Before public void connect() {
63+
storage = backend.computeStorageBuilder().index(index(testName))
64+
.strictTraceId(false).build();
65+
}
66+
67+
@Override protected StorageComponent storage() {
68+
return storage;
69+
}
70+
71+
@Before @Override public void clear() throws IOException {
72+
storage.clear();
73+
}
74+
}
75+
76+
public static class ITSpanStore extends zipkin2.storage.ITSpanStore {
77+
@ClassRule public static ElasticsearchStorageRule backend = classRule();
78+
@Rule public TestName testName = new TestName();
79+
80+
ElasticsearchStorage storage;
81+
82+
@Before public void connect() {
83+
storage = backend.computeStorageBuilder().index(index(testName)).build();
84+
}
85+
86+
@Override protected StorageComponent storage() {
87+
return storage;
88+
}
89+
90+
@Before @Override public void clear() throws IOException {
91+
storage.clear();
92+
}
93+
}
5594
}

0 commit comments

Comments
 (0)