diff --git a/src/storage/backend/sqlite/SQLite.cc b/src/storage/backend/sqlite/SQLite.cc index b4213d38b9..4641f027e8 100644 --- a/src/storage/backend/sqlite/SQLite.cc +++ b/src/storage/backend/sqlite/SQLite.cc @@ -2,6 +2,7 @@ #include "zeek/storage/backend/sqlite/SQLite.h" +#include #include #include @@ -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(val)}; + }; + + auto res = RunPragma("page_count", std::nullopt, double_parser); + if ( res.code == ReturnCode::SUCCESS ) { + last_page_count_value = cast_intrusive(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(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; } diff --git a/src/storage/backend/sqlite/SQLite.h b/src/storage/backend/sqlite/SQLite.h index cdab9bf5d2..c10be5c905 100644 --- a/src/storage/backend/sqlite/SQLite.h +++ b/src/storage/backend/sqlite/SQLite.h @@ -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 diff --git a/src/telemetry/Counter.h b/src/telemetry/Counter.h index 6252e4e8f8..e170874c93 100644 --- a/src/telemetry/Counter.h +++ b/src/telemetry/Counter.h @@ -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; diff --git a/src/telemetry/Gauge.h b/src/telemetry/Gauge.h index 2f5b8962b4..abc1b9bd33 100644 --- a/src/telemetry/Gauge.h +++ b/src/telemetry/Gauge.h @@ -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; diff --git a/testing/btest/Baseline/scripts.base.frameworks.storage.sqlite-basic/out b/testing/btest/Baseline/scripts.base.frameworks.storage.sqlite-basic/out index a37c13e929..e93c61da36 100644 --- a/testing/btest/Baseline/scripts.base.frameworks.storage.sqlite-basic/out +++ b/testing/btest/Baseline/scripts.base.frameworks.storage.sqlite-basic/out @@ -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 diff --git a/testing/btest/Baseline/scripts.base.frameworks.storage.sqlite-metrics/out b/testing/btest/Baseline/scripts.base.frameworks.storage.sqlite-metrics/out index de725c3ee4..397b4098a0 100644 --- a/testing/btest/Baseline/scripts.base.frameworks.storage.sqlite-metrics/out +++ b/testing/btest/Baseline/scripts.base.frameworks.storage.sqlite-metrics/out @@ -7,6 +7,8 @@ get result 2, [code=Storage::KEY_NOT_FOUND, error_str=, value=