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

Deterministic Trace-to-Profile Correlation for Continuous CPU and Allocation Invariants

Continuous profilers and distributed tracers must not operate in isolation. Low-overhead continuous CPU and memory profilers must deterministically propagate active OpenTelemetry TraceId and SpanId into profiling sample tags or pprof labels across all asynchronous execution contexts to guarantee sub-millisecond call-stack attribution for tail-latency outliers.

Prohibited Anti-Pattern: Decoupled Wall-Clock Profiling Without Active Trace-Span Context Propagation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Deterministic Trace-to-Profile Correlation for Continuous CPU and Allocation Invariants - Continuous profilers and distributed tracers must not operate in isolation. Low-overhead continuous CPU and memory profilers must deterministically propagate active OpenTelemetry TraceId and SpanId into profiling sample tags or pprof labels across all asynchronous execution contexts to guarantee sub-millisecond call-stack attribution for tail-latency outliers.
globs: src/instrumentation/**/*.{ts,js,go}, src/telemetry/**/*.{ts,js,go}, src/server/**/*.{ts,js,go}, src/middleware/**/*.{ts,js,go}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/otel-continuous-profiling-trace-correlation-standard"
---

# Deterministic Trace-to-Profile Correlation for Continuous CPU and Allocation Invariants

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Decoupled Wall-Clock Profiling Without Active Trace-Span Context Propagation

## System Constraints
You are a Principal Software Architect enforcing deterministic continuous profiling and distributed tracing correlation. Uncorrelated profiling samples are considered dead telemetry. You must ensure that every thread, async task, and event-loop microtask propagates OpenTelemetry TraceId and SpanId directly into the continuous profiler's sample labels (e.g., via pprof labels, Pyroscope tags, or Parca metadata) with zero unhandled context leaks.

## Strict Guidelines
- Services MUST initialize continuous profilers with OpenTelemetry context integration before bootstrapping the HTTP/RPC server or registering message consumers.
- Engineers MUST ALWAYS bind trace identifiers (TraceId, SpanId, TraceFlags) to the profiler label scope whenever entering an active span context.
- Engineers MUST NOT execute blocking or CPU-intensive operations inside detached asynchronous contexts that lose OTel Context or thread-local profiler labels.
- Profiler integrations MUST maintain overhead strictly below 2% CPU and memory utilization in production environments by using sampling-based pprof or eBPF mechanisms.
- Custom worker pools or manual Promise orchestration MUST explicitly restore the active OpenTelemetry context using `context.with()` to guarantee profiler sample attribution.
- Services MUST export profile data conforming to the OpenTelemetry Profiling Data Model (OTel Profiles) or standardized pprof formats containing span identifiers as sample labels.

## Bad vs Good Implementations
### Asynchronous Trace Context & Profiler Label Synchronization

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Continuous profiling runs as an isolated daemon. When `computeRiskScores` induces a P99 latency spike or allocations trigger GC pause, the flamegraph cannot be filtered by `trace_id` or `span_id`, rendering distributed trace diagnostics ineffective.
```typescript
import { trace } from '@opentelemetry/api';
import Pyroscope from '@pyroscope/nodejs';

// ANTI-PATTERN: Profiler runs globally without trace context injection;
// heavy CPU/allocation loops lose correlation to the specific span.
Pyroscope.init({ serverAddress: 'http://pyroscope:4040', appName: 'payment-service' });
Pyroscope.start();

export async function processTransaction(payload: any) {
  const tracer = trace.getTracer('payments');
  return tracer.startActiveSpan('processTransaction', async (span) => {
    try {
      // CPU intensive operation runs without span labels in the profiler sample
      const result = await computeRiskScores(payload);
      return result;
    } finally {
      span.end();
    }
  });
}

async function computeRiskScores(data: any) {
  // Tail-latency hotspot here is unlinked to span.traceId in flamegraphs
  return heavyComputation(data);
}
```

