|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package com.example.javaagent.smoketest; |
| 20 | + |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 22 | +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; |
| 23 | +import static com.github.tomakehurst.wiremock.client.WireMock.post; |
| 24 | +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; |
| 25 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
| 26 | +import static com.github.tomakehurst.wiremock.client.WireMock.verify; |
| 27 | +import static org.awaitility.Awaitility.await; |
| 28 | + |
| 29 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 30 | +import java.io.FileInputStream; |
| 31 | +import java.time.Duration; |
| 32 | +import java.util.Objects; |
| 33 | +import java.util.function.Consumer; |
| 34 | +import java.util.jar.Attributes; |
| 35 | +import java.util.jar.JarInputStream; |
| 36 | +import java.util.jar.Manifest; |
| 37 | +import org.junit.jupiter.api.AfterAll; |
| 38 | +import org.junit.jupiter.api.AfterEach; |
| 39 | +import org.junit.jupiter.api.BeforeAll; |
| 40 | +import org.junit.jupiter.api.Test; |
| 41 | +import org.slf4j.Logger; |
| 42 | +import org.slf4j.LoggerFactory; |
| 43 | +import org.testcontainers.Testcontainers; |
| 44 | +import org.testcontainers.containers.GenericContainer; |
| 45 | +import org.testcontainers.containers.output.Slf4jLogConsumer; |
| 46 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 47 | +import org.testcontainers.utility.MountableFile; |
| 48 | + |
| 49 | +public class UserAgentHeaderTest { |
| 50 | + |
| 51 | + private static final Logger logger = LoggerFactory.getLogger(UserAgentHeaderTest.class); |
| 52 | + |
| 53 | + private static final String AGENT_VERSION = extractVersion(SmokeTest.AGENT_PATH); |
| 54 | + |
| 55 | + private static WireMockServer wireMock; |
| 56 | + |
| 57 | + @BeforeAll |
| 58 | + public static void startWireMock() { |
| 59 | + wireMock = new WireMockServer(); |
| 60 | + wireMock.start(); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterAll |
| 64 | + public static void stopWireMock() { |
| 65 | + wireMock.stop(); |
| 66 | + } |
| 67 | + |
| 68 | + @AfterEach |
| 69 | + public void resetWiremock() { |
| 70 | + wireMock.resetAll(); |
| 71 | + } |
| 72 | + |
| 73 | + private String wiremockHostFromContainer() { |
| 74 | + return "host.testcontainers.internal:" + wireMock.port(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void verifyHttpExporterAgentHeaders() { |
| 79 | + wireMock.stubFor( |
| 80 | + post(urlEqualTo("/v1/traces")) |
| 81 | + .willReturn( |
| 82 | + aResponse() |
| 83 | + .withStatus(200) |
| 84 | + .withHeader("Content-Type", "application/json") |
| 85 | + .withBody("{\"status\": \"ok\"}"))); |
| 86 | + |
| 87 | + wireMock.stubFor( |
| 88 | + post(urlEqualTo("/v1/metrics")) |
| 89 | + .willReturn( |
| 90 | + aResponse() |
| 91 | + .withStatus(200) |
| 92 | + .withHeader("Content-Type", "application/json") |
| 93 | + .withBody("{\"status\": \"ok\"}"))); |
| 94 | + |
| 95 | + wireMock.stubFor( |
| 96 | + post(urlEqualTo("/v1/logs")) |
| 97 | + .willReturn( |
| 98 | + aResponse() |
| 99 | + .withStatus(200) |
| 100 | + .withHeader("Content-Type", "application/json") |
| 101 | + .withBody("{\"status\": \"ok\"}"))); |
| 102 | + |
| 103 | + try (GenericContainer<?> container = |
| 104 | + runTestApp( |
| 105 | + cont -> |
| 106 | + cont.withEnv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf") |
| 107 | + .withEnv( |
| 108 | + "OTEL_EXPORTER_OTLP_ENDPOINT", "http://" + wiremockHostFromContainer()))) { |
| 109 | + |
| 110 | + await() |
| 111 | + .atMost(Duration.ofSeconds(10)) |
| 112 | + .untilAsserted( |
| 113 | + () -> { |
| 114 | + // Traces ands logs are generated via the container health check |
| 115 | + verify( |
| 116 | + postRequestedFor(urlEqualTo("/v1/traces")) |
| 117 | + .withHeader( |
| 118 | + "User-Agent", equalTo("elastic-otlp-http-java/" + AGENT_VERSION))); |
| 119 | + verify( |
| 120 | + postRequestedFor(urlEqualTo("/v1/metrics")) |
| 121 | + .withHeader( |
| 122 | + "User-Agent", equalTo("elastic-otlp-http-java/" + AGENT_VERSION))); |
| 123 | + verify( |
| 124 | + postRequestedFor(urlEqualTo("/v1/logs")) |
| 125 | + .withHeader( |
| 126 | + "User-Agent", equalTo("elastic-otlp-http-java/" + AGENT_VERSION))); |
| 127 | + }); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void verifyGrpcExporterAgentHeaders() { |
| 133 | + |
| 134 | + // These responses are not correct for GRPC, but we don't care - we only care about the request |
| 135 | + // headers |
| 136 | + wireMock.stubFor( |
| 137 | + post(urlEqualTo("/opentelemetry.proto.collector.trace.v1.TraceService/Export")) |
| 138 | + .willReturn(aResponse().withStatus(200))); |
| 139 | + wireMock.stubFor( |
| 140 | + post(urlEqualTo("/opentelemetry.proto.collector.metrics.v1.MetricsService/Export")) |
| 141 | + .willReturn(aResponse().withStatus(200))); |
| 142 | + wireMock.stubFor( |
| 143 | + post(urlEqualTo("/opentelemetry.proto.collector.logs.v1.LogsService/Export")) |
| 144 | + .willReturn(aResponse().withStatus(200))); |
| 145 | + |
| 146 | + try (GenericContainer<?> container = |
| 147 | + runTestApp( |
| 148 | + cont -> |
| 149 | + cont.withEnv("OTEL_EXPORTER_OTLP_PROTOCOL", "grpc") |
| 150 | + .withEnv( |
| 151 | + "OTEL_EXPORTER_OTLP_ENDPOINT", "http://" + wiremockHostFromContainer()))) { |
| 152 | + |
| 153 | + await() |
| 154 | + .atMost(Duration.ofSeconds(10)) |
| 155 | + .untilAsserted( |
| 156 | + () -> { |
| 157 | + // Traces ands logs are generated via the container health check |
| 158 | + verify( |
| 159 | + postRequestedFor( |
| 160 | + urlEqualTo( |
| 161 | + "/opentelemetry.proto.collector.trace.v1.TraceService/Export")) |
| 162 | + .withHeader( |
| 163 | + "User-Agent", equalTo("elastic-otlp-grpc-java/" + AGENT_VERSION))); |
| 164 | + verify( |
| 165 | + postRequestedFor( |
| 166 | + urlEqualTo( |
| 167 | + "/opentelemetry.proto.collector.metrics.v1.MetricsService/Export")) |
| 168 | + .withHeader( |
| 169 | + "User-Agent", equalTo("elastic-otlp-grpc-java/" + AGENT_VERSION))); |
| 170 | + verify( |
| 171 | + postRequestedFor( |
| 172 | + urlEqualTo("/opentelemetry.proto.collector.logs.v1.LogsService/Export")) |
| 173 | + .withHeader( |
| 174 | + "User-Agent", equalTo("elastic-otlp-grpc-java/" + AGENT_VERSION))); |
| 175 | + }); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private GenericContainer<?> runTestApp(Consumer<GenericContainer<?>> customizer) { |
| 180 | + Testcontainers.exposeHostPorts(wireMock.port()); |
| 181 | + GenericContainer<?> target = |
| 182 | + new GenericContainer<>(TestAppSmokeTest.TEST_APP_IMAGE) |
| 183 | + .withLogConsumer(new Slf4jLogConsumer(logger)) |
| 184 | + .withCopyFileToContainer(MountableFile.forHostPath(SmokeTest.AGENT_PATH), "/agent.jar") |
| 185 | + .withEnv("JAVA_TOOL_OPTIONS", JavaExecutable.jvmAgentArgument("/agent.jar")) |
| 186 | + // speed up exports |
| 187 | + .withEnv("OTEL_BSP_MAX_EXPORT_BATCH", "1") |
| 188 | + .withEnv("OTEL_BSP_SCHEDULE_DELAY", "10") |
| 189 | + .withEnv("OTEL_BLRP_MAX_EXPORT_BATCH", "1") |
| 190 | + .withEnv("OTEL_BLRP_SCHEDULE_DELAY", "10") |
| 191 | + .withEnv("OTEL_METRIC_EXPORT_INTERVAL", "10") |
| 192 | + // use grpc endpoint as default is now http/protobuf with agent 2.x |
| 193 | + .withExposedPorts(TestAppSmokeTest.PORT) |
| 194 | + .waitingFor(Wait.forHttp("/health").forPort(TestAppSmokeTest.PORT)); |
| 195 | + customizer.accept(target); |
| 196 | + target.start(); |
| 197 | + return target; |
| 198 | + } |
| 199 | + |
| 200 | + private static String extractVersion(String agentJarPath) { |
| 201 | + try (JarInputStream jarStream = new JarInputStream(new FileInputStream(agentJarPath))) { |
| 202 | + Manifest mf = jarStream.getManifest(); |
| 203 | + Attributes attributes = mf.getMainAttributes(); |
| 204 | + return Objects.requireNonNull(attributes.getValue("Implementation-Version")); |
| 205 | + } catch (Exception e) { |
| 206 | + throw new IllegalStateException(e); |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments