Skip to content

Add DB pool metrics and OpenTelemetry tracing - #1277

Merged
javuto merged 1 commit into
developfrom
db-pool-metrics-opentelemetry
Nov 25, 2026
Merged

javuto merged 1 commit into
developfrom
db-pool-metrics-opentelemetry

Conversation

@javuto

@javuto javuto commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Add DB pool metrics and OpenTelemetry tracing

Summary

Adds two production observability pieces:

  • Primary and read-replica database pool metrics for osctrl-api and osctrl-tls.
  • Optional OpenTelemetry tracing over OTLP HTTP for HTTP requests, GORM operations, Redis commands, and key background workers.

Tracing is disabled by default. The new metrics are only registered when the existing Prometheus listener is enabled.

Closes #1261.

Motivation

The previous observability work exposed HTTP and cache metrics, but operators still could not see:

  • whether the database pool was saturated or waiting on connections
  • where request latency was spent across HTTP, SQL, and Redis
  • whether vulnerability, alert, and retention workers were taking unusually long

Changes

Database pool metrics

Added pkg/backend/metrics.go with a scrape-time collector. It exposes:

Metric Meaning
osctrl_db_pool_open_connections Open connections
osctrl_db_pool_in_use_connections Connections currently in use
osctrl_db_pool_idle_connections Idle connections
osctrl_db_pool_max_open_connections Configured maximum open connections
osctrl_db_pool_wait_count_total Times a caller waited for a connection
osctrl_db_pool_wait_duration_seconds_total Total time spent waiting

Each metric carries role="primary" or role="read" when a read replica is configured. The collector reads sql.DB.Stats() at scrape time and adds no hot-path cost.

Registered on both API and TLS Prometheus listeners.

OpenTelemetry tracing

Added pkg/tracing with:

  • OTLP HTTP exporter setup
  • parent-based trace-id sampling
  • W3C traceparent propagation
  • resource attributes for service name, version, and deployment environment
  • graceful shutdown flushing
  • no-op behavior when disabled

Instrumentation

  • HTTP: server spans for API and TLS requests.
    • method, route, status, host, and user agent
    • preserves Flusher, Hijacker, Pusher, and ReaderFrom for SSE and downloads
    • span names use the matched route pattern, not raw paths
  • GORM: client spans for create, query, update, delete, row, and raw callbacks.
    • operation, dialect, table, and rows affected
    • never records SQL text or query parameters
  • Redis: spans for commands and pipelines.
    • command name and pipeline length
  • Workers:
    • vulnerability feed sync/matching tick
    • alert dispatch
    • API data-retention sweep
    • TLS log-retention sweep

Configuration

YAML:

service:
  tracingEnabled: false
  tracingEndpoint: ""
  tracingInsecure: false
  tracingSampleRatio: 0.1

Flags:

--tracing-enabled
--tracing-endpoint
--tracing-insecure
--tracing-sample-ratio

Environment variables:

SERVICE_TRACING_ENABLED
SERVICE_TRACING_ENDPOINT
SERVICE_TRACING_INSECURE
SERVICE_TRACING_SAMPLE_RATIO

tracingEndpoint accepts host:port or a full URL. When empty, the exporter falls back to the standard OTEL_EXPORTER_OTLP_* environment variables. http:// endpoints and tracingInsecure: true disable exporter TLS.

The sample ratio defaults to 0.1. An explicit 0 disables sampling.

Both sample configs include the new section:

  • deploy/config/api.yml
  • deploy/config/tls.yml

Documentation

  • Added a Tracing section to ARCHITECTURE.md.
  • Added example PromQL for request rate, p95 latency, DB pool saturation, wait pressure, and retention work.
  • Added an observability bullet to README.md.

Safety and privacy

  • Tracing is disabled by default; every helper is a no-op when off.
  • Database spans do not include SQL text or parameters.
  • Node UUIDs, environment names, and rule identifiers are not used as span names or metric labels.
  • HTTP span names use the matched route pattern; unmatched requests use unmatched.
  • The metrics listener remains separate and loopback-bound by default.

Dependencies

  • Promotes OpenTelemetry core/trace/SDK packages to direct dependencies.
  • Adds go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp.
  • go.sum updated via go mod tidy.

Tests

  • DB pool collector registers and exports primary/read role metrics.
  • Tracing disabled path is a no-op.
  • Invalid sample ratios are rejected.
  • HTTP middleware records the matched route and status.
  • GORM plugin creates and ends create spans.
  • Redis hook records command spans.
  • Existing API, TLS, alerts, vulnerability, and config tests continue to pass.

Validation

  • go test ./... — passed.
  • git diff --check — clean.

Follow-ups

  • Propagate trace context to outbound OIDC, SAML, webhook, and feed HTTP clients.
  • Add a sample Grafana dashboard JSON alongside the documented PromQL.
  • Optionally instrument TLS log ingestion and console/file-explorer internals with more granular spans.

@javuto javuto added ✨ enhancement New feature or request osctrl-tls osctrl-tls related changes osctrl-api osctrl-api related changes backend Backend related issues 📊 metrics Metrics related issues labels Nov 25, 2026
Comment thread pkg/tracing/http.go Dismissed
@javuto
javuto merged commit 2981940 into develop Nov 25, 2026
8 checks passed
@javuto
javuto deleted the db-pool-metrics-opentelemetry branch November 25, 2026 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend related issues ✨ enhancement New feature or request 📊 metrics Metrics related issues osctrl-api osctrl-api related changes osctrl-tls osctrl-tls related changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add DB pool metrics and OpenTelemetry tracing for API/TLS

2 participants