This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: otel-non-blocking-batch-span-drop-backpressure-guardrail

OpenTelemetry Non-Blocking Batch Span Drop Backpressure Guardrail

Enforces bounded memory limits and non-blocking drop-on-saturation policies for OpenTelemetry span processors to eliminate application thread contention and garbage collection pause storms. Mandates explicit maxQueueSize, aggressive export timeouts, and non-blocking batch exports during collector degradation or network partitions. Disallows synchronous span processing, unconstrained in-memory queues, and blocking flush calls within the hot path.

Prohibited Anti-Pattern: Unbounded Telemetry Buffering and Synchronous Export Stalling Microservice Event Loops Under Network Degradation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry Non-Blocking Batch Span Drop Backpressure Guardrail - Enforces bounded memory limits and non-blocking drop-on-saturation policies for OpenTelemetry span processors to eliminate application thread contention and garbage collection pause storms. Mandates explicit maxQueueSize, aggressive export timeouts, and non-blocking batch exports during collector degradation or network partitions. Disallows synchronous span processing, unconstrained in-memory queues, and blocking flush calls within the hot path.
globs: src/**/tracing.{ts,js}, src/**/telemetry/**/*.{ts,js}, src/**/instrumentation.{ts,js}, src/**/tracer.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/otel-non-blocking-batch-span-drop-backpressure-guardrail"
---

# OpenTelemetry Non-Blocking Batch Span Drop Backpressure Guardrail

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Unbounded Telemetry Buffering and Synchronous Export Stalling Microservice Event Loops Under Network Degradation

## System Constraints
You are a Principal Software Architect enforcing production-grade observability resilience. Microservices must strictly decouple operational telemetry from core application execution paths. Under no circumstances may an observability failure (such as OTLP collector network latency, queue saturation, or telemetry pipeline degradation) induce GC pressure, event-loop starvation, memory leaks, or p99 tail-latency regressions on business logic. You must mandate bounded BatchSpanProcessor configurations with explicit drop policies, short timeout bounds, and isolated asynchronous workers.

## Strict Guidelines
- Engineers MUST ALWAYS configure OpenTelemetry with BatchSpanProcessor and explicit, bounded buffer dimensions: maxQueueSize (e.g., 2048) and maxExportBatchSize (e.g., 512).
- Engineers MUST ALWAYS configure aggressive exportTimeoutMillis (maximum 3000ms-5000ms) to prevent collector socket timeouts from back-propagating into buffer retention.
- Engineers MUST NOT use SimpleSpanProcessor in any staging, pre-production, or production service runtime.
- Code SHOULD export dropped-span counters via self-diagnostics metrics or OpenTelemetry internal metrics to preserve visibility into load shedding.
- Services MUST NEVER invoke tracerProvider.forceFlush() or tracerProvider.shutdown() within hot request/response execution paths or high-frequency middleware.
- Components NEVER block thread execution or event loop cycles when span buffers become saturated; excess spans must be shed immediately via bounded ring-buffer discard semantics.

## Bad vs Good Implementations
### OpenTelemetry NodeSDK Tracer Initialization

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using SimpleSpanProcessor or unconfigured default batch processors causes synchronous export stalls and memory ballooning. When the target collector stalls, the node process queues unbounded network calls or blocks span completion, creating massive garbage collection storms and cascading request timeouts.
```typescript
import { NodeSDK } from '@opentelemetry/sdk-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-node';

// ANTI-PATTERN: SimpleSpanProcessor synchronously blocks on span export,
// and default/unbounded exporter configurations cause tail latency spikes and event-loop lag
// when OTLP collector is degraded or disconnected.
const sdk = new NodeSDK({
  traceExporter: new OTLPTraceExporter({
    url: 'http://collector.internal:4317',
    // Default timeout can be up to 30000ms
  }),
  // Synchronous processor: every span end blocks or waits on exporter network roundtrip
  spanProcessor: new SimpleSpanProcessor(new OTLPTraceExporter()),
});

sdk.start();
```

