Decide after you've seen the whole trace — tail-based sampling and the decision window
To judge a trace on whole-trace facts you must route every span of that trace to the same decision point, hold them for a window, and accept that anything arriving after the window is judged on its own.
A head decision is made before anyone knows how the request turned out. The only way to keep exactly the interesting traces is to decide after the fact.
Scene 09
Decide after you've seen the whole trace
- Watch
- Try it
- Predict
- Capture
How do you keep exactly the traces worth keeping, when whether a trace is worth keeping — did anything error, was the whole checkout over 2 s — is only knowable after the request is over? You wait. Watch six spans of each trace arrive from Shopfront's services and get handed to two decision points, which hold them instead of writing them straight to storage. Count the filled segments on each box as the spans land, and watch both memory meters at the same time.
Highlighted lines are the ones running in the diagram right now.
# routing_key: traceID is the default for tracesdef route(span, boxes):if routing_key == "round_robin":i = next_index() # whoever is nextelse:i = consistent_hash(span.trace_id) % len(boxes)boxes[i].send(span)# membership change moves ~R/N of the trace ids
decision_wait = 30s # defaultnum_traces = 50_000 # circular bufferdef on_span(span):tid = span.trace_idif tid not in buffer: # judged ones were poppedif len(buffer) == num_traces:evict_oldest() # ..._dropped_too_earlybuffer[tid] = new_trace()start_timer(tid, decision_wait)buffer[tid].spans.append(span)
def decide(tid):trace = buffer.pop(tid)spans = trace.spanskept = Falseif "status_code" in policies:kept |= any(sp.status == ERROR for sp in spans)if "latency" in policies:kept |= duration(spans) > 2sif "probabilistic" in policies:kept |= hash(tid) < 0.01if kept:storage.write(spans)
Where this sits in Build a distributed tracing system (Jaeger / Zipkin style)
Scene 09 of 17, in the Sampling act — You can't keep it all; decide once, and don't lie.. To judge a trace on whole-trace facts you must route every span of it to the same decision point, hold them for a window, and accept that anything arriving later is judged on its own — the queue hop again.
Up next. Deciding late means every span of one trace has to meet somewhere outside the application. Before a span can reach anywhere at all, it has to leave the process — without making the customer wait.
All 17 scenes in Build a distributed tracing system (Jaeger / Zipkin style) · Every curriculum