15
15
16
16
import java .util .Collections ;
17
17
import java .util .Iterator ;
18
- import java .util .LinkedHashMap ;
19
18
import java .util .List ;
20
19
import java .util .Locale ;
21
20
import java .util .Map ;
38
37
39
38
final class ElasticsearchHttpSpanStore implements AsyncSpanStore {
40
39
41
- static final String SPAN = "span" ;
42
40
static final String DEPENDENCY_LINK = "dependencylink" ;
43
- static final String SERVICE_SPAN = "servicespan " ;
41
+ static final String SPAN2 = "span2 " ;
44
42
45
43
final SearchCallFactory search ;
46
44
final String [] allIndices ;
@@ -63,39 +61,21 @@ final class ElasticsearchHttpSpanStore implements AsyncSpanStore {
63
61
SearchRequest .Filters filters = new SearchRequest .Filters ();
64
62
filters .addRange ("timestamp_millis" , beginMillis , endMillis );
65
63
if (request .serviceName != null ) {
66
- filters .addNestedTerms (asList (
67
- "annotations.endpoint.serviceName" ,
68
- "binaryAnnotations.endpoint.serviceName"
69
- ), request .serviceName );
64
+ filters .addTerm ("localEndpoint.serviceName" , request .serviceName );
70
65
}
71
66
72
67
if (request .spanName != null ) {
73
68
filters .addTerm ("name" , request .spanName );
74
69
}
75
70
76
71
for (String annotation : request .annotations ) {
77
- Map <String , String > annotationValues = new LinkedHashMap <>();
78
- annotationValues .put ("annotations.value" , annotation );
79
- Map <String , String > binaryAnnotationKeys = new LinkedHashMap <>();
80
- binaryAnnotationKeys .put ("binaryAnnotations.key" , annotation );
81
- if (request .serviceName != null ) {
82
- annotationValues .put ("annotations.endpoint.serviceName" , request .serviceName );
83
- binaryAnnotationKeys .put ("binaryAnnotations.endpoint.serviceName" , request .serviceName );
84
- }
85
- filters .addNestedTerms (annotationValues , binaryAnnotationKeys );
72
+ filters .should ()
73
+ .addTerm ("annotations.value" , annotation )
74
+ .addExists ("tags." + annotation );
86
75
}
87
76
88
77
for (Map .Entry <String , String > kv : request .binaryAnnotations .entrySet ()) {
89
- // In our index template, we make sure the binaryAnnotation value is indexed as string,
90
- // meaning non-string values won't even be indexed at all. This means that we can only
91
- // match string values here, which happens to be exactly what we want.
92
- Map <String , String > nestedTerms = new LinkedHashMap <>();
93
- nestedTerms .put ("binaryAnnotations.key" , kv .getKey ());
94
- nestedTerms .put ("binaryAnnotations.value" , kv .getValue ());
95
- if (request .serviceName != null ) {
96
- nestedTerms .put ("binaryAnnotations.endpoint.serviceName" , request .serviceName );
97
- }
98
- filters .addNestedTerms (nestedTerms );
78
+ filters .addTerm ("tags." + kv .getKey (), kv .getValue ());
99
79
}
100
80
101
81
if (request .minDuration != null ) {
@@ -114,7 +94,7 @@ final class ElasticsearchHttpSpanStore implements AsyncSpanStore {
114
94
.orderBy ("timestamp_millis" , "desc" );
115
95
116
96
List <String > indices = indexNameFormatter .indexNamePatternsForRange (beginMillis , endMillis );
117
- SearchRequest esRequest = SearchRequest .forIndicesAndType (indices , SPAN )
97
+ SearchRequest esRequest = SearchRequest .forIndicesAndType (indices , SPAN2 )
118
98
.filters (filters ).addAggregation (traceIdTimestamp );
119
99
120
100
HttpCall <List <String >> traceIdsCall = search .newCall (esRequest , BodyConverters .SORTED_KEYS );
@@ -146,7 +126,7 @@ final class ElasticsearchHttpSpanStore implements AsyncSpanStore {
146
126
callback .onSuccess (Collections .emptyList ());
147
127
return ;
148
128
}
149
- SearchRequest request = SearchRequest .forIndicesAndType (indices , SPAN )
129
+ SearchRequest request = SearchRequest .forIndicesAndType (indices , SPAN2 )
150
130
.terms ("traceId" , traceIds );
151
131
search .newCall (request , BodyConverters .SPANS ).submit (successCallback );
152
132
}
@@ -182,7 +162,7 @@ final class ElasticsearchHttpSpanStore implements AsyncSpanStore {
182
162
public void getRawTrace (long traceIdHigh , long traceIdLow , Callback <List <Span >> callback ) {
183
163
String traceIdHex = Util .toLowerHex (strictTraceId ? traceIdHigh : 0L , traceIdLow );
184
164
185
- SearchRequest request = SearchRequest .forIndicesAndType (asList (allIndices ), SPAN )
165
+ SearchRequest request = SearchRequest .forIndicesAndType (asList (allIndices ), SPAN2 )
186
166
.term ("traceId" , traceIdHex );
187
167
188
168
search .newCall (request , BodyConverters .NULLABLE_SPANS ).submit (callback );
@@ -193,28 +173,15 @@ public void getRawTrace(long traceIdHigh, long traceIdLow, Callback<List<Span>>
193
173
long beginMillis = endMillis - namesLookback ;
194
174
195
175
List <String > indices = indexNameFormatter .indexNamePatternsForRange (beginMillis , endMillis );
196
- SearchRequest request = SearchRequest .forIndicesAndType (indices , SERVICE_SPAN )
197
- .addAggregation (Aggregation .terms ("serviceName" , Integer .MAX_VALUE ));
198
-
199
- search .newCall (request , BodyConverters .SORTED_KEYS ).submit (new Callback <List <String >>() {
200
- @ Override public void onSuccess (List <String > value ) {
201
- if (!value .isEmpty ()) callback .onSuccess (value );
202
-
203
- // Special cased code until sites update their collectors. What this does is do a more
204
- // expensive nested query to get service names when the servicespan type returns nothing.
205
- SearchRequest .Filters filters = new SearchRequest .Filters ();
206
- filters .addRange ("timestamp_millis" , beginMillis , endMillis );
207
- SearchRequest request = SearchRequest .forIndicesAndType (indices , SPAN )
208
- .filters (filters )
209
- .addAggregation (Aggregation .nestedTerms ("annotations.endpoint.serviceName" ))
210
- .addAggregation (Aggregation .nestedTerms ("binaryAnnotations.endpoint.serviceName" ));
211
- search .newCall (request , BodyConverters .SORTED_KEYS ).submit (callback );
212
- }
213
-
214
- @ Override public void onError (Throwable t ) {
215
- callback .onError (t );
216
- }
217
- });
176
+ // Service name queries include both local and remote endpoints. This is different than
177
+ // Span name, as a span name can only be on a local endpoint.
178
+ SearchRequest .Filters filters = new SearchRequest .Filters ();
179
+ filters .addRange ("timestamp_millis" , beginMillis , endMillis );
180
+ SearchRequest request = SearchRequest .forIndicesAndType (indices , SPAN2 )
181
+ .filters (filters )
182
+ .addAggregation (Aggregation .terms ("localEndpoint.serviceName" , Integer .MAX_VALUE ))
183
+ .addAggregation (Aggregation .terms ("remoteEndpoint.serviceName" , Integer .MAX_VALUE ));
184
+ search .newCall (request , BodyConverters .SORTED_KEYS ).submit (callback );
218
185
}
219
186
220
187
@ Override public void getSpanNames (String serviceName , Callback <List <String >> callback ) {
@@ -228,32 +195,15 @@ public void getRawTrace(long traceIdHigh, long traceIdLow, Callback<List<Span>>
228
195
229
196
List <String > indices = indexNameFormatter .indexNamePatternsForRange (beginMillis , endMillis );
230
197
231
- SearchRequest request = SearchRequest .forIndicesAndType (indices , SERVICE_SPAN )
232
- .term ("serviceName" , serviceName .toLowerCase (Locale .ROOT ))
233
- .addAggregation (Aggregation .terms ("spanName" , Integer .MAX_VALUE ));
234
-
235
- search .newCall (request , BodyConverters .SORTED_KEYS ).submit (new Callback <List <String >>() {
236
- @ Override public void onSuccess (List <String > value ) {
237
- if (!value .isEmpty ()) callback .onSuccess (value );
238
-
239
- // Special cased code until sites update their collectors. What this does is do a more
240
- // expensive nested query to get span names when the servicespan type returns nothing.
241
- SearchRequest .Filters filters = new SearchRequest .Filters ();
242
- filters .addRange ("timestamp_millis" , beginMillis , endMillis );
243
- filters .addNestedTerms (asList (
244
- "annotations.endpoint.serviceName" ,
245
- "binaryAnnotations.endpoint.serviceName"
246
- ), serviceName .toLowerCase (Locale .ROOT ));
247
- SearchRequest request = SearchRequest .forIndicesAndType (indices , SPAN )
248
- .filters (filters )
249
- .addAggregation (Aggregation .terms ("name" , Integer .MAX_VALUE ));
250
- search .newCall (request , BodyConverters .SORTED_KEYS ).submit (callback );
251
- }
198
+ // A span name is only valid on a local endpoint, as a span name is defined locally
199
+ SearchRequest .Filters filters = new SearchRequest .Filters ()
200
+ .addRange ("timestamp_millis" , beginMillis , endMillis )
201
+ .addTerm ("localEndpoint.serviceName" , serviceName .toLowerCase (Locale .ROOT ));
252
202
253
- @ Override public void onError ( Throwable t ) {
254
- callback . onError ( t );
255
- }
256
- } );
203
+ SearchRequest request = SearchRequest . forIndicesAndType ( indices , SPAN2 )
204
+ . filters ( filters )
205
+ . addAggregation ( Aggregation . terms ( "name" , Integer . MAX_VALUE ));
206
+ search . newCall ( request , BodyConverters . SORTED_KEYS ). submit ( callback );
257
207
}
258
208
259
209
@ Override public void getDependencies (long endTs , @ Nullable Long lookback ,
0 commit comments