Add boot42-otel module: Spring Boot 4.2.0-RC1 OpenTelemetry conventions, shared OTLP settings and MDC keys vs 4.1.2

This commit is contained in:
2026-10-22 18:37:10 +00:00
parent 917d39fb65
commit 8252cba4c5
29 changed files with 958 additions and 0 deletions
@@ -0,0 +1,64 @@
package com.ankurm.otel;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.client.RestClient;
/** Starts the demo application on a random port with the given properties. */
final class Apps {
private Apps() {
}
static ConfigurableApplicationContext start(String... properties) {
List<String> props = new ArrayList<>(List.of("server.port=0", "spring.main.banner-mode=off"));
props.addAll(List.of(properties));
return new SpringApplicationBuilder(OtelApplication.class).web(WebApplicationType.SERVLET)
.properties(props.toArray(String[]::new))
.run();
}
static String call(ConfigurableApplicationContext ctx, String path) {
String port = ctx.getEnvironment().getProperty("local.server.port");
return RestClient.create().get().uri("http://localhost:" + port + path).retrieve().body(String.class);
}
/**
* The server observation stops after the response bytes are on the wire, so a client that
* reads the registry immediately can win the race and see nothing. Poll for up to 5 seconds.
*/
static void awaitHttpMeter(MeterRegistry registry) throws InterruptedException {
for (int i = 0; i < 100; i++) {
if (registry.getMeters().stream().anyMatch(m -> m.getId().getName().startsWith("http.server.request")
&& !m.getId().getName().endsWith(".active"))) {
return;
}
Thread.sleep(50);
}
}
/** One line per meter whose name mentions http: name, then sorted tag key=value pairs. */
static List<String> httpMeters(MeterRegistry registry) {
TreeSet<String> out = new TreeSet<>();
for (Meter m : registry.getMeters()) {
String name = m.getId().getName();
if (name.contains("http")) {
List<String> tags = new ArrayList<>();
for (Tag t : m.getId().getTags()) {
tags.add(t.getKey() + "=" + t.getValue());
}
tags.sort(String::compareTo);
out.add(name + " " + tags);
}
}
return new ArrayList<>(out);
}
}
@@ -0,0 +1,57 @@
package com.ankurm.otel;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import io.micrometer.core.instrument.MeterRegistry;
import org.junit.jupiter.api.Test;
import org.springframework.context.ConfigurableApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
/**
* The reference documentation says management.observations.conventions=open-telemetry. This
* starts the application once per spelling and records whether it starts and which server HTTP
* meter it ends up with. On 4.2.0-RC1 the documented spelling does not start.
*/
class ConventionSpellingsTest {
@Test
void spellings() throws Exception {
Transcript t = new Transcript("07-conventions-spellings");
Map<String, String> outcome = new LinkedHashMap<>();
for (String value : List.of("open-telemetry", "open_telemetry", "OPEN_TELEMETRY", "opentelemetry",
"openTelemetry", "micrometer", "MICROMETER")) {
String result;
try {
ConfigurableApplicationContext ctx = Apps.start("management.observations.conventions=" + value,
"management.otlp.metrics.export.enabled=false");
Apps.call(ctx, "/hello/x");
MeterRegistry registry = ctx.getBean(MeterRegistry.class);
Apps.awaitHttpMeter(registry);
result = "starts, server meter: " + registry.getMeters().stream()
.map(m -> m.getId().getName())
.filter(n -> n.startsWith("http.server.request") && !n.endsWith(".active"))
.distinct()
.sorted()
.toList();
ctx.close();
}
catch (Exception ex) {
result = "FAILS to start";
}
outcome.put(value, result);
t.line(String.format("%-16s -> %s", value, result));
}
t.write();
if (Transcript.LABEL.equals("4.2.0-RC1")) {
assertThat(outcome.get("open-telemetry")).isEqualTo("FAILS to start");
assertThat(outcome.get("opentelemetry")).contains("http.server.request.duration");
assertThat(outcome.get("micrometer")).contains("http.server.requests");
}
else if (!Transcript.is42()) {
assertThat(outcome.values()).allMatch(v -> v.contains("http.server.requests"));
}
}
}
@@ -0,0 +1,91 @@
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;
}
}
@@ -0,0 +1,88 @@
package com.ankurm.otel;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.context.ConfigurableApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Two smaller 4.2 changes that break dashboards and log queries rather than builds: the MDC keys
* that carry the trace and span ids, and the active-observation LongTaskTimer that is no longer
* created by default.
*/
@ExtendWith(OutputCaptureExtension.class)
class MdcAndLongTaskTimerTest {
@Test
void mdcKeys(CapturedOutput output) {
Transcript t = new Transcript("04-mdc-keys");
// The pattern reads both the default key names and the custom ones, so one log line shows
// which key the tracing bridge really populated.
ConfigurableApplicationContext ctx = Apps.start("management.otlp.metrics.export.enabled=false",
"management.tracing.mdc.trace-id-key=trace_id", "management.tracing.mdc.span-id-key=span_id",
"logging.pattern.correlation=default[%X{traceId:-}] custom[%X{trace_id:-}] ");
Apps.call(ctx, "/hello/ankur");
ctx.close();
String line = output.getAll().lines().filter(l -> l.contains("handling hello for ankur")).findFirst().orElseThrow();
String correlation = line.substring(line.indexOf("default["), line.indexOf("] : ") > 0 ? line.indexOf("] :") : line.length());
t.line("management.tracing.mdc.trace-id-key = trace_id");
t.line("management.tracing.mdc.span-id-key = span_id");
t.line("log pattern correlation part reads both MDC keys");
t.line("");
t.line("log line: " + line.replaceAll("^\\S+\\s+", "").replaceAll("(?<=\\[)[0-9a-f]{32}(?=\\])", "<32-hex-trace-id>"));
boolean defaultKeyFilled = line.matches(".*default\\[[0-9a-f]{32}\\].*");
boolean customKeyFilled = line.matches(".*custom\\[[0-9a-f]{32}\\].*");
t.line("default key traceId populated: " + defaultKeyFilled);
t.line("custom key trace_id populated: " + customKeyFilled);
if (Transcript.is42()) {
assertThat(customKeyFilled).isTrue();
assertThat(defaultKeyFilled).isFalse();
}
else {
assertThat(customKeyFilled).isFalse();
assertThat(defaultKeyFilled).isTrue();
}
t.write();
}
@Test
void activeLongTaskTimer() throws Exception {
Transcript t = new Transcript("05-active-long-task-timer");
for (String setting : List.of("(default)", "management.metrics.observations.include-active-long-task-timer=true")) {
ConfigurableApplicationContext ctx = setting.startsWith("(")
? Apps.start("management.otlp.metrics.export.enabled=false")
: Apps.start("management.otlp.metrics.export.enabled=false", setting);
Apps.call(ctx, "/hello/ankur");
MeterRegistry registry = ctx.getBean(MeterRegistry.class);
Apps.awaitHttpMeter(registry);
TreeSet<String> names = new TreeSet<>();
for (Meter m : registry.getMeters()) {
if (m.getId().getName().startsWith("http.server")) {
names.add(m.getId().getName() + " (" + m.getId().getType() + ")");
}
}
t.line("");
t.line("== " + setting);
names.forEach(n -> t.line(" " + n));
boolean active = names.stream().anyMatch(n -> n.startsWith("http.server.requests.active"));
t.line(" http.server.requests.active present: " + active);
if (Transcript.is42()) {
assertThat(active).isEqualTo(!setting.startsWith("("));
}
else if (setting.startsWith("(")) {
assertThat(active).isTrue();
}
ctx.close();
}
t.write();
}
}
@@ -0,0 +1,91 @@
package com.ankurm.otel;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.zip.GZIPInputStream;
import com.sun.net.httpserver.HttpServer;
/**
* A tiny OTLP/HTTP receiver: it accepts any POST, answers 200 with an empty protobuf body and
* remembers the path, a few headers and the (decompressed) payload bytes. Enough to see where an
* exporter really sends data, with which headers, and which strings are inside the payload.
*/
final class OtlpSink implements AutoCloseable {
record Hit(String path, String authorization, String xShared, String contentEncoding, String contentType, byte[] body) {
/** Printable ASCII runs of at least 6 characters, a poor man's protobuf string reader. */
List<String> strings() {
TreeSet<String> out = new TreeSet<>();
StringBuilder sb = new StringBuilder();
for (byte b : body) {
if (b >= 32 && b < 127) {
sb.append((char) b);
}
else {
flush(sb, out);
}
}
flush(sb, out);
return new ArrayList<>(out);
}
private static void flush(StringBuilder sb, TreeSet<String> out) {
if (sb.length() >= 6) {
out.add(sb.toString());
}
sb.setLength(0);
}
}
private final HttpServer server;
private final List<Hit> hits = new CopyOnWriteArrayList<>();
OtlpSink() throws IOException {
server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
server.createContext("/", exchange -> {
byte[] raw = exchange.getRequestBody().readAllBytes();
String enc = exchange.getRequestHeaders().getFirst("Content-Encoding");
byte[] body = raw;
if ("gzip".equalsIgnoreCase(enc)) {
try (InputStream in = new GZIPInputStream(new java.io.ByteArrayInputStream(raw))) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
in.transferTo(bos);
body = bos.toByteArray();
}
}
hits.add(new Hit(exchange.getRequestURI().getPath(), exchange.getRequestHeaders().getFirst("Authorization"),
exchange.getRequestHeaders().getFirst("X-Shared"), enc, exchange.getRequestHeaders().getFirst("Content-Type"), body));
exchange.getResponseHeaders().add("Content-Type", "application/x-protobuf");
exchange.sendResponseHeaders(200, -1);
exchange.close();
});
server.start();
}
int port() {
return server.getAddress().getPort();
}
String baseUrl() {
return "http://localhost:" + port();
}
List<Hit> hits() {
return List.copyOf(hits);
}
@Override
public void close() {
server.stop(0);
hits.clear();
}
}
@@ -0,0 +1,57 @@
package com.ankurm.otel;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import org.junit.jupiter.api.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.client.RestClient;
import static org.assertj.core.api.Assertions.assertThat;
/**
* What a dashboard query sees. Scrapes /actuator/prometheus after one request and lists the
* http_server* families and one labelled sample, for the default conventions and for the
* open-telemetry conventions (spelled the way RC1 accepts it).
*/
class PrometheusNamesTest {
@Test
void promNames() throws Exception {
Transcript t = new Transcript("06-prometheus-names");
List<String> modes = List.of("micrometer", "opentelemetry");
java.util.Map<String, List<String>> families = new java.util.LinkedHashMap<>();
for (String mode : modes) {
ConfigurableApplicationContext ctx = Apps.start("management.observations.conventions=" + mode,
"management.otlp.metrics.export.enabled=false");
Apps.call(ctx, "/hello/ankur");
io.micrometer.core.instrument.MeterRegistry registry = ctx.getBean(io.micrometer.core.instrument.MeterRegistry.class);
Apps.awaitHttpMeter(registry);
String port = ctx.getEnvironment().getProperty("local.server.port");
String body = RestClient.create().get().uri("http://localhost:" + port + "/actuator/prometheus")
.retrieve().body(String.class);
TreeSet<String> names = new TreeSet<>();
String sample = "";
for (String line : body.split("\n")) {
if (line.startsWith("http_server")) {
names.add(line.substring(0, line.indexOf('{') > 0 ? line.indexOf('{') : line.indexOf(' ')));
if (line.contains("_count{") && sample.isEmpty()) {
sample = line.substring(0, line.lastIndexOf(' '));
}
}
}
t.line("");
t.line("== management.observations.conventions=" + mode);
names.forEach(n -> t.line(" " + n));
t.line(" sample: " + sample);
families.put(mode, new ArrayList<>(names));
ctx.close();
}
t.write();
if (Transcript.is42()) {
assertThat(families.get("micrometer")).contains("http_server_requests_seconds_count");
assertThat(families.get("opentelemetry")).contains("http_server_request_duration_seconds_count");
}
}
}
@@ -0,0 +1,92 @@
package com.ankurm.otel;
import java.util.List;
import java.util.TreeSet;
import org.junit.jupiter.api.Test;
import org.springframework.context.ConfigurableApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
/**
* 4.2.0-M2 added management.opentelemetry.otlp.{endpoint,headers,compression}, shared by traces,
* logs and metrics. A local OTLP receiver shows which paths the exporters really call, with which
* headers, and what a signal-specific property does to the shared one.
*/
class SharedOtlpTest {
@Test
void sharedEndpointHeadersAndCompression() throws Exception {
Transcript t = new Transcript("02-shared-otlp-endpoint");
try (OtlpSink sink = new OtlpSink()) {
ConfigurableApplicationContext ctx = Apps.start(
"management.opentelemetry.otlp.endpoint=" + sink.baseUrl(),
"management.opentelemetry.otlp.headers.Authorization=Bearer shared-token",
"management.opentelemetry.otlp.compression=gzip",
"management.otlp.metrics.export.step=1s");
Apps.call(ctx, "/hello/ankur");
Thread.sleep(2500);
ctx.close();
Thread.sleep(500);
t.line("management.opentelemetry.otlp.endpoint = <sink> (one property, no per-signal URLs)");
t.line("management.opentelemetry.otlp.headers = Authorization: Bearer shared-token");
t.line("management.opentelemetry.otlp.compression = gzip");
t.line("");
TreeSet<String> seen = new TreeSet<>();
for (OtlpSink.Hit h : sink.hits()) {
seen.add(String.format("POST %-12s Authorization=%s Content-Encoding=%s", h.path(), h.authorization(),
h.contentEncoding()));
}
t.line("requests that reached the sink:");
seen.forEach(l -> t.line(" " + l));
if (Transcript.is42()) {
assertThat(seen).anyMatch(l -> l.contains("/v1/traces") && l.contains("Bearer shared-token") && l.contains("gzip"));
assertThat(seen).anyMatch(l -> l.contains("/v1/metrics") && l.contains("Bearer shared-token") && l.contains("gzip"));
}
else {
assertThat(sink.hits()).isEmpty();
t.line(" (none: 4.1.2 ignores the shared properties and exports to its default localhost:4318)");
}
}
t.write();
}
@Test
void signalSpecificPropertyWins() throws Exception {
Transcript t = new Transcript("03-signal-specific-overrides-shared");
try (OtlpSink sink = new OtlpSink()) {
ConfigurableApplicationContext ctx = Apps.start(
"management.opentelemetry.otlp.endpoint=" + sink.baseUrl(),
"management.opentelemetry.otlp.headers.Authorization=Bearer shared-token",
"management.opentelemetry.otlp.headers.X-Shared=shared-only",
"management.opentelemetry.tracing.export.otlp.endpoint=" + sink.baseUrl() + "/custom/spans",
"management.opentelemetry.tracing.export.otlp.headers.Authorization=Bearer traces-token",
"management.otlp.metrics.export.step=1s");
Apps.call(ctx, "/hello/ankur");
Thread.sleep(2500);
ctx.close();
Thread.sleep(500);
t.line("shared endpoint = <sink>");
t.line("tracing.export.otlp.endpoint = <sink>/custom/spans");
t.line("shared header Authorization = Bearer shared-token");
t.line("shared header X-Shared = shared-only");
t.line("tracing.export.otlp.headers.Authorization = Bearer traces-token");
t.line("");
List<String> lines = sink.hits().stream()
.map(h -> String.format("POST %-14s Authorization=%s X-Shared=%s", h.path(), h.authorization(), h.xShared()))
.distinct()
.sorted()
.toList();
t.line("requests that reached the sink:");
lines.forEach(l -> t.line(" " + l));
if (Transcript.is42()) {
assertThat(lines).anyMatch(l -> l.contains("/custom/spans") && l.contains("Bearer traces-token")
&& l.contains("X-Shared=shared-only"));
assertThat(lines).anyMatch(l -> l.contains("/v1/metrics") && l.contains("Bearer shared-token")
&& l.contains("X-Shared=shared-only"));
assertThat(lines).noneMatch(l -> l.contains("/custom/spans/v1/traces"));
}
}
t.write();
}
}
@@ -0,0 +1,41 @@
package com.ankurm.otel;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
/** Collects lines and writes output/NN-name[-on-4.1.2].txt. Tests assert what they write. */
final class Transcript {
static final String LABEL = System.getProperty("boot.label", "4.2.0-RC1");
static boolean is42() {
return LABEL.startsWith("4.2");
}
private final List<String> lines = new ArrayList<>();
private final Path file;
Transcript(String name) {
String suffix = is42() ? "" : "-on-" + LABEL;
this.file = Path.of("output", name + suffix + ".txt");
line("# " + name + " (Spring Boot " + LABEL + ", JDK " + Runtime.version().feature() + ")");
}
Transcript line(String s) {
lines.add(s);
return this;
}
void write() {
try {
Files.createDirectories(file.getParent());
Files.write(file, lines);
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
}