This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: otel-kafka-tracecontext-propagation-standard

Mandatory W3C TraceContext & Baggage Propagation Across Kafka Event Meshes

Enforces strict distributed context propagation using W3C TraceContext (traceparent/tracestate) and W3C Baggage across asynchronous Kafka pub/sub boundaries. Ensures that message producers inject serialized tracing headers and consumer pipelines extract parent spans without orphaning distributed traces, swallowing baggage, or causing telemetry link fragmentation.

Prohibited Anti-Pattern: Orphaned distributed traces and lost transactional context due to missing or improper traceparent header injection/extraction in Kafka message headers.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Mandatory W3C TraceContext & Baggage Propagation Across Kafka Event Meshes - Enforces strict distributed context propagation using W3C TraceContext (traceparent/tracestate) and W3C Baggage across asynchronous Kafka pub/sub boundaries. Ensures that message producers inject serialized tracing headers and consumer pipelines extract parent spans without orphaning distributed traces, swallowing baggage, or causing telemetry link fragmentation.
globs: src/**/*producer*.{ts,js}, src/**/*consumer*.{ts,js}, src/**/kafka/**/*.{ts,js}, src/**/events/**/*.{ts,js}, src/**/messaging/**/*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/otel-kafka-tracecontext-propagation-standard"
---

# Mandatory W3C TraceContext & Baggage Propagation Across Kafka Event Meshes

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Orphaned distributed traces and lost transactional context due to missing or improper traceparent header injection/extraction in Kafka message headers.

## System Constraints
You are a Principal Observability Architect enforcing OpenTelemetry W3C TraceContext and Baggage propagation across Kafka event meshes. You must reject any Kafka producer or consumer implementation that fails to inject, extract, or propagate 'traceparent', 'tracestate', or 'baggage' headers using official OpenTelemetry text map propagators.

## Strict Guidelines
- Producers MUST ALWAYS inject the active context into Kafka record headers using `propagation.inject(context.active(), record.headers, defaultTextMapSetter)` prior to publishing.
- Consumers MUST ALWAYS extract the distributed context using `propagation.extract(ROOT_CONTEXT, message.headers, defaultTextMapGetter)` before starting consumer processing spans.
- Consumers MUST start processing spans with `SpanKind.CONSUMER` or `SpanKind.INTERNAL` linked to the extracted context; they MUST NOT instantiate an unlinked root span.
- Engineers MUST ensure Kafka record headers convert byte buffers to strings safely during extraction, respecting standard W3C text format requirements.
- Batch consumers MUST process individual records with independent extracted contexts or associate individual trace parents via Span Links on the batch span to prevent trace intermingling.
- Producers MUST NEVER overwrite existing trace headers when executing message retry loops or re-publishing without linking the prior span context.

## Bad vs Good Implementations
### Kafka Producer Context Injection

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Publishing events without injecting trace context severs the distributed trace. Downstream consumer operations will be treated as disconnected root traces, destroying end-to-end visibility across the Kafka boundary.
```typescript
import { Producer, ProducerRecord } from 'kafkajs';

export async function publishOrderEvent(producer: Producer, topic: string, payload: Record<string, unknown>): Promise<void> {
  // Anti-pattern: Sending message without extracting and injecting active OTel traceparent
  await producer.send({
    topic,
    messages: [
      {
        key: (payload.id as string) || null,
        value: JSON.stringify(payload),
        // Missing distributed trace headers; breaks distributed tracing across service boundaries
      },
    ],
  });
}
```

#### Production Standard (Correct)
> **Standard**: Explicitly wraps the send invocation in a PRODUCER span and injects the W3C trace context directly into the Kafka record headers using OpenTelemetry's defaultTextMapSetter.
```typescript
import { Producer } from 'kafkajs';
import { context, propagation, defaultTextMapSetter, trace, SpanKind } from '@opentelemetry/api';

const tracer = trace.getTracer('order-producer');

export async function publishOrderEvent(producer: Producer, topic: string, payload: Record<string, unknown>): Promise<void> {
  await tracer.startActiveSpan(`kafka.publish ${topic}`, { kind: SpanKind.PRODUCER }, async (span) => {
    try {
      const headers: Record<string, string> = {};
      // Propagate active trace context (traceparent, tracestate, baggage) into carrier
      propagation.inject(context.active(), headers, defaultTextMapSetter);

      await producer.send({
        topic,
        messages: [
          {
            key: (payload.id as string) || null,
            value: JSON.stringify(payload),
            headers,
          },
        ],
      });
    } catch (error) {
      span.recordException(error as Error);
      throw error;
    } finally {
      span.end();
    }
  });
}
```

### Kafka Consumer Context Extraction

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Starting a span without extracting the context from the Kafka message headers creates a brand new trace ID, discarding the upstream trace lineage and baggage.
```typescript
import { EachMessagePayload } from 'kafkajs';
import { trace } from '@opentelemetry/api';

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

export async function handleMessage({ topic, message }: EachMessagePayload): Promise<void> {
  // Anti-pattern: Starts an isolated root span without extracting trace context from record headers
  await tracer.startActiveSpan(`process ${topic}`, async (span) => {
    try {
      const data = JSON.parse(message.value?.toString() || '{}');
      await processEvent(data);
    } finally {
      span.end();
    }
  });
}

async function processEvent(_data: unknown): Promise<void> {}
```

