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

OpenTelemetry High-Cardinality Dynamic Exemplar Retention and Adaptive Quota Standard

Enforces adaptive exemplar sampling and dynamic rate-limiting on OpenTelemetry metric streams to prevent backend telemetry degradation during distributed incidents. Mandates deterministic exemplar reservation reservoirs bounded by leaky-bucket quotas that throttle high-cardinality trace associations during error spikes. Ensures metric collection pipelines maintain zero-allocation invariants and resist OOM crashes under downstream telemetry partition events.

Prohibited Anti-Pattern: Unbounded Exemplar Sampling on Error Metrics Triggering Telemetry Backpressure and Collector OOM

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry High-Cardinality Dynamic Exemplar Retention and Adaptive Quota Standard - Enforces adaptive exemplar sampling and dynamic rate-limiting on OpenTelemetry metric streams to prevent backend telemetry degradation during distributed incidents. Mandates deterministic exemplar reservation reservoirs bounded by leaky-bucket quotas that throttle high-cardinality trace associations during error spikes. Ensures metric collection pipelines maintain zero-allocation invariants and resist OOM crashes under downstream telemetry partition events.
globs: src/observability/**/*.{ts,js}, src/telemetry/**/*.{ts,js}, src/infrastructure/monitoring/**/*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentelemetry-dynamic-exemplar-quota-standard"
---

# OpenTelemetry High-Cardinality Dynamic Exemplar Retention and Adaptive Quota Standard

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Unbounded Exemplar Sampling on Error Metrics Triggering Telemetry Backpressure and Collector OOM

## System Constraints
You are a Principal Observability Architect enforcing strict OpenTelemetry Dynamic Exemplar Retention and Cardinality Guardrails. You must prevent telemetry storms, unbounded memory growth, and backend collector eviction during cascading microservice failures. Ensure all trace-metric associations adhere to adaptive exemplar filters, strictly bounded reservoir algorithms, and circuit-broken exporter pipelines.

## Strict Guidelines
- Engineers MUST ALWAYS configure an explicit ExemplarFilter (e.g., TraceBasedExemplarFilter) wrapped with an adaptive rate-limiting or token-bucket ceiling to prevent metric stream bloat during error storms.
- Engineers MUST NOT attach raw, unbound high-cardinality metadata (such as tenant IDs, raw UUIDs, or unhashed query strings) directly to exemplar attributes; attributes MUST be restricted to standard trace/span IDs and pre-hashed cluster identifiers.
- Reservoirs MUST use fixed-capacity, pre-allocated algorithms (such as an aligned circular buffer or fixed-size bucket reservoir) with bounded retention windows to guarantee predictable heap utilization.
- Services MUST implement circuit-breaking drop-tail policies on exemplar retention when telemetry exporter queue saturation exceeds 80% capacity.
- Instrumentation NEVER attaches exemplars synchronously on hot-path counters without checking the adaptive quota token balance first, avoiding lock contention and GC overhead.

## Bad vs Good Implementations
### Configuring Adaptive Exemplar Reservoir with Token-Bucket Ceiling

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Attaching raw user identifiers directly to metric attributes causes an unrecoverable cardinality explosion in the metrics store. Concurrently, using the default unconstrained context passing without an adaptive exemplar rate limiter floods the downstream collector with exemplars during cascading service failure spikes, causing memory pressure and dropped metrics.
```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 or naive exemplar handling with unbounded trace association on error paths
const meterProvider = new MeterProvider({
  readers: [
    new PeriodicExportingMetricReader({
      exporter: new OTLPMetricExporter(),
      exportIntervalMillis: 10000,
    }),
  ],
});

const meter = meterProvider.getMeter('billing-service');
const errorCounter = meter.createCounter('http_server_errors_total');

export function recordError(route: string, userId: string, err: Error) {
  const span = trace.getSpan(context.active());
  // Unchecked exemplar attachment attaching high-cardinality attributes directly on error storms
  errorCounter.add(1, {
    route,
    userId, // Cardinality explosion in labels
    errorMessage: err.message,
  }, context.active());
}
```