#### Production Standard (Correct)
> **Standard**: Wraps span execution within the profiler's label context (`wrapExecutionWithLabels` or equivalent pprof label setter). Samples collected during CPU or heap profiling contain the exact `trace_id` and `span_id`, allowing instantaneous bi-directional pivots between OTel spans and flamegraphs.
```typescript
import { trace, context } from '@opentelemetry/api';
import Pyroscope from '@pyroscope/nodejs';

Pyroscope.init({
  serverAddress: 'http://pyroscope:4040',
  appName: 'payment-service',
  tags: { env: process.env.NODE_ENV || 'production' }
});
Pyroscope.start();

export async function processTransaction(payload: any) {
  const tracer = trace.getTracer('payments');
  return tracer.startActiveSpan('processTransaction', async (span) => {
    const spanContext = span.spanContext();
    // PRODUCTION FIX: Explicitly bind active SpanId and TraceId to the profiler context
    return Pyroscope.wrapExecutionWithLabels(
      {
        trace_id: spanContext.traceId,
        span_id: spanContext.spanId,
        trace_flags: spanContext.traceFlags.toString(16).padStart(2, '0')
      },
      async () => {
        try {
          const result = await computeRiskScores(payload);
          return result;
        } finally {
          span.end();
        }
      }
    );
  });
}

async function computeRiskScores(data: any) {
  // Every sample collected here carries trace_id and span_id labels
  return heavyComputation(data);
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Deterministic Trace-to-Profile Correlation for Continuous CPU and Allocation Invariants

Deterministic Trace-to-Profile Correlation for Continuous CPU and Allocation Invariants defines an industrial-grade behavioral standard for devops engineering. Continuous profilers and distributed tracers must not operate in isolation. Low-overhead continuous CPU and memory profilers must deterministically propagate active OpenTelemetry TraceId and SpanId into profiling sample tags or pprof labels across all asynchronous execution contexts to guarantee sub-millisecond call-stack attribution for tail-latency outliers. 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 Wall-Clock Profiling Without Active Trace-Span Context Propagation 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 Wall-Clock Profiling Without Active Trace-Span Context PropagationCritical
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 initialize continuous profilers with OpenTelemetry context integration before bootstrapping the HTTP/RPC server or registering message consumers.

Services MUST initialize continuous profilers with OpenTelemetry context integration before bootstrapping the HTTP/RPC server or registering message consumers.

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 bind trace identifiers (TraceId, SpanId, TraceFlags) to the profiler label scope whenever entering an active span context.

Engineers MUST ALWAYS bind trace identifiers (TraceId, SpanId, TraceFlags) to the profiler label scope whenever entering an active span context.

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

Engineers MUST NOT execute blocking or CPU-intensive operations inside detached asynchronous contexts that lose OTel Context or thread-local profiler labels.

Engineers MUST NOT execute blocking or CPU-intensive operations inside detached asynchronous contexts that lose OTel Context or thread-local profiler labels.

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

Profiler integrations MUST maintain overhead strictly below 2% CPU and memory utilization in production environments by using sampling-based pprof or eBPF mechanisms.

Profiler integrations MUST maintain overhead strictly below 2% CPU and memory utilization in production environments by using sampling-based pprof or eBPF mechanisms.

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

Custom worker pools or manual Promise orchestration MUST explicitly restore the active OpenTelemetry context using `context.with()` to guarantee profiler sample attribution.

Custom worker pools or manual Promise orchestration MUST explicitly restore the active OpenTelemetry context using `context.with()` to guarantee profiler sample attribution.

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

Services MUST export profile data conforming to the OpenTelemetry Profiling Data Model (OTel Profiles) or standardized pprof formats containing span identifiers as sample labels.

Services MUST export profile data conforming to the OpenTelemetry Profiling Data Model (OTel Profiles) or standardized pprof formats containing span identifiers as sample labels.

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 Trace-to-Profile Correlation for Continuous CPU and Allocation Invariants critical for AI coding assistants?

Continuous profilers and distributed tracers must not operate in isolation. Low-overhead continuous CPU and memory profilers must deterministically propagate active OpenTelemetry TraceId and SpanId into profiling sample tags or pprof labels across all asynchronous execution contexts to guarantee sub-millisecond call-stack attribution for tail-latency outliers. 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 Wall-Clock Profiling Without Active Trace-Span Context Propagation) 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