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:
@@ -0,0 +1,47 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.ankurm</groupId>
|
||||
<artifactId>otel-agent-vs-micrometer</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>otlp-sink</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.opentelemetry.proto</groupId>
|
||||
<artifactId>opentelemetry-proto</artifactId>
|
||||
<version>1.11.1-alpha</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.protobuf</groupId>
|
||||
<artifactId>protobuf-java</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>otlp-sink</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals><goal>shade</goal></goals>
|
||||
<configuration>
|
||||
<transformers combine.self="override">
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>com.ankurm.otelcmp.sink.OtlpSink</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,180 @@
|
||||
package com.ankurm.otelcmp.sink;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest;
|
||||
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
|
||||
import io.opentelemetry.proto.common.v1.AnyValue;
|
||||
import io.opentelemetry.proto.common.v1.KeyValue;
|
||||
import io.opentelemetry.proto.metrics.v1.Metric;
|
||||
import io.opentelemetry.proto.trace.v1.Span;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HexFormat;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
/**
|
||||
* A minimal OTLP/HTTP (protobuf) receiver that decodes what it is sent, so a transcript can show
|
||||
* real span names, kinds, parent links and attribute keys instead of grepping raw bytes.
|
||||
*
|
||||
* POST /v1/traces, /v1/metrics receive OTLP; /v1/logs is accepted and ignored
|
||||
* POST /reset forget everything
|
||||
* GET /spans span tree, one line per span
|
||||
* GET /span-attrs attribute KEYS per span name (values are not printed)
|
||||
* GET /span-values?name=POST&key=url.full the values of one attribute on spans with that name
|
||||
* GET /metrics metric names with type and unit, per instrumentation scope
|
||||
* GET /resource resource attribute keys and values of what was received
|
||||
*/
|
||||
public class OtlpSink {
|
||||
|
||||
record SpanRec(String scope, String traceId, String spanId, String parentId, String name,
|
||||
String kind, Map<String, String> attrs, long startNs, long endNs, String status) {}
|
||||
|
||||
record MetricRec(String scope, String name, String type, String unit) {}
|
||||
|
||||
static final List<SpanRec> SPANS = new CopyOnWriteArrayList<>();
|
||||
static final List<MetricRec> METRICS = new CopyOnWriteArrayList<>();
|
||||
static final Map<String, String> RESOURCE = new java.util.concurrent.ConcurrentHashMap<>();
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
int port = args.length > 0 ? Integer.parseInt(args[0]) : 4318;
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
|
||||
server.createContext("/v1/traces", ex -> receive(ex, true));
|
||||
server.createContext("/v1/metrics", ex -> receive(ex, false));
|
||||
server.createContext("/v1/logs", ex -> { ex.getRequestBody().readAllBytes(); reply(ex, 200, ""); });
|
||||
server.createContext("/reset", ex -> { SPANS.clear(); METRICS.clear(); RESOURCE.clear(); reply(ex, 200, "reset\n"); });
|
||||
server.createContext("/spans", ex -> reply(ex, 200, spanTree()));
|
||||
server.createContext("/span-attrs", ex -> reply(ex, 200, spanAttrs()));
|
||||
server.createContext("/metrics", ex -> reply(ex, 200, metricList()));
|
||||
server.createContext("/span-values", ex -> reply(ex, 200, spanValues(ex.getRequestURI().getQuery())));
|
||||
server.createContext("/resource", ex -> reply(ex, 200, resource()));
|
||||
server.start();
|
||||
System.out.println("otlp-sink listening on " + port);
|
||||
}
|
||||
|
||||
static void receive(HttpExchange ex, boolean traces) throws IOException {
|
||||
try (InputStream in = ex.getRequestBody()) {
|
||||
byte[] body = in.readAllBytes();
|
||||
if ("gzip".equalsIgnoreCase(ex.getRequestHeaders().getFirst("Content-Encoding"))) {
|
||||
body = new GZIPInputStream(new java.io.ByteArrayInputStream(body)).readAllBytes();
|
||||
}
|
||||
if (traces) {
|
||||
for (var rs : ExportTraceServiceRequest.parseFrom(body).getResourceSpansList()) {
|
||||
for (KeyValue kv : rs.getResource().getAttributesList()) RESOURCE.put(kv.getKey(), str(kv.getValue()));
|
||||
for (var ss : rs.getScopeSpansList()) {
|
||||
for (Span s : ss.getSpansList()) {
|
||||
Map<String, String> attrs = new TreeMap<>();
|
||||
for (KeyValue kv : s.getAttributesList()) attrs.put(kv.getKey(), str(kv.getValue()));
|
||||
SPANS.add(new SpanRec(ss.getScope().getName(), hex(s.getTraceId().toByteArray()),
|
||||
hex(s.getSpanId().toByteArray()), hex(s.getParentSpanId().toByteArray()),
|
||||
s.getName(), s.getKind().name().replace("SPAN_KIND_", ""), attrs,
|
||||
s.getStartTimeUnixNano(), s.getEndTimeUnixNano(), s.getStatus().getCode().name().replace("STATUS_CODE_", "")));
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (var rm : ExportMetricsServiceRequest.parseFrom(body).getResourceMetricsList()) {
|
||||
for (var sm : rm.getScopeMetricsList()) {
|
||||
for (Metric m : sm.getMetricsList()) {
|
||||
METRICS.add(new MetricRec(sm.getScope().getName(), m.getName(), m.getDataCase().name(), m.getUnit()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("decode failed: " + e);
|
||||
}
|
||||
reply(ex, 200, "");
|
||||
}
|
||||
|
||||
static String spanTree() {
|
||||
List<SpanRec> spans = new ArrayList<>(SPANS);
|
||||
Map<String, List<SpanRec>> byTrace = new LinkedHashMap<>();
|
||||
spans.stream().sorted(Comparator.comparingLong(SpanRec::startNs))
|
||||
.forEach(s -> byTrace.computeIfAbsent(s.traceId(), k -> new ArrayList<>()).add(s));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int n = 1;
|
||||
for (var e : byTrace.entrySet()) {
|
||||
sb.append("trace ").append(n++).append(" (").append(e.getValue().size()).append(" spans)\n");
|
||||
Map<String, SpanRec> ids = new LinkedHashMap<>();
|
||||
e.getValue().forEach(s -> ids.put(s.spanId(), s));
|
||||
for (SpanRec s : e.getValue()) {
|
||||
if (s.parentId().isEmpty() || !ids.containsKey(s.parentId())) {
|
||||
print(sb, s, e.getValue(), 1, s.parentId().isEmpty() ? "" : " [parent not in this trace]");
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
static void print(StringBuilder sb, SpanRec s, List<SpanRec> all, int depth, String note) {
|
||||
sb.append(" ".repeat(depth)).append(s.name()).append(" [").append(s.kind()).append(", scope=")
|
||||
.append(s.scope()).append(s.status().equals("UNSET") ? "" : ", status=" + s.status()).append("]").append(note).append('\n');
|
||||
all.stream().filter(c -> c.parentId().equals(s.spanId())).sorted(Comparator.comparingLong(SpanRec::startNs))
|
||||
.forEach(c -> print(sb, c, all, depth + 1, ""));
|
||||
}
|
||||
|
||||
static String spanAttrs() {
|
||||
Map<String, TreeSet<String>> byName = new TreeMap<>();
|
||||
for (SpanRec s : SPANS) byName.computeIfAbsent(s.name() + " [" + s.kind() + "]", k -> new TreeSet<>()).addAll(s.attrs().keySet());
|
||||
StringBuilder sb = new StringBuilder();
|
||||
byName.forEach((k, v) -> sb.append(k).append("\n ").append(String.join(", ", v)).append('\n'));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
static String spanValues(String query) {
|
||||
Map<String, String> q = new LinkedHashMap<>();
|
||||
for (String kv : query.split("&")) { String[] p = kv.split("=", 2); q.put(p[0], java.net.URLDecoder.decode(p[1], StandardCharsets.UTF_8)); }
|
||||
StringBuilder sb = new StringBuilder();
|
||||
new TreeSet<>(SPANS.stream().filter(s -> s.name().equals(q.get("name")))
|
||||
.map(s -> s.attrs().getOrDefault(q.get("key"), "(absent)")).toList()).forEach(v -> sb.append(v).append('\n'));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
static String metricList() {
|
||||
Map<String, TreeSet<String>> byScope = new TreeMap<>();
|
||||
for (MetricRec m : METRICS) {
|
||||
byScope.computeIfAbsent(m.scope().isEmpty() ? "(no scope name)" : m.scope(), k -> new TreeSet<>())
|
||||
.add(m.name() + " " + m.type().toLowerCase() + (m.unit().isEmpty() ? "" : " unit=" + m.unit()));
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
byScope.forEach((k, v) -> { sb.append("scope ").append(k).append(" (").append(v.size()).append(" metrics)\n"); v.forEach(x -> sb.append(" ").append(x).append('\n')); });
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
static String resource() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
new TreeMap<>(RESOURCE).forEach((k, v) -> sb.append(k).append(" = ").append(v).append('\n'));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
static String str(AnyValue v) {
|
||||
return switch (v.getValueCase()) {
|
||||
case STRING_VALUE -> v.getStringValue();
|
||||
case INT_VALUE -> Long.toString(v.getIntValue());
|
||||
case BOOL_VALUE -> Boolean.toString(v.getBoolValue());
|
||||
case DOUBLE_VALUE -> Double.toString(v.getDoubleValue());
|
||||
default -> v.getValueCase().name().toLowerCase();
|
||||
};
|
||||
}
|
||||
|
||||
static String hex(byte[] b) { return HexFormat.of().formatHex(b); }
|
||||
|
||||
static void reply(HttpExchange ex, int code, String body) throws IOException {
|
||||
byte[] out = body.getBytes(StandardCharsets.UTF_8);
|
||||
ex.sendResponseHeaders(code, out.length == 0 ? -1 : out.length);
|
||||
if (out.length > 0) ex.getResponseBody().write(out);
|
||||
ex.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user