Skip to content

feat(metrics): Export worker, background-work, and transaction-age gauges - #5401

Open
colinbarry wants to merge 15 commits into
masterfrom
feat/diagnosis-scrape-metrics
Open

colinbarry wants to merge 15 commits into
masterfrom
feat/diagnosis-scrape-metrics

Conversation

@colinbarry

@colinbarry colinbarry commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Throughput drops with idle CPU were hard to diagnose from the metrics: nothing showed whether queries were queueing for a worker, whether a snapshot or garbage collection was running at that moment, whether one long transaction was holding others up, or whether snapshots had stopped succeeding.

This adds the following metrics. The snapshot ones are updated when a snapshot ends; the rest are read at scrape time, and cost the query path nothing beyond a brief per-session check during a scrape:

Metric Labels Value
memgraph_workers Worker threads that run client queries (--bolt-num-workers)
memgraph_workers_busy Workers running a query now, including queries waiting on a lock
memgraph_worker_queue_length Tasks queued on worker threads, including parallel query subtasks
memgraph_snapshot_running_seconds database, uuid Time the running snapshot has taken so far; 0 when none is running
memgraph_gc_running_seconds database, uuid The same for garbage collection
memgraph_longest_transaction_seconds database, uuid Age of the oldest open transaction; 0 when none is open
memgraph_last_snapshot_timestamp_seconds database, uuid Unix time the database was last confirmed snapshot-current: a snapshot written, or a check that found nothing new to write; 0 until then
memgraph_snapshot_failures_total database, uuid, reason Snapshots that failed: no_space or write_error. A requested abort is not a failure
process_start_time_seconds Process start time, the standard Prometheus name, for uptime and restart detection

The worker gauges are absent when Memgraph runs without the priority worker pool. The longest-transaction gauge reads sessions the same way SHOW TRANSACTIONS does.

SHOW METRICS INFO adds Workers, WorkersBusy, WorkerQueueLength, ProcessStartTime, SnapshotFailuresNoSpace, and SnapshotFailuresWriteError. The running times and the longest transaction are already in SHOW TRANSACTIONS, and the last snapshot in SHOW SNAPSHOTS.

memgraph_workers and memgraph_workers_busy report the size of the query
worker pool and how many workers are running a query. A query blocked on a
lock still holds its worker, so a busy pool with low throughput points to
waiting rather than load.
memgraph_worker_queue_length reports the query tasks waiting for a free
worker. A queue that stays above zero means the worker pool is the limit.
memgraph_snapshot_running_seconds and memgraph_gc_running_seconds report,
per database, how long the running snapshot or GC run has taken so far, and
0 when none is running. The duration histograms record only when the work
finishes, so a long snapshot was invisible while it ran.
memgraph_longest_transaction_seconds reports, per database, how long the
oldest open transaction has been open, so a transaction that holds up others
or blocks garbage collection is visible. process_start_time_seconds is the
standard Prometheus start time, for uptime and restart detection.
@colinbarry colinbarry added this to the mg-v3.15.0 milestone Nov 15, 2026
@colinbarry colinbarry added Docs needed Docs needed CI -build=release -test=core Run release build and core tests on push CI -build=release -test=e2e Run release build and e2e tests on push labels Nov 15, 2026
@colinbarry colinbarry self-assigned this Nov 15, 2026
@colinbarry

colinbarry commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Tracking

Standard development

  • Update unit/E2E tests: unit tests for the pool counts and each gauge; the OpenMetrics e2e family list includes the new per-database gauges; show_metrics.py checks the new SHOW METRICS INFO rows
  • Compare the benchmarking results between the master branch this branch: not needed: every value is read at scrape time, nothing runs on the query path

CI Testing Labels

  • Select the appropriate CI test labels (CI -build=build-name -test=test-suite): coverage core, release core, release e2e

Kubernetes operator / Helm charts

