This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: otel-tail-sampling-span-link-preservation-production-standard

OpenTelemetry Distributed Trace Tail-Sampling Invariant with Span Link Preservation

Enforces trace context propagation and span linking invariants across asynchronous boundary queues while guaranteeing deterministic trace routing across autonomous tail-sampling collector clusters. Mandates the use of OpenTelemetry span links for decoupled consumer operations alongside load-balancing collector routing keyed by trace ID to prevent causal graph fragmentation and orphaned downstream error traces.

Prohibited Anti-Pattern: Asymmetric Tail-Sampling Eviction and Fragmented DAG Parentage Across Message Queues

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry Distributed Trace Tail-Sampling Invariant with Span Link Preservation - Enforces trace context propagation and span linking invariants across asynchronous boundary queues while guaranteeing deterministic trace routing across autonomous tail-sampling collector clusters. Mandates the use of OpenTelemetry span links for decoupled consumer operations alongside load-balancing collector routing keyed by trace ID to prevent causal graph fragmentation and orphaned downstream error traces.
globs: src/**/*.ts, deploy/otel-collector*.yaml, src/telemetry/**/*.ts, src/consumers/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/otel-tail-sampling-span-link-preservation-production-standard"
---

# OpenTelemetry Distributed Trace Tail-Sampling Invariant with Span Link Preservation

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Asymmetric Tail-Sampling Eviction and Fragmented DAG Parentage Across Message Queues

## System Constraints
You are a Principal Observability and Infrastructure Architect enforcing OpenTelemetry standards for distributed tracing, asynchronous context propagation, and tail-sampling integrity. Your mandate is to prevent broken causal trace graphs, orphaned consumer traces, and asymmetric sampling drops across asynchronous boundaries like Kafka, SQS, and RabbitMQ.

## Strict Guidelines
- Engineers MUST ALWAYS initialize asynchronous consumer spans as new root spans linked via Span Links (`links: [{ context: extractedContext }]`) rather than continuing direct parent-child hierarchies over decoupled background job lifecycles.
- Engineers MUST NOT allow autonomous OpenTelemetry Collectors to execute tail-sampling decisions without deploying a deterministic upstream `loadbalancingexporter` routing strictly by `trace_id`.
- Services MUST extract W3C TraceContext (`traceparent`, `tracestate`) from incoming message carrier metadata into an extracted SpanContext before instantiating consumer processing spans.
- Code SHOULD inject baggage and queue ingestion timestamps into span link attributes to preserve cross-system causal latency tracking.
- Collectors NEVER evaluate tail-sampling drop/keep rules on isolated spans without configuring a minimum buffer duration that accounts for message broker transit latency and dead-letter queue retries.

## Bad vs Good Implementations
### Asynchronous Message Consumer Context Propagation with Span Links

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Adopting the extracted context as the active parent directly couples the consumer execution duration and lifecycle to the producer trace. When collector tail-sampling pools buffer and evaluate traces based on producer completion, downstream queue delay causes spans to hit separate collector instances or miss the tail-sampling window, creating severed traces.
```typescript
import { trace, context, defaultTextMapGetter } from '@opentelemetry/api';

interface MessagePayload {
  headers: Record<string, string>;
  data: unknown;
}

export async function processQueueMessage(payload: MessagePayload): Promise<void> {
  // ANTI-PATTERN: Direct parent context inheritance across an asynchronous decoupled boundary.
  // If the producer trace was already sampled out or timed out in collector tail-buffers,
  // downstream consumer spans and errors will be permanently orphaned or discarded.
  const extractedContext = trace.getTracer('worker').extract(
    context.active(),
    payload.headers,
    defaultTextMapGetter
  );

  await context.with(extractedContext, async () => {
    const tracer = trace.getTracer('worker');
    const span = tracer.startSpan('process-message');
    try {
      // Execute processing logic
      span.setStatus({ code: 1 }); // OK
    } catch (err: unknown) {
      span.recordException(err as Error);
      span.setStatus({ code: 2, message: (err as Error).message });
      throw err;
    } finally {
      span.end();
    }
  });
}
```

