16
16
import io .opentelemetry .proto .collector .metrics .v1 .ExportMetricsServiceResponse ;
17
17
import io .opentelemetry .proto .collector .metrics .v1 .MetricsServiceGrpc ;
18
18
import java .io .IOException ;
19
+ import java .time .Duration ;
19
20
import java .util .ArrayList ;
21
+ import java .util .Arrays ;
20
22
import java .util .List ;
23
+ import java .util .Locale ;
21
24
import java .util .concurrent .BlockingQueue ;
22
25
import java .util .concurrent .ExecutionException ;
23
26
import java .util .concurrent .LinkedBlockingDeque ;
32
35
import org .testcontainers .containers .GenericContainer ;
33
36
import org .testcontainers .containers .Network ;
34
37
import org .testcontainers .containers .output .Slf4jLogConsumer ;
38
+ import org .testcontainers .containers .wait .strategy .Wait ;
39
+ import org .testcontainers .utility .MountableFile ;
35
40
36
41
public abstract class TargetSystemIntegrationTest {
37
42
38
43
private static final Logger logger = LoggerFactory .getLogger (TargetSystemIntegrationTest .class );
44
+ private static String otlpEndpoint ;
39
45
40
46
/**
41
47
* Create target system container
@@ -45,14 +51,14 @@ public abstract class TargetSystemIntegrationTest {
45
51
*/
46
52
protected abstract GenericContainer <?> createTargetContainer (int jmxPort );
47
53
48
- // assert on received metrics
54
+ protected abstract String getTargetSystem ();
49
55
50
56
private static Network network ;
51
57
private static OtlpGrpcServer otlpServer ;
52
58
private GenericContainer <?> target ;
53
59
private GenericContainer <?> scraper ;
54
60
55
- // private static final String OTLP_HOST = "host.testcontainers.internal";
61
+ private static final String OTLP_HOST = "host.testcontainers.internal" ;
56
62
private static final int JMX_PORT = 9999 ;
57
63
58
64
@ BeforeAll
@@ -61,7 +67,7 @@ static void beforeAll() {
61
67
otlpServer = new OtlpGrpcServer ();
62
68
otlpServer .start ();
63
69
Testcontainers .exposeHostPorts (otlpServer .httpPort ());
64
- // String otlpEndpoint = "http://" + OTLP_HOST + ":" + otlpServer.httpPort();
70
+ otlpEndpoint = "http://" + OTLP_HOST + ":" + otlpServer .httpPort ();
65
71
}
66
72
67
73
@ AfterAll
@@ -98,21 +104,21 @@ void endToEndTest() {
98
104
.withNetworkAliases ("target_system" );
99
105
target .start ();
100
106
107
+ String targetHost = target .getHost ();
108
+ Integer targetPort = target .getMappedPort (JMX_PORT );
101
109
logger .info (
102
- "Target system started, JMX port: {} mapped to {}:{}" ,
103
- JMX_PORT ,
104
- target .getHost (),
105
- target .getMappedPort (JMX_PORT ));
110
+ "Target system started, JMX port: {} mapped to {}:{}" , JMX_PORT , targetHost , targetPort );
106
111
107
- scraper = createScraperContainer ();
112
+ scraper =
113
+ createScraperContainer (otlpEndpoint , getTargetSystem (), null , "target_system" , JMX_PORT );
114
+ logger .info (
115
+ "starting scraper with JVM arguments : {}" , String .join (" " , scraper .getCommandParts ()));
108
116
109
- // TODO: start scraper container
110
- // scraper.start();
117
+ scraper .start ();
111
118
112
119
// TODO : wait for metrics to be sent and add assertions on what is being captured
113
120
// 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 ()) {
121
+ try (JMXConnector connector = JmxRemoteClient .createNew (targetHost , targetPort ).connect ()) {
116
122
assertThat (connector .getMBeanServerConnection ()).isNotNull ();
117
123
} catch (IOException e ) {
118
124
throw new RuntimeException (e );
@@ -121,8 +127,54 @@ void endToEndTest() {
121
127
assertThat (otlpServer .getMetrics ()).isEmpty ();
122
128
}
123
129
124
- private static GenericContainer <?> createScraperContainer () {
125
- return null ;
130
+ protected GenericContainer <?> createScraperContainer (
131
+ String otlpEndpoint ,
132
+ String targetSystem ,
133
+ String customYaml ,
134
+ String targetHost ,
135
+ int targetPort ) {
136
+
137
+ String scraperJarPath = System .getProperty ("shadow.jar.path" );
138
+ assertThat (scraperJarPath ).isNotNull ();
139
+
140
+ // TODO: adding a way to provide 'host:port' syntax would make this easier for common use
141
+ String url =
142
+ String .format (
143
+ Locale .getDefault (),
144
+ "service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi" ,
145
+ targetHost ,
146
+ targetPort );
147
+
148
+ // for now only configure through JVM args
149
+ List <String > arguments =
150
+ new ArrayList <>(
151
+ Arrays .asList (
152
+ "java" ,
153
+ "-Dotel.exporter.otlp.endpoint=" + otlpEndpoint ,
154
+ "-Dotel.jmx.target.system=" + targetSystem ,
155
+ "-Dotel.jmx.interval.milliseconds=1000" ,
156
+ "-Dotel.jmx.service.url=" + url ,
157
+ "-jar" ,
158
+ "/scraper.jar" ));
159
+
160
+ GenericContainer <?> scraper =
161
+ new GenericContainer <>("openjdk:8u272-jre-slim" )
162
+ .withNetwork (network )
163
+ .withCopyFileToContainer (MountableFile .forHostPath (scraperJarPath ), "/scraper.jar" )
164
+ .withLogConsumer (new Slf4jLogConsumer (logger ))
165
+ .waitingFor (
166
+ Wait .forLogMessage (".*JMX scraping started.*" , 1 )
167
+ .withStartupTimeout (Duration .ofSeconds (10 )));
168
+
169
+ if (customYaml != null ) {
170
+ arguments .add ("-Dotel.jmx.config=/custom.yaml" );
171
+ scraper .withCopyFileToContainer (
172
+ MountableFile .forClasspathResource (customYaml ), "/custom.yaml" );
173
+ }
174
+
175
+ scraper .withCommand (arguments .toArray (new String [0 ]));
176
+
177
+ return scraper ;
126
178
}
127
179
128
180
private static class OtlpGrpcServer extends ServerExtension {
0 commit comments