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

OpenTelemetry W3C Distributed Context Propagation and Sampling Invariant Standard

Codifies mandatory W3C distributed trace context and baggage propagation across all asynchronous boundaries, RPC boundaries, and message queues. Enforces Parent-Based Ratio Head Sampling combined with Collector-level Tail-Based Sampling to prevent telemetry egress budget explosion while preserving 100% of anomalous and error spans.

Prohibited Anti-Pattern: Orphaned Async Trace Islands and Egress Budget Blowouts via Unpropagated Context and Naive Head Sampling

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry W3C Distributed Context Propagation and Sampling Invariant Standard - Codifies mandatory W3C distributed trace context and baggage propagation across all asynchronous boundaries, RPC boundaries, and message queues. Enforces Parent-Based Ratio Head Sampling combined with Collector-level Tail-Based Sampling to prevent telemetry egress budget explosion while preserving 100% of anomalous and error spans.
globs: src/**/*.{ts,js,tsx}, packages/**/*.{ts,js}, services/**/*.{ts,js}, config/otel-collector*.{yaml,yml}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentelemetry-w3c-context-propagation-sampling-budget-standard"
---

# OpenTelemetry W3C Distributed Context Propagation and Sampling Invariant Standard

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Orphaned Async Trace Islands and Egress Budget Blowouts via Unpropagated Context and Naive Head Sampling

## System Constraints
You are a Principal Observability and Infrastructure Architect enforcing OpenTelemetry W3C context propagation and tail-based sampling invariants. Every outbound HTTP, gRPC, and messaging payload MUST carry injected W3C traceparent, tracestate, and baggage. Downstream consumers MUST extract context before span creation to prevent severed trace trees. Trace sampling MUST use ParentBased delegation with bounded ratio head sampling, leaving final retention decisions to tail-sampling collectors to ensure zero unmonitored error traces without egress budget exhaustion.

## Strict Guidelines
- Engineers MUST ALWAYS inject W3C Trace Context ('traceparent', 'tracestate') and OpenTelemetry Baggage ('baggage') into outbound network and message broker payloads using the global OpenTelemetry propagation API.
- Engineers MUST extract context from incoming network carriers into an explicit context instance and pass that extracted context as the parent when creating server, consumer, or job-processor spans.
- Engineers MUST NOT rely on uninstrumented ambient Node.js or async runtimes across non-continuous boundaries like message queues, database outboxes, or scheduled job brokers.
- Services MUST configure the TracerProvider with a ParentBasedSampler wrapping a TraceIdRatioBasedSampler at the head, preventing leaf services from overriding parent sampling decisions.
- Telemetry pipelines MUST implement Collector-level Tail-Based Sampling retaining 100% of spans with StatusCode === ERROR, spans exceeding p95 latency thresholds, and traces matching critical tenant baggage keys.
- Components NEVER drop propagation headers when sanitizing or whitelisting outbound HTTP headers in proxy or gateway middleware.

## Bad vs Good Implementations
### Message Broker Context Injection and Extraction (Async Boundary)

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Publishing events without injecting traceparent and baggage creates broken trace graphs. The downstream consumer spawns a disconnected root trace, destroying end-to-end distributed transaction visibility.
```typescript
import { trace } from '@opentelemetry/api';

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

// ANTI-PATTERN: Emitting event without trace context injection creates orphaned trace downstream.
export async function publishOrderEvent(order: { id: string; total: number }, queue: any) {
  return tracer.startActiveSpan('publish-order', async (span) => {
    try {
      const payload = JSON.stringify(order);
      await queue.send({
        body: payload,
        properties: { timestamp: Date.now() }
      });
    } finally {
      span.end();
    }
  });
}

// Downstream Consumer: Starts a root trace with no causal parent
export async function handleOrderEvent(message: any) {
  return tracer.startActiveSpan('process-order', async (span) => {
    // Context gap: This span has a brand new traceId, severing observability
    await processOrder(JSON.parse(message.body));
    span.end();
  });
}
```

#### Production Standard (Correct)
> **Standard**: Correctly serializes W3C traceparent and baggage into message headers using propagation.inject. Downstream extracts parent context using propagation.extract, maintaining continuous trace topology across decoupled message boundaries.
```typescript
import { trace, context, propagation, ROOT_CONTEXT, SpanKind } from '@opentelemetry/api';

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

interface QueueCarrier {
  headers?: Record<string, string>;
  body: string;
}

export async function publishOrderEvent(order: { id: string; total: number }, queue: any) {
  return tracer.startActiveSpan('publish-order', { kind: SpanKind.PRODUCER }, async (span) => {
    try {
      const carrier: Record<string, string> = {};
      // Invariant: Always inject active context (traceparent + baggage) into message carrier
      propagation.inject(context.active(), carrier);

      await queue.send({
        body: JSON.stringify(order),
        properties: { headers: carrier, timestamp: Date.now() }
      });
    } finally {
      span.end();
    }
  });
}

export async function handleOrderEvent(message: { properties?: { headers?: Record<string, string> }; body: string }) {
  // Invariant: Extract context from carrier headers into parent context
  const extractedContext = propagation.extract(ROOT_CONTEXT, message.properties?.headers ?? {});
  
  return tracer.startActiveSpan(
    'process-order',
    { kind: SpanKind.CONSUMER },
    extractedContext,
    async (span) => {
      try {
        await processOrder(JSON.parse(message.body));
      } catch (err: any) {
        span.recordException(err);
        span.setStatus({ code: 2, message: err?.message ?? 'Execution Error' });
        throw err;
      } finally {
        span.end();
      }
    }
  );
}
```

### Deterministic Parent-Based Head Sampler Initialization

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using AlwaysOnSampler or hardcoded unparented ratio samplers causes leaf services to drop parent trace context or induces catastrophic telemetry ingestion bills during traffic surges.
```typescript
import { NodeSDK } from '@opentelemetry/sdk-node';
import { AlwaysOnSampler } from '@opentelemetry/sdk-trace-base';

// ANTI-PATTERN: Unconditional 100% head sampling saturates network egress and costs thousands in SaaS ingestion
export const otelSdk = new NodeSDK({
  sampler: new AlwaysOnSampler(),
  // When load spikes to 10k rps, telemetry costs scale linearly, exhausting budgets
});
```

#### Production Standard (Correct)
> **Standard**: Configures ParentBasedSampler wrapping a TraceIdRatioBasedSampler. Root traces sample at a deterministic budget (5% in prod), while downstream child spans strictly inherit parent sample decisions, enabling precise tail-sampling downstream.
```typescript
import { NodeSDK } from '@opentelemetry/sdk-node';
import {
  ParentBasedSampler,
  TraceIdRatioBasedSampler
} from '@opentelemetry/sdk-trace-base';

// Invariant: Deterministic parent-based ratio sampling. Respects parent decisions;
// initiates new traces at configured sample budget (e.g., 5% head sample for steady-state traffic).
const targetHeadSampleRatio = process.env.NODE_ENV === 'production' ? 0.05 : 1.0;

export const otelSdk = new NodeSDK({
  sampler: new ParentBasedSampler({
    root: new TraceIdRatioBasedSampler(targetHeadSampleRatio),
    // Honor upstream client sampling decisions
    remoteParentSampled: undefined, 
    remoteParentNotSampled: undefined
  })
});
```

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: OpenTelemetry W3C Distributed Context Propagation and Sampling Invariant Standard

OpenTelemetry W3C Distributed Context Propagation and Sampling Invariant Standard defines an industrial-grade behavioral standard for devops engineering. Codifies mandatory W3C distributed trace context and baggage propagation across all asynchronous boundaries, RPC boundaries, and message queues. Enforces Parent-Based Ratio Head Sampling combined with Collector-level Tail-Based Sampling to prevent telemetry egress budget explosion while preserving 100% of anomalous and error spans. 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 Async Trace Islands and Egress Budget Blowouts via Unpropagated Context and Naive Head Sampling 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 Async Trace Islands and Egress Budget Blowouts via Unpropagated Context and Naive Head SamplingCritical
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 inject W3C Trace Context ('traceparent', 'tracestate') and OpenTelemetry Baggage ('baggage') into outbound network and message broker payloads using the global OpenTelemetry propagation API.

Engineers MUST ALWAYS inject W3C Trace Context ('traceparent', 'tracestate') and OpenTelemetry Baggage ('baggage') into outbound network and message broker payloads using the global OpenTelemetry propagation API.

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 extract context from incoming network carriers into an explicit context instance and pass that extracted context as the parent when creating server, consumer, or job-processor spans.

Engineers MUST extract context from incoming network carriers into an explicit context instance and pass that extracted context as the parent when creating server, consumer, or job-processor 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 NOT rely on uninstrumented ambient Node.js or async runtimes across non-continuous boundaries like message queues, database outboxes, or scheduled job brokers.

Engineers MUST NOT rely on uninstrumented ambient Node.js or async runtimes across non-continuous boundaries like message queues, database outboxes, or scheduled job brokers.

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 configure the TracerProvider with a ParentBasedSampler wrapping a TraceIdRatioBasedSampler at the head, preventing leaf services from overriding parent sampling decisions.

Services MUST configure the TracerProvider with a ParentBasedSampler wrapping a TraceIdRatioBasedSampler at the head, preventing leaf services from overriding parent sampling decisions.

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

Telemetry pipelines MUST implement Collector-level Tail-Based Sampling retaining 100% of spans with StatusCode === ERROR, spans exceeding p95 latency thresholds, and traces matching critical tenant baggage keys.

Telemetry pipelines MUST implement Collector-level Tail-Based Sampling retaining 100% of spans with StatusCode === ERROR, spans exceeding p95 latency thresholds, and traces matching critical tenant baggage keys.

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 propagation headers when sanitizing or whitelisting outbound HTTP headers in proxy or gateway middleware.

Components NEVER drop propagation headers when sanitizing or whitelisting outbound HTTP headers in proxy or gateway middleware.

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 Distributed Context Propagation and Sampling Invariant Standard critical for AI coding assistants?

Codifies mandatory W3C distributed trace context and baggage propagation across all asynchronous boundaries, RPC boundaries, and message queues. Enforces Parent-Based Ratio Head Sampling combined with Collector-level Tail-Based Sampling to prevent telemetry egress budget explosion while preserving 100% of anomalous and error spans. 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 Async Trace Islands and Egress Budget Blowouts via Unpropagated Context and Naive Head Sampling) 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