Repository navigation
Expand file tree
/
Copy pathExpDistributedTracing.java
More file actions
341 lines (281 loc) · 15 KB
/
Copy pathExpDistributedTracing.java
File metadata and controls
341 lines (281 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import java.util.*;
/**
* Simulación de Distributed Tracing estilo OpenTelemetry con Java puro.
*
* Conceptos demostrados:
* - Trace: árbol de spans que representa una operación end-to-end
* - Span: unidad de trabajo con traceId, spanId, parentSpanId y timings
* - Context propagation: traceId viaja en headers HTTP (traceparent)
* - SpanExporter: imprime el árbol de spans formateado
* - Análisis: detectar spans lentos en el árbol
*/
public class ExpDistributedTracing {
// ─────────────────────────────────────────────
// SPAN
// ─────────────────────────────────────calls───
// ─────────────────────────────────────────────
static class Span {
private final String traceId;
private final String spanId;
private final String parentSpanId; // null si es root span
private final String operationName;
private final String serviceName;
private final long startNanos;
private long endNanos = -1;
private final Map<String, String> attributes = new LinkedHashMap<>();
private String status = "OK";
Span(String traceId, String spanId, String parentSpanId,
String operationName, String serviceName) {
this.traceId = traceId;
this.spanId = spanId;
this.parentSpanId = parentSpanId;
this.operationName = operationName;
this.serviceName = serviceName;
this.startNanos = System.nanoTime();
}
void end() { endNanos = System.nanoTime(); }
void setStatus(String status) { this.status = status; }
void setAttribute(String key, String value) { attributes.put(key, value); }
long durationMicros() {
if (endNanos < 0) return -1;
return (endNanos - startNanos) / 1_000;
}
boolean isRoot() { return parentSpanId == null; }
String traceId() { return traceId; }
String spanId() { return spanId; }
String parentSpanId() { return parentSpanId; }
String operationName() { return operationName; }
String serviceName() { return serviceName; }
String status() { return status; }
Map<String, String> attributes() { return attributes; }
// Genera el header traceparent: version-traceId-spanId-flags (W3C TraceContext)
String traceparent() {
return String.format("00-%s-%s-01", traceId, spanId);
}
}
// ─────────────────────────────────────────────
// TRACER: crea y almacena spans
// ─────────────────────────────────────────────
static class Tracer {
private final String serviceName;
private final SpanExporter exporter;
// Lista global de spans (en producción: exportados async al collector)
private final List<Span> completedSpans;
Tracer(String serviceName, SpanExporter exporter, List<Span> completedSpans) {
this.serviceName = serviceName;
this.exporter = exporter;
this.completedSpans = completedSpans;
}
// Iniciar un span raíz (nuevo trace)
Span startRootSpan(String operationName) {
String traceId = generateId(16);
String spanId = generateId(8);
Span span = new Span(traceId, spanId, null, operationName, serviceName);
System.out.printf("[%s] → START span='%s' traceId='%s'%n",
serviceName, operationName, traceId);
return span;
}
// Iniciar un span hijo con el contexto del padre (propagado por traceparent)
Span startSpan(String operationName, String parentTraceparent) {
String[] parts = parentTraceparent.split("-");
String traceId = parts[1]; // misma traza
String parentSpanId = parts[2];
String spanId = generateId(8);
Span span = new Span(traceId, spanId, parentSpanId, operationName, serviceName);
System.out.printf("[%s] → START span='%s' parent='%s'%n",
serviceName, operationName, parentSpanId);
return span;
}
void endSpan(Span span) {
span.end();
completedSpans.add(span);
System.out.printf("[%s] ← END span='%s' duration=%dµs status=%s%n",
serviceName, span.operationName(), span.durationMicros(), span.status());
}
private String generateId(int hexChars) {
// ID corto y legible para la demo (no criptográfico)
StringBuilder sb = new StringBuilder();
Random rnd = new Random();
for (int i = 0; i < hexChars; i++) {
sb.append(Integer.toHexString(rnd.nextInt(16)));
}
return sb.toString();
}
}
// ─────────────────────────────────────────────
// SPAN EXPORTER: imprime el árbol de traza
// ─────────────────────────────────────────────
static class SpanExporter {
private final List<Span> spans; // referencia al mismo store del tracer
SpanExporter(List<Span> spans) {
this.spans = spans;
}
void printTrace(String traceId) {
System.out.println("\n" + "═".repeat(65));
System.out.printf(" TRACE %s%n", traceId);
System.out.println("═".repeat(65));
// Encontrar el root span
Optional<Span> rootOpt = spans.stream()
.filter(s -> s.traceId().equals(traceId) && s.isRoot())
.findFirst();
if (rootOpt.isEmpty()) {
System.out.println(" (trace no encontrado)");
return;
}
Span root = rootOpt.get();
long totalDuration = root.durationMicros();
printSpanTree(traceId, root.spanId(), 0, totalDuration);
System.out.println("─".repeat(65));
System.out.printf(" Total trace duration: %dµs%n", totalDuration);
}
private void printSpanTree(String traceId, String spanId, int depth, long totalDuration) {
Optional<Span> spanOpt = spans.stream()
.filter(s -> s.traceId().equals(traceId) && s.spanId().equals(spanId))
.findFirst();
if (spanOpt.isEmpty()) return;
Span span = spanOpt.get();
String indent = " " + " ".repeat(depth);
long dur = span.durationMicros();
double pct = totalDuration > 0 ? (dur * 100.0 / totalDuration) : 0;
String slowMark = dur > 500 ? " ⚡SLOW" : ""; // marcar spans lentos
System.out.printf("%s[%s] %s (%dµs, %.0f%%)%s%n",
indent, span.serviceName(), span.operationName(), dur, pct, slowMark);
if (!span.attributes().isEmpty()) {
span.attributes().forEach((k, v) ->
System.out.printf("%s attr: %s=%s%n", indent, k, v));
}
// Imprimir hijos recursivamente
spans.stream()
.filter(s -> s.traceId().equals(traceId) && spanId.equals(s.parentSpanId()))
.forEach(child -> printSpanTree(traceId, child.spanId(), depth + 1, totalDuration));
}
List<Span> findSlowSpans(String traceId, long thresholdMicros) {
return spans.stream()
.filter(s -> s.traceId().equals(traceId) && s.durationMicros() > thresholdMicros)
.sorted(Comparator.comparingLong(Span::durationMicros).reversed())
.toList();
}
}
// ─────────────────────────────────────────────
// SERVICIOS SIMULADOS
// ─────────────────────────────────────────────
static class ApiGatewayService {
private final Tracer tracer;
ApiGatewayService(Tracer tracer) { this.tracer = tracer; }
String handleRequest(String requestPath, ServiceA serviceA) {
Span span = tracer.startRootSpan("HTTP GET " + requestPath);
span.setAttribute("http.method", "GET");
span.setAttribute("http.path", requestPath);
span.setAttribute("http.client_ip", "10.0.0.42");
try {
// Llamar a service-A, pasando el contexto de traza
String result = serviceA.process(span.traceparent());
span.setAttribute("http.status_code", "200");
return result;
} finally {
tracer.endSpan(span);
}
}
}
static class ServiceA {
private final Tracer tracer;
ServiceA(Tracer tracer) { this.tracer = tracer; }
String process(String incomingTraceparent) {
Span span = tracer.startSpan("processOrder", incomingTraceparent);
span.setAttribute("service.component", "order-processor");
try {
// Simular algo de trabajo
simulateWork(200);
// Llamar a service-B
ServiceB serviceB = new ServiceB(new Tracer("service-b", null, extractSharedStore()));
String dbResult = serviceB.queryDatabase(span.traceparent());
return "order-processed: " + dbResult;
} finally {
tracer.endSpan(span);
}
}
// Hack para la demo: compartir la lista global de spans
private List<Span> extractSharedStore() {
// En una implementación real el tracer inyecta el store global
return GLOBAL_SPANS;
}
}
static class ServiceB {
private final Tracer tracer;
ServiceB(Tracer tracer) { this.tracer = tracer; }
String queryDatabase(String incomingTraceparent) {
Span span = tracer.startSpan("queryDatabase", incomingTraceparent);
span.setAttribute("db.system", "postgresql");
span.setAttribute("db.statement", "SELECT * FROM orders WHERE id=?");
try {
// Simular query lenta para demostrar detección de slow spans
simulateWork(800);
return "row{id=123, status=confirmed}";
} finally {
tracer.endSpan(span);
}
}
}
// Store global de spans para que todos los tracers de la demo compartan el mismo
static final List<Span> GLOBAL_SPANS = new ArrayList<>();
static void simulateWork(long micros) {
long nanos = micros * 1_000L;
long start = System.nanoTime();
// Busy-wait para simular duración sin Thread.sleep (precisión en µs)
while (System.nanoTime() - start < nanos) { /* spin */ }
}
// ─────────────────────────────────────────────
// MAIN
// ─────────────────────────────────────────────
public static void main(String[] args) {
System.out.println("═".repeat(65));
System.out.println(" DISTRIBUTED TRACING — Java puro (OpenTelemetry style)");
System.out.println("═".repeat(65));
SpanExporter exporter = new SpanExporter(GLOBAL_SPANS);
Tracer gatewayTracer = new Tracer("api-gateway", exporter, GLOBAL_SPANS);
Tracer serviceATracer = new Tracer("service-a", exporter, GLOBAL_SPANS);
ApiGatewayService gateway = new ApiGatewayService(gatewayTracer);
ServiceA serviceA = new ServiceA(serviceATracer);
// ── Demo: un request que atraviesa 3 servicios ─────────────────
System.out.println("\n── Ejecutando request: GET /api/orders/123 ──");
String result = gateway.handleRequest("/api/orders/123", serviceA);
System.out.printf(" Resultado: %s%n", result);
// Recuperar el traceId del root span para imprimir la traza
String traceId = GLOBAL_SPANS.stream()
.filter(Span::isRoot)
.findFirst()
.map(Span::traceId)
.orElse("unknown");
// ── Imprimir el árbol de traza ─────────────────────────────────
exporter.printTrace(traceId);
// ── Detectar spans lentos ──────────────────────────────────────
System.out.println("\n── Análisis: spans lentos (> 500µs) ──");
List<Span> slowSpans = exporter.findSlowSpans(traceId, 500);
if (slowSpans.isEmpty()) {
System.out.println(" No hay spans lentos");
} else {
slowSpans.forEach(s ->
System.out.printf(" SLOW: [%s] %s — %dµs%n",
s.serviceName(), s.operationName(), s.durationMicros()));
System.out.println(" → Investigar: " + slowSpans.get(0).serviceName() +
" / " + slowSpans.get(0).operationName());
}
// ── Mostrar context propagation ────────────────────────────────
System.out.println("\n── Context propagation via HTTP header ──");
Span rootSpan = GLOBAL_SPANS.stream().filter(Span::isRoot).findFirst().orElseThrow();
System.out.println(" Header propagado entre servicios:");
System.out.printf(" traceparent: %s%n", rootSpan.traceparent());
System.out.println(" Formato W3C: version-traceId-spanId-flags");
System.out.println(" → traceId es el mismo en todos los servicios del request");
System.out.println(" → spanId identifica el span actual (el hijo pone su propio spanId)");
System.out.println("\n" + "═".repeat(65));
System.out.println(" RESUMEN DISTRIBUTED TRACING");
System.out.println("═".repeat(65));
System.out.println(" traceId → único por request end-to-end (cross-service)");
System.out.println(" spanId → único por operación dentro de la traza");
System.out.println(" traceparent → header HTTP W3C para propagar contexto");
System.out.println(" Uso: identificar dónde se gasta el tiempo, ver cascadas de fallos");
System.out.println(" Herramientas: Jaeger, Zipkin, AWS X-Ray, Grafana Tempo");
System.out.println("═".repeat(65));
}
}