Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Tokenserver metrics #1200

Merged
merged 5 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/server/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use futures::future::Ready;

use crate::error::ApiError;
use crate::server::ServerState;
use crate::settings::Settings;
use crate::web::tags::Tags;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -176,18 +175,21 @@ pub fn metrics_from_req(req: &HttpRequest) -> Result<Box<StatsdClient>, Error> {
.clone())
}

/// Create a cadence StatsdClient from the given options
pub fn metrics_from_opts(opts: &Settings) -> Result<StatsdClient, ApiError> {
let builder = if let Some(statsd_host) = opts.statsd_host.as_ref() {
pub fn metrics_from_opts(
label: String,
host: Option<String>,
Copy link
Member

@pjenvey pjenvey Jan 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not terribly important but this could take references instead. Option::as_deref can easily give you an Option<&str> from the host field Option<String> (as_ref doesn't)

Suggested change
label: String,
host: Option<String>,
label: &str
host: Option<&str>,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

port: u16,
) -> Result<StatsdClient, ApiError> {
let builder = if let Some(statsd_host) = host.as_ref() {
let socket = UdpSocket::bind("0.0.0.0:0")?;
socket.set_nonblocking(true)?;

let host = (statsd_host.as_str(), opts.statsd_port);
let host = (statsd_host.as_str(), port);
let udp_sink = BufferedUdpMetricSink::from(host, socket)?;
let sink = QueuingMetricSink::from(udp_sink);
StatsdClient::builder(opts.statsd_label.as_ref(), sink)
StatsdClient::builder(label.as_ref(), sink)
} else {
StatsdClient::builder(opts.statsd_label.as_ref(), NopMetricSink)
StatsdClient::builder(label.as_ref(), NopMetricSink)
};
Ok(builder
.with_error_handler(|err| {
Expand Down
23 changes: 18 additions & 5 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ macro_rules! build_app_without_syncstorage {
impl Server {
pub async fn with_settings(settings: Settings) -> Result<dev::Server, ApiError> {
let settings_copy = settings.clone();
let metrics = metrics::metrics_from_opts(&settings)?;
let metrics = metrics::metrics_from_opts(
settings.statsd_label.clone(),
settings.statsd_host.clone(),
settings.statsd_port,
)?;
let host = settings.host.clone();
let port = settings.port;
let db_pool = pool_from_settings(&settings, &Metrics::from(&metrics)).await?;
Expand All @@ -241,7 +245,11 @@ impl Server {
let tokenserver_state = if settings.tokenserver.enabled {
Some(tokenserver::ServerState::from_settings(
&settings.tokenserver,
metrics.clone(),
metrics::metrics_from_opts(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tokenserver will have its own separate metrics client -- is that okay? It seems like it shouldn't be a problem to me, but I wanted to double-check

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, it's not a big deal for now.

settings.tokenserver.statsd_label.clone(),
settings.statsd_host,
settings.statsd_port,
)?,
)?)
} else {
None
Expand Down Expand Up @@ -287,9 +295,14 @@ impl Server {
let host = settings.host.clone();
let port = settings.port;
let secrets = Arc::new(settings.master_secret.clone());
let metrics = metrics::metrics_from_opts(&settings)?;
let tokenserver_state =
tokenserver::ServerState::from_settings(&settings.tokenserver, metrics)?;
let tokenserver_state = tokenserver::ServerState::from_settings(
&settings.tokenserver,
metrics::metrics_from_opts(
settings.tokenserver.statsd_label.clone(),
settings.statsd_host,
settings.statsd_port,
)?,
)?;
let server = HttpServer::new(move || {
build_app_without_syncstorage!(
Some(tokenserver_state.clone()),
Expand Down
1 change: 1 addition & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ impl Settings {
s.set_default("tokenserver.fxa_metrics_hash_secret", "secret")?;
s.set_default("tokenserver.test_mode_enabled", false)?;
s.set_default("tokenserver.node_type", "spanner")?;
s.set_default("tokenserver.statsd_label", "syncstorage.tokenserver")?;

// Set Cors defaults
s.set_default(
Expand Down
11 changes: 10 additions & 1 deletion src/tokenserver/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,8 @@ mod tests {
}

fn make_state(verifier: MockOAuthVerifier) -> ServerState {
let settings = Settings::default();

ServerState {
fxa_email_domain: "test.com".to_owned(),
fxa_metrics_hash_secret: "".to_owned(),
Expand All @@ -1043,7 +1045,14 @@ mod tests {
node_capacity_release_rate: None,
node_type: NodeType::default(),
service_id: None,
metrics: Box::new(metrics::metrics_from_opts(&Settings::default()).unwrap()),
metrics: Box::new(
metrics::metrics_from_opts(
settings.tokenserver.statsd_label,
settings.statsd_host,
settings.statsd_port,
)
.unwrap(),
),
}
}
}
4 changes: 4 additions & 0 deletions src/tokenserver/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub struct Settings {

/// The type of the storage nodes used by this instance of Tokenserver.
pub node_type: NodeType,

/// The label to be used when reporting Metrics.
pub statsd_label: String,
}

impl Default for Settings {
Expand All @@ -52,6 +55,7 @@ impl Default for Settings {
test_mode_enabled: false,
node_capacity_release_rate: None,
node_type: NodeType::Spanner,
statsd_label: "syncstorage.tokenserver".to_owned(),
}
}
}
9 changes: 8 additions & 1 deletion src/web/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,14 @@ mod tests {
limits: Arc::clone(&SERVER_LIMITS),
limits_json: serde_json::to_string(&**SERVER_LIMITS).unwrap(),
port: 8000,
metrics: Box::new(metrics::metrics_from_opts(&settings).unwrap()),
metrics: Box::new(
metrics::metrics_from_opts(
settings.statsd_label,
settings.statsd_host,
settings.statsd_port,
)
.unwrap(),
),
quota_enabled: settings.enable_quota,
deadman: Arc::new(RwLock::new(Deadman::default())),
}
Expand Down