Skip to content

Commit

Permalink
server: add server info metrics for DBasS (tikv#9582) (tikv#9591)
Browse files Browse the repository at this point in the history
cherry-pick tikv#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 tikv#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 <myosmanthustree@gmail.com>

### 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 <!--REMOVE the items that are not applicable-->

Tests <!-- At least one of them must be included. -->

- 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 <!-- bugfixes or new feature need a release note -->

- server: add server info metrics for DBasS
  • Loading branch information
ti-srebot authored and gengliqi committed Feb 19, 2021
1 parent 1333fa0 commit 535aa56
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/server/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down
17 changes: 15 additions & 2 deletions src/server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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;
Expand Down Expand Up @@ -245,6 +246,18 @@ impl<T: RaftStoreRouter, S: StoreAddrResolver + 'static> Server<T, S> {
)
};

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(())
}
Expand Down

0 comments on commit 535aa56

Please sign in to comment.