Skip to content

Commit c57eb5c

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

3 files changed

Lines changed: 39 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: 26 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,8 +262,18 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
261262
}
262263
}
263264
add_row_to_index(raw_row, i);
265+
#if defined(NOT_COMPILE_FOR_SWIG) && !defined(KNOWHERE_WITH_LIGHT)
266+
knowhere_sparse_dataset_nnz_len.Observe(count);
267+
#endif
264268
}
265269

270+
#if defined(NOT_COMPILE_FOR_SWIG) && !defined(KNOWHERE_WITH_LIGHT)
271+
for (size_t i = 0; i < dim_map_.size(); ++i) {
272+
knowhere_sparse_inverted_index_posting_list_len.Observe(inverted_index_ids_[i].size());
273+
}
274+
knowhere_sparse_inverted_index_size.Increment((double)size() / 1024.0 / 1024.0);
275+
#endif
276+
266277
n_rows_internal_ = rows;
267278

268279
return Status::success;
@@ -438,6 +449,9 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
438449
}
439450

440451
auto q_vec = parse_query(query, approx_params.drop_ratio_search);
452+
#if defined(NOT_COMPILE_FOR_SWIG) && !defined(KNOWHERE_WITH_LIGHT)
453+
knowhere_sparse_query_nnz_len.Observe(q_vec.size());
454+
#endif
441455
if (q_vec.empty()) {
442456
return;
443457
}
@@ -921,7 +935,18 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
921935
}
922936
inverted_index_ids_[dim_it->second].emplace_back(vec_id);
923937
inverted_index_vals_[dim_it->second].emplace_back(get_quant_val(val));
924-
if constexpr (algo == InvertedIndexAlgo::DAAT_WAND || algo == InvertedIndexAlgo::DAAT_MAXSCORE) {
938+
}
939+
// update max_score_in_dim_
940+
if constexpr (algo == InvertedIndexAlgo::DAAT_WAND || algo == InvertedIndexAlgo::DAAT_MAXSCORE) {
941+
for (size_t j = 0; j < row.size(); ++j) {
942+
auto [dim, val] = row[j];
943+
if (val == 0) {
944+
continue;
945+
}
946+
auto dim_it = dim_map_.find(dim);
947+
if (dim_it == dim_map_.cend()) {
948+
throw std::runtime_error("unexpected vector dimension in InvertedIndex");
949+
}
925950
auto score = static_cast<float>(val);
926951
if (metric_type_ == SparseMetricType::METRIC_BM25) {
927952
score = bm25_params_->max_score_computer(val, row_sum);

0 commit comments

Comments
 (0)