From e69e3d3281d8f060d298ceb8637002fe48d0555f Mon Sep 17 00:00:00 2001
From: chasingegg <chao.gao@zilliz.com>
Date: Mon, 16 Oct 2023 12:04:27 +0800
Subject: [PATCH] Record knowhere index build time

Signed-off-by: chasingegg <chao.gao@zilliz.com>
---
 include/knowhere/prometheus_client.h | 1 +
 src/common/index.cc                  | 9 ++++++++-
 src/common/prometheus_client.cc      | 1 +
 3 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/include/knowhere/prometheus_client.h b/include/knowhere/prometheus_client.h
index 7083e6f73..8a437d56a 100644
--- a/include/knowhere/prometheus_client.h
+++ b/include/knowhere/prometheus_client.h
@@ -78,6 +78,7 @@ DECLARE_PROMETHEUS_COUNTER(knowhere_build_count);
 DECLARE_PROMETHEUS_COUNTER(knowhere_search_count);
 DECLARE_PROMETHEUS_COUNTER(knowhere_ann_iterator_count);
 DECLARE_PROMETHEUS_COUNTER(knowhere_range_search_count);
+DECLARE_PROMETHEUS_HISTOGRAM(knowhere_build_latency);
 DECLARE_PROMETHEUS_HISTOGRAM(knowhere_search_topk);
 DECLARE_PROMETHEUS_HISTOGRAM(knowhere_search_latency);
 DECLARE_PROMETHEUS_HISTOGRAM(knowhere_ann_iterator_init_latency);
diff --git a/src/common/index.cc b/src/common/index.cc
index 0bedb4a29..3d372ffe2 100644
--- a/src/common/index.cc
+++ b/src/common/index.cc
@@ -40,9 +40,16 @@ Index<T>::Build(const DataSet& dataset, const Json& json) {
     RETURN_IF_ERROR(cfg->CheckAndAdjustForBuild());
 
 #ifdef NOT_COMPILE_FOR_SWIG
+    TimeRecorder rc("Build index", 2);
+    auto res = this->node->Build(dataset, *cfg);
+    auto span = rc.ElapseFromBegin("done");
+    span *= 0.000001;  // convert to s
+    knowhere_build_latency.Observe(span);
     knowhere_build_count.Increment();
+#else
+    auto res = this->node->Build(dataset, *cfg);
 #endif
-    return this->node->Build(dataset, *cfg);
+    return res;
 }
 
 template <typename T>
diff --git a/src/common/prometheus_client.cc b/src/common/prometheus_client.cc
index 791a14245..83b6a21cc 100644
--- a/src/common/prometheus_client.cc
+++ b/src/common/prometheus_client.cc
@@ -29,6 +29,7 @@ DEFINE_PROMETHEUS_COUNTER(knowhere_build_count, "knowhere index build count")
 DEFINE_PROMETHEUS_COUNTER(knowhere_search_count, "knowhere search count")
 DEFINE_PROMETHEUS_COUNTER(knowhere_ann_iterator_count, "knowhere ann iterator count")
 DEFINE_PROMETHEUS_COUNTER(knowhere_range_search_count, "knowhere range search count")
+DEFINE_PROMETHEUS_HISTOGRAM(knowhere_build_latency, "index build latency in knowhere (s)")
 DEFINE_PROMETHEUS_HISTOGRAM(knowhere_search_topk, "knowhere search topk")
 DEFINE_PROMETHEUS_HISTOGRAM(knowhere_search_latency, "search latency in knowhere (ms)")
 DEFINE_PROMETHEUS_HISTOGRAM(knowhere_ann_iterator_init_latency, "ann iterator init latency in knowhere (ms)")