This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: opentelemetry-exemplar-cardinality-fencing-standard

OpenTelemetry Distributed Exemplar Cardinality Fencing and Reservoir Bounding

Enforces strict reservoir sampling bounds and attribute fencing on OpenTelemetry metric exemplars to prevent memory leaks and downstream time-series database (TSDB) indexing explosions. Unbounded exemplar propagation or injecting high-cardinality business labels into metric exemplar buffers compromises Prometheus, Mimir, and Thanos aggregators. All metric providers must explicitly define exemplar filters, bound reservoir cell sizes, and restrict exemplar baggage strictly to standardized W3C trace identifiers.

Prohibited Anti-Pattern: Attaching raw user or request metadata as un-sampled exemplar baggage inside metric aggregators without cardinality filters

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry Distributed Exemplar Cardinality Fencing and Reservoir Bounding - Enforces strict reservoir sampling bounds and attribute fencing on OpenTelemetry metric exemplars to prevent memory leaks and downstream time-series database (TSDB) indexing explosions. Unbounded exemplar propagation or injecting high-cardinality business labels into metric exemplar buffers compromises Prometheus, Mimir, and Thanos aggregators. All metric providers must explicitly define exemplar filters, bound reservoir cell sizes, and restrict exemplar baggage strictly to standardized W3C trace identifiers.
globs: src/**/telemetry/**/*.{ts,js,py,go}, src/**/metrics/**/*.{ts,js,py,go}, src/**/observability/**/*.{ts,js,py,go}, lib/**/tracing/**/*.{ts,js,py,go}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentelemetry-exemplar-cardinality-fencing-standard"
---

# OpenTelemetry Distributed Exemplar Cardinality Fencing and Reservoir Bounding

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Attaching raw user or request metadata as un-sampled exemplar baggage inside metric aggregators without cardinality filters

## System Constraints
You are a Principal Software Architect enforcing production-grade observability invariants for OpenTelemetry metrics and exemplars. You must strictly mandate that all metric exemplar attachments adhere to fixed-capacity reservoir sampling, strip high-cardinality metadata at the SDK collection layer, and enforce strict boundary fences to protect Prometheus/OTel metric storage buffers.

## Strict Guidelines
- Engineers MUST ALWAYS restrict metric exemplar attachments exclusively to standard W3C trace context keys (`trace_id`, `span_id`).
- Engineers MUST NOT inject business entity identifiers, tenant IDs, email addresses, or unhashed request parameters into exemplar attribute bags.
- Services MUST configure an explicit `ExemplarReservoir` (such as a fixed-size `AlignedHistogramBucketExemplarReservoir`) with deterministic memory bounds for all histogram and counter metric views.
- Code SHOULD enforce `TraceBasedExemplarFilter` or sample-rate bounded filters rather than `AlwaysSampleFilter` in high-throughput ingress pathways.
- Components NEVER instantiate unbounded custom in-memory accumulators that allow linear memory expansion per active time series.

## Bad vs Good Implementations
### TypeScript: Unbounded Exemplar Attributes vs Cardinally-Fenced Histogram Reservoir

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: This configuration fails to configure an explicit ExemplarFilter or Reservoir, allowing raw user identifiers and arbitrary context to bind directly into metric aggregation buffers. In high-traffic environments, this causes massive TSDB memory bloat and collector crash-loops.
```typescript
import { MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
import { trace, context } from '@opentelemetry/api';

// ANTI-PATTERN: Default exemplar configuration captures arbitrary context labels
// without reservoir sizing or attribute fencing, polluting the TSDB index.
const meterProvider = new MeterProvider({
  readers: [
    new PeriodicExportingMetricReader({
      exporter: new OTLPMetricExporter(),
      exportIntervalMillis: 15000,
    }),
  ],
});

const meter = meterProvider.getMeter('billing-service');
const latencyHistogram = meter.createHistogram('http_request_duration_ms');

export function recordBadLatency(durationMs: number, userId: string, tenantId: string) {
  // DANGEROUS: High-cardinality attributes mixed with unbounded exemplar capture
  latencyHistogram.record(durationMs, {
    'user.id': userId,       // Causes metric explosion
    'tenant.id': tenantId,   // Unfenced dimension
  }, context.active());
}
```

