Skip to content

Commit

Permalink
[graphql] Add version to prom metrics (MystenLabs#16408)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
stefan-mysten authored Feb 27, 2024
1 parent dc8bc5d commit 555e263
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/sui-graphql-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions crates/sui-graphql-rpc/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 29 additions & 4 deletions crates/sui-graphql-rpc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 12 additions & 5 deletions crates/sui-graphql-rpc/src/server/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Self, Error> {
pub async fn from_config(config: &ServerConfig, version: &Version) -> Result<Self, Error> {
// PROMETHEUS
let prom_addr: SocketAddr = format!(
"{}:{}",
Expand All @@ -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(&registry);
Expand Down
18 changes: 13 additions & 5 deletions crates/sui-graphql-rpc/src/server/graphiql_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,17 +16,25 @@ async fn graphiql(ide_title: axum::Extension<Option<String>>) -> 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
}

Expand Down
5 changes: 4 additions & 1 deletion crates/sui-graphql-rpc/src/test_infra/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
})
}

Expand Down

0 comments on commit 555e263

Please sign in to comment.