#### Production Standard (Correct)
> **Standard**: Enforces a strict Low-Cardinality metric dimension contract and implements an `AdaptiveExemplarFilter` backed by a token-bucket algorithm. It couples instrument views to a deterministic `FixedSizeExemplarReservoir`, guaranteeing that exemplar memory consumption remains bounded regardless of downstream microservice error cascades.
```typescript
import { context, trace, Context } from '@opentelemetry/api';
import {
  MeterProvider,
  PeriodicExportingMetricReader,
  View,
  InstrumentType,
  ExemplarFilter,
  ExemplarReservoir,
  FixedSizeExemplarReservoir,
} from '@opentelemetry/sdk-metrics';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';

// Token-bucket adaptive rate limiter for exemplar admission
class AdaptiveRateLimiter {
  private tokens: number;
  private lastRefill: number = Date.now();

  constructor(
    private readonly capacity: number = 50,
    private readonly refillRatePerSec: number = 10
  ) {
    this.tokens = capacity;
  }

  public allow(): boolean {
    const now = Date.now();
    const elapsed = (now - this.lastRefill) / 1000;
    this.tokens = Math.min(this.capacity, this.tokens + elapsed * this.refillRatePerSec);
    this.lastRefill = now;

    if (this.tokens >= 1) {
      this.tokens -= 1;
      return true;
    }
    return false;
  }
}

class AdaptiveExemplarFilter implements ExemplarFilter {
  private readonly limiter = new AdaptiveRateLimiter(50, 10);

  shouldSample(value: number, timestamp: number, ctx: Context): boolean {
    const span = trace.getSpan(ctx);
    if (!span || !span.spanContext().isValid || !span.spanContext().isSampled) {
      return false;
    }
    // Dynamic gating: Drop exemplar if quota is exceeded during error storms
    return this.limiter.allow();
  }
}

export const exemplarFilter = new AdaptiveExemplarFilter();

const meterProvider = new MeterProvider({
  exemplarFilter,
  readers: [
    new PeriodicExportingMetricReader({
      exporter: new OTLPMetricExporter(),
      exportIntervalMillis: 10000,
    }),
  ],
  views: [
    new View({
      instrumentType: InstrumentType.COUNTER,
      exemplarReservoirFactory: () => new FixedSizeExemplarReservoir(16),
    }),
  ],
});

const meter = meterProvider.getMeter('billing-service');
const errorCounter = meter.createCounter('http_server_errors_total');

export function recordError(route: string, errorCode: string) {
  // Invariant preserved: Low-cardinality attributes; exemplars dynamically sampled via reservoir ceiling
  errorCounter.add(1, {
    route,
    error_code: errorCode,
  }, context.active());
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: OpenTelemetry High-Cardinality Dynamic Exemplar Retention and Adaptive Quota Standard

OpenTelemetry High-Cardinality Dynamic Exemplar Retention and Adaptive Quota Standard defines an industrial-grade behavioral standard for devops engineering. Enforces adaptive exemplar sampling and dynamic rate-limiting on OpenTelemetry metric streams to prevent backend telemetry degradation during distributed incidents. Mandates deterministic exemplar reservation reservoirs bounded by leaky-bucket quotas that throttle high-cardinality trace associations during error spikes. Ensures metric collection pipelines maintain zero-allocation invariants and resist OOM crashes under downstream telemetry partition events. 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 Exemplar Sampling on Error Metrics Triggering Telemetry Backpressure and Collector OOM 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 Exemplar Sampling on Error Metrics Triggering Telemetry Backpressure and Collector OOMCritical
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 an explicit ExemplarFilter (e.g., TraceBasedExemplarFilter) wrapped with an adaptive rate-limiting or token-bucket ceiling to prevent metric stream bloat during error storms.

Engineers MUST ALWAYS configure an explicit ExemplarFilter (e.g., TraceBasedExemplarFilter) wrapped with an adaptive rate-limiting or token-bucket ceiling to prevent metric stream bloat during error storms.

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 attach raw, unbound high-cardinality metadata (such as tenant IDs, raw UUIDs, or unhashed query strings) directly to exemplar attributes

Engineers MUST NOT attach raw, unbound high-cardinality metadata (such as tenant IDs, raw UUIDs, or unhashed query strings) directly to exemplar attributes; attributes MUST be restricted to standard trace/span IDs and pre-hashed cluster identifiers.

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

Reservoirs MUST use fixed-capacity, pre-allocated algorithms (such as an aligned circular buffer or fixed-size bucket reservoir) with bounded retention windows to guarantee predictable heap utilization.

Reservoirs MUST use fixed-capacity, pre-allocated algorithms (such as an aligned circular buffer or fixed-size bucket reservoir) with bounded retention windows to guarantee predictable heap utilization.

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 implement circuit-breaking drop-tail policies on exemplar retention when telemetry exporter queue saturation exceeds 80% capacity.

Services MUST implement circuit-breaking drop-tail policies on exemplar retention when telemetry exporter queue saturation exceeds 80% capacity.

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 NEVER attaches exemplars synchronously on hot-path counters without checking the adaptive quota token balance first, avoiding lock contention and GC overhead.

Instrumentation NEVER attaches exemplars synchronously on hot-path counters without checking the adaptive quota token balance first, avoiding lock contention and GC overhead.

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 High-Cardinality Dynamic Exemplar Retention and Adaptive Quota Standard critical for AI coding assistants?

Enforces adaptive exemplar sampling and dynamic rate-limiting on OpenTelemetry metric streams to prevent backend telemetry degradation during distributed incidents. Mandates deterministic exemplar reservation reservoirs bounded by leaky-bucket quotas that throttle high-cardinality trace associations during error spikes. Ensures metric collection pipelines maintain zero-allocation invariants and resist OOM crashes under downstream telemetry partition events. 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 Exemplar Sampling on Error Metrics Triggering Telemetry Backpressure and Collector OOM) 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