Missing OpenTelemetry spans: OPA CI gate
A refactor can strip every span from a service while all 194 tests stay green. An OPA CI gate parses handlers into a manifest and blocks the merge in 9 seconds. Here is the setup and its failure modes.

At 2:07 PM on a Thursday, checkout-worker v4.11.2 shipped to production. All 194 unit tests were green. ArgoCD synced, health checks returned 200, and Grafana stayed green all afternoon. At 8:19 PM a merchant asked in Slack why no paid orders since 2:30 had triggered fulfillment webhooks. The answer was 1,840 dead-letter jobs. A Redis connection-pool change was dropping connections on the third retry, and a refactor two weeks earlier had split handleWebhookJob() into a new module without its withTelemetry() wrapper, so the outage emitted zero spans and zero counters. Missing OpenTelemetry spans look exactly like this: working code, passing tests, and no way to see any of it.
Coverage that day was 86%. Every gate in that pipeline measured behavior, and not one of them measured visibility.
Missing OpenTelemetry spans fool every normal check
Coverage counts executed lines, and the handler executed fine. Unit tests assert behavior, and behavior is exactly what kept working; the mocks stand in for the collector, so a suite can pass while the service emits nothing an SRE could see. Code review fails on scale rather than on principle: the postmortem found 11 more endpoints and queue consumers across four TypeScript services with zero spans, stale metric names, or catch blocks logging to console.log. Reviewing a 19-file PR that fixes a P1 incident, nobody checks whether otel_span_name is still wired up. You are asking a human to find the line that is not there.
| Defense | Catches the blind handler? | Why |
|---|---|---|
| Unit tests | No | They assert behavior, and mocks hide the missing collector |
| Code coverage | No | 86% the day the service went blind |
| Code review | Sometimes | Fails on 19-file PRs; nobody greps the diff for spans |
| grep for startActiveSpan | Barely | One renamed helper or wrapped line breaks the pattern |
| AST manifest plus Rego gate | Yes | Reads structure, and the policy itself is unit-tested |
grep is the obvious first move, and it is also brittle. A pattern like grep -r startActiveSpan survives until someone renames the helper or wraps one call across two lines, then the gate keeps passing while looking strict. Structure, not text matching, is what a merge gate needs.
Split the problem: a manifest and a policy
A recent write-up on enforcing observability contracts in CI/CD with Open Policy Agent splits enforcement into two boring pieces, which is why the shape generalizes past that team’s stack. A 110-line TypeScript AST extractor walks src/handlers and src/workers, reads every exported handler, and writes otel-manifest.json: file, handler name, whether it calls withTelemetry or tracer.startActiveSpan, whether any counter is incremented with .inc(). No service startup, no local collector, about 400 milliseconds of parsing.
The second piece is a Rego policy that consumes the manifest as plain JSON. That split is the actual design win. The policy knows nothing about TypeScript, so the same deny rules work on a manifest produced by Python’s ast module or Go’s go/ast; only the extractor changes per language. Rego also brings opa test, so the gate itself gets unit tests and cannot rot into always-passing without anyone noticing.
The gate is one command
OPA inverts the usual logic on first contact: you query data.ci.observability.deny with –fail-defined, and the build fails when deny produces any message. The exempt set holds two names, healthzHandler and readinessCheck, because Kubernetes probes run every few seconds and nobody wants span spam from a liveness check. The whole job takes about 9 seconds on ubuntu-latest.
jobs:
telemetry-gate:
runs-on: ubuntu-latest
timeout-minutes: 2
steps:
- run: npx tsx scripts/extract-otel-manifest.ts
- run: |
opa eval --fail-defined \
-i otel-manifest.json \
-d policy/observability_gate.rego \
"data.ci.observability.deny"
The deploy job declares needs: [test, telemetry-gate], so the merge dies in CI at a cost of 9 seconds instead of dying in production six hours later. Post-incident tooling answers a different question; witr traces a port back to the process that owns it, but no tracing tool tells you at merge time that a handler went mute. That check has to live in the pipeline.
The first run found 14 handlers
Day one across four services flagged 14 handlers. Two were legitimate exemptions. The other 12 were real blind spots, including a Stripe dispute webhook that had logged its errors to bare stderr for four months without one metric attached. Shared middleware caused the only edge case: two Express routes delegated error counting to a global errorMetricsMiddleware, so the AST saw no .inc() inside the handlers themselves. The fix went the right way. Instead of weakening the policy, the team standardized on wrapping every route with withTelemetry(‘route.name’, handler) at the export site, which puts tracing and counting in one visible place per file.
The same scanner earned a second job. Someone had passed order_id as a Prometheus label on a retry counter, and within 48 hours the account blew past 400,000 unique time series and $1,640 of monitoring overage. The extractor now carries a bannedLabelKeys list covering userId, orderId, email, requestId and sessionId, and the policy blocks any counter that attaches one of them. Logs and traces get identifiers. Metrics get bounded enums.
Warn-only week, then hard fail
Dropping a hard gate on a legacy monorepo on a Monday morning blocks 40 unrelated PRs and loses the room before lunch. The rollout that worked ran in two phases: seven days of opa eval without –fail-defined, posting PR comments instead of failing builds, then the flip to hard failure once main reached zero violations. I would not ship the strict mode first, and neither did that team.
Exemptions carry their own risk. An exemptions list is an allowlist, and allowlists drift the same way a permissive Claude Code allowlist leaked secrets; every name in the set should cost a reviewed line in the diff and disappear when the handler does.
Where the OPA CI gate still fails you
The scanner reads text, not intent. It string-matches withTelemetry and startActiveSpan call names, so a renamed wrapper or a handler produced by a factory reads as a blind spot and pushes someone toward a pointless exemption. It checks presence, not quality: a handler can emit a span named span with no attributes and pass. The CI snippet also pulls the OPA 1.0.0 binary straight from openpolicyagent.org with curl, which is an unpinned supply-chain input; hash-pin it like anything else that can execute in your build.
Manifest freshness is the last gap. The extractor stamps generatedAt into otel-manifest.json, and nothing in the gate itself verifies that the stamp is recent, so a silently broken extractor job evaluates last week’s code and still shows green. Two opa test rules run in under 200 milliseconds. Run them. Test the extractor too, and treat manifest plus policy as one tool that can fail in two places.
One experiment settles whether your pipeline could see a blind service. Delete a withTelemetry wrapper somewhere, run the full suite, and watch what fails. If the answer is nothing, then the only difference between your pipeline and the one that let six hours of checkout-worker outage pass at 2:07 PM on a Thursday is that your turn has not come yet.