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

OpenTelemetry Trace-Correlated Metric Cardinality Governor and Exemplar Safeguard

Enforces deterministic metric cardinality budgets across all OpenTelemetry telemetry pipelines to protect time-series databases (TSDBs) from memory exhaustion during traffic anomalies. Dynamically normalizes or drops volatile labels (such as customer IDs, UUIDs, or raw query parameters) while systematically capturing trace context via exemplars on Prometheus and OTLP exporters.

Prohibited Anti-Pattern: Unbounded Metric Attribute Pollution Causing TSDB Out-of-Memory Crashing Under Peak Traffic

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry Trace-Correlated Metric Cardinality Governor and Exemplar Safeguard - Enforces deterministic metric cardinality budgets across all OpenTelemetry telemetry pipelines to protect time-series databases (TSDBs) from memory exhaustion during traffic anomalies. Dynamically normalizes or drops volatile labels (such as customer IDs, UUIDs, or raw query parameters) while systematically capturing trace context via exemplars on Prometheus and OTLP exporters.
globs: src/**/telemetry/**/*.{ts,js}, src/**/metrics/**/*.{ts,js}, src/**/monitoring/**/*.{ts,js}, src/**/*telemetry*.{ts,js}, src/**/*metric*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentelemetry-cardinality-governor-exemplar-preservation"
---

# OpenTelemetry Trace-Correlated Metric Cardinality Governor and Exemplar Safeguard

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Unbounded Metric Attribute Pollution Causing TSDB Out-of-Memory Crashing Under Peak Traffic

## System Constraints
You are a Principal Observability and Site Reliability Engineer enforcing OpenTelemetry metric cardinality governance. Uncontrolled time-series explosion destroys TSDB stability (Prometheus, Cortex, M3DB) during traffic spikes and incident investigations. You must mandate strict dynamic attribute shedding for high-cardinality values, route volatile entity identifiers into trace spans or metric exemplars, and enforce cardinality limiters at instrument or meter registration boundaries.

## Strict Guidelines
- Engineers MUST NEVER inject dynamic, user-generated, or unbounded values (such as user IDs, session tokens, tenant IDs, email addresses, order IDs, or raw URL paths) as raw Metric attributes.
- Engineers MUST capture fine-grained transactional identifiers inside OpenTelemetry Spans and attach current trace/span IDs as exemplars to aggregated metric recordings.
- Metrics pipelines MUST configure explicit cardinality limiting processors (e.g., View aggregators with attribute filter allowlists or bounded LRU label caches) that clamp unexpected labels into a fallback value such as '__other__'.
- Telemetry modules MUST utilize route templates (e.g., '/api/v1/users/:id') rather than literal resolved request URLs (e.g., '/api/v1/users/12839123') when recording HTTP metrics.
- Exemplar filters MUST be explicitly bound to TraceContext (such as TraceContextExemplarFilter or AlwaysOnExemplarFilter) to ensure correlation survives attribute shedding.

## Bad vs Good Implementations
### Preventing Unbounded Metric Attributes via Exemplar Preservation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Directly injecting raw entity IDs (`customerId`, `invoiceId`) into metric label attributes causes unbounded time-series explosion (high cardinality), leading to TSDB OOM crashes under high traffic.
```typescript
import { metrics, trace, context } from '@opentelemetry/api';

const meter = metrics.getMeter('billing-service');
const invoiceCounter = meter.createCounter('invoices_processed_total', {
  description: 'Tracks processed invoices',
});

export function processInvoice(invoiceId: string, customerId: string, amount: number) {
  // CRITICAL BUG: Injecting customerId and invoiceId creates unbounded metric series.
  // Under peak anomaly or retry loops, this triggers a TSDB Out-Of-Memory (OOM) event.
  invoiceCounter.add(1, {
    'customer.id': customerId,
    'invoice.id': invoiceId,
    'currency': 'USD'
  });
}
```