#### Production Standard (Correct)
> **Standard**: Configures an explicit metric View restricting attributes strictly to a low-cardinality allowlist and validates span context presence to ensure exemplars only store standard W3C trace keys without leaking arbitrary entity metadata into metrics collectors.
```typescript
import { 
  MeterProvider, 
  PeriodicExportingMetricReader, 
  View,
  ExplicitBucketHistogramAggregation,
  InstrumentType
} from '@opentelemetry/sdk-metrics';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
import { trace, context, isSpanContextValid } from '@opentelemetry/api';

// PRODUCTION FIX: Explicitly constrain metric attributes and exemplar boundaries
const ALLOWED_METRIC_ATTRIBUTES = new Set(['http.status_code', 'http.method', 'route']);

const meterProvider = new MeterProvider({
  readers: [
    new PeriodicExportingMetricReader({
      exporter: new OTLPMetricExporter(),
      exportIntervalMillis: 15000,
    }),
  ],
  views: [
    new View({
      instrumentType: InstrumentType.HISTOGRAM,
      instrumentName: 'http_request_duration_ms',
      aggregation: new ExplicitBucketHistogramAggregation([5, 10, 25, 50, 100, 250, 500, 1000, 2500]),
      attributeFilter: (attributeName: string) => ALLOWED_METRIC_ATTRIBUTES.has(attributeName),
    }),
  ],
});

const meter = meterProvider.getMeter('billing-service');
const latencyHistogram = meter.createHistogram('http_request_duration_ms', {
  unit: 'ms',
  description: 'HTTP request duration partitioned with fenced attributes and bounded exemplars',
});

export function recordFencedLatency(durationMs: number, method: string, route: string, statusCode: number) {
  const currentSpan = trace.getSpan(context.active());
  const spanCtx = currentSpan?.spanContext();

  // Strictly controlled dimension keys; exemplar metadata is bounded to W3C trace identifiers
  if (spanCtx && isSpanContextValid(spanCtx) && spanCtx.isRemote === false) {
    latencyHistogram.record(durationMs, {
      'http.method': method,
      'route': route,
      'http.status_code': statusCode,
    }, context.active());
  } else {
    latencyHistogram.record(durationMs, {
      'http.method': method,
      'route': route,
      'http.status_code': statusCode,
    });
  }
}
```

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 Distributed Exemplar Cardinality Fencing and Reservoir Bounding

OpenTelemetry Distributed Exemplar Cardinality Fencing and Reservoir Bounding defines an industrial-grade behavioral standard for devops engineering. Enforces strict reservoir sampling bounds and attribute fencing on OpenTelemetry metric exemplars to prevent memory leaks and downstream time-series database (TSDB) indexing explosions. Unbounded exemplar propagation or injecting high-cardinality business labels into metric exemplar buffers compromises Prometheus, Mimir, and Thanos aggregators. All metric providers must explicitly define exemplar filters, bound reservoir cell sizes, and restrict exemplar baggage strictly to standardized W3C trace identifiers. 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: Attaching raw user or request metadata as un-sampled exemplar baggage inside metric aggregators without cardinality filters 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 DriftAttaching raw user or request metadata as un-sampled exemplar baggage inside metric aggregators without cardinality filtersCritical
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 restrict metric exemplar attachments exclusively to standard W3C trace context keys (`trace_id`, `span_id`).

Engineers MUST ALWAYS restrict metric exemplar attachments exclusively to standard W3C trace context keys (`trace_id`, `span_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.
2

Engineers MUST NOT inject business entity identifiers, tenant IDs, email addresses, or unhashed request parameters into exemplar attribute bags.

Engineers MUST NOT inject business entity identifiers, tenant IDs, email addresses, or unhashed request parameters into exemplar attribute bags.

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 configure an explicit `ExemplarReservoir` (such as a fixed-size `AlignedHistogramBucketExemplarReservoir`) with deterministic memory bounds for all histogram and counter metric views.

Services MUST configure an explicit `ExemplarReservoir` (such as a fixed-size `AlignedHistogramBucketExemplarReservoir`) with deterministic memory bounds for all histogram and counter metric views.

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

Code SHOULD enforce `TraceBasedExemplarFilter` or sample-rate bounded filters rather than `AlwaysSampleFilter` in high-throughput ingress pathways.

Code SHOULD enforce `TraceBasedExemplarFilter` or sample-rate bounded filters rather than `AlwaysSampleFilter` in high-throughput ingress pathways.

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 instantiate unbounded custom in-memory accumulators that allow linear memory expansion per active time series.

Components NEVER instantiate unbounded custom in-memory accumulators that allow linear memory expansion per active time series.

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 Distributed Exemplar Cardinality Fencing and Reservoir Bounding critical for AI coding assistants?

Enforces strict reservoir sampling bounds and attribute fencing on OpenTelemetry metric exemplars to prevent memory leaks and downstream time-series database (TSDB) indexing explosions. Unbounded exemplar propagation or injecting high-cardinality business labels into metric exemplar buffers compromises Prometheus, Mimir, and Thanos aggregators. All metric providers must explicitly define exemplar filters, bound reservoir cell sizes, and restrict exemplar baggage strictly to standardized W3C trace identifiers. 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 (Attaching raw user or request metadata as un-sampled exemplar baggage inside metric aggregators without cardinality filters) 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