#### Production Standard (Correct)
> **Standard**: Employs BatchSpanProcessor with fixed bounded queue bounds and a 3000ms hard timeout. When collector saturation occurs, excess spans are dropped instantly at the processor boundary without blocking the Node.js event loop or growing the V8 heap into a garbage collection pause storm.
```typescript
import { NodeSDK } from '@opentelemetry/sdk-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-node';
import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api';

diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.WARN);

// PRODUCTION FIX: Explicit bounded ring buffer with fast export timeout and non-blocking span shedding
const exporter = new OTLPTraceExporter({
  url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT ?? 'http://collector.internal:4317',
  timeoutMillis: 3000, // Hard stop on outbound gRPC deadlocks
});

const boundedSpanProcessor = new BatchSpanProcessor(exporter, {
  maxQueueSize: 2048,           // Maximum spans buffered before shedding drops incoming
  maxExportBatchSize: 512,       // Balanced chunking for OTLP frame efficiency
  scheduledDelayMillis: 1000,    // Steady-state flush cadence
  exportTimeoutMillis: 3000,     // Prevents exporter queue saturation
});

const sdk = new NodeSDK({
  spanProcessor: boundedSpanProcessor,
});

sdk.start();

process.on('SIGTERM', () => {
  sdk.shutdown()
    .then(() => console.log('Tracing terminated cleanly'))
    .catch((err) => console.error('Error shutting down tracing', err))
    .finally(() => process.exit(0));
});
```

Recommended MCP Servers (mcp-serverlist.com)

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

Architectural Specification & Invariant Rationale

Architectural Invariant Design: OpenTelemetry Non-Blocking Batch Span Drop Backpressure Guardrail

OpenTelemetry Non-Blocking Batch Span Drop Backpressure Guardrail defines an industrial-grade behavioral standard for devops engineering. Enforces bounded memory limits and non-blocking drop-on-saturation policies for OpenTelemetry span processors to eliminate application thread contention and garbage collection pause storms. Mandates explicit maxQueueSize, aggressive export timeouts, and non-blocking batch exports during collector degradation or network partitions. Disallows synchronous span processing, unconstrained in-memory queues, and blocking flush calls within the hot path. 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 Telemetry Buffering and Synchronous Export Stalling Microservice Event Loops Under Network Degradation 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 Telemetry Buffering and Synchronous Export Stalling Microservice Event Loops Under Network DegradationCritical
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 configure OpenTelemetry with BatchSpanProcessor and explicit, bounded buffer dimensions

Engineers MUST ALWAYS configure OpenTelemetry with BatchSpanProcessor and explicit, bounded buffer dimensions: maxQueueSize (e.g., 2048) and maxExportBatchSize (e.g., 512).

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 configure aggressive exportTimeoutMillis (maximum 3000ms-5000ms) to prevent collector socket timeouts from back-propagating into buffer retention.

Engineers MUST ALWAYS configure aggressive exportTimeoutMillis (maximum 3000ms-5000ms) to prevent collector socket timeouts from back-propagating into buffer 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.
3

Engineers MUST NOT use SimpleSpanProcessor in any staging, pre-production, or production service runtime.

Engineers MUST NOT use SimpleSpanProcessor in any staging, pre-production, or production service runtime.

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

Code SHOULD export dropped-span counters via self-diagnostics metrics or OpenTelemetry internal metrics to preserve visibility into load shedding.

Code SHOULD export dropped-span counters via self-diagnostics metrics or OpenTelemetry internal metrics to preserve visibility into load 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.
5

Services MUST NEVER invoke tracerProvider.forceFlush() or tracerProvider.shutdown() within hot request/response execution paths or high-frequency middleware.

Services MUST NEVER invoke tracerProvider.forceFlush() or tracerProvider.shutdown() within hot request/response execution paths or high-frequency middleware.

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

Components NEVER block thread execution or event loop cycles when span buffers become saturated

Components NEVER block thread execution or event loop cycles when span buffers become saturated; excess spans must be shed immediately via bounded ring-buffer discard semantics.

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 Non-Blocking Batch Span Drop Backpressure Guardrail critical for AI coding assistants?

Enforces bounded memory limits and non-blocking drop-on-saturation policies for OpenTelemetry span processors to eliminate application thread contention and garbage collection pause storms. Mandates explicit maxQueueSize, aggressive export timeouts, and non-blocking batch exports during collector degradation or network partitions. Disallows synchronous span processing, unconstrained in-memory queues, and blocking flush calls within the hot path. 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 Telemetry Buffering and Synchronous Export Stalling Microservice Event Loops Under Network Degradation) 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