#### Production Standard (Correct)
> **Standard**: By starting a new root span and establishing a span link to the producer's SpanContext, the consumer trace receives independent sampling evaluation and avoids skewed trace durations. Collector tail-samplers can evaluate the consumer's errors independently while APM backends render the causal DAG via span links.
```typescript
import { trace, context, propagation, defaultTextMapGetter, SpanStatusCode } from '@opentelemetry/api';

interface MessagePayload {
  headers: Record<string, string>;
  data: unknown;
}

export async function processQueueMessage(payload: MessagePayload): Promise<void> {
  // PROD FIX: Extract context and preserve causal provenance via Span Links.
  // The consumer creates its own root trace with its own tail-sampling lifecycle
  // while retaining causal edge links to the original producer span.
  const parentContext = propagation.extract(context.active(), payload.headers, defaultTextMapGetter);
  const linkedSpanContext = trace.getSpanContext(parentContext);

  const tracer = trace.getTracer('queue-worker');
  const links = linkedSpanContext && linkedSpanContext.isValid
    ? [{ context: linkedSpanContext, attributes: { 'messaging.operation': 'receive' } }]
    : [];

  const consumerSpan = tracer.startSpan('processQueueMessage', {
    root: true,
    links,
    attributes: {
      'messaging.system': 'kafka',
      'messaging.destination_kind': 'queue'
    }
  });

  await context.with(trace.setSpan(context.active(), consumerSpan), async () => {
    try {
      // Execute business processing safely linked to caller
      consumerSpan.setStatus({ code: SpanStatusCode.OK });
    } catch (err: unknown) {
      const error = err instanceof Error ? err : new Error(String(err));
      consumerSpan.recordException(error);
      consumerSpan.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
      throw error;
    } finally {
      consumerSpan.end();
    }
  });
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: OpenTelemetry Distributed Trace Tail-Sampling Invariant with Span Link Preservation

OpenTelemetry Distributed Trace Tail-Sampling Invariant with Span Link Preservation defines an industrial-grade behavioral standard for devops engineering. Enforces trace context propagation and span linking invariants across asynchronous boundary queues while guaranteeing deterministic trace routing across autonomous tail-sampling collector clusters. Mandates the use of OpenTelemetry span links for decoupled consumer operations alongside load-balancing collector routing keyed by trace ID to prevent causal graph fragmentation and orphaned downstream error traces. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Asymmetric Tail-Sampling Eviction and Fragmented DAG Parentage Across Message Queues Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftAsymmetric Tail-Sampling Eviction and Fragmented DAG Parentage Across Message QueuesCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS initialize asynchronous consumer spans as new root spans linked via Span Links (`links

Engineers MUST ALWAYS initialize asynchronous consumer spans as new root spans linked via Span Links (`links: [{ context: extractedContext }]`) rather than continuing direct parent-child hierarchies over decoupled background job lifecycles.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT allow autonomous OpenTelemetry Collectors to execute tail-sampling decisions without deploying a deterministic upstream `loadbalancingexporter` routing strictly by `trace_id`.

Engineers MUST NOT allow autonomous OpenTelemetry Collectors to execute tail-sampling decisions without deploying a deterministic upstream `loadbalancingexporter` routing strictly by `trace_id`.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Services MUST extract W3C TraceContext (`traceparent`, `tracestate`) from incoming message carrier metadata into an extracted SpanContext before instantiating consumer processing spans.

Services MUST extract W3C TraceContext (`traceparent`, `tracestate`) from incoming message carrier metadata into an extracted SpanContext before instantiating consumer processing spans.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Code SHOULD inject baggage and queue ingestion timestamps into span link attributes to preserve cross-system causal latency tracking.

Code SHOULD inject baggage and queue ingestion timestamps into span link attributes to preserve cross-system causal latency tracking.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Collectors NEVER evaluate tail-sampling drop/keep rules on isolated spans without configuring a minimum buffer duration that accounts for message broker transit latency and dead-letter queue retries.

Collectors NEVER evaluate tail-sampling drop/keep rules on isolated spans without configuring a minimum buffer duration that accounts for message broker transit latency and dead-letter queue retries.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is OpenTelemetry Distributed Trace Tail-Sampling Invariant with Span Link Preservation critical for AI coding assistants?

Enforces trace context propagation and span linking invariants across asynchronous boundary queues while guaranteeing deterministic trace routing across autonomous tail-sampling collector clusters. Mandates the use of OpenTelemetry span links for decoupled consumer operations alongside load-balancing collector routing keyed by trace ID to prevent causal graph fragmentation and orphaned downstream error traces. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Asymmetric Tail-Sampling Eviction and Fragmented DAG Parentage Across Message Queues) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

OpenTelemetry