#### Production Standard (Correct)
> **Standard**: High-cardinality attributes (`customerId`, `invoiceId`) are moved exclusively to the active Span. Metric attributes are clamped to a deterministic, low-cardinality set (`tier`, `currency`, `status`). The active context is passed to the metric `.add()` call, allowing the exporter to preserve trace-correlated exemplars without expanding TSDB series.
```typescript
import { metrics, trace, context, SpanStatusCode } from '@opentelemetry/api';

const meter = metrics.getMeter('billing-service');
const tracer = trace.getTracer('billing-service');

const invoiceCounter = meter.createCounter('invoices_processed_total', {
  description: 'Tracks processed invoices with bounded cardinality',
});

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

export async function processInvoice(
  invoiceId: string,
  customerId: string,
  tier: string,
  amount: number
): Promise<void> {
  return tracer.startActiveSpan('processInvoice', async (span) => {
    try {
      // High-cardinality values are safely isolated to trace span attributes
      span.setAttribute('customer.id', customerId);
      span.setAttribute('invoice.id', invoiceId);
      span.setAttribute('invoice.amount', amount);

      // Cardinality Governor: Enforce strict bounded label allowlist
      const safeTier = ALLOWED_TIERS.has(tier) ? tier : '__other__';

      // TraceContext is implicitly captured via exemplar filters on the active context
      invoiceCounter.add(1, {
        'customer.tier': safeTier,
        'currency': 'USD',
        'status': 'success'
      }, context.active());

      span.setStatus({ code: SpanStatusCode.OK });
    } catch (error) {
      span.recordException(error as Error);
      span.setStatus({ code: SpanStatusCode.ERROR });
      invoiceCounter.add(1, {
        'customer.tier': 'unknown',
        'currency': 'USD',
        'status': 'error'
      }, context.active());
      throw error;
    } finally {
      span.end();
    }
  });
}
```

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 Trace-Correlated Metric Cardinality Governor and Exemplar Safeguard

OpenTelemetry Trace-Correlated Metric Cardinality Governor and Exemplar Safeguard defines an industrial-grade behavioral standard for devops engineering. Enforces deterministic metric cardinality budgets across all OpenTelemetry telemetry pipelines to protect time-series databases (TSDBs) from memory exhaustion during traffic anomalies. Dynamically normalizes or drops volatile labels (such as customer IDs, UUIDs, or raw query parameters) while systematically capturing trace context via exemplars on Prometheus and OTLP exporters. 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 Metric Attribute Pollution Causing TSDB Out-of-Memory Crashing Under Peak Traffic 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 Metric Attribute Pollution Causing TSDB Out-of-Memory Crashing Under Peak TrafficCritical
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 NEVER inject dynamic, user-generated, or unbounded values (such as user IDs, session tokens, tenant IDs, email addresses, order IDs, or raw URL paths) as raw Metric attributes.

Engineers MUST NEVER inject dynamic, user-generated, or unbounded values (such as user IDs, session tokens, tenant IDs, email addresses, order IDs, or raw URL paths) as raw Metric attributes.

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 capture fine-grained transactional identifiers inside OpenTelemetry Spans and attach current trace/span IDs as exemplars to aggregated metric recordings.

Engineers MUST capture fine-grained transactional identifiers inside OpenTelemetry Spans and attach current trace/span IDs as exemplars to aggregated metric recordings.

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

Metrics pipelines MUST configure explicit cardinality limiting processors (e.g., View aggregators with attribute filter allowlists or bounded LRU label caches) that clamp unexpected labels into a fallback value such as '__other__'.

Metrics pipelines MUST configure explicit cardinality limiting processors (e.g., View aggregators with attribute filter allowlists or bounded LRU label caches) that clamp unexpected labels into a fallback value such as '__other__'.

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

Telemetry modules MUST utilize route templates (e.g., '/api/v1/users/

Telemetry modules MUST utilize route templates (e.g., '/api/v1/users/:id') rather than literal resolved request URLs (e.g., '/api/v1/users/12839123') when recording HTTP metrics.

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

Exemplar filters MUST be explicitly bound to TraceContext (such as TraceContextExemplarFilter or AlwaysOnExemplarFilter) to ensure correlation survives attribute shedding.

Exemplar filters MUST be explicitly bound to TraceContext (such as TraceContextExemplarFilter or AlwaysOnExemplarFilter) to ensure correlation survives attribute shedding.

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 Trace-Correlated Metric Cardinality Governor and Exemplar Safeguard critical for AI coding assistants?

Enforces deterministic metric cardinality budgets across all OpenTelemetry telemetry pipelines to protect time-series databases (TSDBs) from memory exhaustion during traffic anomalies. Dynamically normalizes or drops volatile labels (such as customer IDs, UUIDs, or raw query parameters) while systematically capturing trace context via exemplars on Prometheus and OTLP exporters. 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 Metric Attribute Pollution Causing TSDB Out-of-Memory Crashing Under Peak Traffic) 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