This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: opentelemetry-w3c-trace-dlq-causality-guardrail

Deterministic W3C TraceContext Lineage and Span Link Causality in DLQ Lifecycles

Enforces deterministic distributed tracing lineage across asynchronous retry topics and Dead-Letter Queues (DLQ). Guarantees that W3C TraceContext headers are preserved, propagated through broker envelopes, and re-attached via OpenTelemetry Span Links rather than corruptive direct-parent hierarchy or context drops.

Prohibited Anti-Pattern: Trace Severing and Direct-Parent Graph Corruption During Asynchronous Message Retry and Dead-Letter Forwarding

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Deterministic W3C TraceContext Lineage and Span Link Causality in DLQ Lifecycles - Enforces deterministic distributed tracing lineage across asynchronous retry topics and Dead-Letter Queues (DLQ). Guarantees that W3C TraceContext headers are preserved, propagated through broker envelopes, and re-attached via OpenTelemetry Span Links rather than corruptive direct-parent hierarchy or context drops.
globs: src/**/*.ts, src/**/*.js, src/consumers/**/*.ts, src/producers/**/*.ts, src/messaging/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentelemetry-w3c-trace-dlq-causality-guardrail"
---

# Deterministic W3C TraceContext Lineage and Span Link Causality in DLQ Lifecycles

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Trace Severing and Direct-Parent Graph Corruption During Asynchronous Message Retry and Dead-Letter Forwarding

## System Constraints
You are a Principal Software Architect enforcing OpenTelemetry W3C TraceContext compliance across distributed event-driven systems. You must guarantee that asynchronous message failures, retries, and dead-letter queue (DLQ) forwardings maintain deterministic causal graphs. Prevent trace termination, header erasure, and misleading direct-parent inheritance by mandating OpenTelemetry Span Links and envelope metadata preservation.

## Strict Guidelines
- Engineers MUST ALWAYS preserve original W3C TraceContext headers ('traceparent' and 'tracestate') in the outbound broker message envelope or header collection when routing a failed message to a retry topic or DLQ.
- Engineers MUST NOT adopt the dead-lettered message's context as the direct parent span when processing messages out of a DLQ or delayed-retry processor; consumers MUST start a new root/independent span and establish causality using OpenTelemetry Span Links ('links: [{ context }]').
- Services MUST inject the active DLQ routing span's context into secondary diagnostic headers (e.g., 'x-dlq-routed-by', 'x-dlq-exception-timestamp') without mutating or overwriting the original incoming 'traceparent' and 'tracestate' attributes.
- Engineers MUST ALWAYS record broker and exception attributes on the intermediate routing span, including 'messaging.destination.name', 'messaging.operation=publish', 'messaging.kafka.destination.is_dlq=true', and the serialized error stack trace.
- Components NEVER drop propagation contexts or instantiate naked carrier records when orchestrating multi-step retry schedules (e.g., topic-5m -> topic-15m -> DLQ).

## Bad vs Good Implementations
### DLQ Routing and Reprocessing with Span Links and Header Preservation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The DLQ forwarding handler fails to propagate incoming W3C trace headers, breaking causality in downstream observability tools. When consuming from the DLQ, it processes the record without extracting prior trace contexts or linking them, creating an orphaned span with zero lineage to the original producer.
```typescript
import { Tracer } from '@opentelemetry/api';
import { Message, Producer } from './broker';

export async function handleFailedMessage(message: Message, error: Error, dlqProducer: Producer): Promise<void> {
  // Anti-Pattern: Headers are completely omitted, severing the W3C TraceContext.
  // The DLQ message creates a disconnected trace with no historical causality.
  await dlqProducer.send({
    topic: 'orders-dlq',
    messages: [{
      value: message.value,
      headers: {
        'error-message': error.message
      }
    }]
  });
}

export async function consumeDlqMessage(tracer: Tracer, message: Message): Promise<void> {
  // Anti-Pattern: Treating DLQ retry as a direct child span stretches root trace duration
  // across hours/days and breaks APM SLAs.
  await tracer.startActiveSpan('process-dlq-order', async (span) => {
    await processOrderPayload(message.value);
    span.end();
  });
}
```

