From 555e26327e3955e79dec5423eef4c7bb3ac4dbad Mon Sep 17 00:00:00 2001 From: stefan-mysten <135084671+stefan-mysten@users.noreply.github.com> Date: Tue, 27 Feb 2024 09:40:58 -0800 Subject: [PATCH] [graphql] Add version to prom metrics (#16408) ## Description This PR adds a version to the prometheus metrics using the default `uptime_metric` defined in the `mysten_metrics` crate, following the similar pattern done in `sui-node` and `sui-proxy`. ## Test Plan Local service check ``` # HELP uptime uptime of the node service in seconds # TYPE uptime counter uptime{chain_identifier="unknown",process="graphql",version="2024.2.0-3e521daa1bf4"} 525 ``` --- If your changes are not user-facing and do not break anything, you can skip the following section. Otherwise, please briefly describe what has changed under the Release Notes section. ### Type of Change (Check all that apply) - [ ] protocol change - [ ] user-visible impact - [ ] breaking change for a client SDKs - [ ] breaking change for FNs (FN binary must upgrade) - [ ] breaking change for validators or node operators (must upgrade binaries) - [ ] breaking change for on-chain data layout - [ ] necessitate either a data wipe or data migration ### Release notes --- Cargo.lock | 2 ++ crates/sui-graphql-rpc/Cargo.toml | 2 ++ crates/sui-graphql-rpc/src/config.rs | 3 ++ crates/sui-graphql-rpc/src/main.rs | 33 ++++++++++++++++--- crates/sui-graphql-rpc/src/server/builder.rs | 17 +++++++--- .../src/server/graphiql_server.rs | 18 +++++++--- .../sui-graphql-rpc/src/test_infra/cluster.rs | 5 ++- 7 files changed, 65 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 330d9bc54d199..869b36ad4b5f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12491,11 +12491,13 @@ dependencies = [ "bcs", "chrono", "clap", + "const-str", "diesel", "either", "expect-test", "fastcrypto", "futures", + "git-version", "hex", "http", "hyper", diff --git a/crates/sui-graphql-rpc/Cargo.toml b/crates/sui-graphql-rpc/Cargo.toml index 8768ec8f1c3d1..87b875cbe5a7a 100644 --- a/crates/sui-graphql-rpc/Cargo.toml +++ b/crates/sui-graphql-rpc/Cargo.toml @@ -16,10 +16,12 @@ async-trait.workspace = true axum.workspace = true chrono.workspace = true clap.workspace = true +const-str.workspace = true diesel.workspace = true either.workspace = true fastcrypto = { workspace = true, features = ["copy_key"] } futures.workspace = true +git-version.workspace = true hex.workspace = true http.workspace = true hyper.workspace = true diff --git a/crates/sui-graphql-rpc/src/config.rs b/crates/sui-graphql-rpc/src/config.rs index 10fe9d65a1b01..f81f77fdd7660 100644 --- a/crates/sui-graphql-rpc/src/config.rs +++ b/crates/sui-graphql-rpc/src/config.rs @@ -97,6 +97,9 @@ pub struct Limits { pub max_move_value_depth: u32, } +#[derive(Debug)] +pub struct Version(pub &'static str); + impl Limits { /// Extract limits for the package resolver. pub fn package_resolver_limits(&self) -> sui_package_resolver::Limits { diff --git a/crates/sui-graphql-rpc/src/main.rs b/crates/sui-graphql-rpc/src/main.rs index 2de1368dbd25e..dadf468283f2c 100644 --- a/crates/sui-graphql-rpc/src/main.rs +++ b/crates/sui-graphql-rpc/src/main.rs @@ -6,14 +6,37 @@ use std::path::PathBuf; use clap::Parser; use sui_graphql_rpc::commands::Command; -use sui_graphql_rpc::config::{ConnectionConfig, ServerConfig, ServiceConfig}; -use sui_graphql_rpc::config::{Ide, TxExecFullNodeConfig}; +use sui_graphql_rpc::config::{ + ConnectionConfig, Ide, ServerConfig, ServiceConfig, TxExecFullNodeConfig, Version, +}; use sui_graphql_rpc::server::builder::export_schema; use sui_graphql_rpc::server::graphiql_server::{ start_graphiql_server, start_graphiql_server_from_cfg_path, }; use tracing::error; +// WARNING!!! +// +// Do not move or use similar logic to generate git revision information outside of a binary entry +// point (e.g. main.rs). Placing the below logic into a library can result in unnecessary builds. +const GIT_REVISION: &str = { + if let Some(revision) = option_env!("GIT_REVISION") { + revision + } else { + git_version::git_version!( + args = ["--always", "--abbrev=12", "--dirty", "--exclude", "*"], + fallback = "DIRTY" + ) + } +}; + +// VERSION mimics what other sui binaries use for the same const +static VERSION: Version = Version(const_str::concat!( + env!("CARGO_PKG_VERSION"), + "-", + GIT_REVISION +)); + #[tokio::main] async fn main() { let cmd: Command = Command::parse(); @@ -89,11 +112,13 @@ async fn main() { ..ServerConfig::default() }; - start_graphiql_server(&server_config).await.unwrap(); + start_graphiql_server(&server_config, &VERSION) + .await + .unwrap(); } Command::FromConfig { path } => { println!("Starting server..."); - start_graphiql_server_from_cfg_path(path.to_str().unwrap()) + start_graphiql_server_from_cfg_path(path.to_str().unwrap(), &VERSION) .await .map_err(|x| { error!("Error: {:?}", x); diff --git a/crates/sui-graphql-rpc/src/server/builder.rs b/crates/sui-graphql-rpc/src/server/builder.rs index 29d76b35e04e3..57b5affa8b669 100644 --- a/crates/sui-graphql-rpc/src/server/builder.rs +++ b/crates/sui-graphql-rpc/src/server/builder.rs @@ -2,11 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 use crate::config::{ - ConnectionConfig, MAX_CONCURRENT_REQUESTS, RPC_TIMEOUT_ERR_SLEEP_RETRY_PERIOD, + ConnectionConfig, Version, MAX_CONCURRENT_REQUESTS, RPC_TIMEOUT_ERR_SLEEP_RETRY_PERIOD, }; use crate::context_data::package_cache::DbPackageStore; use crate::data::Db; - use crate::metrics::Metrics; use crate::mutation::Mutation; use crate::types::move_object::IMoveObject; @@ -225,14 +224,17 @@ impl ServerBuilder { }) } - pub async fn from_yaml_config(path: &str) -> Result<(Self, ServerConfig), Error> { + pub async fn from_yaml_config( + path: &str, + version: &Version, + ) -> Result<(Self, ServerConfig), Error> { let config = ServerConfig::from_yaml(path)?; - Self::from_config(&config) + Self::from_config(&config, version) .await .map(|builder| (builder, config)) } - pub async fn from_config(config: &ServerConfig) -> Result { + pub async fn from_config(config: &ServerConfig, version: &Version) -> Result { // PROMETHEUS let prom_addr: SocketAddr = format!( "{}:{}", @@ -248,6 +250,11 @@ impl ServerBuilder { let registry_service = mysten_metrics::start_prometheus_server(prom_addr); info!("Starting Prometheus HTTP endpoint at {}", prom_addr); let registry = registry_service.default_registry(); + registry + .register(mysten_metrics::uptime_metric( + "graphql", version.0, "unknown", + )) + .unwrap(); // METRICS let metrics = Metrics::new(®istry); diff --git a/crates/sui-graphql-rpc/src/server/graphiql_server.rs b/crates/sui-graphql-rpc/src/server/graphiql_server.rs index 8de2a392f7f18..174ef257f2bfc 100644 --- a/crates/sui-graphql-rpc/src/server/graphiql_server.rs +++ b/crates/sui-graphql-rpc/src/server/graphiql_server.rs @@ -3,7 +3,7 @@ use tracing::info; -use crate::config::ServerConfig; +use crate::config::{ServerConfig, Version}; use crate::error::Error; use crate::server::builder::ServerBuilder; @@ -16,17 +16,25 @@ async fn graphiql(ide_title: axum::Extension>) -> impl axum::resp } } -pub async fn start_graphiql_server(server_config: &ServerConfig) -> Result<(), Error> { +pub async fn start_graphiql_server( + server_config: &ServerConfig, + version: &Version, +) -> Result<(), Error> { info!("Starting server with config: {:?}", server_config); + info!("Server version: {:?}", version); start_graphiql_server_impl( - ServerBuilder::from_config(server_config).await?, + ServerBuilder::from_config(server_config, version).await?, server_config.ide.ide_title.clone(), ) .await } -pub async fn start_graphiql_server_from_cfg_path(server_config_path: &str) -> Result<(), Error> { - let (server_builder, config) = ServerBuilder::from_yaml_config(server_config_path).await?; +pub async fn start_graphiql_server_from_cfg_path( + server_config_path: &str, + version: &Version, +) -> Result<(), Error> { + let (server_builder, config) = + ServerBuilder::from_yaml_config(server_config_path, version).await?; start_graphiql_server_impl(server_builder, config.ide.ide_title).await } diff --git a/crates/sui-graphql-rpc/src/test_infra/cluster.rs b/crates/sui-graphql-rpc/src/test_infra/cluster.rs index 087a5c615ab9f..2245740acbdf6 100644 --- a/crates/sui-graphql-rpc/src/test_infra/cluster.rs +++ b/crates/sui-graphql-rpc/src/test_infra/cluster.rs @@ -4,6 +4,7 @@ use crate::config::ConnectionConfig; use crate::config::ServerConfig; use crate::config::ServiceConfig; +use crate::config::Version; use crate::server::graphiql_server::start_graphiql_server; use std::net::SocketAddr; use std::sync::Arc; @@ -166,7 +167,9 @@ pub async fn start_graphql_server_with_fn_rpc( // Starts graphql server tokio::spawn(async move { - start_graphiql_server(&server_config).await.unwrap(); + start_graphiql_server(&server_config, &Version("test")) + .await + .unwrap(); }) }