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

OpenTelemetry Tail-Based Sampling and W3C Baggage Ingress Guardrail

Enforces edge extraction, sanitization, and span-attribute promotion of W3C Baggage customer metadata alongside collector-level tail-based sampling configurations. Ensures deterministic trace retention for P99 latency spikes, downstream unhandled exceptions, and high-value customer tiers while constraining telemetry storage expenditure.

Prohibited Anti-Pattern: Head-Based Dropping of VIP Anomaly Traces and Missing W3C Baggage Attribute Promotion

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry Tail-Based Sampling and W3C Baggage Ingress Guardrail - Enforces edge extraction, sanitization, and span-attribute promotion of W3C Baggage customer metadata alongside collector-level tail-based sampling configurations. Ensures deterministic trace retention for P99 latency spikes, downstream unhandled exceptions, and high-value customer tiers while constraining telemetry storage expenditure.
globs: src/**/*.{ts,js,go,java,py}, deploy/**/*.yaml, otel-collector*.{yaml,yml}, k8s/**/*.{yaml,yml}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentelemetry-tail-sampling-w3c-baggage-policy"
---

# OpenTelemetry Tail-Based Sampling and W3C Baggage Ingress Guardrail

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Head-Based Dropping of VIP Anomaly Traces and Missing W3C Baggage Attribute Promotion

## System Constraints
You are a Principal Software Architect enforcing enterprise-grade OpenTelemetry tail-based sampling and W3C Baggage ingress governance. Mandate that boundary services sanitize and promote critical context (e.g., customer tier, account ID) into W3C Baggage and explicit Span attributes, ensuring downstream OpenTelemetry Collector instances can evaluate latency, status code, and tenant-tier tail-sampling policies without missing mission-critical anomalies.

## Strict Guidelines
- Engineers MUST ALWAYS configure the OpenTelemetry SDK at ingress boundaries with 'AlwaysOn' (100%) head-sampling so trace evaluation is deferred to the tail-based collector pipeline.
- Engineers MUST ALWAYS sanitize and validate untrusted tenant metadata from HTTP headers before setting W3C Baggage entries (e.g., 'customer.tier', 'tenant.id').
- Services MUST ALWAYS mirror critical W3C Baggage entries onto the root Span's attributes, as OpenTelemetry Collector tail-sampling processors evaluate Span attributes, not raw propagation headers.
- Engineers MUST NOT allow unconstrained baggage keys or arbitrary payload propagation across microservice boundaries to prevent memory leaks and header-inflation denial-of-service.
- OpenTelemetry Collector pipeline configs MUST define an explicit 'tail_sampling' processor with composite policies: 100% retention on ERROR status, numeric duration threshold (P95/P99 latency), string attribute matching for VIP customer tiers, and inverted probabilistic rate-limiting (e.g., 1-5%) for standard successful traces.

## Bad vs Good Implementations
### Express Ingress Middleware & Collector Configuration

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using probabilistic head-based sampling drops anomalous traces before their runtime latency or error status is known. Failing to set W3C Baggage and span attributes prevents downstream tail-sampling collectors from identifying high-value tenants or matching retention policies.
```typescript
// Anti-pattern: SDK uses head-based 5% sampling, drops VIP context, and ignores baggage span promotion
import { trace, propagation } from '@opentelemetry/api';
import { Request, Response, NextFunction } from 'express';

export function telemetryMiddleware(req: Request, res: Response, next: NextFunction) {
  // Anti-pattern: Head-based sampling will randomly drop P99 latency spikes and 500 errors
  const tracer = trace.getTracer('ingress-proxy');
  const span = tracer.startSpan(`${req.method} ${req.path}`);

  // Bad: Headers are forwarded unvalidated, never bound to Baggage or Span attributes
  const tenantTier = req.header('x-customer-tier') || 'standard';

  res.on('finish', () => {
    span.end();
  });

  next();
}
```