#### Production Standard (Correct)
> **Standard**: The DLQ forwarding handler preserves the original trace context headers while creating an explicit producer span for the routing event. When consuming from the DLQ, a new root-level consumer span is initiated under `context.ROOT_CONTEXT` with a `SpanLink` targeting the original failure context. This avoids trace duration distortion over long retry delays while preserving bidirectional causality in OTel-compliant backends.
```typescript
import { context, propagation, trace, SpanKind, SpanStatusCode } from '@opentelemetry/api';
import { Message, Producer } from './broker';

export async function handleFailedMessage(message: Message, error: Error, dlqProducer: Producer): Promise<void> {
  const tracer = trace.getTracer('order-dlq-router');
  
  await tracer.startActiveSpan('orders.route-to-dlq', { kind: SpanKind.PRODUCER }, async (span) => {
    try {
      span.recordException(error);
      span.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
      span.setAttribute('messaging.destination.name', 'orders-dlq');
      span.setAttribute('messaging.operation', 'publish');

      // Preserve existing headers and inject current context for DLQ dispatch
      const carrierHeaders: Record<string, string> = { ...message.headers };
      if (message.headers['traceparent']) {
        carrierHeaders['x-original-traceparent'] = String(message.headers['traceparent']);
      }
      propagation.inject(context.active(), carrierHeaders);

      await dlqProducer.send({
        topic: 'orders-dlq',
        messages: [{
          value: message.value,
          headers: carrierHeaders
        }]
      });
    } finally {
      span.end();
    }
  });
}

export async function consumeDlqMessage(message: Message): Promise<void> {
  const tracer = trace.getTracer('dlq-consumer');
  
  // Extract origin and routing contexts to connect causality via Span Links
  const extractedContext = propagation.extract(context.active(), message.headers);
  const linkedSpanContext = trace.getSpanContext(extractedContext);

  const links = linkedSpanContext && linkedSpanContext.isValid
    ? [{ context: linkedSpanContext, attributes: { 'causality.type': 'dead_letter_replay' } }]
    : [];

  // Root-level span for replay lifecycle, causally linked to avoid skewing original trace duration
  await tracer.startActiveSpan(
    'orders-dlq.process',
    { kind: SpanKind.CONSUMER, links },
    context.ROOT_CONTEXT,
    async (span) => {
      try {
        await processOrderPayload(message.value);
        span.setStatus({ code: SpanStatusCode.OK });
      } catch (err: any) {
        span.recordException(err);
        span.setStatus({ code: SpanStatusCode.ERROR, message: err.message });
        throw err;
      } finally {
        span.end();
      }
    }
  );
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Deterministic W3C TraceContext Lineage and Span Link Causality in DLQ Lifecycles

Deterministic W3C TraceContext Lineage and Span Link Causality in DLQ Lifecycles defines an industrial-grade behavioral standard for devops engineering. Enforces deterministic distributed tracing lineage across asynchronous retry topics and Dead-Letter Queues (DLQ). Guarantees that W3C TraceContext headers are preserved, propagated through broker envelopes, and re-attached via OpenTelemetry Span Links rather than corruptive direct-parent hierarchy or context drops. 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: Trace Severing and Direct-Parent Graph Corruption During Asynchronous Message Retry and Dead-Letter Forwarding 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 DriftTrace Severing and Direct-Parent Graph Corruption During Asynchronous Message Retry and Dead-Letter ForwardingCritical
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 preserve original W3C TraceContext headers ('traceparent' and 'tracestate') in the outbound broker message envelope or header collection when routing a failed message to a retry topic or DLQ.

Engineers MUST ALWAYS preserve original W3C TraceContext headers ('traceparent' and 'tracestate') in the outbound broker message envelope or header collection when routing a failed message to a retry topic or DLQ.

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 adopt the dead-lettered message's context as the direct parent span when processing messages out of a DLQ or delayed-retry processor

Engineers MUST NOT adopt the dead-lettered message's context as the direct parent span when processing messages out of a DLQ or delayed-retry processor; consumers MUST start a new root/independent span and establish causality using OpenTelemetry Span Links ('links: [{ context }]').

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 inject the active DLQ routing span's context into secondary diagnostic headers (e.g., 'x-dlq-routed-by', 'x-dlq-exception-timestamp') without mutating or overwriting the original incoming 'traceparent' and 'tracestate' attributes.

Services MUST inject the active DLQ routing span's context into secondary diagnostic headers (e.g., 'x-dlq-routed-by', 'x-dlq-exception-timestamp') without mutating or overwriting the original incoming 'traceparent' and 'tracestate' attributes.

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

Engineers MUST ALWAYS record broker and exception attributes on the intermediate routing span, including 'messaging.destination.name', 'messaging.operation=publish', 'messaging.kafka.destination.is_dlq=true', and the serialized error stack trace.

Engineers MUST ALWAYS record broker and exception attributes on the intermediate routing span, including 'messaging.destination.name', 'messaging.operation=publish', 'messaging.kafka.destination.is_dlq=true', and the serialized error stack trace.

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

Components NEVER drop propagation contexts or instantiate naked carrier records when orchestrating multi-step retry schedules (e.g., topic-5m -> topic-15m -> DLQ).

Components NEVER drop propagation contexts or instantiate naked carrier records when orchestrating multi-step retry schedules (e.g., topic-5m -> topic-15m -> DLQ).

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 Deterministic W3C TraceContext Lineage and Span Link Causality in DLQ Lifecycles critical for AI coding assistants?

Enforces deterministic distributed tracing lineage across asynchronous retry topics and Dead-Letter Queues (DLQ). Guarantees that W3C TraceContext headers are preserved, propagated through broker envelopes, and re-attached via OpenTelemetry Span Links rather than corruptive direct-parent hierarchy or context drops. 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 (Trace Severing and Direct-Parent Graph Corruption During Asynchronous Message Retry and Dead-Letter Forwarding) 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

Deterministic