Architectural Invariant Design: Go Chi OpenTelemetry W3C Trace Context and Baggage Propagation Invariant
Go Chi OpenTelemetry W3C Trace Context and Baggage Propagation Invariant defines an industrial-grade behavioral standard for backend engineering. Mandates deterministic W3C trace context (traceparent, tracestate) and baggage extraction/injection across Go Chi HTTP routing layers, downstream network calls, and concurrent Goroutine boundaries. Prohibits context severance caused by raw context.Background() usage in asynchronous operations or unpropagated outbound HTTP requests. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.
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 Mode | AI Symptom (The Anti-Pattern) | Severity | Production Impact & Remedy |
|---|---|---|---|
| AI Hallucination & Deprecation Drift | Trace Context Severance via Unpropagated Goroutines and Raw Outbound HTTP Requests | Critical | 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 Inconsistency | AI 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. |
Step-by-Step Production Directives
Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:
Services MUST configure otel.SetTextMapPropagator with propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}) during service bootstrap.
Services MUST configure otel.SetTextMapPropagator with propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}) during service bootstrap.
Chi routers MUST use otelchi middleware (or an equivalent strict W3C TextMapPropagator extraction middleware) as the root middleware before any business or logging logic.
Chi routers MUST use otelchi middleware (or an equivalent strict W3C TextMapPropagator extraction middleware) as the root middleware before any business or logging logic.
Handlers spawning concurrent asynchronous tasks MUST NEVER pass raw r.Context() to long-running Goroutines
Handlers spawning concurrent asynchronous tasks MUST NEVER pass raw r.Context() to long-running Goroutines; doing so causes immediate context.Canceled errors when the HTTP response completes.
Handlers spawning asynchronous tasks MUST NEVER create an isolated context.Background() or context.TODO()
Handlers spawning asynchronous tasks MUST NEVER create an isolated context.Background() or context.TODO(); they MUST use context.WithoutCancel(r.Context()) (Go 1.21+) or an equivalent wrapper to preserve trace spans, span context, and baggage while decoupling lifecycle cancellation.
Outbound HTTP clients MUST ALWAYS call otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header)) before executing requests, or wrap http.RoundTripper with otelhttp.NewTransport.
Outbound HTTP clients MUST ALWAYS call otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header)) before executing requests, or wrap http.RoundTripper with otelhttp.NewTransport.
Trace-derived metadata and baggage MUST NEVER be manually parsed using raw string slicing on the 'traceparent' header
Trace-derived metadata and baggage MUST NEVER be manually parsed using raw string slicing on the 'traceparent' header; use official OpenTelemetry API accessors exclusively.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Go Chi OpenTelemetry W3C Trace Context and Baggage Propagation Invariant critical for AI coding assistants?
Mandates deterministic W3C trace context (traceparent, tracestate) and baggage extraction/injection across Go Chi HTTP routing layers, downstream network calls, and concurrent Goroutine boundaries. Prohibits context severance caused by raw context.Background() usage in asynchronous operations or unpropagated outbound HTTP requests. 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 (Trace Context Severance via Unpropagated Goroutines and Raw Outbound HTTP Requests) 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.