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

OpenTelemetry W3C Trace Context and Baggage Preservation in Event-Driven DLQ Pipelines

Enforces strict extraction, preservation, and propagation of W3C traceparent, tracestate, and OpenTelemetry baggage across asynchronous message retries and Dead Letter Queue (DLQ) routing. Prevents distributed trace truncation and observability blind spots by mandating explicit Span Links and deterministic header propagation when redirecting poisoned payloads to dead-letter brokers.

Prohibited Anti-Pattern: Telemetry orphan routing where consumer failures publish to DLQs with blank headers or disconnected root spans.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry W3C Trace Context and Baggage Preservation in Event-Driven DLQ Pipelines - Enforces strict extraction, preservation, and propagation of W3C traceparent, tracestate, and OpenTelemetry baggage across asynchronous message retries and Dead Letter Queue (DLQ) routing. Prevents distributed trace truncation and observability blind spots by mandating explicit Span Links and deterministic header propagation when redirecting poisoned payloads to dead-letter brokers.
globs: src/**/*consumer*.{ts,js}, src/**/*producer*.{ts,js}, src/**/*dlq*.{ts,js}, src/**/*broker*.{ts,js}, src/**/*event*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/otel-w3c-trace-context-dlq-preservation"
---

# OpenTelemetry W3C Trace Context and Baggage Preservation in Event-Driven DLQ Pipelines

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Telemetry orphan routing where consumer failures publish to DLQs with blank headers or disconnected root spans.

## System Constraints
You are a Principal Software Architect enforcing OpenTelemetry W3C Trace Context and Distributed Tracing Invariants in event-driven systems. You must ensure that asynchronous message processing failures, retries, and dead-letter queue (DLQ) republish operations never sever the causal telemetry graph. Require that all broker consumers extract W3C headers (traceparent, tracestate, baggage), establish valid spans with appropriate SpanKind, and deterministically propagate both the active lineage and original failure context into dead-letter event metadata using OpenTelemetry Span Links and canonical header injection.

## Strict Guidelines
- Engineers MUST ALWAYS extract incoming W3C trace context (traceparent, tracestate) and baggage using OpenTelemetry TextMapPropagator before processing message payloads.
- Engineers MUST NOT initialize new, detached root traces when routing failed messages to retry topics or Dead Letter Queues (DLQs).
- Code MUST capture error attributes (exception.type, exception.message, exception.stacktrace) on the consumer span and record an error status before initiating DLQ transfer.
- Services MUST preserve original W3C metadata within reserved event headers (e.g., `x-original-traceparent`, `x-death-reason`) when mutating the active trace context during DLQ republication.
- Producers publishing to a DLQ from a separate remediation or batch context MUST associate the original message's trace context via OpenTelemetry Span Links.
- Components NEVER drop W3C baggage across message boundaries; baggage entries MUST be extracted, carried forward, and re-injected into outgoing DLQ carrier payloads.

## Bad vs Good Implementations
### DLQ Routing with Trace Disconnection vs. Deterministic W3C Link & Propagation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The failure handler drops incoming W3C trace headers, initializes an isolated span without parent extraction, and publishes the dead-letter payload without injecting W3C traceparent, tracestate, or baggage. This causes downstream DLQ consumers and incident investigators to lose all causal linkage to the originating client request.
```typescript
import { Kafka, EachMessagePayload } from 'kafkajs';
import { trace } from '@opentelemetry/api';

const tracer = trace.getTracer('payment-consumer');

export async function handlePaymentMessage(payload: EachMessagePayload, dlqProducer: any) {
  const { topic, partition, message } = payload;
  
  // ANTI-PATTERN: Completely ignores incoming W3C headers, generating an orphaned trace
  await tracer.startActiveSpan('process_payment', async (span) => {
    try {
      const event = JSON.parse(message.value?.toString() || '{}');
      await processTransaction(event);
      span.end();
    } catch (err: any) {
      span.recordException(err);
      span.end();
      
      // CRITICAL FLAW: DLQ message published with clean/empty headers and no trace linkage
      await dlqProducer.send({
        topic: 'payments-dlq',
        messages: [{
          key: message.key,
          value: message.value,
          headers: {
            'error-message': err.message
          }
        }]
      });
    }
  });
}
```

#### Production Standard (Correct)
> **Standard**: Extracts the incoming W3C carrier from the broker message, starts a consumer span within that context, and on failure creates a dedicated PRODUCER span linked back to the failed operation. Injects the updated W3C traceparent and baggage into the DLQ message while archiving the original context in `x-original-traceparent` for zero-loss auditability.
```typescript
import { EachMessagePayload } from 'kafkajs';
import {
  trace,
  propagation,
  context,
  SpanKind,
  SpanStatusCode,
  ROOT_CONTEXT,
  defaultTextMapGetter,
  defaultTextMapSetter
} from '@opentelemetry/api';

const tracer = trace.getTracer('payment-consumer');

export async function handlePaymentMessage(payload: EachMessagePayload, dlqProducer: any) {
  const { topic, partition, message } = payload;
  
  // Extract existing W3C TraceContext and Baggage from broker headers
  const stringHeaders: Record<string, string> = {};
  if (message.headers) {
    for (const [k, v] of Object.entries(message.headers)) {
      if (v) stringHeaders[k] = Buffer.isBuffer(v) ? v.toString('utf-8') : String(v);
    }
  }
  const extractedContext = propagation.extract(ROOT_CONTEXT, stringHeaders, defaultTextMapGetter);

  await tracer.startActiveSpan('payment_consumer.receive', { kind: SpanKind.CONSUMER }, extractedContext, async (span) => {
    try {
      const event = JSON.parse(message.value?.toString() || '{}');
      await processTransaction(event);
      span.setStatus({ code: SpanStatusCode.OK });
    } catch (err: any) {
      span.recordException(err);
      span.setStatus({ code: SpanStatusCode.ERROR, message: err.message });

      // Create DLQ publish span with explicit link to the failed consumption context
      const dlqCarrier: Record<string, string> = {
        ...stringHeaders,
        'x-original-traceparent': stringHeaders['traceparent'] ?? '',
        'x-dead-letter-reason': err.message,
        'x-dead-letter-timestamp': new Date().toISOString()
      };

      await tracer.startActiveSpan(
        'payment_consumer.send_dlq',
        { kind: SpanKind.PRODUCER, links: [{ context: span.spanContext() }] },
        context.active(),
        async (dlqSpan) => {
          try {
            // Deterministically re-inject active traceparent and baggage into DLQ payload
            propagation.inject(context.active(), dlqCarrier, defaultTextMapSetter);

            await dlqProducer.send({
              topic: 'payments-dlq',
              messages: [{
                key: message.key,
                value: message.value,
                headers: dlqCarrier
              }]
            });
            dlqSpan.setStatus({ code: SpanStatusCode.OK });
          } finally {
            dlqSpan.end();
          }
        }
      );
    } 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: OpenTelemetry W3C Trace Context and Baggage Preservation in Event-Driven DLQ Pipelines

OpenTelemetry W3C Trace Context and Baggage Preservation in Event-Driven DLQ Pipelines defines an industrial-grade behavioral standard for devops engineering. Enforces strict extraction, preservation, and propagation of W3C traceparent, tracestate, and OpenTelemetry baggage across asynchronous message retries and Dead Letter Queue (DLQ) routing. Prevents distributed trace truncation and observability blind spots by mandating explicit Span Links and deterministic header propagation when redirecting poisoned payloads to dead-letter brokers. 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: Telemetry orphan routing where consumer failures publish to DLQs with blank headers or disconnected root spans. 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 DriftTelemetry orphan routing where consumer failures publish to DLQs with blank headers or disconnected root spans.Critical
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 extract incoming W3C trace context (traceparent, tracestate) and baggage using OpenTelemetry TextMapPropagator before processing message payloads.

Engineers MUST ALWAYS extract incoming W3C trace context (traceparent, tracestate) and baggage using OpenTelemetry TextMapPropagator before processing message payloads.

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 initialize new, detached root traces when routing failed messages to retry topics or Dead Letter Queues (DLQs).

Engineers MUST NOT initialize new, detached root traces when routing failed messages to retry topics or Dead Letter Queues (DLQs).

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

Code MUST capture error attributes (exception.type, exception.message, exception.stacktrace) on the consumer span and record an error status before initiating DLQ transfer.

Code MUST capture error attributes (exception.type, exception.message, exception.stacktrace) on the consumer span and record an error status before initiating DLQ transfer.

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

Services MUST preserve original W3C metadata within reserved event headers (e.g., `x-original-traceparent`, `x-death-reason`) when mutating the active trace context during DLQ republication.

Services MUST preserve original W3C metadata within reserved event headers (e.g., `x-original-traceparent`, `x-death-reason`) when mutating the active trace context during DLQ republication.

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

Producers publishing to a DLQ from a separate remediation or batch context MUST associate the original message's trace context via OpenTelemetry Span Links.

Producers publishing to a DLQ from a separate remediation or batch context MUST associate the original message's trace context via OpenTelemetry Span Links.

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.
6

Components NEVER drop W3C baggage across message boundaries

Components NEVER drop W3C baggage across message boundaries; baggage entries MUST be extracted, carried forward, and re-injected into outgoing DLQ carrier payloads.

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 W3C Trace Context and Baggage Preservation in Event-Driven DLQ Pipelines critical for AI coding assistants?

Enforces strict extraction, preservation, and propagation of W3C traceparent, tracestate, and OpenTelemetry baggage across asynchronous message retries and Dead Letter Queue (DLQ) routing. Prevents distributed trace truncation and observability blind spots by mandating explicit Span Links and deterministic header propagation when redirecting poisoned payloads to dead-letter brokers. 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 (Telemetry orphan routing where consumer failures publish to DLQs with blank headers or disconnected root spans.) 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