|
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | +package com.google.api.client.http; |
| 15 | + |
| 16 | +import com.google.api.client.testing.http.MockHttpTransport; |
| 17 | +import com.google.api.client.testing.http.MockLowLevelHttpRequest; |
| 18 | +import com.google.api.client.testing.http.MockLowLevelHttpResponse; |
| 19 | +import io.opencensus.common.Functions; |
| 20 | +import io.opencensus.testing.export.TestHandler; |
| 21 | +import io.opencensus.trace.AttributeValue; |
| 22 | +import io.opencensus.trace.MessageEvent; |
| 23 | +import io.opencensus.trace.Status; |
| 24 | +import io.opencensus.trace.Tracing; |
| 25 | +import io.opencensus.trace.config.TraceParams; |
| 26 | +import io.opencensus.trace.export.SpanData; |
| 27 | +import io.opencensus.trace.samplers.Samplers; |
| 28 | +import org.junit.After; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +import static com.google.api.client.http.OpenCensusUtils.SPAN_NAME_HTTP_REQUEST_EXECUTE; |
| 36 | +import static org.junit.Assert.assertEquals; |
| 37 | +import static org.junit.Assert.assertNotNull; |
| 38 | +import static org.junit.Assert.assertTrue; |
| 39 | +import static org.junit.Assert.fail; |
| 40 | + |
| 41 | +public class HttpRequestTracingTest { |
| 42 | + private static final TestHandler testHandler = new TestHandler(); |
| 43 | + |
| 44 | + @Before |
| 45 | + public void setupTestTracer() { |
| 46 | + Tracing.getExportComponent().getSpanExporter().registerHandler("test", testHandler); |
| 47 | + TraceParams params = |
| 48 | + Tracing.getTraceConfig() |
| 49 | + .getActiveTraceParams() |
| 50 | + .toBuilder() |
| 51 | + .setSampler(Samplers.alwaysSample()) |
| 52 | + .build(); |
| 53 | + Tracing.getTraceConfig().updateActiveTraceParams(params); |
| 54 | + } |
| 55 | + |
| 56 | + @After |
| 57 | + public void teardownTestTracer() { |
| 58 | + Tracing.getExportComponent().getSpanExporter().unregisterHandler("test"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test(timeout = 20_000L) |
| 62 | + public void executeCreatesSpan() throws IOException { |
| 63 | + MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse() |
| 64 | + .setStatusCode(200); |
| 65 | + HttpTransport transport = new MockHttpTransport.Builder() |
| 66 | + .setLowLevelHttpResponse(mockResponse) |
| 67 | + .build(); |
| 68 | + HttpRequest request = new HttpRequestFactory(transport, null) |
| 69 | + .buildGetRequest(new GenericUrl("https://google.com/")); |
| 70 | + request.execute(); |
| 71 | + |
| 72 | + // This call blocks - we set a timeout on this test to ensure we don't wait forever |
| 73 | + List<SpanData> spans = testHandler.waitForExport(1); |
| 74 | + assertEquals(1, spans.size()); |
| 75 | + SpanData span = spans.get(0); |
| 76 | + |
| 77 | + // Ensure the span name is set |
| 78 | + assertEquals(SPAN_NAME_HTTP_REQUEST_EXECUTE, span.getName()); |
| 79 | + |
| 80 | + // Ensure we have basic span attributes |
| 81 | + assertAttributeEquals(span, "http.path", "/"); |
| 82 | + assertAttributeEquals(span, "http.host", "google.com"); |
| 83 | + assertAttributeEquals(span, "http.url", "https://google.com/"); |
| 84 | + assertAttributeEquals(span, "http.method", "GET"); |
| 85 | + |
| 86 | + // Ensure we have a single annotation for starting the first attempt |
| 87 | + assertEquals(1, span.getAnnotations().getEvents().size()); |
| 88 | + |
| 89 | + // Ensure we have 2 message events, SENT and RECEIVED |
| 90 | + assertEquals(2, span.getMessageEvents().getEvents().size()); |
| 91 | + assertEquals(MessageEvent.Type.SENT, span.getMessageEvents().getEvents().get(0).getEvent().getType()); |
| 92 | + assertEquals(MessageEvent.Type.RECEIVED, span.getMessageEvents().getEvents().get(1).getEvent().getType()); |
| 93 | + |
| 94 | + // Ensure we record the span status as OK |
| 95 | + assertEquals(Status.OK, span.getStatus()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test(timeout = 20_000L) |
| 99 | + public void executeExceptionCreatesSpan() throws IOException { |
| 100 | + HttpTransport transport = new MockHttpTransport.Builder() |
| 101 | + .setLowLevelHttpRequest(new MockLowLevelHttpRequest() { |
| 102 | + @Override |
| 103 | + public LowLevelHttpResponse execute() throws IOException { |
| 104 | + throw new IOException("some IOException"); |
| 105 | + } |
| 106 | + }) |
| 107 | + .build(); |
| 108 | + HttpRequest request = new HttpRequestFactory(transport, null) |
| 109 | + .buildGetRequest(new GenericUrl("https://google.com/")); |
| 110 | + |
| 111 | + try { |
| 112 | + request.execute(); |
| 113 | + fail("expected to throw an IOException"); |
| 114 | + } catch (IOException expected) { |
| 115 | + } |
| 116 | + |
| 117 | + // This call blocks - we set a timeout on this test to ensure we don't wait forever |
| 118 | + List<SpanData> spans = testHandler.waitForExport(1); |
| 119 | + assertEquals(1, spans.size()); |
| 120 | + SpanData span = spans.get(0); |
| 121 | + |
| 122 | + // Ensure the span name is set |
| 123 | + assertEquals(SPAN_NAME_HTTP_REQUEST_EXECUTE, span.getName()); |
| 124 | + |
| 125 | + // Ensure we have basic span attributes |
| 126 | + assertAttributeEquals(span, "http.path", "/"); |
| 127 | + assertAttributeEquals(span, "http.host", "google.com"); |
| 128 | + assertAttributeEquals(span, "http.url", "https://google.com/"); |
| 129 | + assertAttributeEquals(span, "http.method", "GET"); |
| 130 | + |
| 131 | + // Ensure we have a single annotation for starting the first attempt |
| 132 | + assertEquals(1, span.getAnnotations().getEvents().size()); |
| 133 | + |
| 134 | + // Ensure we have 2 message events, SENT and RECEIVED |
| 135 | + assertEquals(1, span.getMessageEvents().getEvents().size()); |
| 136 | + assertEquals(MessageEvent.Type.SENT, span.getMessageEvents().getEvents().get(0).getEvent().getType()); |
| 137 | + |
| 138 | + // Ensure we record the span status as UNKNOWN |
| 139 | + assertEquals(Status.UNKNOWN, span.getStatus()); } |
| 140 | + |
| 141 | + void assertAttributeEquals(SpanData span, String attributeName, String expectedValue) { |
| 142 | + Object attributeValue = span.getAttributes().getAttributeMap().get(attributeName); |
| 143 | + assertNotNull("expected span to contain attribute: " + attributeName, attributeValue); |
| 144 | + assertTrue(attributeValue instanceof AttributeValue); |
| 145 | + String value = ((AttributeValue) attributeValue).match( |
| 146 | + Functions.returnToString(), |
| 147 | + Functions.returnToString(), |
| 148 | + Functions.returnToString(), |
| 149 | + Functions.returnToString(), |
| 150 | + Functions.</*@Nullable*/ String>returnNull()); |
| 151 | + assertEquals(expectedValue, value); |
| 152 | + } |
| 153 | +} |
0 commit comments