Skip to content

Commit fa96fab

Browse files
committed
sparse: add some prometheus metrics for sparse
Signed-off-by: Shawn Wang <shawn.wang@zilliz.com>
1 parent 8a705a0 commit fa96fab

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

include/knowhere/prometheus_client.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,9 @@ DECLARE_PROMETHEUS_HISTOGRAM(hnsw_search_hops, PROMETHEUS_LABEL_KNOWHERE);
131131
DECLARE_PROMETHEUS_HISTOGRAM(diskann_bitset_ratio, PROMETHEUS_LABEL_KNOWHERE);
132132
DECLARE_PROMETHEUS_HISTOGRAM(diskann_search_hops, PROMETHEUS_LABEL_KNOWHERE);
133133
DECLARE_PROMETHEUS_HISTOGRAM(diskann_range_search_iters, PROMETHEUS_LABEL_KNOWHERE);
134+
135+
DECLARE_PROMETHEUS_HISTOGRAM(sparse_dataset_nnz_len, PROMETHEUS_LABEL_KNOWHERE);
136+
DECLARE_PROMETHEUS_HISTOGRAM(sparse_query_nnz_len, PROMETHEUS_LABEL_KNOWHERE);
137+
DECLARE_PROMETHEUS_HISTOGRAM(sparse_inverted_index_posting_list_len, PROMETHEUS_LABEL_KNOWHERE);
138+
DECLARE_PROMETHEUS_COUNTER(sparse_inverted_index_size, PROMETHEUS_LABEL_KNOWHERE);
134139
} // namespace knowhere

src/common/prometheus_client.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,12 @@ DEFINE_PROMETHEUS_HISTOGRAM_FAMILY(diskann_range_search_iters, "DISKANN range se
122122
DEFINE_PROMETHEUS_HISTOGRAM_WITH_BUCKETS(diskann_range_search_iters, PROMETHEUS_LABEL_KNOWHERE,
123123
diskannRangeSearchIterBuckets)
124124

125+
DEFINE_PROMETHEUS_HISTOGRAM_FAMILY(sparse_dataset_nnz_len, "sparse dataset nnz length")
126+
DEFINE_PROMETHEUS_HISTOGRAM(sparse_dataset_nnz_len, PROMETHEUS_LABEL_KNOWHERE)
127+
DEFINE_PROMETHEUS_HISTOGRAM_FAMILY(sparse_query_nnz_len, "sparse query nnz length")
128+
DEFINE_PROMETHEUS_HISTOGRAM(sparse_query_nnz_len, PROMETHEUS_LABEL_KNOWHERE)
129+
DEFINE_PROMETHEUS_HISTOGRAM_FAMILY(sparse_inverted_index_posting_list_len, "sparse inverted index posting list length")
130+
DEFINE_PROMETHEUS_HISTOGRAM(sparse_inverted_index_posting_list_len, PROMETHEUS_LABEL_KNOWHERE)
131+
DEFINE_PROMETHEUS_COUNTER_FAMILY(sparse_inverted_index_size, "sparse inverted index size (MB)")
132+
DEFINE_PROMETHEUS_COUNTER(sparse_inverted_index_size, PROMETHEUS_LABEL_KNOWHERE)
125133
} // namespace knowhere

src/index/sparse/sparse_inverted_index.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "knowhere/comp/index_param.h"
3131
#include "knowhere/expected.h"
3232
#include "knowhere/log.h"
33+
#include "knowhere/prometheus_client.h"
3334
#include "knowhere/sparse_utils.h"
3435
#include "knowhere/utils.h"
3536

@@ -261,10 +262,17 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
261262
}
262263
}
263264
add_row_to_index(raw_row, i);
265+
knowhere_sparse_dataset_nnz_len.Observe(count);
266+
}
267+
268+
for (size_t i = 0; i < dim_map_.size(); ++i) {
269+
knowhere_sparse_inverted_index_posting_list_len.Observe(inverted_index_ids_[i].size());
264270
}
265271

266272
n_rows_internal_ = rows;
267273

274+
knowhere_sparse_inverted_index_size.Increment((double)size() / 1024.0 / 1024.0);
275+
268276
return Status::success;
269277
}
270278

@@ -438,6 +446,7 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
438446
}
439447

440448
auto q_vec = parse_query(query, approx_params.drop_ratio_search);
449+
knowhere_sparse_query_nnz_len.Observe(q_vec.size());
441450
if (q_vec.empty()) {
442451
return;
443452
}
@@ -921,7 +930,18 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
921930
}
922931
inverted_index_ids_[dim_it->second].emplace_back(vec_id);
923932
inverted_index_vals_[dim_it->second].emplace_back(get_quant_val(val));
924-
if constexpr (algo == InvertedIndexAlgo::DAAT_WAND || algo == InvertedIndexAlgo::DAAT_MAXSCORE) {
933+
}
934+
// update max_score_in_dim_
935+
if constexpr (algo == InvertedIndexAlgo::DAAT_WAND || algo == InvertedIndexAlgo::DAAT_MAXSCORE) {
936+
for (size_t j = 0; j < row.size(); ++j) {
937+
auto [dim, val] = row[j];
938+
if (val == 0) {
939+
continue;
940+
}
941+
auto dim_it = dim_map_.find(dim);
942+
if (dim_it == dim_map_.cend()) {
943+
throw std::runtime_error("unexpected vector dimension in InvertedIndex");
944+
}
925945
auto score = static_cast<float>(val);
926946
if (metric_type_ == SparseMetricType::METRIC_BM25) {
927947
score = bm25_params_->max_score_computer(val, row_sum);

0 commit comments

Comments
 (0)