This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: otel-bounded-exemplar-trace-correlation

OpenTelemetry Bounded Exemplar Trace-Metric Correlation Standard

Enforces strict memory-bounded sampling and rate-limiting on OpenTelemetry metric exemplars attached to high-cardinality latency measurements. Prevents telemetry collector and pipeline ingestion nodes from encountering Out-Of-Memory (OOM) crashes by restricting exemplar allocation to fixed-size reservoir samplers and filtering out non-sampled or sub-p99 traces.

Prohibited Anti-Pattern: Attaching Unfiltered Trace Contexts to Metrics via Unbounded Exemplar Reservoirs

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry Bounded Exemplar Trace-Metric Correlation Standard - Enforces strict memory-bounded sampling and rate-limiting on OpenTelemetry metric exemplars attached to high-cardinality latency measurements. Prevents telemetry collector and pipeline ingestion nodes from encountering Out-Of-Memory (OOM) crashes by restricting exemplar allocation to fixed-size reservoir samplers and filtering out non-sampled or sub-p99 traces.
globs: src/telemetry/**/*.{ts,js}, src/observability/**/*.{ts,js}, src/metrics/**/*.{ts,js}, src/instrumentation/**/*.{ts,js}, otel-collector/**/*.{yaml,yml}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/otel-bounded-exemplar-trace-correlation"
---

# OpenTelemetry Bounded Exemplar Trace-Metric Correlation Standard

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Attaching Unfiltered Trace Contexts to Metrics via Unbounded Exemplar Reservoirs

## System Constraints
You are a Principal Observability and Infrastructure Architect enforcing the OpenTelemetry Bounded Exemplar Trace-Metric Correlation Standard. Unbounded exemplar collection leaks high-cardinality baggage and trace identifiers into time-series pipelines, overwhelming downstream collectors and storage backends (Prometheus, Cortex, Mimir). You must strictly enforce bounded reservoir algorithms, threshold-gated exemplar sampling, and rigorous dimension filtering across all metric instruments.

## Strict Guidelines
- Services MUST ALWAYS configure fixed-capacity reservoir samplers (e.g., AlignedHistogramBucketExemplarReservoir or BoundedMemoryReservoir) when attaching trace contexts to metric histograms.
- Engineers MUST NOT attach exemplars to metrics on every HTTP/RPC event; exemplars MUST be gated behind sampled span context checks and latency threshold predicates.
- Instrumentation MUST limit exemplar attributes strictly to trace_id and span_id, explicitly stripping unbounded baggage or dynamic request headers.
- Metrics aggregation configurations MUST enforce explicit bucket count boundaries and prevent unbounded dynamic label generation alongside exemplar tracking.
- Telemetry pipelines MUST configure drop policies on collectors to prune redundant exemplars exceeding defined ingestion rate limits.

## Bad vs Good Implementations
### Bounded vs Unbounded Exemplar Recording in TypeScript Instrumentation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Directly attaching trace contexts and arbitrary user headers into metric attributes bypasses exemplar reservoir limits, turning time-series keys into high-cardinality memory sinks that crash collectors.
```typescript
import { trace, context } from '@opentelemetry/api';
import { MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';

const meterProvider = new MeterProvider();
const meter = meterProvider.getMeter('api-gateway');
const latencyHistogram = meter.createHistogram('http_request_duration_ms');

export function recordRequestDurationBad(durationMs: number, reqHeaders: Record<string, string>) {
  const span = trace.getSpan(context.active());
  // ANTI-PATTERN: Attaching raw span context and dynamic high-cardinality headers
  // on every single request without checking sampling state or bounds.
  latencyHistogram.record(durationMs, {
    trace_id: span?.spanContext().traceId ?? 'none',
    user_agent: reqHeaders['user-agent'] ?? 'unknown',
    user_id: reqHeaders['x-user-id'] ?? 'anonymous',
    exemplar_data: JSON.stringify(reqHeaders)
  });
}
```

