|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 the original author or 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 | + * https://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 org.springframework.boot.actuate.autoconfigure.tracing.zipkin; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.net.URI; |
| 21 | +import java.net.http.HttpClient; |
| 22 | +import java.time.Duration; |
| 23 | +import java.util.Base64; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | +import java.util.concurrent.atomic.AtomicInteger; |
| 28 | +import java.util.function.Consumer; |
| 29 | + |
| 30 | +import okhttp3.mockwebserver.MockResponse; |
| 31 | +import okhttp3.mockwebserver.MockWebServer; |
| 32 | +import okhttp3.mockwebserver.QueueDispatcher; |
| 33 | +import okhttp3.mockwebserver.RecordedRequest; |
| 34 | +import org.junit.jupiter.api.AfterAll; |
| 35 | +import org.junit.jupiter.api.BeforeAll; |
| 36 | +import org.junit.jupiter.api.BeforeEach; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import zipkin2.reporter.BytesMessageSender; |
| 39 | +import zipkin2.reporter.Encoding; |
| 40 | +import zipkin2.reporter.HttpEndpointSupplier; |
| 41 | +import zipkin2.reporter.HttpEndpointSuppliers; |
| 42 | + |
| 43 | +import org.springframework.http.HttpHeaders; |
| 44 | + |
| 45 | +import static org.assertj.core.api.Assertions.assertThat; |
| 46 | +import static org.assertj.core.api.Assertions.assertThatException; |
| 47 | +import static org.assertj.core.api.Assertions.assertThatIOException; |
| 48 | + |
| 49 | +/** |
| 50 | + * Tests for {@link ZipkinHttpClientSender}. |
| 51 | + * |
| 52 | + * @author Moritz Halbritter |
| 53 | + */ |
| 54 | +class ZipkinHttpClientSenderTests extends ZipkinHttpSenderTests { |
| 55 | + |
| 56 | + private static ClearableDispatcher dispatcher; |
| 57 | + |
| 58 | + private static MockWebServer mockBackEnd; |
| 59 | + |
| 60 | + private static String zipkinUrl; |
| 61 | + |
| 62 | + @BeforeAll |
| 63 | + static void beforeAll() throws IOException { |
| 64 | + dispatcher = new ClearableDispatcher(); |
| 65 | + mockBackEnd = new MockWebServer(); |
| 66 | + mockBackEnd.setDispatcher(dispatcher); |
| 67 | + mockBackEnd.start(); |
| 68 | + zipkinUrl = mockBackEnd.url("/api/v2/spans").toString(); |
| 69 | + } |
| 70 | + |
| 71 | + @AfterAll |
| 72 | + static void afterAll() throws IOException { |
| 73 | + mockBackEnd.shutdown(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + @BeforeEach |
| 78 | + void beforeEach() throws Exception { |
| 79 | + super.beforeEach(); |
| 80 | + clearResponses(); |
| 81 | + clearRequests(); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + BytesMessageSender createSender() { |
| 86 | + return createSender(Encoding.JSON, Duration.ofSeconds(10)); |
| 87 | + } |
| 88 | + |
| 89 | + ZipkinHttpClientSender createSender(Encoding encoding, Duration timeout) { |
| 90 | + return createSender(HttpEndpointSuppliers.constantFactory(), encoding, timeout); |
| 91 | + } |
| 92 | + |
| 93 | + ZipkinHttpClientSender createSender(HttpEndpointSupplier.Factory endpointSupplierFactory, Encoding encoding, |
| 94 | + Duration timeout) { |
| 95 | + HttpClient httpClient = HttpClient.newBuilder().connectTimeout(timeout).build(); |
| 96 | + return new ZipkinHttpClientSender(encoding, endpointSupplierFactory, zipkinUrl, httpClient, timeout); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void sendShouldSendSpansToZipkin() throws IOException, InterruptedException { |
| 101 | + mockBackEnd.enqueue(new MockResponse()); |
| 102 | + List<byte[]> encodedSpans = List.of(toByteArray("span1"), toByteArray("span2")); |
| 103 | + this.sender.send(encodedSpans); |
| 104 | + requestAssertions((request) -> { |
| 105 | + assertThat(request.getMethod()).isEqualTo("POST"); |
| 106 | + assertThat(request.getHeader("Content-Type")).isEqualTo("application/json"); |
| 107 | + assertThat(request.getBody().readUtf8()).isEqualTo("[span1,span2]"); |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void sendShouldSendSpansToZipkinInProto3() throws IOException, InterruptedException { |
| 113 | + mockBackEnd.enqueue(new MockResponse()); |
| 114 | + List<byte[]> encodedSpans = List.of(toByteArray("span1"), toByteArray("span2")); |
| 115 | + try (BytesMessageSender sender = createSender(Encoding.PROTO3, Duration.ofSeconds(10))) { |
| 116 | + sender.send(encodedSpans); |
| 117 | + } |
| 118 | + requestAssertions((request) -> { |
| 119 | + assertThat(request.getMethod()).isEqualTo("POST"); |
| 120 | + assertThat(request.getHeader("Content-Type")).isEqualTo("application/x-protobuf"); |
| 121 | + assertThat(request.getBody().readUtf8()).isEqualTo("span1span2"); |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * This tests that a dynamic {@linkplain HttpEndpointSupplier} updates are visible to |
| 127 | + * {@link HttpSender#postSpans(URI, HttpHeaders, byte[])}. |
| 128 | + */ |
| 129 | + @Test |
| 130 | + void sendUsesDynamicEndpoint() throws Exception { |
| 131 | + mockBackEnd.enqueue(new MockResponse()); |
| 132 | + mockBackEnd.enqueue(new MockResponse()); |
| 133 | + AtomicInteger suffix = new AtomicInteger(); |
| 134 | + try (BytesMessageSender sender = createSender((e) -> new HttpEndpointSupplier() { |
| 135 | + @Override |
| 136 | + public String get() { |
| 137 | + return zipkinUrl + "/" + suffix.incrementAndGet(); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void close() { |
| 142 | + } |
| 143 | + }, Encoding.JSON, Duration.ofSeconds(10))) { |
| 144 | + sender.send(Collections.emptyList()); |
| 145 | + sender.send(Collections.emptyList()); |
| 146 | + } |
| 147 | + assertThat(mockBackEnd.takeRequest().getPath()).endsWith("/1"); |
| 148 | + assertThat(mockBackEnd.takeRequest().getPath()).endsWith("/2"); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + void sendShouldHandleHttpFailures() throws InterruptedException { |
| 153 | + mockBackEnd.enqueue(new MockResponse().setResponseCode(500)); |
| 154 | + assertThatException().isThrownBy(() -> this.sender.send(Collections.emptyList())) |
| 155 | + .withMessageContaining("Expected HTTP status 2xx, got 500"); |
| 156 | + requestAssertions((request) -> assertThat(request.getMethod()).isEqualTo("POST")); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + void sendShouldCompressData() throws IOException, InterruptedException { |
| 161 | + String uncompressed = "a".repeat(10000); |
| 162 | + // This is gzip compressed 10000 times 'a' |
| 163 | + byte[] compressed = Base64.getDecoder() |
| 164 | + .decode("H4sIAAAAAAAA/+3BMQ0AAAwDIKFLj/k3UR8NcA8AAAAAAAAAAAADUsAZfeASJwAA"); |
| 165 | + mockBackEnd.enqueue(new MockResponse()); |
| 166 | + this.sender.send(List.of(toByteArray(uncompressed))); |
| 167 | + requestAssertions((request) -> { |
| 168 | + assertThat(request.getMethod()).isEqualTo("POST"); |
| 169 | + assertThat(request.getHeader("Content-Type")).isEqualTo("application/json"); |
| 170 | + assertThat(request.getHeader("Content-Encoding")).isEqualTo("gzip"); |
| 171 | + assertThat(request.getBody().readByteArray()).isEqualTo(compressed); |
| 172 | + }); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + void shouldTimeout() throws IOException { |
| 177 | + try (BytesMessageSender sender = createSender(Encoding.JSON, Duration.ofMillis(1))) { |
| 178 | + MockResponse response = new MockResponse().setResponseCode(200).setHeadersDelay(100, TimeUnit.MILLISECONDS); |
| 179 | + mockBackEnd.enqueue(response); |
| 180 | + assertThatIOException().isThrownBy(() -> sender.send(Collections.emptyList())) |
| 181 | + .withMessageContaining("timed out"); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private void requestAssertions(Consumer<RecordedRequest> assertions) throws InterruptedException { |
| 186 | + RecordedRequest request = mockBackEnd.takeRequest(); |
| 187 | + assertThat(request).satisfies(assertions); |
| 188 | + } |
| 189 | + |
| 190 | + private static void clearRequests() throws InterruptedException { |
| 191 | + RecordedRequest request; |
| 192 | + do { |
| 193 | + request = mockBackEnd.takeRequest(0, TimeUnit.SECONDS); |
| 194 | + } |
| 195 | + while (request != null); |
| 196 | + } |
| 197 | + |
| 198 | + private static void clearResponses() { |
| 199 | + dispatcher.clear(); |
| 200 | + } |
| 201 | + |
| 202 | + private static final class ClearableDispatcher extends QueueDispatcher { |
| 203 | + |
| 204 | + void clear() { |
| 205 | + getResponseQueue().clear(); |
| 206 | + } |
| 207 | + |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments