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

fix(kms): add metrics to external_services kms #1237

Merged
merged 2 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 5 additions & 5 deletions Cargo.lock

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

12 changes: 10 additions & 2 deletions crates/external_services/src/kms.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Interactions with the AWS KMS SDK

use std::time::Instant;

use aws_config::meta::region::RegionProviderChain;
use aws_sdk_kms::{config::Region, primitives::Blob, Client};
use base64::Engine;
Expand Down Expand Up @@ -52,6 +54,7 @@ impl KmsClient {
/// `AWS_SECRET_ACCESS_KEY`) either set in environment variables, or that the SDK is running in
/// a machine that is able to assume an IAM role.
pub async fn decrypt(&self, data: impl AsRef<[u8]>) -> CustomResult<String, KmsError> {
let start = Instant::now();
let data = consts::BASE64_ENGINE
.decode(data)
.into_report()
Expand All @@ -75,15 +78,20 @@ impl KmsClient {
.into_report()
.change_context(KmsError::DecryptionFailed)?;

decrypt_output
let output = decrypt_output
.plaintext
.ok_or(KmsError::MissingPlaintextDecryptionOutput)
.into_report()
.and_then(|blob| {
String::from_utf8(blob.into_inner())
.into_report()
.change_context(KmsError::Utf8DecodingFailed)
})
})?;

let time_taken = start.elapsed();
metrics::AWS_KMS_DECRYPT_TIME.record(&metrics::CONTEXT, time_taken.as_secs_f64(), &[]);

Ok(output)
}
}

Expand Down
5 changes: 4 additions & 1 deletion crates/external_services/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ pub mod consts {
/// Metrics for interactions with external systems.
#[cfg(feature = "kms")]
pub mod metrics {
use router_env::{counter_metric, global_meter, metrics_context};
use router_env::{counter_metric, global_meter, histogram_metric, metrics_context};

metrics_context!(CONTEXT);
global_meter!(GLOBAL_METER, "EXTERNAL_SERVICES");

#[cfg(feature = "kms")]
counter_metric!(AWS_KMS_FAILURES, GLOBAL_METER); // No. of AWS KMS API failures

#[cfg(feature = "kms")]
histogram_metric!(AWS_KMS_DECRYPT_TIME, GLOBAL_METER); // Histogram for KMS decryption time (in sec)
}