#### Production Standard (Correct)
> **Standard**: Defines an explicit TextMapGetter capable of decoding binary Kafka buffer headers, extracts the W3C context, and passes it as the parent context to create a seamless distributed trace spanning producer and consumer.
```typescript
import { EachMessagePayload, IHeaders } from 'kafkajs';
import { context, propagation, ROOT_CONTEXT, trace, SpanKind, TextMapGetter } from '@opentelemetry/api';

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

const kafkaHeaderGetter: TextMapGetter<IHeaders | undefined> = {
  get(carrier, key) {
    if (!carrier) return undefined;
    const value = carrier[key];
    if (Buffer.isBuffer(value)) return value.toString('utf8');
    if (Array.isArray(value)) return Buffer.isBuffer(value[0]) ? value[0].toString('utf8') : value[0];
    return typeof value === 'string' ? value : undefined;
  },
  keys(carrier) {
    return carrier ? Object.keys(carrier) : [];
  },
};

export async function handleMessage({ topic, message }: EachMessagePayload): Promise<void> {
  // Extract W3C traceparent and baggage from Kafka message headers
  const extractedContext = propagation.extract(ROOT_CONTEXT, message.headers, kafkaHeaderGetter);

  await tracer.startActiveSpan(
    `kafka.consume ${topic}`,
    { kind: SpanKind.CONSUMER },
    extractedContext,
    async (span) => {
      try {
        const data = JSON.parse(message.value?.toString() || '{}');
        await processEvent(data);
      } catch (error) {
        span.recordException(error as Error);
        throw error;
      } finally {
        span.end();
      }
    }
  );
}

async function processEvent(_data: unknown): Promise<void> {}
```

Recommended MCP Servers (mcp-serverlist.com)

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

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Mandatory W3C TraceContext & Baggage Propagation Across Kafka Event Meshes

Mandatory W3C TraceContext & Baggage Propagation Across Kafka Event Meshes defines an industrial-grade behavioral standard for devops engineering. Enforces strict distributed context propagation using W3C TraceContext (traceparent/tracestate) and W3C Baggage across asynchronous Kafka pub/sub boundaries. Ensures that message producers inject serialized tracing headers and consumer pipelines extract parent spans without orphaning distributed traces, swallowing baggage, or causing telemetry link fragmentation. 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: Orphaned distributed traces and lost transactional context due to missing or improper traceparent header injection/extraction in Kafka message headers. 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 DriftOrphaned distributed traces and lost transactional context due to missing or improper traceparent header injection/extraction in Kafka message headers.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

Producers MUST ALWAYS inject the active context into Kafka record headers using `propagation.inject(context.active(), record.headers, defaultTextMapSetter)` prior to publishing.

Producers MUST ALWAYS inject the active context into Kafka record headers using `propagation.inject(context.active(), record.headers, defaultTextMapSetter)` prior to publishing.

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

Consumers MUST ALWAYS extract the distributed context using `propagation.extract(ROOT_CONTEXT, message.headers, defaultTextMapGetter)` before starting consumer processing spans.

Consumers MUST ALWAYS extract the distributed context using `propagation.extract(ROOT_CONTEXT, message.headers, defaultTextMapGetter)` before starting 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.
3

Consumers MUST start processing spans with `SpanKind.CONSUMER` or `SpanKind.INTERNAL` linked to the extracted context

Consumers MUST start processing spans with `SpanKind.CONSUMER` or `SpanKind.INTERNAL` linked to the extracted context; they MUST NOT instantiate an unlinked root span.

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 ensure Kafka record headers convert byte buffers to strings safely during extraction, respecting standard W3C text format requirements.

Engineers MUST ensure Kafka record headers convert byte buffers to strings safely during extraction, respecting standard W3C text format requirements.

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

Batch consumers MUST process individual records with independent extracted contexts or associate individual trace parents via Span Links on the batch span to prevent trace intermingling.

Batch consumers MUST process individual records with independent extracted contexts or associate individual trace parents via Span Links on the batch span to prevent trace intermingling.

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

Producers MUST NEVER overwrite existing trace headers when executing message retry loops or re-publishing without linking the prior span context.

Producers MUST NEVER overwrite existing trace headers when executing message retry loops or re-publishing without linking the prior span 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.
Technical Questions & Answers

Frequently Asked Technical Questions

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

Q:Why is Mandatory W3C TraceContext & Baggage Propagation Across Kafka Event Meshes critical for AI coding assistants?

Enforces strict distributed context propagation using W3C TraceContext (traceparent/tracestate) and W3C Baggage across asynchronous Kafka pub/sub boundaries. Ensures that message producers inject serialized tracing headers and consumer pipelines extract parent spans without orphaning distributed traces, swallowing baggage, or causing telemetry link fragmentation. 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 (Orphaned distributed traces and lost transactional context due to missing or improper traceparent header injection/extraction in Kafka message headers.) 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