This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: opentelemetry-w3c-tracecontext-exemplar-correlation-invariant

OpenTelemetry W3C TraceContext Ingestion & Exemplar Correlation Invariant

Enforces strict extraction and propagation of W3C TraceContext headers ('traceparent' and 'tracestate') across polyglot boundaries while guaranteeing active spans attach TraceID and SpanID as exemplars to Prometheus/OpenMetrics histograms. Eliminates distributed diagnostic blindspots by ensuring 100% correlation between tail-latency metric excursions and corresponding distributed trace spans.

Prohibited Anti-Pattern: Unlinked Metric Anomalies from Stripped Trace Headers and Missing Exemplar Context

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry W3C TraceContext Ingestion & Exemplar Correlation Invariant - Enforces strict extraction and propagation of W3C TraceContext headers ('traceparent' and 'tracestate') across polyglot boundaries while guaranteeing active spans attach TraceID and SpanID as exemplars to Prometheus/OpenMetrics histograms. Eliminates distributed diagnostic blindspots by ensuring 100% correlation between tail-latency metric excursions and corresponding distributed trace spans.
globs: src/**/*.{ts,tsx,js,jsx}, instrumentation/**/*.{ts,js}, middleware/**/*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentelemetry-w3c-tracecontext-exemplar-correlation-invariant"
---

# OpenTelemetry W3C TraceContext Ingestion & Exemplar Correlation Invariant

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Unlinked Metric Anomalies from Stripped Trace Headers and Missing Exemplar Context

## System Constraints
You are a Principal Observability Architect enforcing strict OpenTelemetry W3C TraceContext propagation and Metric Exemplar linking. You must reject any telemetry design where metric recordings bypass active span context or where HTTP/gRPC ingress handlers fail to propagate inbound W3C 'traceparent' headers. Mandate that every high-cardinality latency measurement records the active TraceID and SpanID as OpenMetrics exemplars.

## Strict Guidelines
- Services MUST ALWAYS extract incoming 'traceparent' and 'tracestate' headers using OpenTelemetry propagation API ('propagation.extract') into an active context before executing business logic.
- Engineers MUST ALWAYS pass the extracted context explicitly to downstream asynchronous operations and outbound HTTP/gRPC clients via 'propagation.inject'.
- Metric recording instrumentation MUST explicitly pass the active OpenTelemetry 'Context' containing the sampled span to histogram/gauge recording APIs to enable OpenMetrics Exemplar attachment.
- Engineers MUST NOT manually parse or strip W3C headers ('traceparent', 'tracestate') via custom string split operations or regex.
- Services MUST NOT record duration histograms in isolated root contexts that disconnect metric samples from the active trace ID.
- Custom HTTP fetch wrappers and axios interceptors MUST inject 'traceparent' and 'tracestate' on all egress network requests to preserve trace continuity across polyglot microservice boundaries.
- Instrumentation MUST reject un-sampled span exemplars when configured for high-frequency low-overhead telemetry pipelines.

## Bad vs Good Implementations
### W3C TraceContext Extraction and Metric Exemplar Ingestion in Node.js HTTP Handler

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The handler ignores incoming distributed headers, resetting the trace tree into isolated fragments. Furthermore, latencyHistogram.record() is called without the active context parameter, causing Prometheus/OpenMetrics exporters to omit exemplar metadata (TraceID and SpanID), creating an irrecoverable blindspot between latency spikes and trace data.
```typescript
import { metrics } from '@opentelemetry/api';
import { IncomingMessage, ServerResponse } from 'http';

const meter = metrics.getMeter('billing-service');
const latencyHistogram = meter.createHistogram('http_request_duration_seconds', {
  description: 'HTTP request duration in seconds',
  unit: 's'
});

export async function handleRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {
  const startTime = process.hrtime();
  
  // ANTI-PATTERN: Ignoring incoming W3C traceparent headers and creating orphaned contexts.
  // Recording latency without passing the active OpenTelemetry context drops exemplar correlation.
  try {
    await processPayment();
    res.statusCode = 200;
    res.end('OK');
  } finally {
    const elapsed = process.hrtime(startTime);
    const durationSec = elapsed[0] + elapsed[1] / 1e9;
    // Missing context parameter: Prometheus cannot bind TraceID/SpanID exemplars to this bucket
    latencyHistogram.record(durationSec, { route: '/payments', status_code: '200' });
  }
}

async function processPayment(): Promise<void> {
  await new Promise(resolve => setTimeout(resolve, 50));
}
```

#### Production Standard (Correct)
> **Standard**: The invariant is satisfied: propagation.extract reads W3C 'traceparent' and 'tracestate' to preserve the global trace lineage. The active span is explicitly supplied via the context argument to latencyHistogram.record(), enabling the telemetry exporter to attach the exact TraceID and SpanID as an OpenMetrics exemplar.
```typescript
import {
  metrics,
  trace,
  propagation,
  ROOT_CONTEXT,
  SpanKind
} from '@opentelemetry/api';
import { IncomingMessage, ServerResponse } from 'http';

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

const latencyHistogram = meter.createHistogram('http_request_duration_seconds', {
  description: 'HTTP request duration in seconds with W3C exemplar linkage',
  unit: 's'
});

export async function handleRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {
  const startTime = process.hrtime();

  // Extract W3C TraceContext ('traceparent', 'tracestate') from raw headers
  const extractedContext = propagation.extract(ROOT_CONTEXT, req.headers);

  return tracer.startActiveSpan(
    'HTTP ' + (req.method ?? 'GET') + ' ' + (req.url ?? '/'),
    { kind: SpanKind.SERVER },
    extractedContext,
    async (span) => {
      try {
        await processPayment();
        res.statusCode = 200;
        res.end('OK');
      } catch (err: unknown) {
        span.recordException(err as Error);
        res.statusCode = 500;
        res.end('Internal Error');
        throw err;
      } finally {
        const elapsed = process.hrtime(startTime);
        const durationSec = elapsed[0] + elapsed[1] / 1e9;

        // Pass trace.setSpan(extractedContext, span) so metric exemplars capture TraceID/SpanID
        const activeCtx = trace.setSpan(extractedContext, span);
        latencyHistogram.record(durationSec, { route: '/payments', status_code: String(res.statusCode) }, activeCtx);
        span.end();
      }
    }
  );
}

async function processPayment(): Promise<void> {
  await new Promise(resolve => setTimeout(resolve, 50));
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: OpenTelemetry W3C TraceContext Ingestion & Exemplar Correlation Invariant

OpenTelemetry W3C TraceContext Ingestion & Exemplar Correlation Invariant defines an industrial-grade behavioral standard for devops engineering. Enforces strict extraction and propagation of W3C TraceContext headers ('traceparent' and 'tracestate') across polyglot boundaries while guaranteeing active spans attach TraceID and SpanID as exemplars to Prometheus/OpenMetrics histograms. Eliminates distributed diagnostic blindspots by ensuring 100% correlation between tail-latency metric excursions and corresponding distributed trace spans. 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: Unlinked Metric Anomalies from Stripped Trace Headers and Missing Exemplar 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 DriftUnlinked Metric Anomalies from Stripped Trace Headers and Missing Exemplar 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

Services MUST ALWAYS extract incoming 'traceparent' and 'tracestate' headers using OpenTelemetry propagation API ('propagation.extract') into an active context before executing business logic.

Services MUST ALWAYS extract incoming 'traceparent' and 'tracestate' headers using OpenTelemetry propagation API ('propagation.extract') into an active context before executing business logic.

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 ALWAYS pass the extracted context explicitly to downstream asynchronous operations and outbound HTTP/gRPC clients via 'propagation.inject'.

Engineers MUST ALWAYS pass the extracted context explicitly to downstream asynchronous operations and outbound HTTP/gRPC clients via 'propagation.inject'.

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

Metric recording instrumentation MUST explicitly pass the active OpenTelemetry 'Context' containing the sampled span to histogram/gauge recording APIs to enable OpenMetrics Exemplar attachment.

Metric recording instrumentation MUST explicitly pass the active OpenTelemetry 'Context' containing the sampled span to histogram/gauge recording APIs to enable OpenMetrics Exemplar attachment.

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

Engineers MUST NOT manually parse or strip W3C headers ('traceparent', 'tracestate') via custom string split operations or regex.

Engineers MUST NOT manually parse or strip W3C headers ('traceparent', 'tracestate') via custom string split operations or regex.

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

Services MUST NOT record duration histograms in isolated root contexts that disconnect metric samples from the active trace ID.

Services MUST NOT record duration histograms in isolated root contexts that disconnect metric samples from the active trace 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.
6

Custom HTTP fetch wrappers and axios interceptors MUST inject 'traceparent' and 'tracestate' on all egress network requests to preserve trace continuity across polyglot microservice boundaries.

Custom HTTP fetch wrappers and axios interceptors MUST inject 'traceparent' and 'tracestate' on all egress network requests to preserve trace continuity across polyglot microservice boundaries.

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.
7

Instrumentation MUST reject un-sampled span exemplars when configured for high-frequency low-overhead telemetry pipelines.

Instrumentation MUST reject un-sampled span exemplars when configured for high-frequency low-overhead telemetry pipelines.

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 W3C TraceContext Ingestion & Exemplar Correlation Invariant critical for AI coding assistants?

Enforces strict extraction and propagation of W3C TraceContext headers ('traceparent' and 'tracestate') across polyglot boundaries while guaranteeing active spans attach TraceID and SpanID as exemplars to Prometheus/OpenMetrics histograms. Eliminates distributed diagnostic blindspots by ensuring 100% correlation between tail-latency metric excursions and corresponding distributed trace spans. 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 (Unlinked Metric Anomalies from Stripped Trace Headers and Missing Exemplar 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

OpenTelemetry