Prompt
How do I map service dependencies from distributed traces
Latest observation
To map service dependencies from distributed traces, you typically turn each trace into a set of parent→child service call edges, then aggregate those edges across many traces to build a dependency graph.
Basic idea
A distributed trace is a tree or DAG of spans. Each span usually has:
trace_idspan_idparent_span_idservice.nameor equivalent- timing information
If span A is the parent of span B, and they belong to different services, then that implies a dependency:
- service of A → service of B
Over many traces, you count these edges to discover the service topology.
Step-by-step approach
1) Normalize span data
Make sure you can extract:
- Trace ID
- Span ID
- Parent span ID
- Service name
- Start/end time
Common sources:
- OpenTelemetry
- Jaeger
- Zipkin
- Honeycomb
- Datadog traces
2) Build parent-child relationships per trace
For each trace:
- group spans by
trace_id - index spans by
span_id - for each span, find its parent via
parent_span_id
3) Convert span relationships to service edges
For each parent-child span pair:
- let
src = parent.service.name - let
dst = child.service.name - if
src != dst, record edgesrc -> dst
You can optionally deduplicate edges within a trace if you only want one edge per service pair per trace.
4) Aggregate across traces
Count:
- number of calls from
srctodst - total latency
- error rate
- percentiles
- number of unique traces containing the edge
This gives you a weighted service dependency graph.
5) Visualize the graph
Represent:
- nodes = services
- edges = dependencies
- edge thickness = call volume
- edge color = error rate or latency
Tools:
- Graphviz
- Gephi
- D3.js
- Cytoscape
- service maps in observability platforms
Example
Suppose a trace contains spans:
| span_id | parent_span_id | service |
|---|---|---|
| s1 | null | api-gateway |
| s2 | s1 | auth-service |
| s3 | s1 | orders-service |
| s4 | s3 | inventory-service |
Then the service edges are:
- api-gateway → auth-service
- api-gateway → orders-service
- orders-service → inventory-service
If this pattern appears in many traces, aggregate counts might look like:
| source | target | count |
|---|---|---|
| api-gateway | auth-service | 1200 |
| api-gateway | orders-service | 980 |
| orders-service | inventory-service | 850 |
Important considerations
Same-service spans
Not every parent-child span implies a service dependency if both spans are in the same service. Usually you ignore those for service maps.
Async messaging
For queues, pub/sub, and event-driven systems, parent-child relationships may not always be preserved directly. You may need to infer dependencies from:
- messaging span attributes
- producer/consumer semantics
- headers propagated across messages
- span kind (
PRODUCER,CONSUMER)
A dependency may then be:
- service A publishes to topic X
- service B consumes from topic X
Batch / fan-out / retries
A single trace can show multiple edges to the same target service. Decide whether to:
- count every span-level invocation
- count unique service pairs per trace
- treat retries separately or collapse them
Cross-trace causality
Traditional traces show causality within one trace only. If you need end-to-end service dependency across asynchronous boundaries, you may need additional correlation IDs or message tracing.
Sampling
If traces are sampled, your graph is an estimate. Ensure sampling is sufficient or weighted appropriately.
Practical algorithm
Pseudocode:
from collections import defaultdict
edges = defaultdict(int)
for trace in traces:
spans = {span.span_id: span for span in trace.spans}
for span in trace.spans:
if span.parent_span_id is None:
continue
parent = spans.get(span.parent_span_id)
if not parent:
continue
if parent.service != span.service:
edges[(parent.service, span.service)] += 1
To add latency aggregation:
edge_stats = defaultdict(lambda: {"count": 0, "latency_sum": 0})
for trace in traces:
spans = {span.span_id: span for span in trace.spans}
for span in trace.spans:
if span.parent_span_id is None:
continue
parent = spans.get(span.parent_span_id)
if not parent:
continue
if parent.service != span.service:
key = (parent.service, span.service)
edge_stats[key]["count"] += 1
edge_stats[key]["latency_sum"] += span.duration_ms
If you already use OpenTelemetry
OpenTelemetry span fields commonly used:
service.nametrace_idspan_idparent_span_idspan.kindpeer.serviceor network attributesrpc.system,http.route,messaging.system
For service dependency mapping, service.name plus parent-child relationships is the main requirement.
Recommended output schema
A useful edge record might include:
source_servicetarget_servicecall_countunique_trace_countavg_latency_msp95_latency_mserror_counterror_raterelationship_type(sync,async,db, etc.)
Summary
To map service dependencies from traces:
- Group spans by trace
- Link spans through
parent_span_id - Convert cross-service parent→child span pairs into service edges
- Aggregate counts and performance metrics across traces
- Visualize as a dependency graph
If you want, I can also show:
- a SQL approach for trace data in a warehouse
- a Python example using OpenTelemetry/Jaeger JSON
- how to infer dependencies for Kafka / RabbitMQ / async workflows