Skip to content

Commit 6859776

Browse files
authored
[Trace Caching] Handling empty trace search result (#47)
1 parent 9125b7f commit 6859776

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

astra/src/main/java/com/slack/astra/zipkinApi/ZipkinService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public HttpResponse getTraceByTraceId(
275275
List<LogWireMessage> messages = searchResultToLogWireMessage(searchResult);
276276
String output = convertLogWireMessageToZipkinSpan(messages);
277277

278-
if (userRequest.isPresent() && userRequest.get() && !output.isEmpty()) {
278+
if (userRequest.isPresent() && userRequest.get() && !messages.isEmpty()) {
279279
long dataFreshnessInSecondsValue =
280280
dataFreshnessInSeconds.orElse(
281281
this.defaultDataFreshnessInSeconds); // default to 15 minutes if not

astra/src/test/java/com/slack/astra/zipkinApi/ZipkinServiceTest.java

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class ZipkinServiceTest {
5353
@Mock private AstraQueryServiceBase searcher;
5454
private ZipkinService zipkinService;
5555
private AstraSearch.SearchResult mockSearchResult;
56+
private AstraSearch.SearchResult mockEmptySearchResult;
5657
private BlobStore mockBlobStore;
5758

5859
private static final int defaultMaxSpans = 2000;
@@ -91,6 +92,14 @@ public void setup() throws IOException {
9192
AstraSearch.SearchResult.Builder builder = AstraSearch.SearchResult.newBuilder();
9293
JsonFormat.parser().merge(jsonString, builder);
9394
mockSearchResult = builder.build();
95+
96+
// Build mockEmptySearchResult
97+
objectMapper = new ObjectMapper();
98+
jsonNode = objectMapper.readTree(Resources.getResource("zipkinApi/empty_search_result.json"));
99+
jsonString = objectMapper.writeValueAsString(jsonNode);
100+
builder = AstraSearch.SearchResult.newBuilder();
101+
JsonFormat.parser().merge(jsonString, builder);
102+
mockEmptySearchResult = builder.build();
94103
}
95104

96105
@Test
@@ -232,7 +241,8 @@ public void testGetTraceByTraceId_respectUserRequest_upload_after_search() throw
232241
}
233242

234243
@Test
235-
public void testGetTraceByTraceId_respectUserRequest_skip_search() throws Exception {
244+
public void testGetTraceByTraceId_respectUserRequest_no_upload_after_search_empty_search_result()
245+
throws Exception {
236246
try (MockedStatic<Tracing> mockedTracing = mockStatic(Tracing.class)) {
237247
// Mocking Tracing and Span
238248
Tracer mockTracer = mock(Tracer.class);
@@ -243,6 +253,51 @@ public void testGetTraceByTraceId_respectUserRequest_skip_search() throws Except
243253

244254
String traceId = "test_trace_4";
245255
String traceFilePath = String.format("%s/%s/traceData.json.gz", TRACE_CACHE_PREFIX, traceId);
256+
when(searcher.doSearch(any())).thenReturn(mockEmptySearchResult);
257+
258+
boolean userRequest = true;
259+
260+
HttpResponse response =
261+
zipkinService.getTraceByTraceId(
262+
traceId,
263+
Optional.empty(),
264+
Optional.empty(),
265+
Optional.empty(),
266+
Optional.of(userRequest),
267+
Optional.empty());
268+
269+
verify(searcher)
270+
.doSearch(
271+
Mockito.argThat(
272+
request -> request.getQuery().contains("\"trace_id\":\"" + traceId + "\"")));
273+
274+
verify(mockBlobStore, never()).uploadData(anyString(), anyString(), eq(true));
275+
verify(mockBlobStore, never()).copyFile(anyString(), eq(traceFilePath));
276+
verify(mockBlobStore, never()).delete(anyString());
277+
278+
assertNotNull(response, "Response should not be null");
279+
response
280+
.aggregate()
281+
.thenAccept(
282+
aggregatedResponse -> {
283+
assertEquals(HttpStatus.OK, aggregatedResponse.status());
284+
assertEquals(mockSearchResult.toString(), aggregatedResponse.contentUtf8());
285+
});
286+
}
287+
}
288+
289+
@Test
290+
public void testGetTraceByTraceId_respectUserRequest_skip_search() throws Exception {
291+
try (MockedStatic<Tracing> mockedTracing = mockStatic(Tracing.class)) {
292+
// Mocking Tracing and Span
293+
Tracer mockTracer = mock(Tracer.class);
294+
Span mockSpan = mock(Span.class);
295+
296+
mockedTracing.when(Tracing::currentTracer).thenReturn(mockTracer);
297+
when(mockTracer.currentSpan()).thenReturn(mockSpan);
298+
299+
String traceId = "test_trace_5";
300+
String traceFilePath = String.format("%s/%s/traceData.json.gz", TRACE_CACHE_PREFIX, traceId);
246301

247302
Path filePath = Paths.get(Resources.getResource("zipkinApi/traceData.json").toURI());
248303

@@ -286,7 +341,7 @@ public void testGetTraceByTraceId_respectUserRequest_respectDataFreshness_perfor
286341
mockedTracing.when(Tracing::currentTracer).thenReturn(mockTracer);
287342
when(mockTracer.currentSpan()).thenReturn(mockSpan);
288343

289-
String traceId = "test_trace_5";
344+
String traceId = "test_trace_6";
290345
String traceFilePath = String.format("%s/%s/traceData.json.gz", TRACE_CACHE_PREFIX, traceId);
291346

292347
boolean userRequest = true;
@@ -344,7 +399,7 @@ public void testGetTraceByTraceId_respectUserRequest_respectDataFreshness_perfor
344399
mockedTracing.when(Tracing::currentTracer).thenReturn(mockTracer);
345400
when(mockTracer.currentSpan()).thenReturn(mockSpan);
346401

347-
String traceId = "test_trace_6";
402+
String traceId = "test_trace_7";
348403
String traceFilePath = String.format("%s/%s/traceData.json.gz", TRACE_CACHE_PREFIX, traceId);
349404
boolean userRequest = true;
350405
long dataFreshnessInSeconds = 100;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"hits": [],
3+
"tookMicros": "2732",
4+
"totalNodes": 1,
5+
"totalSnapshots": 1,
6+
"snapshotsWithReplicas": 1
7+
}

0 commit comments

Comments
 (0)