This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: opentelemetry-span-link-async-event-streams

OpenTelemetry Span Link Lineage for Asynchronous Event Streams

Mandates non-hierarchical OpenTelemetry Span Links instead of direct parent-child span parenting in partitioned asynchronous event consumers (e.g., Kafka, RabbitMQ). Directly parenting consumer loops to incoming message trace contexts causes unbounded distributed trace graph bloat, skewed latency calculations, and incorrect causal dependency representations.

Prohibited Anti-Pattern: Hierarchical Parent-Child Span Context Chaining in Batched Event Stream Consumers

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry Span Link Lineage for Asynchronous Event Streams - Mandates non-hierarchical OpenTelemetry Span Links instead of direct parent-child span parenting in partitioned asynchronous event consumers (e.g., Kafka, RabbitMQ). Directly parenting consumer loops to incoming message trace contexts causes unbounded distributed trace graph bloat, skewed latency calculations, and incorrect causal dependency representations.
globs: src/**/*.{ts,tsx,js,jsx}, apps/**/*.{ts,tsx,js,jsx}, services/**/*.{ts,tsx,js,jsx}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentelemetry-span-link-async-event-streams"
---

# OpenTelemetry Span Link Lineage for Asynchronous Event Streams

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Hierarchical Parent-Child Span Context Chaining in Batched Event Stream Consumers

## System Constraints
You are a Principal Software Architect enforcing OpenTelemetry observability invariants for asynchronous stream processors (Kafka, RabbitMQ, SQS). You must prevent distributed trace graph bloat and false causal lineage by enforcing Span Links over direct parent-child hierarchy in consumer loops and batch operations.

## Strict Guidelines
- Engineers MUST ALWAYS extract incoming message trace contexts using OpenTelemetry's `propagation.extract` and pass them as non-hierarchical `links` (`SpanLink`) when starting consumer processing spans.
- Engineers MUST NOT assign an extracted message context as the direct parent context of a batch consumer loop span, as this artificially bundles unrelated upstream transactions into a single distorted trace.
- Services MUST record separate consumer spans for the polling/receiving phase (`messaging.operation.type = 'receive'`) and the discrete per-message execution phase (`messaging.operation.type = 'process'`).
- Code SHOULD include relevant messaging semantic attributes (`messaging.system`, `messaging.destination.name`, `messaging.consumer.group`, `messaging.message.id`) on all consumer and linked spans.
- Components NEVER drop propagation context headers (e.g., `traceparent`, `tracestate`) during message serialization, deserialization, or routing in dead-letter and retry queues.

## Bad vs Good Implementations
### Correlating Batch Kafka Consumer Messages via Span Links

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Parenting the entire batch to the first message's context fuses heterogeneous traces into a single giant trace graph. This inflates trace depth, distorts duration metrics in backends like Jaeger/Tempo, and breaks tracing for all subsequent messages that originated from distinct upstream producers.
```typescript
import { trace, context, defaultTextMapGetter } from '@opentelemetry/api';
import { EachBatchPayload } from 'kafkajs';

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

export async function handleOrderBatch({ batch, resolveOffset, heartbeat }: EachBatchPayload): Promise<void> {
  // ANTI-PATTERN: Extracting the first message context and setting it as the direct parent of the entire batch
  const firstMessage = batch.messages[0];
  const extractedContext = defaultTextMapGetter.get(firstMessage.headers, 'traceparent')
    ? trace.getSpanContext(trace.setSpan(context.active(), {} as any))
    : context.active();

  await context.with(extractedContext, async () => {
    const batchSpan = tracer.startSpan('process_order_batch');
    for (const message of batch.messages) {
      // Corrupts trace hierarchy: all subsequent messages appear as children of the first message's trace
      await processOrder(message);
      resolveOffset(message.offset);
    }
    batchSpan.end();
  });
}
```

