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
40
+ static final String DEPENDENCY = "dependency" ;
41
41
static final String SPAN = "span" ;
42
- static final String DEPENDENCY_LINK = "dependencylink" ;
43
- static final String SERVICE_SPAN = "servicespan" ;
44
42
45
43
final SearchCallFactory search ;
46
- final String [] allIndices ;
44
+ final String [] allSpanIndices ;
47
45
final IndexNameFormatter indexNameFormatter ;
48
46
final boolean strictTraceId ;
49
47
final int namesLookback ;
50
48
51
49
ElasticsearchHttpSpanStore (ElasticsearchHttpStorage es ) {
52
50
this .search = new SearchCallFactory (es .http ());
53
- this .allIndices = new String [] {es .indexNameFormatter ().allIndices ()};
51
+ this .allSpanIndices = new String [] {es .indexNameFormatter ().allSpanIndices ()};
54
52
this .indexNameFormatter = es .indexNameFormatter ();
55
53
this .strictTraceId = es .strictTraceId ();
56
54
this .namesLookback = es .namesLookback ();
@@ -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 ) {
@@ -113,8 +93,9 @@ final class ElasticsearchHttpSpanStore implements AsyncSpanStore {
113
93
.addSubAggregation (Aggregation .min ("timestamp_millis" ))
114
94
.orderBy ("timestamp_millis" , "desc" );
115
95
116
- List <String > indices = indexNameFormatter .indexNamePatternsForRange (beginMillis , endMillis );
117
- SearchRequest esRequest = SearchRequest .forIndicesAndType (indices , SPAN )
96
+ List <String > indices =
97
+ indexNameFormatter .indexNamePatternsForRange (SPAN , beginMillis , endMillis );
98
+ SearchRequest esRequest = SearchRequest .create (indices )
118
99
.filters (filters ).addAggregation (traceIdTimestamp );
119
100
120
101
HttpCall <List <String >> traceIdsCall = search .newCall (esRequest , BodyConverters .SORTED_KEYS );
@@ -146,7 +127,7 @@ final class ElasticsearchHttpSpanStore implements AsyncSpanStore {
146
127
callback .onSuccess (Collections .emptyList ());
147
128
return ;
148
129
}
149
- SearchRequest request = SearchRequest .forIndicesAndType (indices , SPAN )
130
+ SearchRequest request = SearchRequest .create (indices )
150
131
.terms ("traceId" , traceIds );
151
132
search .newCall (request , BodyConverters .SPANS ).submit (successCallback );
152
133
}
@@ -182,7 +163,7 @@ final class ElasticsearchHttpSpanStore implements AsyncSpanStore {
182
163
public void getRawTrace (long traceIdHigh , long traceIdLow , Callback <List <Span >> callback ) {
183
164
String traceIdHex = Util .toLowerHex (strictTraceId ? traceIdHigh : 0L , traceIdLow );
184
165
185
- SearchRequest request = SearchRequest .forIndicesAndType (asList (allIndices ), SPAN )
166
+ SearchRequest request = SearchRequest .create (asList (allSpanIndices ) )
186
167
.term ("traceId" , traceIdHex );
187
168
188
169
search .newCall (request , BodyConverters .NULLABLE_SPANS ).submit (callback );
@@ -192,29 +173,17 @@ public void getRawTrace(long traceIdHigh, long traceIdLow, Callback<List<Span>>
192
173
long endMillis = System .currentTimeMillis ();
193
174
long beginMillis = endMillis - namesLookback ;
194
175
195
- 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
+ List <String > indices =
177
+ indexNameFormatter .indexNamePatternsForRange (SPAN , beginMillis , endMillis );
178
+ // Service name queries include both local and remote endpoints. This is different than
179
+ // Span name, as a span name can only be on a local endpoint.
180
+ SearchRequest .Filters filters = new SearchRequest .Filters ();
181
+ filters .addRange ("timestamp_millis" , beginMillis , endMillis );
182
+ SearchRequest request = SearchRequest .create (indices )
183
+ .filters (filters )
184
+ .addAggregation (Aggregation .terms ("localEndpoint.serviceName" , Integer .MAX_VALUE ))
185
+ .addAggregation (Aggregation .terms ("remoteEndpoint.serviceName" , Integer .MAX_VALUE ));
186
+ search .newCall (request , BodyConverters .SORTED_KEYS ).submit (callback );
218
187
}
219
188
220
189
@ Override public void getSpanNames (String serviceName , Callback <List <String >> callback ) {
@@ -226,34 +195,18 @@ public void getRawTrace(long traceIdHigh, long traceIdLow, Callback<List<Span>>
226
195
long endMillis = System .currentTimeMillis ();
227
196
long beginMillis = endMillis - namesLookback ;
228
197
229
- List <String > indices = indexNameFormatter .indexNamePatternsForRange (beginMillis , endMillis );
230
-
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
+ List <String > indices =
199
+ indexNameFormatter .indexNamePatternsForRange (SPAN , beginMillis , endMillis );
252
200
253
- @ Override public void onError (Throwable t ) {
254
- callback .onError (t );
255
- }
256
- });
201
+ // A span name is only valid on a local endpoint, as a span name is defined locally
202
+ SearchRequest .Filters filters = new SearchRequest .Filters ()
203
+ .addRange ("timestamp_millis" , beginMillis , endMillis )
204
+ .addTerm ("localEndpoint.serviceName" , serviceName .toLowerCase (Locale .ROOT ));
205
+
206
+ SearchRequest request = SearchRequest .create (indices )
207
+ .filters (filters )
208
+ .addAggregation (Aggregation .terms ("name" , Integer .MAX_VALUE ));
209
+ search .newCall (request , BodyConverters .SORTED_KEYS ).submit (callback );
257
210
}
258
211
259
212
@ Override public void getDependencies (long endTs , @ Nullable Long lookback ,
@@ -262,13 +215,8 @@ public void getRawTrace(long traceIdHigh, long traceIdLow, Callback<List<Span>>
262
215
long beginMillis = lookback != null ? endTs - lookback : 0 ;
263
216
// We just return all dependencies in the days that fall within endTs and lookback as
264
217
// dependency links themselves don't have timestamps.
265
- List <String > indices = indexNameFormatter .indexNamePatternsForRange (beginMillis , endTs );
266
- getDependencies (indices , callback );
267
- }
268
-
269
- void getDependencies (List <String > indices , Callback <List <DependencyLink >> callback ) {
270
- SearchRequest request = SearchRequest .forIndicesAndType (indices , DEPENDENCY_LINK );
271
-
272
- search .newCall (request , BodyConverters .DEPENDENCY_LINKS ).submit (callback );
218
+ List <String > indices =
219
+ indexNameFormatter .indexNamePatternsForRange (DEPENDENCY , beginMillis , endTs );
220
+ search .newCall (SearchRequest .create (indices ), BodyConverters .DEPENDENCY_LINKS ).submit (callback );
273
221
}
274
222
}
0 commit comments