Add SQLite page_count and file_size metrics

This commit is contained in:
Tim Wojtulewicz 2025-07-03 09:49:54 -07:00
parent f73ac7089f
commit d0a6d84237
6 changed files with 47 additions and 1 deletions

View file

@ -2,6 +2,7 @@
#include "zeek/storage/backend/sqlite/SQLite.h"
#include <sys/stat.h>
#include <filesystem>
#include <thread>
@ -12,7 +13,7 @@
#include "zeek/Func.h"
#include "zeek/Val.h"
#include "zeek/storage/ReturnCode.h"
#include "zeek/telemetry/Counter.h"
#include "zeek/telemetry/Manager.h"
#include "const.bif.netvar_h"
@ -260,6 +261,33 @@ OperationResult SQLite::DoOpen(OpenResultCallback* cb, RecordValPtr options) {
get_expiry_last_run_stmt = std::move(stmt_ptrs[6]);
update_expiry_last_run_stmt = std::move(stmt_ptrs[7]);
page_count_metric =
telemetry_mgr->GaugeInstance("zeek", "storage_sqlite_database_size", {{"config", GetConfigMetricsLabel()}},
"Storage sqlite backend value of page_count pragma", "pages", [this]() {
static auto double_parser = [](sqlite3_stmt* stmt) -> OperationResult {
double val = sqlite3_column_double(stmt, 0);
return {ReturnCode::SUCCESS, "", make_intrusive<DoubleVal>(val)};
};
auto res = RunPragma("page_count", std::nullopt, double_parser);
if ( res.code == ReturnCode::SUCCESS ) {
last_page_count_value = cast_intrusive<DoubleVal>(res.value)->Get();
}
return last_page_count_value;
});
file_size_metric =
telemetry_mgr->GaugeInstance("zeek", "storage_sqlite_database_size", {{"config", GetConfigMetricsLabel()}},
"Storage sqlite backend database file size on disk", "bytes", [this]() {
struct stat s{0};
if ( int ret = stat(full_path.c_str(), &s); ret == 0 ) {
last_file_size_value = static_cast<double>(s.st_size);
}
return last_file_size_value;
});
return {ReturnCode::SUCCESS};
}
@ -306,6 +334,12 @@ OperationResult SQLite::DoClose(ResultCallback* cb) {
expire_db = nullptr;
}
if ( page_count_metric )
page_count_metric->RemoveCallback();
if ( file_size_metric )
file_size_metric->RemoveCallback();
return op_res;
}

View file

@ -76,6 +76,12 @@ private:
std::string table_name;
std::chrono::milliseconds pragma_timeout;
std::chrono::milliseconds pragma_wait_on_busy;
telemetry::GaugePtr page_count_metric;
telemetry::GaugePtr file_size_metric;
double last_page_count_value = 0.0;
double last_file_size_value = 0.0;
};
} // namespace zeek::storage::backend::sqlite

View file

@ -62,6 +62,7 @@ public:
bool HasCallback() const noexcept { return callback != nullptr; }
double RunCallback() const { return callback(); }
void RemoveCallback() { callback = nullptr; }
private:
friend class CounterFamily;

View file

@ -84,6 +84,7 @@ public:
bool HasCallback() const noexcept { return callback != nullptr; }
double RunCallback() const { return callback(); }
void RemoveCallback() { callback = nullptr; }
private:
FamilyType* family = nullptr;

View file

@ -12,6 +12,8 @@ get result same as inserted, T
Post-operation metrics:
Telemetry::COUNTER, zeek, zeek_storage_backends_opened_total, [], [], 1.0
Telemetry::GAUGE, zeek, zeek_storage_sqlite_database_size_bytes, [config], [test.sqlite-testing], 4096.0
Telemetry::GAUGE, zeek, zeek_storage_sqlite_database_size_pages, [config], [test.sqlite-testing], 5.0
Telemetry::COUNTER, zeek, zeek_storage_backend_data_read_bytes_total, [config, type], [test.sqlite-testing, Storage::STORAGE_BACKEND_SQLITE], 18.0
Telemetry::COUNTER, zeek, zeek_storage_backend_expired_entries_total, [config, type], [test.sqlite-testing, Storage::STORAGE_BACKEND_SQLITE], 0.0
Telemetry::COUNTER, zeek, zeek_storage_backend_operation_results_total, [config, operation, result, type], [test.sqlite-testing, erase, error, Storage::STORAGE_BACKEND_SQLITE], 0.0

View file

@ -7,6 +7,8 @@ get result 2, [code=Storage::KEY_NOT_FOUND, error_str=<uninitialized>, value=<un
Post-operation metrics:
Telemetry::COUNTER, zeek, zeek_storage_backends_opened_total, [], [], 1.0
Telemetry::GAUGE, zeek, zeek_storage_sqlite_database_size_bytes, [config], [test.sqlite-testing], 4096.0
Telemetry::GAUGE, zeek, zeek_storage_sqlite_database_size_pages, [config], [test.sqlite-testing], 5.0
Telemetry::COUNTER, zeek, zeek_storage_backend_data_read_bytes_total, [config, type], [test.sqlite-testing, Storage::STORAGE_BACKEND_SQLITE], 18.0
Telemetry::COUNTER, zeek, zeek_storage_backend_expired_entries_total, [config, type], [test.sqlite-testing, Storage::STORAGE_BACKEND_SQLITE], 0.0
Telemetry::COUNTER, zeek, zeek_storage_backend_operation_results_total, [config, operation, result, type], [test.sqlite-testing, erase, error, Storage::STORAGE_BACKEND_SQLITE], 0.0