#### Production Standard (Correct)
> **Standard**: Processes each message within its own discrete CONSUMER span and attaches the upstream context via OpenTelemetry `links`. This isolates latency, correctly attributes errors to specific messages, and keeps the distributed trace graph bounded and queryable.
```typescript
import { trace, context, propagation, SpanKind, Link, defaultTextMapGetter } from '@opentelemetry/api';
import { EachBatchPayload, KafkaMessage } from 'kafkajs';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

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

export async function handleOrderBatch({ batch, resolveOffset, heartbeat }: EachBatchPayload): Promise<void> {
  for (const message of batch.messages) {
    // Extract the upstream trace context from message headers safely
    const extractedContext = propagation.extract(context.active(), message.headers, defaultTextMapGetter);
    const upstreamSpanContext = trace.getSpan(extractedContext)?.spanContext();

    // Create non-hierarchical Span Link to preserve lineage without trace graph explosion
    const links: Link[] = upstreamSpanContext ? [{ context: upstreamSpanContext }] : [];

    const processSpan = tracer.startSpan(`orders.process`, {
      kind: SpanKind.CONSUMER,
      links,
      attributes: {
        [SemanticAttributes.MESSAGING_SYSTEM]: 'kafka',
        [SemanticAttributes.MESSAGING_DESTINATION_NAME]: batch.topic,
        [SemanticAttributes.MESSAGING_OPERATION]: 'process',
        [SemanticAttributes.MESSAGING_KAFKA_PARTITION]: batch.partition,
        [SemanticAttributes.MESSAGING_MESSAGE_ID]: message.offset,
      },
    });

    await context.with(trace.setSpan(context.active(), processSpan), async () => {
      try {
        await processOrder(message);
        resolveOffset(message.offset);
      } catch (error) {
        processSpan.recordException(error as Error);
        throw error;
      } finally {
        processSpan.end();
      }
    });
    await heartbeat();
  }
}
```

Recommended MCP Servers (mcp-serverlist.com)

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

Architectural Specification & Invariant Rationale

Architectural Invariant Design: OpenTelemetry Span Link Lineage for Asynchronous Event Streams

OpenTelemetry Span Link Lineage for Asynchronous Event Streams defines an industrial-grade behavioral standard for devops engineering. Mandates non-hierarchical OpenTelemetry Span Links instead of direct parent-child span parenting in partitioned asynchronous event consumers (e.g., Kafka, RabbitMQ). Directly parenting consumer loops to incoming message trace contexts causes unbounded distributed trace graph bloat, skewed latency calculations, and incorrect causal dependency representations. 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: Hierarchical Parent-Child Span Context Chaining in Batched Event Stream Consumers 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 DriftHierarchical Parent-Child Span Context Chaining in Batched Event Stream ConsumersCritical
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 message trace contexts using OpenTelemetry's `propagation.extract` and pass them as non-hierarchical `links` (`SpanLink`) when starting consumer processing spans.

Engineers MUST ALWAYS extract incoming message trace contexts using OpenTelemetry's `propagation.extract` and pass them as non-hierarchical `links` (`SpanLink`) when 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.
2

Engineers MUST NOT assign an extracted message context as the direct parent context of a batch consumer loop span, as this artificially bundles unrelated upstream transactions into a single distorted trace.

Engineers MUST NOT assign an extracted message context as the direct parent context of a batch consumer loop span, as this artificially bundles unrelated upstream transactions into a single distorted 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.
3

Services MUST record separate consumer spans for the polling/receiving phase (`messaging.operation.type = 'receive'`) and the discrete per-message execution phase (`messaging.operation.type = 'process'`).

Services MUST record separate consumer spans for the polling/receiving phase (`messaging.operation.type = 'receive'`) and the discrete per-message execution phase (`messaging.operation.type = 'process'`).

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 include relevant messaging semantic attributes (`messaging.system`, `messaging.destination.name`, `messaging.consumer.group`, `messaging.message.id`) on all consumer and linked spans.

Code SHOULD include relevant messaging semantic attributes (`messaging.system`, `messaging.destination.name`, `messaging.consumer.group`, `messaging.message.id`) on all consumer and linked 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.
5

Components NEVER drop propagation context headers (e.g., `traceparent`, `tracestate`) during message serialization, deserialization, or routing in dead-letter and retry queues.

Components NEVER drop propagation context headers (e.g., `traceparent`, `tracestate`) during message serialization, deserialization, or routing in dead-letter and retry queues.

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 Span Link Lineage for Asynchronous Event Streams critical for AI coding assistants?

Mandates non-hierarchical OpenTelemetry Span Links instead of direct parent-child span parenting in partitioned asynchronous event consumers (e.g., Kafka, RabbitMQ). Directly parenting consumer loops to incoming message trace contexts causes unbounded distributed trace graph bloat, skewed latency calculations, and incorrect causal dependency representations. 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 (Hierarchical Parent-Child Span Context Chaining in Batched Event Stream Consumers) 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