#### Production Standard (Correct)
> **Standard**: Validates sampling flags and restricts exemplar attachment exclusively to tail latency (>= p99 threshold), using low-cardinality standard attributes and native context passing to leverage SDK-managed bounded reservoirs.
```typescript
import { trace, context, TraceFlags } from '@opentelemetry/api';
import { MeterProvider, View, ExplicitBucketHistogramAggregation } from '@opentelemetry/sdk-metrics';

const P99_THRESHOLD_MS = 250;
const meterProvider = new MeterProvider();
const meter = meterProvider.getMeter('api-gateway');

// Explicitly bounded histogram view to restrict aggregation memory
meterProvider.addView(
  new View({
    instrumentName: 'http_request_duration_ms',
    aggregation: new ExplicitBucketHistogramAggregation([5, 25, 50, 100, 250, 500, 1000, 2500])
  })
);

const latencyHistogram = meter.createHistogram('http_request_duration_ms', {
  unit: 'ms',
  description: 'HTTP duration with bounded tail exemplars'
});

export function recordRequestDurationGood(durationMs: number, method: string, status: number) {
  const spanContext = trace.getSpan(context.active())?.spanContext();

  // INVARIANT: Exemplars are only correlated if span is sampled and latency crosses threshold
  const isSampled = spanContext && (spanContext.traceFlags & TraceFlags.SAMPLED) === TraceFlags.SAMPLED;
  const isTailLatency = durationMs >= P99_THRESHOLD_MS;

  const attributes = {
    'http.request.method': method,
    'http.response.status_code': status
  };

  if (isSampled && isTailLatency) {
    // Bound exemplar correlation safely with minimal invariant attributes
    latencyHistogram.record(durationMs, attributes, context.active());
  } else {
    latencyHistogram.record(durationMs, attributes);
  }
}
```

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 Bounded Exemplar Trace-Metric Correlation Standard

OpenTelemetry Bounded Exemplar Trace-Metric Correlation Standard defines an industrial-grade behavioral standard for devops engineering. Enforces strict memory-bounded sampling and rate-limiting on OpenTelemetry metric exemplars attached to high-cardinality latency measurements. Prevents telemetry collector and pipeline ingestion nodes from encountering Out-Of-Memory (OOM) crashes by restricting exemplar allocation to fixed-size reservoir samplers and filtering out non-sampled or sub-p99 traces. 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 Unfiltered Trace Contexts to Metrics via Unbounded Exemplar Reservoirs 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 Unfiltered Trace Contexts to Metrics via Unbounded Exemplar ReservoirsCritical
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

Services MUST ALWAYS configure fixed-capacity reservoir samplers (e.g., AlignedHistogramBucketExemplarReservoir or BoundedMemoryReservoir) when attaching trace contexts to metric histograms.

Services MUST ALWAYS configure fixed-capacity reservoir samplers (e.g., AlignedHistogramBucketExemplarReservoir or BoundedMemoryReservoir) when attaching trace contexts to metric histograms.

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 exemplars to metrics on every HTTP/RPC event

Engineers MUST NOT attach exemplars to metrics on every HTTP/RPC event; exemplars MUST be gated behind sampled span context checks and latency threshold predicates.

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

Instrumentation MUST limit exemplar attributes strictly to trace_id and span_id, explicitly stripping unbounded baggage or dynamic request headers.

Instrumentation MUST limit exemplar attributes strictly to trace_id and span_id, explicitly stripping unbounded baggage or dynamic request 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

Metrics aggregation configurations MUST enforce explicit bucket count boundaries and prevent unbounded dynamic label generation alongside exemplar tracking.

Metrics aggregation configurations MUST enforce explicit bucket count boundaries and prevent unbounded dynamic label generation alongside exemplar tracking.

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

Telemetry pipelines MUST configure drop policies on collectors to prune redundant exemplars exceeding defined ingestion rate limits.

Telemetry pipelines MUST configure drop policies on collectors to prune redundant exemplars exceeding defined ingestion rate limits.

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 Bounded Exemplar Trace-Metric Correlation Standard critical for AI coding assistants?

Enforces strict memory-bounded sampling and rate-limiting on OpenTelemetry metric exemplars attached to high-cardinality latency measurements. Prevents telemetry collector and pipeline ingestion nodes from encountering Out-Of-Memory (OOM) crashes by restricting exemplar allocation to fixed-size reservoir samplers and filtering out non-sampled or sub-p99 traces. 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 Unfiltered Trace Contexts to Metrics via Unbounded Exemplar Reservoirs) 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