1 /* 2 * Copyright 2017, OpenCensus Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.opencensus.contrib.zpages; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Mockito.when; 21 22 import io.opencensus.trace.Status.CanonicalCode; 23 import io.opencensus.trace.export.RunningSpanStore; 24 import io.opencensus.trace.export.SampledSpanStore; 25 import io.opencensus.trace.export.SampledSpanStore.LatencyBucketBoundaries; 26 import java.io.ByteArrayOutputStream; 27 import java.io.OutputStream; 28 import java.util.Collections; 29 import java.util.HashMap; 30 import java.util.Map; 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.junit.runners.JUnit4; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 38 /** Unit tests for {@link TracezZPageHandler}. */ 39 @RunWith(JUnit4.class) 40 public class TracezZPageHandlerTest { 41 private static final String ACTIVE_SPAN_NAME = "TestActiveSpan"; 42 private static final String SAMPLED_SPAN_NAME = "TestSampledSpan"; 43 private static final String ACTIVE_SAMPLED_SPAN_NAME = "TestActiveAndSampledSpan"; 44 @Mock private RunningSpanStore runningSpanStore; 45 @Mock private SampledSpanStore sampledSpanStore; 46 RunningSpanStore.Summary runningSpanStoreSummary; 47 SampledSpanStore.Summary sampledSpanStoreSummary; 48 49 @Before 50 public void setUp() { 51 MockitoAnnotations.initMocks(this); 52 Map<String, RunningSpanStore.PerSpanNameSummary> runningSummaryMap = new HashMap<>(); 53 runningSummaryMap.put(ACTIVE_SPAN_NAME, RunningSpanStore.PerSpanNameSummary.create(3)); 54 runningSummaryMap.put(ACTIVE_SAMPLED_SPAN_NAME, RunningSpanStore.PerSpanNameSummary.create(5)); 55 runningSpanStoreSummary = RunningSpanStore.Summary.create(runningSummaryMap); 56 Map<LatencyBucketBoundaries, Integer> numbersOfLatencySampledSpans = new HashMap<>(); 57 numbersOfLatencySampledSpans.put(LatencyBucketBoundaries.MILLIx1_MILLIx10, 3); 58 numbersOfLatencySampledSpans.put(LatencyBucketBoundaries.MICROSx10_MICROSx100, 7); 59 Map<CanonicalCode, Integer> numbersOfErrorSampledSpans = new HashMap<>(); 60 numbersOfErrorSampledSpans.put(CanonicalCode.CANCELLED, 2); 61 numbersOfErrorSampledSpans.put(CanonicalCode.DEADLINE_EXCEEDED, 5); 62 Map<String, SampledSpanStore.PerSpanNameSummary> sampledSummaryMap = new HashMap<>(); 63 sampledSummaryMap.put( 64 SAMPLED_SPAN_NAME, 65 SampledSpanStore.PerSpanNameSummary.create( 66 numbersOfLatencySampledSpans, numbersOfErrorSampledSpans)); 67 sampledSummaryMap.put( 68 ACTIVE_SAMPLED_SPAN_NAME, 69 SampledSpanStore.PerSpanNameSummary.create( 70 numbersOfLatencySampledSpans, numbersOfErrorSampledSpans)); 71 sampledSpanStoreSummary = SampledSpanStore.Summary.create(sampledSummaryMap); 72 } 73 74 @Test 75 public void emitSummaryTableForEachSpan() { 76 OutputStream output = new ByteArrayOutputStream(); 77 TracezZPageHandler tracezZPageHandler = 78 TracezZPageHandler.create(runningSpanStore, sampledSpanStore); 79 when(runningSpanStore.getSummary()).thenReturn(runningSpanStoreSummary); 80 when(sampledSpanStore.getSummary()).thenReturn(sampledSpanStoreSummary); 81 tracezZPageHandler.emitHtml(Collections.emptyMap(), output); 82 assertThat(output.toString()).contains(ACTIVE_SPAN_NAME); 83 assertThat(output.toString()).contains(SAMPLED_SPAN_NAME); 84 assertThat(output.toString()).contains(ACTIVE_SAMPLED_SPAN_NAME); 85 } 86 87 @Test 88 public void linksForActiveRequests_InSummaryTable() { 89 OutputStream output = new ByteArrayOutputStream(); 90 TracezZPageHandler tracezZPageHandler = 91 TracezZPageHandler.create(runningSpanStore, sampledSpanStore); 92 when(runningSpanStore.getSummary()).thenReturn(runningSpanStoreSummary); 93 when(sampledSpanStore.getSummary()).thenReturn(sampledSpanStoreSummary); 94 tracezZPageHandler.emitHtml(Collections.emptyMap(), output); 95 // 3 active requests 96 assertThat(output.toString()).contains("href='?zspanname=TestActiveSpan&ztype=0&zsubtype=0'>3"); 97 // No active links 98 assertThat(output.toString()) 99 .doesNotContain("href='?zspanname=TestSampledSpan&ztype=0&zsubtype=0'"); 100 // 5 active requests 101 assertThat(output.toString()) 102 .contains("href='?zspanname=TestActiveAndSampledSpan&ztype=0&zsubtype=0'>5"); 103 } 104 105 @Test 106 public void linksForSampledRequests_InSummaryTable() { 107 OutputStream output = new ByteArrayOutputStream(); 108 TracezZPageHandler tracezZPageHandler = 109 TracezZPageHandler.create(runningSpanStore, sampledSpanStore); 110 when(runningSpanStore.getSummary()).thenReturn(runningSpanStoreSummary); 111 when(sampledSpanStore.getSummary()).thenReturn(sampledSpanStoreSummary); 112 tracezZPageHandler.emitHtml(Collections.emptyMap(), output); 113 // No sampled links (ztype=1); 114 assertThat(output.toString()).doesNotContain("href=\"?zspanname=TestActiveSpan&ztype=1"); 115 // Links for 7 samples [10us, 100us) and 3 samples [1ms, 10ms); 116 assertThat(output.toString()) 117 .contains("href='?zspanname=TestSampledSpan&ztype=1&zsubtype=1'>7"); 118 assertThat(output.toString()) 119 .contains("href='?zspanname=TestSampledSpan&ztype=1&zsubtype=3'>3"); 120 // Links for 7 samples [10us, 100us) and 3 samples [1ms, 10ms); 121 assertThat(output.toString()) 122 .contains("href='?zspanname=TestActiveAndSampledSpan&ztype=1&zsubtype=1'>7"); 123 assertThat(output.toString()) 124 .contains("href='?zspanname=TestActiveAndSampledSpan&ztype=1&zsubtype=3'>3"); 125 } 126 127 @Test 128 public void linksForFailedRequests_InSummaryTable() { 129 OutputStream output = new ByteArrayOutputStream(); 130 TracezZPageHandler tracezZPageHandler = 131 TracezZPageHandler.create(runningSpanStore, sampledSpanStore); 132 when(runningSpanStore.getSummary()).thenReturn(runningSpanStoreSummary); 133 when(sampledSpanStore.getSummary()).thenReturn(sampledSpanStoreSummary); 134 tracezZPageHandler.emitHtml(Collections.emptyMap(), output); 135 // No sampled links (ztype=1); 136 assertThat(output.toString()).doesNotContain("href=\"?zspanname=TestActiveSpan&ztype=2"); 137 // Links for 7 errors 2 CANCELLED + 5 DEADLINE_EXCEEDED; 138 assertThat(output.toString()) 139 .contains("href='?zspanname=TestSampledSpan&ztype=2&zsubtype=0'>7"); 140 // Links for 7 errors 2 CANCELLED + 5 DEADLINE_EXCEEDED; 141 assertThat(output.toString()) 142 .contains("href='?zspanname=TestActiveAndSampledSpan&ztype=2&zsubtype=0'>7"); 143 } 144 145 // TODO(bdrutu): Add tests for latency. 146 // TODO(bdrutu): Add tests for samples/running/errors. 147 } 148