CodeXray Database Agent
The CodeXray Database Agent connects directly to your databases and polls their internal statistics — giving you engine-level visibility that no amount of application-side instrumentation can reproduce: active connections, locks, replication lag, cache hit ratios, top queries by engine-reported cost, and more.
It's the engine that powers the Databases module and enriches the Database Insights tab with engine telemetry alongside application spans.
Application-side tracing tells you which queries your service is calling. The Database Agent tells you how the engine is serving them. Together, they tell you whether a slow query is an application bug, a schema problem, or the engine running out of headroom.
Supported Engines
| Engine | Type Code | Notes |
|---|---|---|
| PostgreSQL | PG | Includes query stats, locks, replication, and bloat metrics |
| MySQL | MY | Uses performance_schema — ensure it's enabled |
| MSSQL | MS | SQL Server DMVs |
| MongoDB | — | Supports connection URI or traditional host/port |
| Oracle | OR | AWR-style views |
| Cassandra | — | JMX + system tables |
| Couchbase | CB | REST API polling |
Each engine has its own sample configuration in the agent's sampleConfig/ directory — start from the one that matches your engine.
How It Works
The agent is a long-running process that, on a configurable cycle, connects to each configured database using a read-only monitoring user, runs a well-defined set of diagnostic queries, and ships the resulting metrics and slow-query data to the CodeXray backend.
Each database you monitor is described by its own YAML config entry. A single agent process can monitor many databases — one config file per database, or one directory with many configs.
The Database Agent polls the engine's existing stats views — it does not sit in the query path, add latency to user queries, or modify anything. Worst case, a misconfigured monitoring user sees a few extra reads on system views.
Requirements
Host
- Linux (recommended) — Ubuntu 20.04+, RHEL/Rocky 8+, Debian 11+. Docker-compatible.
- Windows — supported, ships as a cross-compiled binary.
- Architecture:
amd64(default) orarm64.
Network
- The agent must be able to reach each monitored database (direct TCP, or via bastion/proxy you configure).
- Outbound connectivity to the CodeXray collector endpoint.
Database permissions
The agent needs a read-only monitoring user on each database. Exact privileges vary by engine:
| Engine | Minimum privileges |
|---|---|
| PostgreSQL | pg_monitor role (covers pg_stat_*, pg_stat_statements). Install the pg_stat_statements extension for slow-query analysis. |
| MySQL | REPLICATION CLIENT, PROCESS, SELECT on performance_schema.*. |
| MSSQL | VIEW SERVER STATE, VIEW ANY DEFINITION. |
| MongoDB | clusterMonitor role. |
| Oracle | SELECT ANY DICTIONARY (or granular grants on V$ views). |
| Cassandra | Read access to system keyspaces. |
| Couchbase | ro_admin role. |
Credentials
- A project API key from the CodeXray UI (used to authenticate the agent to the backend).
Installation
Docker (recommended)
Run the agent as a container, mounting your config directory:
docker run -d \
--name codexray-db-agent \
-v $(pwd)/configs:/home/configs \
-e DB_USER=monitor_user \
-e DB_PASSWORD=secure_password \
ghcr.io/codifinary/codexray-db-agent:latest \
--config /home/configs/pg.yaml \
--db-user "$DB_USER" \
--db-password "$DB_PASSWORD"
Point the container at a single config file or an entire config directory (it'll load every .yaml inside).
Standalone binary
Cross-platform binaries are available for Linux (amd64/arm64) and Windows (amd64). Drop the binary on a host that can reach your databases and run:
./codexray-db-agent --config ./configs/pg.yaml \
--db-user monitor_user --db-password "$DB_PASSWORD"
For long-running use, wrap it in a systemd unit (Linux) or a Windows Service.
Configuration
Each database is described by a YAML file. A minimal PostgreSQL example:
codexray:
service_name: orders-db-prod
X-API-Key: eyJhbGciOi...your-project-api-key
codexray_backend: https://your-codexray.example.com
secure: true
type: PG
cronCycle: 30 # poll every 30 seconds
hostname: orders-db.internal
port: 5432
user: monitor_user # optional if using --db-user flag
password: "" # optional if using --db-password flag
database: orders
postgres:
sslmode: require
isExtension: true # pg_stat_statements installed
isQueryExtension: true
queryLimit: 10 # number of top queries to report
queryThresholdTime: 1 # milliseconds — filter out trivial queries
queryTimeOut: 10 # seconds — per-poll timeout
logPath: logs/postgres.log
Key fields
| Field | Meaning |
|---|---|
service_name | Name shown in the CodeXray Databases view. Keep it unique and descriptive. |
X-API-Key | Project API key from the CodeXray UI. |
codexray_backend | Base URL of your CodeXray collector. |
type | Engine type code (see Supported Engines table). |
cronCycle | Polling interval in seconds. 30s is typical; use 10s only for high-scrutiny production DBs. |
hostname, port, database | Connection target. |
user, password | Credentials — prefer passing via --db-user / --db-password CLI flags and leaving these blank. |
queryLimit | How many top queries to send per cycle. 10 is a sensible default. |
queryThresholdTime | Exclude queries faster than this from slow-query analysis (in milliseconds). |
queryTimeOut | Per-poll connection/query timeout in seconds. |
Credentials best practices
The safest pattern: leave user and password blank in the YAML, and pass them via --db-user and --db-password flags — backed by a secrets manager or environment variables at runtime.
Multiple databases in one run:
./codexray-db-agent --config ./configs/ \
--db-user "user1|user2|user3" \
--db-password "pass1|pass2|pass3"
Use a pipe (|) separator — the agent matches them to configs in load order.
MongoDB URIs: pass the full URI with --mongo-uri:
--mongo-uri "mongodb://admin:secret@cluster.example.com:27017"
Verifying It's Working
- Agent logs — the agent logs every poll cycle with the service name and status. Look for "successfully sent batch" or engine-specific confirmations.
- CodeXray UI — your database appears in the Databases view within a cycle or two. Top Queries, Metrics, and Active Connections views populate after a few polls of data have accumulated.
- Connection test — on the agent host, make sure the monitoring user can connect and read the relevant stats views (
psql,mysql,mongosh, etc.).
Most failures are authentication or permission issues. Check the agent's log file (logPath) — connection failures and permission errors are logged explicitly with the exact SQL that failed. Fix the monitoring user's grants and the agent will pick up on its next cycle.
Next Steps
- Database Agent Metrics — per-engine metric reference (Postgres, MySQL, MSSQL, Mongo, Oracle, Cassandra, Couchbase)
- CodeXray .NET Agent — the last agent in the setup path
- Database Insights — the application-level tab that correlates agent telemetry with application traces
- Architecture — how database telemetry flows through the CodeXray platform
- Role-Based Access Control — who can view database data in each project