#### Production Standard (Correct)
> **Standard**: Edge middleware validates and standardizes tenant metadata, propagates it via W3C Baggage down the call graph, and mirrors key attributes to the active Span. The accompanied OTel Collector tail-sampling configuration captures 100% of errors, latency violations (>1500ms), and enterprise tenant traces while downsampling standard traffic to 2%.
```typescript
// Production standard: Validates tenant tier, injects W3C Baggage, sets Span attributes for Tail-Sampling
import { trace, propagation, context } from '@opentelemetry/api';
import { Request, Response, NextFunction } from 'express';

const ALLOWED_TIERS = new Set(['free', 'pro', 'enterprise']);

export function telemetryMiddleware(req: Request, res: Response, next: NextFunction) {
  const rawTier = req.header('x-customer-tier')?.toLowerCase() || 'free';
  const customerTier = ALLOWED_TIERS.has(rawTier) ? rawTier : 'free';
  const tenantId = (req.header('x-tenant-id') || 'anonymous').slice(0, 64);

  // 1. Establish W3C Baggage for downstream service context propagation
  let currentContext = propagation.setBaggage(
    context.active(),
    propagation.createBaggage({
      'customer.tier': { value: customerTier },
      'tenant.id': { value: tenantId }
    })
  );

  // 2. Start root span within active context
  const tracer = trace.getTracer('ingress-proxy');
  const span = tracer.startSpan(
    `${req.method} ${req.baseUrl || req.path}`,
    {},
    currentContext
  );

  // 3. Promote Baggage to Span Attributes required by Collector tail-sampling processor
  span.setAttribute('customer.tier', customerTier);
  span.setAttribute('tenant.id', tenantId);
  span.setAttribute('http.status_code', 200);

  context.with(trace.setSpan(currentContext, span), () => {
    res.on('finish', () => {
      span.setAttribute('http.status_code', res.statusCode);
      if (res.statusCode >= 500) {
        span.recordException(new Error(`HTTP ${res.statusCode}`));
      }
      span.end();
    });
    next();
  });
}

/* Corresponding OpenTelemetry Collector tail_sampling processor config:
processors:
  tail_sampling:
    decision_wait: 10s
    num_traces: 100000
    expected_new_traces_per_sec: 2000
    policies:
      - name: drop-errors-retain
        type: status_code
        status_code: { status_codes: [ ERROR ] }
      - name: retain-latency-spikes
        type: latency
        latency: { threshold_ms: 1500 }
      - name: retain-vip-tenants
        type: string_attribute
        string_attribute: { key: customer.tier, values: [ enterprise, pro ] }
      - name: sample-healthy-baseline
        type: probabilistic
        probabilistic: { sampling_percentage: 2.0 }
*/
```

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 Tail-Based Sampling and W3C Baggage Ingress Guardrail

OpenTelemetry Tail-Based Sampling and W3C Baggage Ingress Guardrail defines an industrial-grade behavioral standard for devops engineering. Enforces edge extraction, sanitization, and span-attribute promotion of W3C Baggage customer metadata alongside collector-level tail-based sampling configurations. Ensures deterministic trace retention for P99 latency spikes, downstream unhandled exceptions, and high-value customer tiers while constraining telemetry storage expenditure. 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: Head-Based Dropping of VIP Anomaly Traces and Missing W3C Baggage Attribute Promotion 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 DriftHead-Based Dropping of VIP Anomaly Traces and Missing W3C Baggage Attribute PromotionCritical
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 configure the OpenTelemetry SDK at ingress boundaries with 'AlwaysOn' (100%) head-sampling so trace evaluation is deferred to the tail-based collector pipeline.

Engineers MUST ALWAYS configure the OpenTelemetry SDK at ingress boundaries with 'AlwaysOn' (100%) head-sampling so trace evaluation is deferred to the tail-based collector pipeline.

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 ALWAYS sanitize and validate untrusted tenant metadata from HTTP headers before setting W3C Baggage entries (e.g., 'customer.tier', 'tenant.id').

Engineers MUST ALWAYS sanitize and validate untrusted tenant metadata from HTTP headers before setting W3C Baggage entries (e.g., 'customer.tier', 'tenant.id').

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 ALWAYS mirror critical W3C Baggage entries onto the root Span's attributes, as OpenTelemetry Collector tail-sampling processors evaluate Span attributes, not raw propagation headers.

Services MUST ALWAYS mirror critical W3C Baggage entries onto the root Span's attributes, as OpenTelemetry Collector tail-sampling processors evaluate Span attributes, not raw propagation 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.
4

Engineers MUST NOT allow unconstrained baggage keys or arbitrary payload propagation across microservice boundaries to prevent memory leaks and header-inflation denial-of-service.

Engineers MUST NOT allow unconstrained baggage keys or arbitrary payload propagation across microservice boundaries to prevent memory leaks and header-inflation denial-of-service.

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

OpenTelemetry Collector pipeline configs MUST define an explicit 'tail_sampling' processor with composite policies

OpenTelemetry Collector pipeline configs MUST define an explicit 'tail_sampling' processor with composite policies: 100% retention on ERROR status, numeric duration threshold (P95/P99 latency), string attribute matching for VIP customer tiers, and inverted probabilistic rate-limiting (e.g., 1-5%) for standard successful traces.

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-Based Sampling and W3C Baggage Ingress Guardrail critical for AI coding assistants?

Enforces edge extraction, sanitization, and span-attribute promotion of W3C Baggage customer metadata alongside collector-level tail-based sampling configurations. Ensures deterministic trace retention for P99 latency spikes, downstream unhandled exceptions, and high-value customer tiers while constraining telemetry storage expenditure. 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 (Head-Based Dropping of VIP Anomaly Traces and Missing W3C Baggage Attribute Promotion) 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