Does this change affect how Memgraph is deployed or configured on Kubernetes? It likely does if it adds, removes or renames a flag; changes run-time or startup settings; touches the license, AWS settings, coordinators, replication, TLS, monitoring or another enterprise feature.

  • No, the operator and Helm charts are unaffected: new metrics only, no flags or settings change
  • Yes, and I opened the matching PRs:
    • [ Operator PR link memgraph/kubernetes-operator#XXXX ]
    • [ Helm charts PR link memgraph/helm-charts#XXXX ]

Documentation checklist

  • Add the documentation label
  • Add the bug / feature label
  • Add the milestone for which this feature is intended
    • If not known, set for a later milestone
  • Write a release note, including added/changed clauses
    • Added Prometheus metrics for diagnosing slowdowns: the query worker pool's size, busy workers, and queue length; per database, how long the running snapshot and garbage collection have taken and the age of the oldest open transaction; the time each database was last confirmed snapshot-current and its snapshot failures by reason; and the standard process_start_time_seconds. Use them to tell queueing, background work, and long transactions apart when throughput drops, and to catch snapshots that have stopped succeeding. #5401
  • [ Documentation PR link feat(metrics): Export worker, background-work, and transaction-age gauges documentation#1930 ]
    • Is back linked to this development PR

A failing snapshot was invisible until a restart recovered from an old one. Each database
now exports memgraph_last_snapshot_timestamp_seconds, the last time it was confirmed
snapshot-current (a snapshot written, or nothing new to write), and
memgraph_snapshot_failures_total by reason: no_space or write_error. A requested abort is
not counted as a failure.
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

This PR has potential conflicts with the following other open pull requests which modify the same files:

…tart

A scrape can see a snapshot or GC run as running before its start time is
stored, and reported the steady clock's whole age as the run's duration.
Report the run only once its start time is set.
The snapshot written on the switch from analytical to transactional mode
bypassed the snapshot metrics, so its success did not update the last snapshot
time and its failure was not counted. Both snapshot paths now share the same
recording.
A dropped database's sessions keep running while they drain, and a new
database can take its name meanwhile. Keying the longest transaction age by
name gave the new database the dropped one's transaction ages. Key it by uuid,
which the metrics entries already track.
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Release note advisory

Release note: Added Prometheus metrics for diagnosing slowdowns: the query worker pool's size, busy workers, and queue length; per database, how long the running snapshot and garbage collection have taken and the age of the oldest open transaction; the time each database was last confirmed snapshot-current and its snapshot failures by reason; and the standard `process_start_time_seconds`. Use them to tell queueing, background work, and long transactions apart when throughput drops, and to catch snapshots that have stopped succeeding. [#5401](https://github.com/memgraph/memgraph/pull/5401)

The note contains multiple sentences; the release note must be a single short sentence ending with a period immediately followed by the PR link. Please condense into one sentence.

The queue also holds parallel query subtasks, and a task can wait in one
worker's queue while another worker is idle.
Bound the last snapshot timestamp from above, give the per-database
transaction age test an age for another database incarnation, and drive the
longest transaction gauge end to end with an open transaction.
@colinbarry colinbarry added the CI -build=coverage -test=core Run coverage build and core tests on push label Nov 18, 2026
… in SHOW METRICS INFO

Every metric must be readable through Cypher. Add the snapshot failure
counters, the worker pool gauges, and the process start time to SHOW METRICS
INFO. The worker pool rows are read when the query runs, and are absent when
there is no priority worker pool, as on the metrics endpoint. The deprecated
JSON endpoint is unchanged.
…oint

The JSON endpoint appends every global SHOW METRICS INFO row, so adding the
process start time and worker pool gauges there exposed them on the
deprecated endpoint too. Report them from their own function, which only SHOW
METRICS INFO calls.

@internal-review-service-bot internal-review-service-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by the Memgraph review service

Verdict: APPROVE WITH NITS
Findings: 0 blocker, 6 non-blocking, 0 nit (6 inline) · 1 pre-existing · 2 nits (collapsed)

Pre-existing, not introduced by this PR (1)
  • src/utils/priority_thread_pool.cpp:137-155

    [Non-blocking] GetNumQueuedTasks has a bounded undercount window; test races with the monitor interval

    (pop/repush), 166–170 (GetNumQueuedTasks) ⚠ citation suspect: report cites tests/unit/utils_priority_thread_pool.cpp:923 — file is 589 lines; actual assertion is at line 287.

Nits (2)
  • tests/unit/prometheus_metrics.cpp:139-158

    [Nit] Test uses identical nil UUIDs for two separate databases, making the per-database isolation check vacuous

  • src/storage/v2/inmemory/storage.cpp:98-107

    [Nit] Missing coverage: no test for failure classification logic

    tests/unit/prometheus_metrics.cpp + src/storage/v2/inmemory/storage.cpp:98–107

Comment on lines 3160 to 3161
&snapshot_progress_,
"storage_mode_change");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Non-blocking] memgraph_snapshot_running_seconds reads 0 during storage-mode-change snapshots

Comment on lines 862 to 864
// Populate GlobalMetricHandles — only session, memory, and HA metrics
prometheus::Labels const no_labels{};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Non-blocking] process_start_time_seconds is captured at lazy metrics-singleton construction, not at process start

