This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: otel-tail-sampling-span-link-completeness-protocol

OpenTelemetry Tail-Sampling Span-Link Completeness Protocol

Enforces distributed trace graph integrity by mandating that OpenTelemetry Collector pipelines and messaging clients preserve and evaluate span-linked asynchronous contexts before enacting lossy trace drop decisions. Eliminates causal fragmentation between producer and consumer spans across distributed message queues.

Prohibited Anti-Pattern: Blind Tail-Sampling Drop of Fast Producer Traces Linked to Asynchronous Consumer Workflows

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry Tail-Sampling Span-Link Completeness Protocol - Enforces distributed trace graph integrity by mandating that OpenTelemetry Collector pipelines and messaging clients preserve and evaluate span-linked asynchronous contexts before enacting lossy trace drop decisions. Eliminates causal fragmentation between producer and consumer spans across distributed message queues.
globs: **/otel-collector*.yaml, **/otel-collector*.yml, src/**/*tracing*.ts, src/**/*telemetry*.ts, src/**/*messaging*.ts, src/**/*consumer*.ts, src/**/*producer*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/otel-tail-sampling-span-link-completeness-protocol"
---

# OpenTelemetry Tail-Sampling Span-Link Completeness Protocol

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Blind Tail-Sampling Drop of Fast Producer Traces Linked to Asynchronous Consumer Workflows

## System Constraints
You are a Principal Software Architect enforcing the OpenTelemetry Tail-Sampling Span-Link Completeness Protocol. You must ensure distributed trace graphs remain intact across asynchronous messaging boundaries by strictly maintaining span-link context and preventing premature, lossy trace-drop decisions in Collector pipelines.

## Strict Guidelines
- Engineers MUST ALWAYS capture and inject producer context into messaging payloads as explicit OpenTelemetry SpanLinks containing full TraceState and TraceFlags.
- Collector pipelines MUST NOT drop parent or producer traces with status 200 before evaluating downstream linked asynchronous consumer spans.
- Engineers MUST set tail-sampling 'decision_wait' intervals in the OpenTelemetry Collector to exceed the p99 message broker queue latency.
- Services MUST explicitly record the 'messaging.operation=receive' span linked directly to the extracted 'messaging.operation=publish' span context.
- Tail-sampling routing configurations MUST ensure traces sharing span-links or trace-state linkage tokens route to the same sampling Collector instance via trace-ID-consistent hashing.

## Bad vs Good Implementations
### Async Message Consumer Instrumentation with Span Links and Context Propagation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Creating a detached or newly rooted trace without extracting upstream span context via SpanLinks breaks causal graph coherence. If the producer span finishes quickly with HTTP 200, an upstream tail-sampling collector evaluates and drops it before the consumer span errors.
```typescript
import { trace, context } from '@opentelemetry/api';

export async function handleMessage(message: { payload: string; headers: Record<string, string> }) {
  const tracer = trace.getTracer('payment-service');
  // ANTI-PATTERN: Completely unlinked span or treating message consumption as a child span without preserving links.
  // Discards the sampling flag and breaks tail-sampling graph evaluation.
  return tracer.startActiveSpan('process_payment_message', async (span) => {
    try {
      await processMessagePayload(message.payload);
      span.setStatus({ code: 1 }); // OK
    } catch (err) {
      span.recordException(err as Error);
      span.setStatus({ code: 2 }); // Error
    } finally {
      span.end();
    }
  });
}
```

#### Production Standard (Correct)
> **Standard**: Extracted context is explicitly captured as an OpenTelemetry SpanLink with attributes informing sampling policies. This guarantees that downstream consumer execution is causally bound to the producer, enabling tail-sampling pipelines to retain connected trace graphs.
```typescript
import { trace, context, propagation, ROOT_CONTEXT, TraceFlags, Link } from '@opentelemetry/api';

export async function handleMessage(message: { payload: string; headers: Record<string, string> }) {
  const tracer = trace.getTracer('payment-service');
  
  // Extract remote producer context from headers
  const extractedContext = propagation.extract(ROOT_CONTEXT, message.headers);
  const producerSpanContext = trace.getSpanContext(extractedContext);

  const links: Link[] = [];
  if (producerSpanContext && producerSpanContext.isValid) {
    links.push({
      context: producerSpanContext,
      attributes: {
        'messaging.link.type': 'causal_link',
        'sampling.priority': (producerSpanContext.traceFlags & TraceFlags.SAMPLED) ? 'sampled' : 'deferred'
      }
    });
  }

  // New consumer trace linked to the producer trace context
  const span = tracer.startSpan('process_payment_message', {
    links,
    attributes: {
      'messaging.operation': 'receive',
      'messaging.system': 'kafka'
    }
  }, context.active());

  return context.with(trace.setSpan(context.active(), span), async () => {
    try {
      await processMessagePayload(message.payload);
      span.setStatus({ code: 1 });
    } catch (err) {
      span.recordException(err as Error);
      span.setStatus({ code: 2 });
      throw err;
    } finally {
      span.end();
    }
  });
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: OpenTelemetry Tail-Sampling Span-Link Completeness Protocol

OpenTelemetry Tail-Sampling Span-Link Completeness Protocol defines an industrial-grade behavioral standard for devops engineering. Enforces distributed trace graph integrity by mandating that OpenTelemetry Collector pipelines and messaging clients preserve and evaluate span-linked asynchronous contexts before enacting lossy trace drop decisions. Eliminates causal fragmentation between producer and consumer spans across distributed message queues. 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: Blind Tail-Sampling Drop of Fast Producer Traces Linked to Asynchronous Consumer Workflows 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 DriftBlind Tail-Sampling Drop of Fast Producer Traces Linked to Asynchronous Consumer WorkflowsCritical
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 capture and inject producer context into messaging payloads as explicit OpenTelemetry SpanLinks containing full TraceState and TraceFlags.

Engineers MUST ALWAYS capture and inject producer context into messaging payloads as explicit OpenTelemetry SpanLinks containing full TraceState and TraceFlags.

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

Collector pipelines MUST NOT drop parent or producer traces with status 200 before evaluating downstream linked asynchronous consumer spans.

Collector pipelines MUST NOT drop parent or producer traces with status 200 before evaluating downstream linked asynchronous consumer 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

Engineers MUST set tail-sampling 'decision_wait' intervals in the OpenTelemetry Collector to exceed the p99 message broker queue latency.

Engineers MUST set tail-sampling 'decision_wait' intervals in the OpenTelemetry Collector to exceed the p99 message broker queue latency.

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 explicitly record the 'messaging.operation=receive' span linked directly to the extracted 'messaging.operation=publish' span context.

Services MUST explicitly record the 'messaging.operation=receive' span linked directly to the extracted 'messaging.operation=publish' 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.
5

Tail-sampling routing configurations MUST ensure traces sharing span-links or trace-state linkage tokens route to the same sampling Collector instance via trace-ID-consistent hashing.

Tail-sampling routing configurations MUST ensure traces sharing span-links or trace-state linkage tokens route to the same sampling Collector instance via trace-ID-consistent hashing.

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 Tail-Sampling Span-Link Completeness Protocol critical for AI coding assistants?

Enforces distributed trace graph integrity by mandating that OpenTelemetry Collector pipelines and messaging clients preserve and evaluate span-linked asynchronous contexts before enacting lossy trace drop decisions. Eliminates causal fragmentation between producer and consumer spans across distributed message queues. 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 (Blind Tail-Sampling Drop of Fast Producer Traces Linked to Asynchronous Consumer Workflows) 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