Add otel-agent-vs-micrometer module: OpenTelemetry Java agent 2.34.0 vs Micrometer Tracing on Spring Boot 4.2.0, same app instrumented both ways, with real transcripts

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b7h4ZNF4BDUpJk3Zou
This commit is contained in:
Claude
2026-11-29 02:59:07 +05:30
parent cbcfb05e35
commit 658bc52e2b
31 changed files with 1435 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
#!/usr/bin/env bash
# Shared helpers. Source this; do not run it.
# Kills by recorded PID, never by pattern (a pattern that matches the calling shell's own
# command line kills the shell).
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
DIST="${DIST:-$ROOT/dist}"
OUT="$ROOT/output"
APP_PORT=18480
SINK_PORT=14418
TMPD="${TMPDIR_OTELCMP:-/tmp/otelcmp-run}"; mkdir -p "$TMPD"
build() {
mkdir -p "$DIST"
( cd "$ROOT" && mvn -B -q -DskipTests package ) >/dev/null
cp "$ROOT/app/target/orders-plain.jar" "$DIST/"
cp "$ROOT/sink/target/otlp-sink.jar" "$DIST/"
( cd "$ROOT" && mvn -B -q -DskipTests -Pmicrometer -pl app package ) >/dev/null
cp "$ROOT/app/target/orders-micrometer.jar" "$DIST/"
if [ ! -f "$DIST/agent.jar" ]; then
curl -sfL -o "$DIST/agent.jar" \
https://repo1.maven.org/maven2/io/opentelemetry/javaagent/opentelemetry-javaagent/2.34.0/opentelemetry-javaagent-2.34.0.jar
fi
}
start_sink() {
java -jar "$DIST/otlp-sink.jar" $SINK_PORT >$TMPD/sink.log 2>&1 &
SINK_PID=$!
for i in $(seq 1 30); do curl -s -o /dev/null -X POST localhost:$SINK_PORT/reset && return; sleep 0.5; done
echo "sink did not start" >&2; return 1
}
# start_app <jar-variant: plain|micrometer> <jvm args and -D properties...>
start_app() {
local variant="$1"; shift
java -DPORT=$APP_PORT "$@" -jar "$DIST/orders-$variant.jar" >$TMPD/app.log 2>&1 &
APP_PID=$!
for i in $(seq 1 120); do
curl -s -o /dev/null localhost:$APP_PORT/ping && return
sleep 0.5
done
echo "app did not start" >&2; tail -20 $TMPD/app.log >&2; return 1
}
stop_app() { if [ -n "${APP_PID:-}" ]; then kill "$APP_PID" 2>/dev/null || true; wait "$APP_PID" 2>/dev/null || true; fi; APP_PID=""; }
stop_sink() { if [ -n "${SINK_PID:-}" ]; then kill "$SINK_PID" 2>/dev/null || true; wait "$SINK_PID" 2>/dev/null || true; fi; SINK_PID=""; }
cleanup() { stop_app; stop_sink; }
# JVM flags for the ways of running the app. The export intervals (metrics 2 s, agent span batch 200 ms)
# are shortened so a transcript finishes in seconds; they are not the defaults.
MICROMETER_PROPS=(
-Dmanagement.opentelemetry.otlp.endpoint=http://localhost:$SINK_PORT
-Dmanagement.tracing.sampling.probability=1.0
-Dmanagement.otlp.metrics.export.step=2s
)
AGENT_PROPS=(
-javaagent:"$DIST/agent.jar"
-Dotel.service.name=orders
-Dotel.exporter.otlp.endpoint=http://localhost:$SINK_PORT
-Dotel.exporter.otlp.protocol=http/protobuf
-Dotel.logs.exporter=none
-Dotel.metric.export.interval=2000
-Dotel.bsp.schedule.delay=200
)
+52
View File
@@ -0,0 +1,52 @@
#!/usr/bin/env bash
# Sourced by run-all.sh (or run directly). Measures startup, throughput, latency and RSS for
# the same endpoints under six configurations. Everything shares two vCPUs, so read the result
# as "relative cost on this box", not as production numbers.
set -euo pipefail
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
trap cleanup EXIT
mkdir -p "$OUT"
F="$OUT/08-overhead.txt"
THREADS=4 WARM=5000 N=8000 ROUNDS=3
AGENT_METHODS='com.ankurm.otelcmp.PricingService[price];com.ankurm.otelcmp.AuditService[write]'
{
echo "# 08 Overhead: same jar family, same endpoints, $THREADS client threads, $WARM warm-up + $N measured requests x $ROUNDS rounds"
echo "# JDK $(java -version 2>&1 | awk -F'"' '/version/{print $2}'), $(nproc) vCPU shared by app, OTLP sink and load driver. Indicative only."
} > "$F"
# utime+stime of a process, in clock ticks (100 per second)
cpu_ticks() { awk '{print $14 + $15}' "/proc/$1/stat"; }
run_cfg() {
local label="$1" jar="$2"; shift 2
curl -s -X POST localhost:$SINK_PORT/reset >/dev/null
start_app "$jar" "$@"
sleep 4
{
echo; echo "== $label"
echo "CPU consumed by the JVM from process start to ready (+4 s idle): $(( $(cpu_ticks "$APP_PID") * 10 )) ms"
grep -o 'Started OrdersApplication in .*' $TMPD/app.log | sed 's/)$//; s/$/)/' | head -1
for ep in ping orders/1; do
echo "-- GET /$ep"
java "$ROOT/driver/LoadDriver.java" "http://localhost:$APP_PORT/$ep" $WARM 0 $THREADS 0 >/dev/null 2>&1
local c0 c1
c0=$(cpu_ticks "$APP_PID")
java "$ROOT/driver/LoadDriver.java" "http://localhost:$APP_PORT/$ep" 0 $N $THREADS $ROUNDS 2>&1 | grep -v 'Picked up'
c1=$(cpu_ticks "$APP_PID")
echo "app CPU per request: $(( (c1 - c0) * 10000 / (N * ROUNDS) )) us (utime+stime of the app JVM only, $(( N * ROUNDS )) requests)"
done
echo "-- RSS after the run: $(( $(ps -o rss= -p "$APP_PID") / 1024 )) MiB"
} >> "$F"
stop_app
}
start_sink
run_cfg "baseline: plain jar, no instrumentation" plain
run_cfg "agent, all spans sampled" plain "${AGENT_PROPS[@]}" -Dotel.instrumentation.methods.include="$AGENT_METHODS"
run_cfg "micrometer starter, all spans sampled" micrometer "${MICROMETER_PROPS[@]}"
run_cfg "agent, 10% sampling" plain "${AGENT_PROPS[@]}" -Dotel.instrumentation.methods.include="$AGENT_METHODS" -Dotel.traces.sampler=parentbased_traceidratio -Dotel.traces.sampler.arg=0.1
run_cfg "micrometer starter, 10% sampling" micrometer "${MICROMETER_PROPS[@]}" -Dmanagement.tracing.sampling.probability=0.1
run_cfg "micrometer starter + JDBC observation, all spans sampled" micrometer-jdbc "${MICROMETER_PROPS[@]}"
stop_sink
echo "overhead written to $F"
+28
View File
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
# Boot 4 split RestClient auto-configuration out of spring-boot-starter-webmvc: without
# spring-boot-starter-restclient there is no RestClient.Builder bean. This builds a throwaway copy
# of the app without that starter and captures the startup failure.
set -euo pipefail
source "$(dirname "$0")/lib.sh"
trap cleanup EXIT
mkdir -p "$OUT"
W="$ROOT/.tmp-norest"
rm -rf "$W" || true; mkdir -p "$W"
cp -r "$ROOT/app/src" "$W/src"
python3 - "$ROOT/app/pom.xml" "$W/pom.xml" <<'PY'
import re, sys
s = open(sys.argv[1]).read()
s = re.sub(r"\s*<dependency>\s*<groupId>org.springframework.boot</groupId>\s*<artifactId>spring-boot-starter-restclient</artifactId>\s*</dependency>", "", s, count=1)
open(sys.argv[2], "w").write(s)
PY
( cd "$W" && mvn -B -q -DskipTests package ) >/dev/null
{
echo "# 10 The same app built WITHOUT spring-boot-starter-restclient (Spring Boot 4.2.0)"
echo "# dependencies kept: spring-boot-starter-webmvc, -actuator, -jdbc, h2"
echo
# the JVM exits with status 1 on purpose here, so the pipeline's status is ignored
{ java -DPORT=$APP_PORT -jar "$W/target/orders-plain.jar" 2>&1 | grep -v 'Picked up' \
| sed -n '/APPLICATION FAILED TO START/,/Consider defining/p' | sed 's/^\*\*\*\*.*$/***************************/'; } || true
} > "$OUT/10-restclient-starter-missing.txt"
rm -rf "$W" || true
cat "$OUT/10-restclient-starter-missing.txt"
+144
View File
@@ -0,0 +1,144 @@
#!/usr/bin/env bash
# Regenerates every file in output/. Needs JDK 25, Maven 3.9, curl and network access to Maven Central.
# Timing-sensitive files (07, 08) will differ run to run; the span and metric files should not
# (apart from ordering of attributes and the exact lists noted in the post).
set -euo pipefail
source "$(dirname "$0")/lib.sh"
trap cleanup EXIT
mkdir -p "$OUT"
cd "$ROOT"
[ "${SKIP_BUILD:-0}" = 1 ] || build
# build() already made plain, micrometer and sink; the third variant adds the JDBC observation library
( cd "$ROOT" && mvn -B -q -DskipTests -Pmicrometer,jdbc -pl app package ) >/dev/null
cp "$ROOT/app/target/orders-micrometer-jdbc.jar" "$DIST/"
bash "$ROOT/scripts/restclient-missing.sh" >/dev/null
AGENT_METHODS='com.ankurm.otelcmp.PricingService[price];com.ankurm.otelcmp.AuditService[write]'
# snapshot <title> : print what the sink has received so far
snap() {
sleep 5
echo "== $1"; echo
echo "-- span tree (one line per span: name [kind, instrumentation scope])"
curl -s localhost:$SINK_PORT/spans
}
# traffic: warm the app, reset the sink, then send exactly the requests the transcript describes
traffic() {
curl -s -o /dev/null localhost:$APP_PORT/orders/1
sleep 8 # the Micrometer span exporter batches for 5 s; let the warm-up spans arrive, then discard them
curl -s -X POST localhost:$SINK_PORT/reset >/dev/null
curl -s -o /dev/null localhost:$APP_PORT/orders/1
curl -s -o /dev/null localhost:$APP_PORT/orders/1/audited
}
# ---- 01 agent --------------------------------------------------------------------------
start_sink
start_app plain "${AGENT_PROPS[@]}" -Dotel.instrumentation.methods.include="$AGENT_METHODS"
traffic
{
echo "# 01 OpenTelemetry Java agent 2.34.0 on the unmodified 'plain' jar (Spring Boot 4.2.0, JDK $(java -version 2>&1 | awk -F'"' '/version/{print $2}'))"
echo "# Requests sent after warm-up: GET /orders/1, then GET /orders/1/audited"
snap "spans"
echo; echo "-- attribute keys per span name"
curl -s localhost:$SINK_PORT/span-attrs
} > "$OUT/01-agent-spans.txt"
{
echo "# 09 What each approach puts in the Resource (who is this service?)"
echo "# Machine-specific attributes are left out: host.name, os.*, process.pid, process.executable.path,"
echo "# process.command_args and service.instance.id."
echo "== agent"; curl -s localhost:$SINK_PORT/resource | grep -v '^process.pid\|^service.instance.id\|^host.name\|^os\.\|^process.executable\|^process.command'
} > "$OUT/09-resource.txt"
curl -s localhost:$SINK_PORT/metrics > $TMPD/metrics-agent.txt
stop_app
# ---- 02 micrometer, default conventions -----------------------------------------------------
curl -s -X POST localhost:$SINK_PORT/reset >/dev/null
start_app micrometer "${MICROMETER_PROPS[@]}"
traffic
{
echo "# 02 Micrometer Tracing via spring-boot-starter-opentelemetry, Spring Boot 4.2.0, default conventions"
echo "# Requests sent after warm-up: GET /orders/1, then GET /orders/1/audited (audit.executor=raw)"
snap "spans"
echo; echo "-- attribute keys per span name"
curl -s localhost:$SINK_PORT/span-attrs
} > "$OUT/02-micrometer-spans.txt"
{ echo; echo "== micrometer"; curl -s localhost:$SINK_PORT/resource | grep -v '^service.instance.id'; } >> "$OUT/09-resource.txt"
curl -s localhost:$SINK_PORT/metrics > $TMPD/metrics-micrometer.txt
stop_app
# ---- 03 micrometer, OpenTelemetry conventions ------------------------------------------------------
curl -s -X POST localhost:$SINK_PORT/reset >/dev/null
start_app micrometer "${MICROMETER_PROPS[@]}" -Dmanagement.observations.conventions=opentelemetry
traffic
{
echo "# 03 Same Micrometer build with management.observations.conventions=opentelemetry (new in Spring Boot 4.2)"
snap "spans"
echo; echo "-- attribute keys per span name"
curl -s localhost:$SINK_PORT/span-attrs
sleep 1
echo; echo "-- HTTP metrics exported"
curl -s localhost:$SINK_PORT/metrics | grep -E '^scope|http\.' | grep -E 'http\.|^scope' | grep -B1 'http\.' | grep -v '^--'
} > "$OUT/03-micrometer-otel-conventions.txt"
curl -s localhost:$SINK_PORT/metrics > $TMPD/metrics-micrometer-otelconv.txt
stop_app
# ---- 04 metrics side by side -----------------------------------------------------------------
{
echo "# 04 Metrics received over OTLP, grouped by the instrumentation scope that produced them"
echo; echo "######## agent"; cat $TMPD/metrics-agent.txt
echo; echo "######## micrometer (default conventions)"; cat $TMPD/metrics-micrometer.txt
} > "$OUT/04-metrics.txt"
# ---- 05 thread hand-off -------------------------------------------------------------------------
{
echo "# 05 Work handed to another thread (GET /orders/1/audited submits AuditService.write to a pool)"
echo "# One trace is expected: SERVER span with audit.write below it. Count the traces."
for cfg in "agent|plain|raw" "micrometer|micrometer|raw" "micrometer|micrometer|spring"; do
IFS='|' read -r kind jar ex <<<"$cfg"
curl -s -X POST localhost:$SINK_PORT/reset >/dev/null
if [ "$kind" = agent ]; then
start_app plain "${AGENT_PROPS[@]}" -Dotel.instrumentation.methods.include="$AGENT_METHODS" -Daudit.executor=$ex
else
start_app micrometer "${MICROMETER_PROPS[@]}" -Daudit.executor=$ex
fi
curl -s -o /dev/null localhost:$APP_PORT/orders/1/audited
sleep 8; curl -s -X POST localhost:$SINK_PORT/reset >/dev/null
curl -s -o /dev/null localhost:$APP_PORT/orders/1/audited
sleep 5
echo; echo "== $kind, audit.executor=$ex"
curl -s localhost:$SINK_PORT/spans
stop_app
done
} > "$OUT/05-thread-handoff.txt"
# ---- 06 JDBC gap ---------------------------------------------------------------------------
curl -s -X POST localhost:$SINK_PORT/reset >/dev/null
start_app micrometer-jdbc "${MICROMETER_PROPS[@]}"
traffic
{
echo "# 06 Micrometer build plus net.ttddyy.observation:datasource-micrometer-spring-boot 2.3.1 (third-party)"
snap "spans"
echo; echo "-- attribute keys per span name"
curl -s localhost:$SINK_PORT/span-attrs | grep -A1 'connection\|query\|result-set'
} > "$OUT/06-micrometer-jdbc.txt"
stop_app
# ---- 07 both at once ------------------------------------------------------------------------
curl -s -X POST localhost:$SINK_PORT/reset >/dev/null
start_app micrometer "${MICROMETER_PROPS[@]}" -Dotel.service.name=orders "${AGENT_PROPS[@]}"
traffic
{
echo "# 07 Micrometer build AND the agent in the same JVM, both exporting to the same sink"
snap "spans"
echo; echo "-- attribute keys per span name"
curl -s localhost:$SINK_PORT/span-attrs
echo; echo "-- url.full of the lone POST client spans"
curl -s "localhost:$SINK_PORT/span-values?name=POST&key=url.full"
echo; echo "-- HTTP server metrics received (scope, name)"
curl -s localhost:$SINK_PORT/metrics | grep -E '^scope|http\.server' | grep -B1 'http\.server' | grep -v '^--'
} > "$OUT/07-both-at-once.txt"
stop_app
stop_sink
[ "${SKIP_OVERHEAD:-0}" = 1 ] || source "$(dirname "$0")/overhead.sh"
echo "functional outputs written to $OUT"