singleton at 2707–2710

Comment on lines +98 to 107
void RecordSnapshotFailure(metrics::DatabaseMetricHandles const &handles, durability::SnapshotCreationError const error,
std::atomic_bool const &abort_requested) {
if (error == durability::SnapshotCreationError::kNoSpaceLeft) {
handles.snapshot_failures_no_space.Increment();
} else if (!abort_requested.load(std::memory_order_acquire)) {
handles.snapshot_failures_write_error.Increment();
}
}

constexpr auto ActionToStorageOperation(MetadataDelta::Action const action) -> durability::StorageMetadataOperation {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Non-blocking] Snapshot-failure classification can lose a write_error count when shutdown races with a write failure

@@ -4692,8 +4710,10 @@ std::expected<std::filesystem::path, InMemoryStorage::CreateSnapshotError> InMem
.storage_uuid_ = storage_uuid,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Non-blocking] memgraph_snapshot_failures_total omits broken-storage snapshot refusals

Cited lines 4638-4642 are outside this PR's changes; this comment is on the nearest changed line.

return results;
}

std::map<utils::UUID, double> InterpreterContext::LongestTransactionSeconds(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Non-blocking] (performance hazard) Scrape-frequency longest-transaction walk contends with commits/aborts and holds a spinlock during db_acc_mutex_ acquisition

Cited lines 334-350.

Comment thread src/dbms/dbms_handler.cpp
Comment on lines +96 to +101
.db_embedding_memory_tracked = db.DbEmbeddingMemoryUsage(),
.db_query_memory_tracked = db.DbQueryMemoryUsage()};
if (auto const *mem = dynamic_cast<storage::InMemoryStorage const *>(db.storage())) {
if (auto const progress = mem->TryGetSnapshotProgress(); progress && progress->start_steady_ms > 0) {
snapshot.snapshot_running_seconds = SecondsSince(progress->start_steady_ms);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Non-blocking] (observability gap) Non-InMemoryStorage databases report constant 0 for snapshot/GC metrics

The pool's monitor can hold a task outside every queue while it moves it
off a stuck worker, so a single read right after queuing could undercount.
@sonarqubecloud

Copy link
Copy Markdown

@colinbarry
colinbarry marked this pull request as ready for review November 23, 2026 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI -build=coverage -test=core Run coverage build and core tests on push CI -build=release -test=core Run release build and core tests on push CI -build=release -test=e2e Run release build and e2e tests on push Docs needed Docs needed feature feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant