|
| 1 | +/* |
| 2 | + * Copyright The Hypertrace 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.opentelemetry.javaagent.instrumentation.hypertrace.vertx; |
| 18 | + |
| 19 | +import io.opentelemetry.proto.trace.v1.Span; |
| 20 | +import io.vertx.core.Vertx; |
| 21 | +import io.vertx.core.VertxOptions; |
| 22 | +import io.vertx.core.buffer.Buffer; |
| 23 | +import io.vertx.core.http.HttpClient; |
| 24 | +import io.vertx.core.http.HttpClientOptions; |
| 25 | +import io.vertx.core.http.HttpClientRequest; |
| 26 | +import io.vertx.core.http.HttpMethod; |
| 27 | +import java.util.List; |
| 28 | +import java.util.concurrent.CountDownLatch; |
| 29 | +import java.util.concurrent.TimeoutException; |
| 30 | +import org.hypertrace.agent.testing.AbstractInstrumenterTest; |
| 31 | +import org.hypertrace.agent.testing.TestHttpServer; |
| 32 | +import org.junit.jupiter.api.AfterAll; |
| 33 | +import org.junit.jupiter.api.Assertions; |
| 34 | +import org.junit.jupiter.api.BeforeAll; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | + |
| 37 | +public class VertxClientInstrumentationPostTests extends AbstractInstrumenterTest { |
| 38 | + |
| 39 | + private static final TestHttpServer testHttpServer = new TestHttpServer(); |
| 40 | + private final Vertx vertx = Vertx.vertx(new VertxOptions()); |
| 41 | + private final HttpClientOptions clientOptions = new HttpClientOptions(); |
| 42 | + private final HttpClient httpClient = vertx.createHttpClient(clientOptions); |
| 43 | + |
| 44 | + @BeforeAll |
| 45 | + public static void startServer() throws Exception { |
| 46 | + testHttpServer.start(); |
| 47 | + } |
| 48 | + |
| 49 | + @AfterAll |
| 50 | + public static void closeServer() throws Exception { |
| 51 | + testHttpServer.close(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void postJson_write_end() throws TimeoutException, InterruptedException { |
| 56 | + String uri = String.format("http://localhost:%d/echo", testHttpServer.port()); |
| 57 | + |
| 58 | + CountDownLatch countDownLatch = new CountDownLatch(1); |
| 59 | + HttpClientRequest request = httpClient.requestAbs(HttpMethod.POST, uri); |
| 60 | + request = request.putHeader("Content-Type", "application/json"); |
| 61 | + request.setChunked(true); |
| 62 | + VertxClientInstrumentationTest.BufferHandler bufferHandler = |
| 63 | + new VertxClientInstrumentationTest.BufferHandler(countDownLatch); |
| 64 | + VertxClientInstrumentationTest.ResponseHandler responseHandler = |
| 65 | + new VertxClientInstrumentationTest.ResponseHandler(bufferHandler); |
| 66 | + |
| 67 | + request |
| 68 | + .handler(responseHandler) |
| 69 | + .write("write") |
| 70 | + .write(Buffer.buffer().appendString(" buffer")) |
| 71 | + .write(" str_encoding ", "utf-8") |
| 72 | + .end(); |
| 73 | + countDownLatch.await(); |
| 74 | + |
| 75 | + TEST_WRITER.waitForTraces(1); |
| 76 | + List<List<Span>> traces = |
| 77 | + TEST_WRITER.waitForSpans( |
| 78 | + 1, |
| 79 | + span -> |
| 80 | + span.getKind().equals(Span.SpanKind.SPAN_KIND_SERVER) |
| 81 | + || span.getKind().equals(Span.SpanKind.SPAN_KIND_INTERNAL)); |
| 82 | + Assertions.assertEquals(1, traces.size()); |
| 83 | + Span clientSpan = traces.get(0).get(0); |
| 84 | + Assertions.assertEquals( |
| 85 | + "write buffer str_encoding ", |
| 86 | + TEST_WRITER.getAttributesMap(clientSpan).get("http.request.body").getStringValue()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void postJson_write_end_string() throws TimeoutException, InterruptedException { |
| 91 | + String uri = String.format("http://localhost:%d/echo", testHttpServer.port()); |
| 92 | + |
| 93 | + CountDownLatch countDownLatch = new CountDownLatch(1); |
| 94 | + HttpClientRequest request = httpClient.requestAbs(HttpMethod.POST, uri); |
| 95 | + request = request.putHeader("Content-Type", "application/json"); |
| 96 | + request.setChunked(true); |
| 97 | + VertxClientInstrumentationTest.BufferHandler bufferHandler = |
| 98 | + new VertxClientInstrumentationTest.BufferHandler(countDownLatch); |
| 99 | + VertxClientInstrumentationTest.ResponseHandler responseHandler = |
| 100 | + new VertxClientInstrumentationTest.ResponseHandler(bufferHandler); |
| 101 | + |
| 102 | + request |
| 103 | + .handler(responseHandler) |
| 104 | + .write("write") |
| 105 | + .write(Buffer.buffer().appendString(" buffer")) |
| 106 | + .write(" str_encoding ", "utf-8") |
| 107 | + .end("end"); |
| 108 | + countDownLatch.await(); |
| 109 | + |
| 110 | + TEST_WRITER.waitForTraces(1); |
| 111 | + List<List<Span>> traces = |
| 112 | + TEST_WRITER.waitForSpans(1, span -> span.getKind().equals(Span.SpanKind.SPAN_KIND_SERVER)); |
| 113 | + Assertions.assertEquals(1, traces.size(), String.format("was: %d", traces.size())); |
| 114 | + Span clientSpan = traces.get(0).get(0); |
| 115 | + Assertions.assertEquals( |
| 116 | + "write buffer str_encoding end", |
| 117 | + TEST_WRITER.getAttributesMap(clientSpan).get("http.request.body").getStringValue()); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void postJson_write_end_buffer() throws TimeoutException, InterruptedException { |
| 122 | + String uri = String.format("http://localhost:%d/echo", testHttpServer.port()); |
| 123 | + |
| 124 | + CountDownLatch countDownLatch = new CountDownLatch(1); |
| 125 | + HttpClientRequest request = httpClient.requestAbs(HttpMethod.POST, uri); |
| 126 | + request = request.putHeader("Content-Type", "application/json"); |
| 127 | + request.setChunked(true); |
| 128 | + VertxClientInstrumentationTest.BufferHandler bufferHandler = |
| 129 | + new VertxClientInstrumentationTest.BufferHandler(countDownLatch); |
| 130 | + VertxClientInstrumentationTest.ResponseHandler responseHandler = |
| 131 | + new VertxClientInstrumentationTest.ResponseHandler(bufferHandler); |
| 132 | + |
| 133 | + request |
| 134 | + .handler(responseHandler) |
| 135 | + .write("write") |
| 136 | + .write(Buffer.buffer().appendString(" buffer")) |
| 137 | + .write(" str_encoding ", "utf-8") |
| 138 | + .end(Buffer.buffer("end")); |
| 139 | + countDownLatch.await(); |
| 140 | + |
| 141 | + TEST_WRITER.waitForTraces(1); |
| 142 | + List<List<Span>> traces = |
| 143 | + TEST_WRITER.waitForSpans( |
| 144 | + 1, |
| 145 | + span -> |
| 146 | + !span.getKind().equals(Span.SpanKind.SPAN_KIND_CLIENT) |
| 147 | + || span.getAttributesList().stream() |
| 148 | + .noneMatch( |
| 149 | + keyValue -> |
| 150 | + keyValue.getKey().equals("http.url") |
| 151 | + && keyValue.getValue().getStringValue().contains("/echo"))); |
| 152 | + Assertions.assertEquals(1, traces.size(), String.format("was: %d", traces.size())); |
| 153 | + Span clientSpan = traces.get(0).get(0); |
| 154 | + Assertions.assertEquals( |
| 155 | + "write buffer str_encoding end", |
| 156 | + TEST_WRITER.getAttributesMap(clientSpan).get("http.request.body").getStringValue()); |
| 157 | + } |
| 158 | +} |
0 commit comments