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

OpenTelemetry W3C Baggage Mutation and Cardinality Guardrail

Enforces deterministic validation, strict allowlisting, and byte-budget ceilings on W3C Baggage propagation across distributed tracing contexts. Prevents high-cardinality payloads, sensitive telemetry leakage, and collector memory exhaustion caused by unbounded downstream header amplification.

Prohibited Anti-Pattern: Unrestricted runtime injection of dynamic, high-cardinality baggage metadata across W3C distributed trace contexts

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry W3C Baggage Mutation and Cardinality Guardrail - Enforces deterministic validation, strict allowlisting, and byte-budget ceilings on W3C Baggage propagation across distributed tracing contexts. Prevents high-cardinality payloads, sensitive telemetry leakage, and collector memory exhaustion caused by unbounded downstream header amplification.
globs: src/observability/**/*.ts, src/telemetry/**/*.ts, src/middleware/**/*.ts, src/instrumentation/**/*.ts, src/tracing/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/otel-w3c-baggage-mutation-guardrail"
---

# OpenTelemetry W3C Baggage Mutation and Cardinality Guardrail

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Unrestricted runtime injection of dynamic, high-cardinality baggage metadata across W3C distributed trace contexts

## System Constraints
You are a Principal Software Architect enforcing OpenTelemetry W3C Baggage and trace-context mutation boundaries. Distributed trace propagation MUST NOT be treated as a general-purpose transport layer or key-value store. Mandate immutable context transformations, compile-time/runtime allowlisting of baggage keys, strict value size limits conforming to RFC 7230/W3C Baggage specifications, and immediate rejection of high-cardinality identifiers (UUIDs, timestamps, raw payloads, auth tokens). Ensure that downstream telemetry collectors (Jaeger, Tempo, OtelCollector) and edge proxies are shielded from HTTP 431 header truncation and unbounded memory footprint.

## Strict Guidelines
- Engineers MUST ALWAYS validate baggage keys against an explicit compile-time allowlist enum before propagation.
- Engineers MUST NOT inject non-deterministic high-cardinality data (UUIDs, request payloads, user PII, bearer tokens, or microsecond timestamps) into baggage entries.
- Code SHOULD enforce strict limits on baggage size: individual values MUST NOT exceed 128 bytes, and total baggage header size MUST NOT exceed 1024 bytes (well within the W3C 8192-octet theoretical limit).
- Services MUST treat Baggage and active Context as immutable; baggage updates MUST produce a new Context instance using standard OpenTelemetry API propagation primitives.
- Components NEVER bridge raw incoming HTTP headers, unvalidated search params, or unrestricted application state directly into W3C baggage entries without strict sanitization.

## Bad vs Good Implementations
### Unsanitized High-Cardinality Baggage Injection vs Schema-Bounded Mutation Guardrail

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Injects unvalidated headers, authentication credentials, dynamic UUIDs, and unbounded payload dumps directly into W3C baggage. Downstream edge proxies (e.g., NGINX, Envoy) will reject expanded headers with 431 Request Header Fields Too Large, sensitive PII/secrets will leak into trace stores, and telemetry collectors will suffer memory pressure.
```typescript
import { context, propagation } from '@opentelemetry/api';
import { Request, Response, NextFunction } from 'express';

export function telemetryBaggageMiddleware(req: Request, res: Response, next: NextFunction) {
  // ANTI-PATTERN: Dynamically copying arbitrary request headers and high-cardinality
  // payloads into baggage. Injects unbounded memory footprint and PII into downstream collectors.
  let currentContext = context.active();
  
  const rawHeaders = req.headers;
  for (const [key, val] of Object.entries(rawHeaders)) {
    if (typeof val === 'string') {
      const entry = { value: val };
      currentContext = propagation.setBaggage(
        currentContext,
        propagation.createBaggage({ [key]: entry })
      );
    }
  }

  // ANTI-PATTERN: High-cardinality timestamp and dynamic UUID injected into every request
  const badBaggage = propagation.createBaggage({
    'request.id': { value: req.header('x-request-id') || crypto.randomUUID() },
    'auth.token': { value: req.header('authorization') || '' },
    'payload.dump': { value: JSON.stringify(req.body) },
    'timestamp.ms': { value: Date.now().toString() }
  });

  context.with(propagation.setBaggage(currentContext, badBaggage), () => {
    next();
  });
}
```

#### Production Standard (Correct)
> **Standard**: Implements an explicit allowlist schema for baggage keys, enforces strict per-entry (128 bytes) and aggregate (1024 bytes) memory limits, avoids high-cardinality keys, and correctly updates context immutably through standard @opentelemetry/api mechanisms.
```typescript
import { context, propagation, Baggage, BaggageEntry } from '@opentelemetry/api';
import { Request, Response, NextFunction } from 'express';

export const ALLOWED_BAGGAGE_KEYS = [
  'tenant.tier',
  'routing.datacenter',
  'experiment.bucket'
] as const;

export type AllowedBaggageKey = typeof ALLOWED_BAGGAGE_KEYS[number];

const MAX_VALUE_BYTES = 128;
const MAX_TOTAL_BAGGAGE_BYTES = 1024;

function sanitizeBaggageEntry(key: string, value: string): { key: AllowedBaggageKey; entry: BaggageEntry } | null {
  if (!ALLOWED_BAGGAGE_KEYS.includes(key as AllowedBaggageKey)) {
    return null;
  }

  const byteLength = Buffer.byteLength(value, 'utf8');
  if (byteLength === 0 || byteLength > MAX_VALUE_BYTES) {
    return null;
  }

  return {
    key: key as AllowedBaggageKey,
    entry: { value: value.trim() }
  };
}

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

  // Extract and strictly sanitize only predefined static context
  const candidateMetadata: Partial<Record<AllowedBaggageKey, string | undefined>> = {
    'tenant.tier': req.header('x-tenant-tier'),
    'routing.datacenter': process.env.SERVICE_REGION,
    'experiment.bucket': req.header('x-experiment-variant')
  };

  for (const [key, rawVal] of Object.entries(candidateMetadata)) {
    if (typeof rawVal !== 'string') continue;
    const sanitized = sanitizeBaggageEntry(key, rawVal);
    if (!sanitized) continue;

    const entryByteCost = Buffer.byteLength(sanitized.key, 'utf8') + Buffer.byteLength(sanitized.entry.value, 'utf8');
    if (accumulatedBytes + entryByteCost > MAX_TOTAL_BAGGAGE_BYTES) break;

    sanitizedEntries[sanitized.key] = sanitized.entry;
    accumulatedBytes += entryByteCost;
  }

  // Create new immutable baggage layer using standard OpenTelemetry propagation
  const nextBaggage = propagation.createBaggage(sanitizedEntries);
  const updatedContext = propagation.setBaggage(context.active(), nextBaggage);

  context.with(updatedContext, () => {
    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 Mutation and Cardinality Guardrail

OpenTelemetry W3C Baggage Mutation and Cardinality Guardrail defines an industrial-grade behavioral standard for devops engineering. Enforces deterministic validation, strict allowlisting, and byte-budget ceilings on W3C Baggage propagation across distributed tracing contexts. Prevents high-cardinality payloads, sensitive telemetry leakage, and collector memory exhaustion caused by unbounded downstream header amplification. 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: Unrestricted runtime injection of dynamic, high-cardinality baggage metadata across W3C distributed trace contexts 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 DriftUnrestricted runtime injection of dynamic, high-cardinality baggage metadata across W3C distributed trace contextsCritical
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 validate baggage keys against an explicit compile-time allowlist enum before propagation.

Engineers MUST ALWAYS validate baggage keys against an explicit compile-time allowlist enum before 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.
2

Engineers MUST NOT inject non-deterministic high-cardinality data (UUIDs, request payloads, user PII, bearer tokens, or microsecond timestamps) into baggage entries.

Engineers MUST NOT inject non-deterministic high-cardinality data (UUIDs, request payloads, user PII, bearer tokens, or microsecond timestamps) into baggage entries.

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

Code SHOULD enforce strict limits on baggage size

Code SHOULD enforce strict limits on baggage size: individual values MUST NOT exceed 128 bytes, and total baggage header size MUST NOT exceed 1024 bytes (well within the W3C 8192-octet theoretical limit).

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 treat Baggage and active Context as immutable

Services MUST treat Baggage and active Context as immutable; baggage updates MUST produce a new Context instance using standard OpenTelemetry API propagation primitives.

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 bridge raw incoming HTTP headers, unvalidated search params, or unrestricted application state directly into W3C baggage entries without strict sanitization.

Components NEVER bridge raw incoming HTTP headers, unvalidated search params, or unrestricted application state directly into W3C baggage entries without strict sanitization.

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 Mutation and Cardinality Guardrail critical for AI coding assistants?

Enforces deterministic validation, strict allowlisting, and byte-budget ceilings on W3C Baggage propagation across distributed tracing contexts. Prevents high-cardinality payloads, sensitive telemetry leakage, and collector memory exhaustion caused by unbounded downstream header amplification. 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 (Unrestricted runtime injection of dynamic, high-cardinality baggage metadata across W3C distributed trace contexts) 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