Skip to content

Commit 23e99ef

Browse files
committed
refactor(zerokms): hoist init_cipher timing out of match arms
Compute duration and record histogram once before matching on the ScopedCipher::init result, removing duplicated timing code from both Ok and Err arms.
1 parent 432de47 commit 23e99ef

2 files changed

Lines changed: 12 additions & 18 deletions

File tree

packages/cipherstash-proxy/src/log/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ use tracing_subscriber::{
1616
// All targets are now defined in the targets module using the define_log_targets! macro.
1717
pub use targets::{
1818
AUTHENTICATION, CONFIG, CONTEXT, DECRYPT, DEVELOPMENT, ENCODING, ENCRYPT, ENCRYPT_CONFIG,
19-
KEYSET, MAPPER, MAPPER, MIGRATE, MIGRATE, PROTOCOL, PROTOCOL, PROXY, SCHEMA, SCHEMA,
20-
SLOW_STATEMENTS, ZERO_KMS,
19+
MAPPER, MIGRATE, PROTOCOL, SCHEMA, SLOW_STATEMENTS, ZERO_KMS,
2120
};
2221

2322
static INIT: Once = Once::new();

packages/cipherstash-proxy/src/proxy/zerokms/zerokms.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use crate::{
44
log::{ENCRYPT, ZERO_KMS},
55
postgresql::{Column, KeysetIdentifier},
66
prometheus::{
7-
KEYSET_CIPHER_CACHE_HITS_TOTAL, KEYSET_CIPHER_CACHE_HITS_TOTAL,
8-
KEYSET_CIPHER_CACHE_MISS_TOTAL, KEYSET_CIPHER_INIT_DURATION_SECONDS,
9-
KEYSET_CIPHER_INIT_DURATION_SECONDS, KEYSET_CIPHER_INIT_TOTAL, KEYSET_CIPHER_INIT_TOTAL,
7+
KEYSET_CIPHER_CACHE_HITS_TOTAL, KEYSET_CIPHER_CACHE_MISS_TOTAL,
8+
KEYSET_CIPHER_INIT_DURATION_SECONDS, KEYSET_CIPHER_INIT_TOTAL,
109
},
1110
proxy::EncryptionService,
1211
};
@@ -97,17 +96,18 @@ impl ZeroKms {
9796
let identified_by = keyset_id.as_ref().map(|id| id.0.clone());
9897

9998
let start = Instant::now();
100-
match ScopedCipher::init(zerokms_client, identified_by).await {
101-
Ok(cipher) => {
102-
let init_duration = start.elapsed();
103-
let init_duration_ms = init_duration.as_millis();
99+
let result = ScopedCipher::init(zerokms_client, identified_by).await;
100+
let init_duration = start.elapsed();
101+
let init_duration_ms = init_duration.as_millis();
104102

105-
histogram!(KEYSET_CIPHER_INIT_DURATION_SECONDS).record(init_duration.as_secs_f64());
103+
histogram!(KEYSET_CIPHER_INIT_DURATION_SECONDS).record(init_duration.as_secs_f64());
106104

107-
if init_duration > Duration::from_secs(1) {
108-
warn!(target: ZERO_KMS, msg = "Slow ScopedCipher initialization", ?keyset_id, init_duration_ms);
109-
}
105+
if init_duration > Duration::from_secs(1) {
106+
warn!(target: ZERO_KMS, msg = "Slow ScopedCipher initialization", ?keyset_id, init_duration_ms);
107+
}
110108

109+
match result {
110+
Ok(cipher) => {
111111
let arc_cipher = Arc::new(cipher);
112112

113113
counter!(KEYSET_CIPHER_INIT_TOTAL).increment(1);
@@ -130,11 +130,6 @@ impl ZeroKms {
130130
Ok(arc_cipher)
131131
}
132132
Err(err) => {
133-
let init_duration = start.elapsed();
134-
let init_duration_ms = init_duration.as_millis();
135-
136-
histogram!(KEYSET_CIPHER_INIT_DURATION_SECONDS).record(init_duration.as_secs_f64());
137-
138133
debug!(target: ZERO_KMS, msg = "Error initializing ZeroKMS ScopedCipher", error = err.to_string(), init_duration_ms);
139134
warn!(
140135
msg = "Error initializing ZeroKMS",

0 commit comments

Comments
 (0)