92 lines
3.7 KiB
Java
92 lines
3.7 KiB
Java
package com.ankurm.otel;
|
|
|
|
import java.util.List;
|
|
|
|
import io.micrometer.core.instrument.MeterRegistry;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
|
/**
|
|
* One request against /hello/{name}, measured with each value of
|
|
* management.observations.conventions (and with the property absent). Writes the HTTP meter names
|
|
* and tags, and the span-related strings found in the OTLP trace payload. A start-up failure is
|
|
* recorded, not hidden.
|
|
*/
|
|
class ConventionsTest {
|
|
|
|
@Test
|
|
void metricsAndSpans() throws Exception {
|
|
Transcript t = new Transcript("01-conventions-metrics-and-spans");
|
|
java.util.Map<String, List<String>> meters = new java.util.LinkedHashMap<>();
|
|
java.util.Map<String, String> failures = new java.util.LinkedHashMap<>();
|
|
for (String mode : List.of("(property not set)", "micrometer", "open-telemetry", "opentelemetry")) {
|
|
try (OtlpSink sink = new OtlpSink()) {
|
|
t.line("");
|
|
t.line("== management.observations.conventions=" + mode);
|
|
ConfigurableApplicationContext ctx = null;
|
|
try {
|
|
String[] props = mode.startsWith("(")
|
|
? new String[] { "management.otlp.metrics.export.enabled=false",
|
|
"management.opentelemetry.tracing.export.otlp.endpoint=" + sink.baseUrl() + "/v1/traces" }
|
|
: new String[] { "management.observations.conventions=" + mode,
|
|
"management.otlp.metrics.export.enabled=false",
|
|
"management.opentelemetry.tracing.export.otlp.endpoint=" + sink.baseUrl() + "/v1/traces" };
|
|
ctx = Apps.start(props);
|
|
}
|
|
catch (Exception ex) {
|
|
Throwable root = ex;
|
|
while (root.getCause() != null) {
|
|
root = root.getCause();
|
|
}
|
|
t.line("-- application FAILED to start");
|
|
t.line(" " + root.getClass().getSimpleName() + ": " + firstLine(root.getMessage()));
|
|
failures.put(mode, root.getClass().getSimpleName());
|
|
continue;
|
|
}
|
|
Apps.call(ctx, "/hello/ankur");
|
|
MeterRegistry registry = ctx.getBean(MeterRegistry.class);
|
|
Apps.awaitHttpMeter(registry);
|
|
t.line("-- meters whose name contains 'http':");
|
|
List<String> found = Apps.httpMeters(registry);
|
|
meters.put(mode, found);
|
|
found.forEach(l -> t.line(" " + l));
|
|
ctx.close();
|
|
Thread.sleep(500);
|
|
t.line("-- span-related strings in the OTLP trace payload (" + sink.hits().size() + " request(s)):");
|
|
sink.hits().stream()
|
|
.flatMap(h -> h.strings().stream())
|
|
.filter(s -> s.contains("hello") || s.contains("GET") || s.contains("get "))
|
|
.distinct()
|
|
.forEach(s -> t.line(" " + s));
|
|
}
|
|
}
|
|
t.write();
|
|
if (Transcript.is42()) {
|
|
assertThat(meters.get("(property not set)")).anyMatch(l -> l.startsWith("http.server.requests "));
|
|
assertThat(meters.get("micrometer")).anyMatch(l -> l.startsWith("http.server.requests "));
|
|
assertThat(meters.get("opentelemetry")).anyMatch(l -> l.startsWith("http.server.request.duration "));
|
|
assertThat(meters.get("opentelemetry")).noneMatch(l -> l.startsWith("http.server.requests "));
|
|
}
|
|
if (Transcript.LABEL.equals("4.2.0-RC1")) {
|
|
// Pinned on purpose: when this stops failing, the RC1 section of the article is stale.
|
|
assertThat(failures).containsKey("open-telemetry");
|
|
assertThat(failures).doesNotContainKey("opentelemetry");
|
|
}
|
|
else if (!Transcript.is42()) {
|
|
assertThat(failures).isEmpty();
|
|
assertThat(meters.get("opentelemetry")).anyMatch(l -> l.startsWith("http.server.requests "));
|
|
}
|
|
}
|
|
|
|
private static String firstLine(String s) {
|
|
if (s == null) {
|
|
return "";
|
|
}
|
|
int nl = s.indexOf('\n');
|
|
String line = nl < 0 ? s : s.substring(0, nl);
|
|
return line.length() > 260 ? line.substring(0, 260) + "..." : line;
|
|
}
|
|
}
|