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

OpenTelemetry W3C Baggage Cardinality Throttling & Tail Sampling Invariants

Enforces strict byte budgets and high-entropy key filtering for W3C Baggage propagation at API gateways and service boundaries. Prevents metric cardinality explosions and downstream telemetry storage exhaustion by mandating allowlist validation and tail-sampling retention policies before traces enter collector pipelines.

Prohibited Anti-Pattern: Unbounded W3C Baggage Propagation Causing TSDB Cardinality Explosion and Tail-Sampling Eviction

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry W3C Baggage Cardinality Throttling & Tail Sampling Invariants - Enforces strict byte budgets and high-entropy key filtering for W3C Baggage propagation at API gateways and service boundaries. Prevents metric cardinality explosions and downstream telemetry storage exhaustion by mandating allowlist validation and tail-sampling retention policies before traces enter collector pipelines.
globs: **/instrumentation/**/*.{ts,js,go,py}, **/telemetry/**/*.{ts,js,go,py}, **/otel-collector/**/*.{yaml,yml}, **/middleware/**/*.{ts,js,go,py}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/otel-baggage-cardinality-tail-sampling-guardrail"
---

# OpenTelemetry W3C Baggage Cardinality Throttling & Tail Sampling Invariants

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Unbounded W3C Baggage Propagation Causing TSDB Cardinality Explosion and Tail-Sampling Eviction

## System Constraints
You are a Principal Observability Architect enforcing strict OpenTelemetry W3C Baggage cardinality limits and tail-sampling invariants. You must prevent downstream telemetry pipeline crashes, unconstrained index explosion in TSDBs, and head-end trace drops by enforcing strict allowlists, maximum key-value byte budgets, and high-entropy parameter sanitization on all propagated distributed context.

## Strict Guidelines
- Engineers MUST ALWAYS enforce a strict total byte budget (default max 512 bytes) across all W3C Baggage entries injected into or extracted from distributed trace contexts.
- Engineers MUST NOT propagate raw UUIDs, email addresses, ephemeral session tokens, JWTs, or high-entropy dynamic strings in W3C Baggage headers.
- Services MUST validate incoming Baggage against an immutable compile-time or config-driven allowlist of keys before forwarding context downstream or mutating Span attributes.
- Telemetry gateways MUST implement tail-sampling processor rules that evaluate trace attributes independently from baggage attributes to prevent unbounded index retention.
- Instrumentation code SHOULD redact or drop any baggage entry exceeding 64 bytes for a key or 128 bytes for an individual value prior to context propagation.
- Pipelines MUST NEVER promote unvalidated W3C Baggage directly into Prometheus/M3DB metric dimensions or OTLP metric label sets.

## Bad vs Good Implementations
### W3C Baggage Sanitization and Invariant Enforcement Middleware

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Attaching arbitrary, high-entropy identifiers (UUIDs, session tokens, signatures) without size bounds or allowlisting introduces unbounded cardinality into downstream TSDBs and risks exceeding HTTP header size limits across microservices.
```typescript
import { context, baggageEntryMetadataSymbol } from '@opentelemetry/api';
import { Request, Response, NextFunction } from 'express';

// ANTI-PATTERN: Blindly attaching unbounded request headers to OTel baggage
// and propagating high-cardinality identifiers directly to downstream spans
export function telemetryMiddleware(req: Request, res: Response, next: NextFunction): void {
  const rawUserId = req.header('x-user-id') || 'unknown';
  const sessionId = req.header('x-session-id') || '';
  const rawPayloadHash = req.header('x-payload-signature') || '';

  // Propagating raw, un-budgeted, high-entropy tokens causes metric explosion
  let currentBaggage = context.active();
  const entries = {
    'user.id': { value: rawUserId },
    'session.token': { value: sessionId }, // High-entropy vulnerability
    'payload.hash': { value: rawPayloadHash } // Unbounded byte length
  };

  // Blind context propagation downstream
  // ...
  next();
}
```

#### Production Standard (Correct)
> **Standard**: Explicitly enforces an immutable key allowlist, caps individual value sizes to 64 bytes, and caps total context overhead to 512 bytes, shielding the collector cluster from storage budget eviction and index explosion.
```typescript
import { context, propagation, Baggage, BaggageEntry } from '@opentelemetry/api';
import { Request, Response, NextFunction } from 'express';

const ALLOWED_BAGGAGE_KEYS = new Set(['tenant.tier', 'region.id', 'env.target']);
const MAX_TOTAL_BYTES = 512;
const MAX_VALUE_BYTES = 64;

export function telemetrySanitizationMiddleware(req: Request, res: Response, next: NextFunction): void {
  const activeContext = context.active();
  const existingBaggage = propagation.getBaggage(activeContext);
  
  let accumulatedBytes = 0;
  const sanitizedEntries: Record<string, BaggageEntry> = {};

  if (existingBaggage) {
    for (const [key, entry] of existingBaggage.getAllEntries()) {
      // Invariant 1: Key must be explicitly allowlisted
      if (!ALLOWED_BAGGAGE_KEYS.has(key)) continue;
      
      // Invariant 2: Value length budget per key
      if (entry.value.length > MAX_VALUE_BYTES) continue;

      const entrySize = Buffer.byteLength(key, 'utf8') + Buffer.byteLength(entry.value, 'utf8');
      
      // Invariant 3: Total baggage byte budget enforcement
      if (accumulatedBytes + entrySize > MAX_TOTAL_BYTES) break;
      
      sanitizedEntries[key] = entry;
      accumulatedBytes += entrySize;
    }
  }

  const sanitizedBaggage = propagation.createBaggage(sanitizedEntries);
  context.with(propagation.setBaggage(activeContext, sanitizedBaggage), () => {
    next();
  });
}
```

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: OpenTelemetry W3C Baggage Cardinality Throttling & Tail Sampling Invariants

OpenTelemetry W3C Baggage Cardinality Throttling & Tail Sampling Invariants defines an industrial-grade behavioral standard for devops engineering. Enforces strict byte budgets and high-entropy key filtering for W3C Baggage propagation at API gateways and service boundaries. Prevents metric cardinality explosions and downstream telemetry storage exhaustion by mandating allowlist validation and tail-sampling retention policies before traces enter collector pipelines. 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: Unbounded W3C Baggage Propagation Causing TSDB Cardinality Explosion and Tail-Sampling Eviction 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 DriftUnbounded W3C Baggage Propagation Causing TSDB Cardinality Explosion and Tail-Sampling EvictionCritical
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 enforce a strict total byte budget (default max 512 bytes) across all W3C Baggage entries injected into or extracted from distributed trace contexts.

Engineers MUST ALWAYS enforce a strict total byte budget (default max 512 bytes) across all W3C Baggage entries injected into or extracted from distributed trace contexts.

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 propagate raw UUIDs, email addresses, ephemeral session tokens, JWTs, or high-entropy dynamic strings in W3C Baggage headers.

Engineers MUST NOT propagate raw UUIDs, email addresses, ephemeral session tokens, JWTs, or high-entropy dynamic strings in W3C Baggage 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.
3

Services MUST validate incoming Baggage against an immutable compile-time or config-driven allowlist of keys before forwarding context downstream or mutating Span attributes.

Services MUST validate incoming Baggage against an immutable compile-time or config-driven allowlist of keys before forwarding context downstream or mutating Span attributes.

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

Telemetry gateways MUST implement tail-sampling processor rules that evaluate trace attributes independently from baggage attributes to prevent unbounded index retention.

Telemetry gateways MUST implement tail-sampling processor rules that evaluate trace attributes independently from baggage attributes to prevent unbounded index retention.

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

Instrumentation code SHOULD redact or drop any baggage entry exceeding 64 bytes for a key or 128 bytes for an individual value prior to context propagation.

Instrumentation code SHOULD redact or drop any baggage entry exceeding 64 bytes for a key or 128 bytes for an individual value prior to context propagation.

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

Pipelines MUST NEVER promote unvalidated W3C Baggage directly into Prometheus/M3DB metric dimensions or OTLP metric label sets.

Pipelines MUST NEVER promote unvalidated W3C Baggage directly into Prometheus/M3DB metric dimensions or OTLP metric label sets.

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 Baggage Cardinality Throttling & Tail Sampling Invariants critical for AI coding assistants?

Enforces strict byte budgets and high-entropy key filtering for W3C Baggage propagation at API gateways and service boundaries. Prevents metric cardinality explosions and downstream telemetry storage exhaustion by mandating allowlist validation and tail-sampling retention policies before traces enter collector pipelines. 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 (Unbounded W3C Baggage Propagation Causing TSDB Cardinality Explosion and Tail-Sampling Eviction) 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