From 535aa568ac6050b5338f98910d095d9191cb91f6 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Thu, 28 Jan 2021 14:29:45 +0800 Subject: [PATCH] server: add server info metrics for DBasS (#9582) (#9591) cherry-pick #9582 to release-4.0 You can switch your code base to this Pull Request by using [git-extras](https://github.com/tj/git-extras): ```bash # In tikv repo: git pr https://github.com/tikv/tikv/pull/9591 ``` After apply modifications, you can push your change to this PR via: ```bash git push git@github.com:ti-srebot/tikv.git pr/9591:release-4.0-0fe01b8da4b8 ``` --- Signed-off-by: iosmanthus ### What problem does this PR solve? This pull request adds server info metrics via a `GaugeVec` in format: `(version, githash) -> startup_ts`. Problem Summary: ### Related changes - Need to cherry-pick to the release branch ### Check List Tests - Manual test (add detailed scripts or steps below) ```sh http get 'http://localhost:9090/api/datasources/proxy/1/api/v1/query_range?query=tikv_server_info&start=1611738330&end=1611738465&step=15' { "data": { "result": [ { "metric": { "__name__": "tikv_server_info", "hash": "None", "instance": "127.0.0.1:20180", "job": "tikv", "version": "5.0.0-rc.x" }, "values": [ [ 1611738360, "1611738218" ], [ 1611738375, "1611738218" ], [ 1611738390, "1611738218" ], [ 1611738405, "1611738218" ] ] } ], "resultType": "matrix" }, "status": "success" } ``` Side effects - Performance regression - Consumes more CPU - Consumes more MEM - Breaking backward compatibility ### Release note - server: add server info metrics for DBasS --- src/server/metrics.rs | 6 ++++++ src/server/server.rs | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/server/metrics.rs b/src/server/metrics.rs index ad26c2cc3003..09c0659ba0a9 100644 --- a/src/server/metrics.rs +++ b/src/server/metrics.rs @@ -232,6 +232,12 @@ lazy_static! { exponential_buckets(1f64, 5f64, 10).unwrap() ) .unwrap(); + pub static ref SERVER_INFO_GAUGE_VEC: IntGaugeVec = register_int_gauge_vec!( + "tikv_server_info", + "Indicate the tikv server info, and the value is the server startup timestamp(s).", + &["version", "hash"] + ) + .unwrap(); } make_static_metric! { diff --git a/src/server/server.rs b/src/server/server.rs index 8723a2096c7d..28a93a4f1952 100644 --- a/src/server/server.rs +++ b/src/server/server.rs @@ -4,7 +4,7 @@ use std::i32; use std::net::{IpAddr, SocketAddr}; use std::str::FromStr; use std::sync::{Arc, RwLock}; -use std::time::{Duration, Instant}; +use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; use futures::{Future, Stream}; use grpcio::{ @@ -26,12 +26,13 @@ use tikv_util::worker::Worker; use tikv_util::Either; use super::load_statistics::*; +use super::metrics::SERVER_INFO_GAUGE_VEC; use super::raft_client::RaftClient; use super::resolve::StoreAddrResolver; use super::service::*; use super::snap::{Runner as SnapHandler, Task as SnapTask}; use super::transport::ServerTransport; -use super::{Config, Result}; +use super::{Config, Error, Result}; use crate::read_pool::ReadPool; const LOAD_STATISTICS_SLOTS: usize = 4; @@ -245,6 +246,18 @@ impl Server { ) }; + let startup_ts = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_err(|_| Error::Other(box_err!("Clock may have gone backwards")))? + .as_secs(); + + SERVER_INFO_GAUGE_VEC + .with_label_values(&[ + &("v".to_owned() + env!("CARGO_PKG_VERSION")), + option_env!("TIKV_BUILD_GIT_HASH").unwrap_or("None"), + ]) + .set(startup_ts as i64); + info!("TiKV is ready to serve"); Ok(()) }