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

Mandatory W3C Trace Context and Baggage Propagation Across Asynchronous Boundaries

Enforces strict extraction and injection of W3C Trace Context (traceparent, tracestate) and OpenTelemetry Baggage across asynchronous event brokers, background queues, and ephemeral micro-runtimes. Guarantees unbroken causal distributed telemetry and eliminates silent contextual leaks that break multi-tenant boundary isolation and request attribution.

Prohibited Anti-Pattern: Firing Disconnected Async Tasks or Queue Messages Without Explicit W3C Context Carrier Injection

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Mandatory W3C Trace Context and Baggage Propagation Across Asynchronous Boundaries - Enforces strict extraction and injection of W3C Trace Context (traceparent, tracestate) and OpenTelemetry Baggage across asynchronous event brokers, background queues, and ephemeral micro-runtimes. Guarantees unbroken causal distributed telemetry and eliminates silent contextual leaks that break multi-tenant boundary isolation and request attribution.
globs: src/**/*.{ts,js,mjs,cjs}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentelemetry-w3c-trace-context-baggage-propagation"
---

# Mandatory W3C Trace Context and Baggage Propagation Across Asynchronous Boundaries

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Firing Disconnected Async Tasks or Queue Messages Without Explicit W3C Context Carrier Injection

## System Constraints
You are a Principal Observability Architect enforcing strict OpenTelemetry W3C Distributed Context and Baggage Propagation. Any message dispatched to an external bus, queue (e.g., Kafka, SQS, RabbitMQ), or asynchronous worker runtime MUST explicitly inject current active Context and Baggage into headers. Any consumer or receiver MUST explicitly extract the incoming Context and execute handlers within that activated Context via trace.setSpanContext or context.with. Context drops, unlinked detached promises, and unpropagated tenant isolation metadata are unacceptable.

## Strict Guidelines
- Engineers MUST ALWAYS use OpenTelemetry's propagation.inject() with an explicit carrier interface before dispatching messages to Kafka, SQS, RabbitMQ, or any asynchronous message broker.
- Engineers MUST ALWAYS extract parent contexts at asynchronous boundaries using propagation.extract() and wrap processing logic inside context.with() to maintain causal distributed lineage.
- Engineers MUST NOT instantiate new root spans inside message consumers without explicitly linking the extracted incoming context via Link or creating the new span child of the extracted context.
- Services MUST serialize and propagate multi-tenant isolation attributes (e.g., tenantId, organizationId) via OpenTelemetry Baggage alongside traceparent headers.
- Components NEVER drop propagation headers when transforming, batching, or forwarding messages between internal worker micro-runtimes.

## Bad vs Good Implementations
### Async Message Publishing and Consumption Context Propagation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The producer publishes messages without injecting tracing headers (traceparent, baggage), completely severing the distributed trace. The consumer then starts an orphaned root span, destroying end-to-end latency visibility and losing tenant isolation context.
```typescript
import { trace } from '@opentelemetry/api';
import { Producer, Consumer, EachMessagePayload } from 'kafkajs';

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

export async function publishPaymentEvent(producer: Producer, payload: Record<string, unknown>): Promise<void> {
  // Anti-pattern: Dispatching message without injecting W3C traceparent or baggage headers
  await producer.send({
    topic: 'payments',
    messages: [{ value: JSON.stringify(payload) }],
  });
}

export async function registerPaymentConsumer(consumer: Consumer): Promise<void> {
  await consumer.run({
    eachMessage: async ({ message }: EachMessagePayload) => {
      // Anti-pattern: New root trace created, dropping upstream causal context and tenant baggage
      const span = tracer.startSpan('process_payment');
      try {
        console.log('Processing payment:', message.value?.toString());
      } finally {
        span.end();
      }
    },
  });
}
```

#### Production Standard (Correct)
> **Standard**: Uses propagation.inject to serialize W3C traceparent and baggage into Kafka headers, and propagation.extract combined with context.with on the receiving consumer. This guarantees unbroken causality and continuous propagation of isolation metadata across asynchronous boundaries.
```typescript
import { context, propagation, trace, SpanKind, defaultTextMapGetter, defaultTextMapSetter } from '@opentelemetry/api';
import { Producer, Consumer, EachMessagePayload, IHeaders } from 'kafkajs';

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

export async function publishPaymentEvent(producer: Producer, payload: Record<string, unknown>): Promise<void> {
  const headers: Record<string, string> = {};
  // Invariant: Inject active context and baggage into the message header carrier
  propagation.inject(context.active(), headers, defaultTextMapSetter);

  const kafkaHeaders: IHeaders = Object.entries(headers).reduce((acc, [k, v]) => {
    acc[k] = Buffer.from(v);
    return acc;
  }, {} as IHeaders);

  await producer.send({
    topic: 'payments',
    messages: [{ value: JSON.stringify(payload), headers: kafkaHeaders }],
  });
}

export async function registerPaymentConsumer(consumer: Consumer): Promise<void> {
  await consumer.run({
    eachMessage: async ({ message }: EachMessagePayload) => {
      const rawHeaders: Record<string, string> = {};
      if (message.headers) {
        for (const [key, value] of Object.entries(message.headers)) {
          if (value) {
            rawHeaders[key] = Buffer.isBuffer(value) ? value.toString('utf8') : String(value);
          }
        }
      }

      // Invariant: Extract context from carrier and run execution block inside extracted context
      const parentContext = propagation.extract(context.active(), rawHeaders, defaultTextMapGetter);

      await context.with(parentContext, async () => {
        const span = tracer.startSpan('process_payment', { kind: SpanKind.CONSUMER });
        try {
          console.log('Processing payment:', message.value?.toString());
        } 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: Mandatory W3C Trace Context and Baggage Propagation Across Asynchronous Boundaries

Mandatory W3C Trace Context and Baggage Propagation Across Asynchronous Boundaries defines an industrial-grade behavioral standard for devops engineering. Enforces strict extraction and injection of W3C Trace Context (traceparent, tracestate) and OpenTelemetry Baggage across asynchronous event brokers, background queues, and ephemeral micro-runtimes. Guarantees unbroken causal distributed telemetry and eliminates silent contextual leaks that break multi-tenant boundary isolation and request attribution. 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: Firing Disconnected Async Tasks or Queue Messages Without Explicit W3C Context Carrier Injection 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 DriftFiring Disconnected Async Tasks or Queue Messages Without Explicit W3C Context Carrier InjectionCritical
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 use OpenTelemetry's propagation.inject() with an explicit carrier interface before dispatching messages to Kafka, SQS, RabbitMQ, or any asynchronous message broker.

Engineers MUST ALWAYS use OpenTelemetry's propagation.inject() with an explicit carrier interface before dispatching messages to Kafka, SQS, RabbitMQ, or any asynchronous message broker.

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 ALWAYS extract parent contexts at asynchronous boundaries using propagation.extract() and wrap processing logic inside context.with() to maintain causal distributed lineage.

Engineers MUST ALWAYS extract parent contexts at asynchronous boundaries using propagation.extract() and wrap processing logic inside context.with() to maintain causal distributed lineage.

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

Engineers MUST NOT instantiate new root spans inside message consumers without explicitly linking the extracted incoming context via Link or creating the new span child of the extracted context.

Engineers MUST NOT instantiate new root spans inside message consumers without explicitly linking the extracted incoming context via Link or creating the new span child of the extracted 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.
4

Services MUST serialize and propagate multi-tenant isolation attributes (e.g., tenantId, organizationId) via OpenTelemetry Baggage alongside traceparent headers.

Services MUST serialize and propagate multi-tenant isolation attributes (e.g., tenantId, organizationId) via OpenTelemetry Baggage alongside traceparent headers.

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 headers when transforming, batching, or forwarding messages between internal worker micro-runtimes.

Components NEVER drop propagation headers when transforming, batching, or forwarding messages between internal worker micro-runtimes.

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 Mandatory W3C Trace Context and Baggage Propagation Across Asynchronous Boundaries critical for AI coding assistants?

Enforces strict extraction and injection of W3C Trace Context (traceparent, tracestate) and OpenTelemetry Baggage across asynchronous event brokers, background queues, and ephemeral micro-runtimes. Guarantees unbroken causal distributed telemetry and eliminates silent contextual leaks that break multi-tenant boundary isolation and request attribution. 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 (Firing Disconnected Async Tasks or Queue Messages Without Explicit W3C Context Carrier Injection) 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

Mandatory