|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.jmxscraper.target_systems; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import com.linecorp.armeria.server.ServerBuilder; |
| 11 | +import com.linecorp.armeria.server.grpc.GrpcService; |
| 12 | +import com.linecorp.armeria.testing.junit5.server.ServerExtension; |
| 13 | +import io.grpc.stub.StreamObserver; |
| 14 | +import io.opentelemetry.contrib.jmxscraper.client.JmxRemoteClient; |
| 15 | +import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest; |
| 16 | +import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceResponse; |
| 17 | +import io.opentelemetry.proto.collector.metrics.v1.MetricsServiceGrpc; |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.concurrent.BlockingQueue; |
| 22 | +import java.util.concurrent.ExecutionException; |
| 23 | +import java.util.concurrent.LinkedBlockingDeque; |
| 24 | +import javax.management.remote.JMXConnector; |
| 25 | +import org.junit.jupiter.api.AfterAll; |
| 26 | +import org.junit.jupiter.api.AfterEach; |
| 27 | +import org.junit.jupiter.api.BeforeAll; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | +import org.testcontainers.Testcontainers; |
| 32 | +import org.testcontainers.containers.GenericContainer; |
| 33 | +import org.testcontainers.containers.Network; |
| 34 | +import org.testcontainers.containers.output.Slf4jLogConsumer; |
| 35 | + |
| 36 | +public abstract class TargetSystemIntegrationTest { |
| 37 | + |
| 38 | + private static final Logger logger = LoggerFactory.getLogger(TargetSystemIntegrationTest.class); |
| 39 | + |
| 40 | + /** |
| 41 | + * Create target system container |
| 42 | + * |
| 43 | + * @param jmxPort JMX port target JVM should listen to |
| 44 | + * @return target system container |
| 45 | + */ |
| 46 | + protected abstract GenericContainer<?> createTargetContainer(int jmxPort); |
| 47 | + |
| 48 | + // assert on received metrics |
| 49 | + |
| 50 | + private static Network network; |
| 51 | + private static OtlpGrpcServer otlpServer; |
| 52 | + private GenericContainer<?> target; |
| 53 | + private GenericContainer<?> scraper; |
| 54 | + |
| 55 | + // private static final String OTLP_HOST = "host.testcontainers.internal"; |
| 56 | + private static final int JMX_PORT = 9999; |
| 57 | + |
| 58 | + @BeforeAll |
| 59 | + static void beforeAll() { |
| 60 | + network = Network.newNetwork(); |
| 61 | + otlpServer = new OtlpGrpcServer(); |
| 62 | + otlpServer.start(); |
| 63 | + Testcontainers.exposeHostPorts(otlpServer.httpPort()); |
| 64 | + // String otlpEndpoint = "http://" + OTLP_HOST + ":" + otlpServer.httpPort(); |
| 65 | + } |
| 66 | + |
| 67 | + @AfterAll |
| 68 | + static void afterAll() { |
| 69 | + network.close(); |
| 70 | + try { |
| 71 | + otlpServer.stop().get(); |
| 72 | + } catch (InterruptedException | ExecutionException e) { |
| 73 | + throw new RuntimeException(e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @AfterEach |
| 78 | + void afterEach() { |
| 79 | + if (target != null && target.isRunning()) { |
| 80 | + target.stop(); |
| 81 | + } |
| 82 | + if (scraper != null && scraper.isRunning()) { |
| 83 | + scraper.stop(); |
| 84 | + } |
| 85 | + if (otlpServer != null) { |
| 86 | + otlpServer.reset(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void endToEndTest() { |
| 92 | + |
| 93 | + target = |
| 94 | + createTargetContainer(JMX_PORT) |
| 95 | + .withLogConsumer(new Slf4jLogConsumer(logger)) |
| 96 | + .withNetwork(network) |
| 97 | + .withExposedPorts(JMX_PORT) |
| 98 | + .withNetworkAliases("target_system"); |
| 99 | + target.start(); |
| 100 | + |
| 101 | + logger.info( |
| 102 | + "Target system started, JMX port: {} mapped to {}:{}", |
| 103 | + JMX_PORT, |
| 104 | + target.getHost(), |
| 105 | + target.getMappedPort(JMX_PORT)); |
| 106 | + |
| 107 | + scraper = createScraperContainer(); |
| 108 | + |
| 109 | + // TODO: start scraper container |
| 110 | + // scraper.start(); |
| 111 | + |
| 112 | + // TODO : wait for metrics to be sent and add assertions on what is being captured |
| 113 | + // for now we just test that we can connect to remote JMX using our client. |
| 114 | + try (JMXConnector connector = |
| 115 | + JmxRemoteClient.createNew(target.getHost(), target.getMappedPort(JMX_PORT)).connect()) { |
| 116 | + assertThat(connector.getMBeanServerConnection()).isNotNull(); |
| 117 | + } catch (IOException e) { |
| 118 | + throw new RuntimeException(e); |
| 119 | + } |
| 120 | + // TODO: replace with real assertions |
| 121 | + assertThat(otlpServer.getMetrics()).isEmpty(); |
| 122 | + } |
| 123 | + |
| 124 | + private static GenericContainer<?> createScraperContainer() { |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + private static class OtlpGrpcServer extends ServerExtension { |
| 129 | + |
| 130 | + private final BlockingQueue<ExportMetricsServiceRequest> metricRequests = |
| 131 | + new LinkedBlockingDeque<>(); |
| 132 | + |
| 133 | + List<ExportMetricsServiceRequest> getMetrics() { |
| 134 | + return new ArrayList<>(metricRequests); |
| 135 | + } |
| 136 | + |
| 137 | + void reset() { |
| 138 | + metricRequests.clear(); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + protected void configure(ServerBuilder sb) { |
| 143 | + sb.service( |
| 144 | + GrpcService.builder() |
| 145 | + .addService( |
| 146 | + new MetricsServiceGrpc.MetricsServiceImplBase() { |
| 147 | + @Override |
| 148 | + public void export( |
| 149 | + ExportMetricsServiceRequest request, |
| 150 | + StreamObserver<ExportMetricsServiceResponse> responseObserver) { |
| 151 | + metricRequests.add(request); |
| 152 | + responseObserver.onNext(ExportMetricsServiceResponse.getDefaultInstance()); |
| 153 | + responseObserver.onCompleted(); |
| 154 | + } |
| 155 | + }) |
| 156 | + .build()); |
| 157 | + sb.http(0); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments