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

OpenTelemetry Baggage Cardinality and Cryptographic Bounding Invariant

Enforces deterministic byte-length caps, strict key allowlists, and HMAC signature verification on OpenTelemetry baggage propagators. Unbounded or untrusted baggage injected into W3C headers causes cascaded memory exhaustion in distributed trace contexts and forces premature downstream span-store index eviction.

Prohibited Anti-Pattern: Blind Injection of Unbounded Dynamic Context and Unsigned Payload into Distributed Baggage Headers

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry Baggage Cardinality and Cryptographic Bounding Invariant - Enforces deterministic byte-length caps, strict key allowlists, and HMAC signature verification on OpenTelemetry baggage propagators. Unbounded or untrusted baggage injected into W3C headers causes cascaded memory exhaustion in distributed trace contexts and forces premature downstream span-store index eviction.
globs: src/**/telemetry/**/*.{ts,js}, src/**/tracing/**/*.{ts,js}, src/**/middleware/**/*.{ts,js}, src/**/instrumentation/**/*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/otel-baggage-high-cardinality-sanitization-invariant"
---

# OpenTelemetry Baggage Cardinality and Cryptographic Bounding Invariant

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Blind Injection of Unbounded Dynamic Context and Unsigned Payload into Distributed Baggage Headers

## System Constraints
You are a Principal DevOps and Observability Architect enforcing strict OpenTelemetry Baggage bounds. You must prevent downstream distributed context pollution, W3C header bloat (RFC 7230 8KB HTTP header limits), and span index eviction by enforcing strict key allowlisting, maximum byte size quotas per key/total entry, and cryptographic HMAC-SHA256 integrity verification on baggage propagation.

## Strict Guidelines
- Engineers MUST ALWAYS validate inbound and outbound baggage against an immutable allowlist of authorized propagation keys (e.g., 'tenant_id', 'client_tier', 'datacenter_region').
- Engineers MUST NOT inject unbounded high-cardinality metadata (e.g., raw UUIDs, user-generated input, query params, JWT tokens, stack traces) directly into W3C Baggage.
- Services MUST enforce hard byte-length limits: individual baggage entry values MUST NOT exceed 64 bytes, and aggregate baggage payload MUST NOT exceed 512 bytes.
- Services MUST cryptographically sign and verify critical context keys using HMAC-SHA256 when crossing trust boundaries to prevent baggage tampering and state poisoning.
- Instrumentation MUST drop or sanitize unauthorized or oversized baggage entries before context serialization rather than throwing runtime exceptions that break request lifecycles.

## Bad vs Good Implementations
### Sanitized and Cryptographically Signed OTel Baggage Propagator

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Injecting raw authentication tokens, dynamic query strings, and unchecked values into OTel baggage causes distributed context explosion, memory exhaustion, HTTP 431 Request Header Fields Too Large errors at upstream reverse proxies, and rapid eviction of tracing indices in Jaeger/Tempo.
```typescript
import { propagation, context } from '@opentelemetry/api';
import { Request, Response, NextFunction } from 'express';

export function telemetryMiddleware(req: Request, res: Response, next: NextFunction): void {
  // Anti-pattern: Forwarding unbounded, untrusted request headers and high-cardinality IDs directly
  const rawUserId = req.headers['x-user-id'] as string; 
  const userQuery = req.query.search as string; // Unbounded string
  const bearerToken = req.headers.authorization ?? ''; // Massive JWT token

  let currentBaggage = propagation.getBaggage(context.active()) || propagation.createBaggage();
  
  currentBaggage = currentBaggage
    .setEntry('user_id', { value: rawUserId })
    .setEntry('query', { value: userQuery })
    .setEntry('auth_token', { value: bearerToken }); // Blows past 8KB W3C header limits

  context.with(propagation.setBaggage(context.active(), currentBaggage), () => {
    next();
  });
}
```

#### Production Standard (Correct)
> **Standard**: Implements an allowlist-based gatekeeper with individual key/value byte ceilings and an aggregate payload maximum. Employs HMAC signatures in entry metadata to ensure trust boundary integrity and prevent unbounded memory and context propagation.
```typescript
import { propagation, context, Baggage } from '@opentelemetry/api';
import { createHmac, timingSafeEqual } from 'crypto';
import { Request, Response, NextFunction } from 'express';

const MAX_BAGGAGE_KEY_LENGTH = 32;
const MAX_BAGGAGE_VALUE_LENGTH = 64;
const MAX_TOTAL_BAGGAGE_BYTES = 512;
const ALLOWED_BAGGAGE_KEYS = new Set(['tenant_id', 'tier', 'region']);
const HMAC_SECRET = process.env.BAGGAGE_HMAC_SECRET || 'ephemeral-secret-key';

function signBaggageValue(key: string, value: string): string {
  return createHmac('sha256', HMAC_SECRET).update(`${key}:${value}`).digest('hex').slice(0, 16);
}

export function sanitizeAndSignBaggage(incomingBaggage: Baggage): Baggage {
  let cleanBaggage = propagation.createBaggage();
  let totalSize = 0;

  for (const [key, entry] of incomingBaggage.getAllEntries()) {
    if (!ALLOWED_BAGGAGE_KEYS.has(key)) continue;
    if (key.length > MAX_BAGGAGE_KEY_LENGTH || entry.value.length > MAX_BAGGAGE_VALUE_LENGTH) continue;

    const entrySize = Buffer.byteLength(key, 'utf8') + Buffer.byteLength(entry.value, 'utf8');
    if (totalSize + entrySize > MAX_TOTAL_BAGGAGE_BYTES) break;

    const signature = signBaggageValue(key, entry.value);
    cleanBaggage = cleanBaggage.setEntry(key, {
      value: entry.value,
      metadata: `sig=${signature}`
    });
    totalSize += entrySize;
  }

  return cleanBaggage;
}

export function telemetryMiddleware(req: Request, res: Response, next: NextFunction): void {
  const activeContext = propagation.extract(context.active(), req.headers);
  const extractedBaggage = propagation.getBaggage(activeContext) || propagation.createBaggage();
  const boundedBaggage = sanitizeAndSignBaggage(extractedBaggage);

  context.with(propagation.setBaggage(activeContext, boundedBaggage), () => {
    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 Baggage Cardinality and Cryptographic Bounding Invariant

OpenTelemetry Baggage Cardinality and Cryptographic Bounding Invariant defines an industrial-grade behavioral standard for devops engineering. Enforces deterministic byte-length caps, strict key allowlists, and HMAC signature verification on OpenTelemetry baggage propagators. Unbounded or untrusted baggage injected into W3C headers causes cascaded memory exhaustion in distributed trace contexts and forces premature downstream span-store index eviction. 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 Injection of Unbounded Dynamic Context and Unsigned Payload into Distributed Baggage Headers 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 Injection of Unbounded Dynamic Context and Unsigned Payload into Distributed Baggage HeadersCritical
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 inbound and outbound baggage against an immutable allowlist of authorized propagation keys (e.g., 'tenant_id', 'client_tier', 'datacenter_region').

Engineers MUST ALWAYS validate inbound and outbound baggage against an immutable allowlist of authorized propagation keys (e.g., 'tenant_id', 'client_tier', 'datacenter_region').

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 unbounded high-cardinality metadata (e.g., raw UUIDs, user-generated input, query params, JWT tokens, stack traces) directly into W3C Baggage.

Engineers MUST NOT inject unbounded high-cardinality metadata (e.g., raw UUIDs, user-generated input, query params, JWT tokens, stack traces) directly into W3C Baggage.

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 enforce hard byte-length limits

Services MUST enforce hard byte-length limits: individual baggage entry values MUST NOT exceed 64 bytes, and aggregate baggage payload MUST NOT exceed 512 bytes.

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 cryptographically sign and verify critical context keys using HMAC-SHA256 when crossing trust boundaries to prevent baggage tampering and state poisoning.

Services MUST cryptographically sign and verify critical context keys using HMAC-SHA256 when crossing trust boundaries to prevent baggage tampering and state poisoning.

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 MUST drop or sanitize unauthorized or oversized baggage entries before context serialization rather than throwing runtime exceptions that break request lifecycles.

Instrumentation MUST drop or sanitize unauthorized or oversized baggage entries before context serialization rather than throwing runtime exceptions that break request lifecycles.

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 Baggage Cardinality and Cryptographic Bounding Invariant critical for AI coding assistants?

Enforces deterministic byte-length caps, strict key allowlists, and HMAC signature verification on OpenTelemetry baggage propagators. Unbounded or untrusted baggage injected into W3C headers causes cascaded memory exhaustion in distributed trace contexts and forces premature downstream span-store index eviction. 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 Injection of Unbounded Dynamic Context and Unsigned Payload into Distributed Baggage Headers) 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