Skip to content

Commit ca2f395

Browse files
committed
first working test
1 parent c4442e4 commit ca2f395

5 files changed

Lines changed: 153 additions & 23 deletions

File tree

custom/src/main/java/co/elastic/otel/declarativeconfig/ElasticDeclarativeConfigurationCustomizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class ElasticDeclarativeConfigurationCustomizer
6262
public void customize(DeclarativeConfigurationCustomizer customizer) {
6363
customizer.addModelCustomizer(
6464
model -> {
65-
if(model == null){
65+
if (model == null) {
6666
model = new OpenTelemetryConfigurationModel();
6767
}
6868

custom/src/main/resources/co/elastic/otel/config.yaml

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,43 @@ tracer_provider:
2828
processors:
2929
- batch:
3030
exporter:
31+
# get endpoint from OTEL_EXPORTER_OTLP_ENDPOINT environment variable if set, otherwise fallback to local collector
32+
# get endpoint headers (like authentication) from OTEL_EXPORTER_OTLP_HEADERS environment variable if set
3133
otlp_http:
32-
# get endpoint from OTEL_EXPORTER_OTLP_ENDPOINT environment variable if set, otherwise fallback to local collector
3334
endpoint: ${OTEL_EXPORTER_OTLP_ENDPOINT:-http://localhost:4318}/v1/traces
34-
# get endpoint headers (like authentication) from OTEL_EXPORTER_OTLP_HEADERS environment variable if set
3535
headers_list: ${OTEL_EXPORTER_OTLP_HEADERS}
36+
# use the following for grpc
37+
# otlp_grpc:
38+
# endpoint: ${OTEL_EXPORTER_OTLP_ENDPOINT}
39+
# headers_list: ${OTEL_EXPORTER_OTLP_HEADERS}
3640

3741
meter_provider:
3842
readers:
3943
- periodic:
4044
exporter:
41-
otlp_http:
42-
# get endpoint from OTEL_EXPORTER_OTLP_ENDPOINT environment variable if set, otherwise fallback to local collector
45+
# get endpoint from OTEL_EXPORTER_OTLP_ENDPOINT environment variable if set, otherwise fallback to local collector
46+
# get endpoint headers (like authentication) from OTEL_EXPORTER_OTLP_HEADERS environment variable if set
47+
otlp_grpc:
4348
endpoint: ${OTEL_EXPORTER_OTLP_ENDPOINT:-http://localhost:4318}/v1/metrics
44-
# get endpoint headers (like authentication) from OTEL_EXPORTER_OTLP_HEADERS environment variable if set
4549
headers_list: ${OTEL_EXPORTER_OTLP_HEADERS}
50+
# use the following for grpc
51+
# otlp_grpc:
52+
# endpoint: ${OTEL_EXPORTER_OTLP_ENDPOINT}
53+
# headers_list: ${OTEL_EXPORTER_OTLP_HEADERS}
4654

4755
logger_provider:
4856
processors:
4957
- batch:
5058
exporter:
51-
otlp_http:
52-
# get endpoint from OTEL_EXPORTER_OTLP_ENDPOINT environment variable if set, otherwise fallback to local collector
59+
# get endpoint from OTEL_EXPORTER_OTLP_ENDPOINT environment variable if set, otherwise fallback to local collector
60+
# get endpoint headers (like authentication) from OTEL_EXPORTER_OTLP_HEADERS environment variable if set
61+
otlp_grpc:
5362
endpoint: ${OTEL_EXPORTER_OTLP_ENDPOINT:-http://localhost:4318}/v1/logs
54-
# get endpoint headers (like authentication) from OTEL_EXPORTER_OTLP_HEADERS environment variable if set
5563
headers_list: ${OTEL_EXPORTER_OTLP_HEADERS}
64+
# use the following for grpc
65+
# otlp_grpc:
66+
# endpoint: ${OTEL_EXPORTER_OTLP_ENDPOINT}
67+
# headers_list: ${OTEL_EXPORTER_OTLP_HEADERS}
5668

5769
instrumentation/development:
5870
java:
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 org.assertj.core.api.Assertions.assertThat;
22+
import static org.assertj.core.api.Assertions.tuple;
23+
24+
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
25+
import io.opentelemetry.proto.trace.v1.Span;
26+
import java.io.IOException;
27+
import java.nio.file.Files;
28+
import java.nio.file.Path;
29+
import java.util.List;
30+
import java.util.concurrent.TimeUnit;
31+
import java.util.stream.Collectors;
32+
import org.junit.jupiter.api.AfterAll;
33+
import org.junit.jupiter.api.BeforeAll;
34+
import org.junit.jupiter.api.Test;
35+
import org.testcontainers.utility.MountableFile;
36+
37+
public class DeclarativeConfigSmokeTest extends TestAppSmokeTest {
38+
39+
private static void extractDeclarativeConfig(Path targetPath) {
40+
try {
41+
Process process =
42+
new ProcessBuilder()
43+
.command(JavaExecutable.getBinaryPath(), "-jar", AGENT_PATH, "--default-config-yaml")
44+
.redirectOutput(targetPath.toFile())
45+
.start();
46+
boolean processExit = process.waitFor(5, TimeUnit.SECONDS);
47+
if (!processExit || process.exitValue() != 0) {
48+
throw new IllegalStateException("failed to get default declarative config");
49+
}
50+
} catch (InterruptedException | IOException e) {
51+
throw new IllegalStateException(e);
52+
}
53+
}
54+
55+
@BeforeAll
56+
static void start() throws IOException {
57+
58+
Path configFile = Files.createTempFile("tmp", ".yaml");
59+
extractDeclarativeConfig(configFile);
60+
61+
// need to do some post-processing to switch from http to grpc by default
62+
List<String> modifiedConfig =
63+
Files.readAllLines(configFile).stream()
64+
.map(
65+
line -> {
66+
if (line.contains("otlp_http")) {
67+
// replace http exporter to grpc
68+
return line.replace("otlp_http", "otlp_grpc");
69+
} else if (line.contains("endpoint:")) {
70+
// for grpc the endpoint should not have a path like with HTTP
71+
int index = line.indexOf("endpoint:");
72+
return line.substring(0, index + 10) + "${OTEL_EXPORTER_OTLP_ENDPOINT}";
73+
} else {
74+
return line;
75+
}
76+
})
77+
.collect(Collectors.toList());
78+
Files.write(configFile, modifiedConfig);
79+
80+
startTestApp(
81+
(container) ->
82+
container
83+
.withCopyFileToContainer(MountableFile.forHostPath(configFile), "/config.yaml")
84+
.withEnv("OTEL_CONFIG_FILE", "/config.yaml")
85+
.withEnv("OTEL_EXPORTER_OTLP_ENDPOINT", BACKEND_ENDPOINT),
86+
false);
87+
}
88+
89+
@Test
90+
void healthcheck() {
91+
doRequest(getUrl("/health"), okResponseBody("Alive!"));
92+
93+
List<ExportTraceServiceRequest> traces = waitForTraces();
94+
List<Span> spans = getSpans(traces).toList();
95+
assertThat(spans)
96+
.hasSize(1)
97+
.extracting("name", "kind")
98+
.containsOnly(tuple("GET /health", Span.SpanKind.SPAN_KIND_SERVER));
99+
}
100+
101+
@AfterAll
102+
static void end() {
103+
stopApp();
104+
}
105+
}

smoke-tests/src/test/java/com/example/javaagent/smoketest/SmokeTest.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ abstract class SmokeTest {
7272
protected static final String MOCK_SERVER_HOST = "mock-server";
7373
// map to HTTPS port for kubernetes, mock server will handle both http and https on the same port
7474
protected static final int MOCK_SERVER_PORT = 443;
75+
protected static final String BACKEND_ENDPOINT = "http://backend:8080";
7576

7677
protected static OkHttpClient client = OkHttpUtils.client();
7778

@@ -115,16 +116,7 @@ protected static GenericContainer<?> startTarget(
115116
new GenericContainer<>(image)
116117
.withNetwork(network)
117118
.withLogConsumer(new Slf4jLogConsumer(logger))
118-
.withCopyFileToContainer(MountableFile.forHostPath(AGENT_PATH), JAVAAGENT_JAR_PATH)
119-
120-
// batch span processor: very small batch size for testing
121-
.withEnv("OTEL_BSP_MAX_EXPORT_BATCH", "1")
122-
// batch span processor: very short delay for testing
123-
.withEnv("OTEL_BSP_SCHEDULE_DELAY", "10")
124-
// use grpc endpoint as default is now http/protobuf with agent 2.x
125-
.withEnv("OTEL_EXPORTER_OTLP_PROTOCOL", "grpc")
126-
.withEnv("OTEL_PROPAGATORS", "tracecontext,baggage")
127-
.withEnv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://backend:8080");
119+
.withCopyFileToContainer(MountableFile.forHostPath(AGENT_PATH), JAVAAGENT_JAR_PATH);
128120

129121
StringBuilder jvmArgs = new StringBuilder();
130122

@@ -155,6 +147,18 @@ protected static GenericContainer<?> startTarget(
155147
return target;
156148
}
157149

150+
protected static void setBaseEnvironmentVariables(GenericContainer<?> target) {
151+
target
152+
// batch span processor: very small batch size for testing
153+
.withEnv("OTEL_BSP_MAX_EXPORT_BATCH", "1")
154+
// batch span processor: very short delay for testing
155+
.withEnv("OTEL_BSP_SCHEDULE_DELAY", "10")
156+
// use grpc endpoint as default is now http/protobuf with agent 2.x
157+
.withEnv("OTEL_EXPORTER_OTLP_PROTOCOL", "grpc")
158+
.withEnv("OTEL_PROPAGATORS", "tracecontext,baggage")
159+
.withEnv("OTEL_EXPORTER_OTLP_ENDPOINT", BACKEND_ENDPOINT);
160+
}
161+
158162
private static GenericContainer<?> addDockerDebugHost(GenericContainer<?> target) {
159163
// make the docker host IP available for remote debug
160164
// the 'host-gateway' is automatically translated by docker for all OSes

smoke-tests/src/test/java/com/example/javaagent/smoketest/TestAppSmokeTest.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,23 @@ public class TestAppSmokeTest extends SmokeTest {
3131
private static GenericContainer<?> target;
3232

3333
public static void startTestApp(Consumer<GenericContainer<?>> customizeContainer) {
34+
startTestApp(customizeContainer, true);
35+
}
36+
37+
public static void startTestApp(
38+
Consumer<GenericContainer<?>> customizeContainer, boolean defaultEnv) {
3439
target =
3540
startTarget(
3641
TEST_APP_IMAGE,
3742
customizeContainer.andThen(
38-
container ->
39-
container
40-
.withExposedPorts(PORT)
41-
.waitingFor(Wait.forHttp("/health").forPort(PORT))));
43+
container -> {
44+
if (defaultEnv) {
45+
setBaseEnvironmentVariables(container);
46+
}
47+
container
48+
.withExposedPorts(PORT)
49+
.waitingFor(Wait.forHttp("/health").forPort(PORT));
50+
}));
4251
}
4352

4453
protected static String getContainerId() {

0 commit comments

Comments
 (0)