Skip to content

perf(tracing): make exporter and sampler configurable, sanitize db spans, and flush on shutdown - #1637

Open
comandanto wants to merge 2 commits into
CredenceOrg:mainfrom
comandanto:perf/tracing-configurable-exporter
Open

comandanto wants to merge 2 commits into
CredenceOrg:mainfrom
comandanto:perf/tracing-configurable-exporter

Conversation

@comandanto

Copy link
Copy Markdown

Summary

Closes #1627

This PR replaces hardcoded stdout tracing with data-driven exporter and sampler configuration, sanitizes database span attributes against data leakage, and ensures in-flight spans flush cleanly during graceful shutdown.

Key Changes

  1. Config Keys & Validation (src/config/index.ts):
    • OTEL_TRACES_EXPORTER: 'none' | 'console' | 'otlp'. Default: 'none' in production (NODE_ENV === 'production'), 'console' in dev/test.
    • OTEL_EXPORTER_OTLP_ENDPOINT: URL required when OTEL_TRACES_EXPORTER === 'otlp' (enforced via .superRefine).
    • OTEL_TRACES_SAMPLER_RATIO: Float [0.0, 1.0]. Default: 0.1 in production, 1.0 in dev/test.
  2. OpenTelemetry Exporter & Sampler (src/tracing/tracer.ts):
    • Supports OTLPTraceExporter (@opentelemetry/exporter-trace-otlp-http@^0.57.0) and ConsoleSpanExporter.
    • When exporter is 'none', registers zero span processors so span operations remain zero-overhead no-ops.
    • Configures ParentBasedSampler wrapping TraceIdRatioBasedSampler(ratio).
  3. Database Instrumentation Sanitization (src/db/pool.ts):
    • Truncates db.statement to 1,024 characters.
    • Sanitizes exception events to only include SQLSTATE code (or error name) as exception.type and exception.message truncated to 256 characters.
    • Omitted exception.stacktrace and stripped Postgres query parameters/detail to avoid sensitive parameter exposure.
  4. Graceful Shutdown Integration (src/gracefulShutdown.ts, src/index.ts):
    • Added tracing_shutdown phase in GracefulShutdownManager invoking provider.shutdown() with a 2,000 ms timeout race.
    • Passed initTracing() provider into GracefulShutdownManager.

Verification & Test Output

All 4 test suites (46 tests total) pass cleanly:

 RUN  v4.1.12 Credence-Backend

 ✓ src/config/__tests__/tracingConfig.test.ts (5 tests)
 ✓ src/db/queryTracing.test.ts (4 tests)
 ✓ src/tracing/__tests__/tracer.test.ts (5 tests)
 ✓ src/gracefulShutdown.test.ts (32 tests)

 Test Files  4 passed (4)
      Tests  46 passed (46)

Production Mode Sample (Zero Span Dumps to Stdout)

$ NODE_ENV=production tsx verify_prod_tracing.mjs
[PROD TEST START]
[PROD TEST END - NO SPAN DUMPS DETECTED]

No spans, SQL statements, or exception stack traces are written to stdout in production mode.

…nant budget and per-pool maps

Closes CredenceOrg#1601

- Removed duplicate pg import on lines 1-2
- Rewrote withTenantConnectionBudget to support callback form of connect() needed by pg-pool query()
- Synchronously reserve budget slot before connect to avoid concurrency race condition
- Guaranteed release() idempotency with per-client flag and rollback on connect rejection
- Isolated connection budget maps per pool instance (pool, workerPool, replicaPool)
- Extended __poolTestHooks to support per-pool tenant count introspection
- Added comprehensive unit test suite in src/db/pool.tenantBudget.test.ts
…ans, and flush on shutdown

Closes CredenceOrg#1627

- Add OTEL_TRACES_EXPORTER ('none' | 'console' | 'otlp'), OTEL_EXPORTER_OTLP_ENDPOINT, and OTEL_TRACES_SAMPLER_RATIO config with zod validation
- Support data-driven exporter (OTLPTraceExporter, ConsoleSpanExporter, none) and ParentBasedSampler in initTracing()
- Sanitize database spans: truncate db.statement to 1024 chars, exception.message to 256 chars, and strip stacktrace/detail
- Register tracer provider shutdown in GracefulShutdownManager with a 2000ms timeout race
- Add comprehensive test coverage across tracer, config, query tracing, and graceful shutdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(tracing): production writes every span, including every SQL statement and database error stack trace, to stdout through ConsoleSpanExporter

1 participant