This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: opentelemetry-metric-exemplar-trace-sampling-governance

Deterministic Metric Exemplar and Trace-Sampling Coupling Standard

Enforces deterministic coupling between high-cardinality tail-latency metric histograms and distributed trace sampling decisions. Guarantees that P99+ tail-latency metrics linked to trace exemplars resolve to fully materialized traces, eliminating 404 trace drops and ghost exemplars during production incident triage.

Prohibited Anti-Pattern: Decoupled Probabilistic Head-Sampling Yielding Ghost Exemplars and Missing P99 Trace Context

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Deterministic Metric Exemplar and Trace-Sampling Coupling Standard - Enforces deterministic coupling between high-cardinality tail-latency metric histograms and distributed trace sampling decisions. Guarantees that P99+ tail-latency metrics linked to trace exemplars resolve to fully materialized traces, eliminating 404 trace drops and ghost exemplars during production incident triage.
globs: src/**/telemetry.{ts,js}, src/**/instrumentation.{ts,js}, src/**/otel*.{ts,js}, src/observability/**/*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentelemetry-metric-exemplar-trace-sampling-governance"
---

# Deterministic Metric Exemplar and Trace-Sampling Coupling Standard

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Decoupled Probabilistic Head-Sampling Yielding Ghost Exemplars and Missing P99 Trace Context

## System Constraints
You are a Principal Software Architect enforcing OpenTelemetry Metric Exemplar Trace-Sampling Governance. You must ensure that high-cardinality metric aggregations (specifically tail latency histograms) maintain deterministic references to valid, fully persisted distributed traces. Decoupled configurations where probabilistic head-samplers drop traces while metric exemplar filters point to discarded trace IDs create unresolvable observability black holes. Every metric exemplar recorded MUST reference a trace that is guaranteed to be sampled and exported.

## Strict Guidelines
- Engineers MUST ALWAYS couple ExemplarFilter decisions with the TracerProvider's sampling state, ensuring non-sampled traces are never attached as metric exemplars unless tail-sampling collector pipelines are explicitly configured to retain them.
- Engineers MUST NOT configure decoupled, low-ratio head-sampling (e.g., `TraceIdRatioBased(0.01)`) without setting `TraceBasedExemplarFilter` or an exemplar-aware composite sampler, which otherwise emits 'ghost exemplars' that return 404 in APM backends.
- Code MUST explicitly configure `MeterProvider` with a `View` defining explicit bucket boundaries for latency histograms and an `ExemplarReservoir` tailored for high-percentile latency capture.
- Services MUST ensure span contexts passed to metric recording calls contain active, sampled trace contexts (`TraceFlags.SAMPLED`) when an exemplar is evaluated for retention.
- Trace sampling configurations MUST use adaptive or `ParentBased` composite samplers that force trace retention whenever a downstream operation violates predefined SLA or latency thresholds.

## Bad vs Good Implementations
### Configuring Metric Exemplar Reservoirs with Coordinated Trace Sampling

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using a naive low-ratio head sampler without configuring metric exemplar reservoirs or trace-exemplar coordination causes 'ghost exemplars'. High latency P99/P99.9 outliers will record metrics, but their corresponding distributed traces will be dropped at root span creation, leaving SREs with broken trace links during incidents.
```typescript
import { NodeSDK } from '@opentelemetry/sdk-node';
import { TraceIdRatioBasedSampler, ParentBasedSampler } from '@opentelemetry/sdk-trace-node';
import { MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc';

// ANTI-PATTERN: Decoupled 1% head-sampling with default exemplar behavior.
// 99% of requests that breach P99 latency will have their traces dropped by the sampler,
// but the histogram will still record latency, leaving exemplars pointing to non-existent traces.
export const sdk = new NodeSDK({
  traceExporter: new OTLPTraceExporter(),
  metricReader: new PeriodicExportingMetricReader({
    exporter: new OTLPMetricExporter(),
    exportIntervalMillis: 10000,
  }),
  // Decoupled head sampler drops 99% of spans indiscriminately
  sampler: new ParentBasedSampler({
    root: new TraceIdRatioBasedSampler(0.01),
  }),
});

sdk.start();
```

#### Production Standard (Correct)
> **Standard**: Enforces deterministic coupling by pairing `TraceBasedExemplarFilter` with a coordinated sampler. Metric exemplars are guaranteed to attach only to traces that are definitively sampled and exported, eliminating broken links in observability platforms.
```typescript
import { NodeSDK } from '@opentelemetry/sdk-node';
import {
  Sampler,
  SamplingDecision,
  SamplingResult,
  ParentBasedSampler,
} from '@opentelemetry/sdk-trace-node';
import {
  MeterProvider,
  PeriodicExportingMetricReader,
  View,
  ExplicitBucketHistogramAggregation,
  TraceBasedExemplarFilter,
} from '@opentelemetry/sdk-metrics';
import { Context, TraceFlags } from '@opentelemetry/api';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc';

// Deterministic coupling: Force sampling for SLA-critical paths and link to exemplars
class TailSlaAwareSampler implements Sampler {
  private readonly baseRatioSampler = new ParentBasedSampler({
    root: new (require('@opentelemetry/sdk-trace-node').TraceIdRatioBasedSampler)(0.05),
  });

  shouldSample(context: Context, traceId: string, spanName: string, spanKind: number, attributes: Record<string, unknown>, links: unknown[]): SamplingResult {
    // Guarantee 100% sampling on critical API routes or explicit debug headers
    const isCriticalPath = typeof attributes['http.target'] === 'string' && attributes['http.target'].startsWith('/api/v1/checkout');
    if (isCriticalPath) {
      return { decision: SamplingDecision.RECORD_AND_SAMPLED, attributes: { 'sampling.reason': 'critical_path' } };
    }
    return this.baseRatioSampler.shouldSample(context, traceId, spanName, spanKind, attributes, links);
  }

  toString(): string { return 'TailSlaAwareSampler'; }
}

const metricReader = new PeriodicExportingMetricReader({
  exporter: new OTLPMetricExporter(),
  exportIntervalMillis: 15000,
});

const meterProvider = new MeterProvider({
  readers: [metricReader],
  // INVARIANT: TraceBasedExemplarFilter ensures metric exemplars ONLY capture sampled traces
  exemplarFilter: new TraceBasedExemplarFilter(),
  views: [
    new View({
      instrumentName: '*latency*',
      aggregation: new ExplicitBucketHistogramAggregation([
        5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000,
      ]),
    }),
  ],
});

export const sdk = new NodeSDK({
  traceExporter: new OTLPTraceExporter(),
  meterProvider,
  sampler: new TailSlaAwareSampler(),
});

sdk.start();
```

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: Deterministic Metric Exemplar and Trace-Sampling Coupling Standard

Deterministic Metric Exemplar and Trace-Sampling Coupling Standard defines an industrial-grade behavioral standard for devops engineering. Enforces deterministic coupling between high-cardinality tail-latency metric histograms and distributed trace sampling decisions. Guarantees that P99+ tail-latency metrics linked to trace exemplars resolve to fully materialized traces, eliminating 404 trace drops and ghost exemplars during production incident triage. 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: Decoupled Probabilistic Head-Sampling Yielding Ghost Exemplars and Missing P99 Trace Context 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 DriftDecoupled Probabilistic Head-Sampling Yielding Ghost Exemplars and Missing P99 Trace ContextCritical
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 couple ExemplarFilter decisions with the TracerProvider's sampling state, ensuring non-sampled traces are never attached as metric exemplars unless tail-sampling collector pipelines are explicitly configured to retain them.

Engineers MUST ALWAYS couple ExemplarFilter decisions with the TracerProvider's sampling state, ensuring non-sampled traces are never attached as metric exemplars unless tail-sampling collector pipelines are explicitly configured to retain them.

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 configure decoupled, low-ratio head-sampling (e.g., `TraceIdRatioBased(0.01)`) without setting `TraceBasedExemplarFilter` or an exemplar-aware composite sampler, which otherwise emits 'ghost exemplars' that return 404 in APM backends.

Engineers MUST NOT configure decoupled, low-ratio head-sampling (e.g., `TraceIdRatioBased(0.01)`) without setting `TraceBasedExemplarFilter` or an exemplar-aware composite sampler, which otherwise emits 'ghost exemplars' that return 404 in APM backends.

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

Code MUST explicitly configure `MeterProvider` with a `View` defining explicit bucket boundaries for latency histograms and an `ExemplarReservoir` tailored for high-percentile latency capture.

Code MUST explicitly configure `MeterProvider` with a `View` defining explicit bucket boundaries for latency histograms and an `ExemplarReservoir` tailored for high-percentile latency capture.

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 ensure span contexts passed to metric recording calls contain active, sampled trace contexts (`TraceFlags.SAMPLED`) when an exemplar is evaluated for retention.

Services MUST ensure span contexts passed to metric recording calls contain active, sampled trace contexts (`TraceFlags.SAMPLED`) when an exemplar is evaluated for retention.

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

Trace sampling configurations MUST use adaptive or `ParentBased` composite samplers that force trace retention whenever a downstream operation violates predefined SLA or latency thresholds.

Trace sampling configurations MUST use adaptive or `ParentBased` composite samplers that force trace retention whenever a downstream operation violates predefined SLA or latency thresholds.

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 Deterministic Metric Exemplar and Trace-Sampling Coupling Standard critical for AI coding assistants?

Enforces deterministic coupling between high-cardinality tail-latency metric histograms and distributed trace sampling decisions. Guarantees that P99+ tail-latency metrics linked to trace exemplars resolve to fully materialized traces, eliminating 404 trace drops and ghost exemplars during production incident triage. 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 (Decoupled Probabilistic Head-Sampling Yielding Ghost Exemplars and Missing P99 Trace Context) 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

Deterministic