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

This commit is contained in:
Nishant Joshi
2023-05-25 15:11:37 +05:30
committed by GitHub
parent 253eead301
commit 28f0d1f535
3 changed files with 19 additions and 8 deletions

10
Cargo.lock generated
View File

@ -3000,7 +3000,7 @@ dependencies = [
[[package]] [[package]]
name = "opentelemetry" name = "opentelemetry"
version = "0.18.0" version = "0.18.0"
source = "git+https://github.com/open-telemetry/opentelemetry-rust/?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658" source = "git+https://github.com/open-telemetry/opentelemetry-rust?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658"
dependencies = [ dependencies = [
"opentelemetry_api", "opentelemetry_api",
"opentelemetry_sdk", "opentelemetry_sdk",
@ -3009,7 +3009,7 @@ dependencies = [
[[package]] [[package]]
name = "opentelemetry-otlp" name = "opentelemetry-otlp"
version = "0.11.0" version = "0.11.0"
source = "git+https://github.com/open-telemetry/opentelemetry-rust/?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658" source = "git+https://github.com/open-telemetry/opentelemetry-rust?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"futures", "futures",
@ -3026,7 +3026,7 @@ dependencies = [
[[package]] [[package]]
name = "opentelemetry-proto" name = "opentelemetry-proto"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/open-telemetry/opentelemetry-rust/?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658" source = "git+https://github.com/open-telemetry/opentelemetry-rust?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658"
dependencies = [ dependencies = [
"futures", "futures",
"futures-util", "futures-util",
@ -3038,7 +3038,7 @@ dependencies = [
[[package]] [[package]]
name = "opentelemetry_api" name = "opentelemetry_api"
version = "0.18.0" version = "0.18.0"
source = "git+https://github.com/open-telemetry/opentelemetry-rust/?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658" source = "git+https://github.com/open-telemetry/opentelemetry-rust?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658"
dependencies = [ dependencies = [
"fnv", "fnv",
"futures-channel", "futures-channel",
@ -3053,7 +3053,7 @@ dependencies = [
[[package]] [[package]]
name = "opentelemetry_sdk" name = "opentelemetry_sdk"
version = "0.18.0" version = "0.18.0"
source = "git+https://github.com/open-telemetry/opentelemetry-rust/?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658" source = "git+https://github.com/open-telemetry/opentelemetry-rust?rev=44b90202fd744598db8b0ace5b8f0bad7ec45658#44b90202fd744598db8b0ace5b8f0bad7ec45658"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"crossbeam-channel", "crossbeam-channel",

View File

@ -1,5 +1,7 @@
//! Interactions with the AWS KMS SDK //! Interactions with the AWS KMS SDK
use std::time::Instant;
use aws_config::meta::region::RegionProviderChain; use aws_config::meta::region::RegionProviderChain;
use aws_sdk_kms::{config::Region, primitives::Blob, Client}; use aws_sdk_kms::{config::Region, primitives::Blob, Client};
use base64::Engine; use base64::Engine;
@ -52,6 +54,7 @@ impl KmsClient {
/// `AWS_SECRET_ACCESS_KEY`) either set in environment variables, or that the SDK is running in /// `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. /// a machine that is able to assume an IAM role.
pub async fn decrypt(&self, data: impl AsRef<[u8]>) -> CustomResult<String, KmsError> { pub async fn decrypt(&self, data: impl AsRef<[u8]>) -> CustomResult<String, KmsError> {
let start = Instant::now();
let data = consts::BASE64_ENGINE let data = consts::BASE64_ENGINE
.decode(data) .decode(data)
.into_report() .into_report()
@ -75,7 +78,7 @@ impl KmsClient {
.into_report() .into_report()
.change_context(KmsError::DecryptionFailed)?; .change_context(KmsError::DecryptionFailed)?;
decrypt_output let output = decrypt_output
.plaintext .plaintext
.ok_or(KmsError::MissingPlaintextDecryptionOutput) .ok_or(KmsError::MissingPlaintextDecryptionOutput)
.into_report() .into_report()
@ -83,7 +86,12 @@ impl KmsClient {
String::from_utf8(blob.into_inner()) String::from_utf8(blob.into_inner())
.into_report() .into_report()
.change_context(KmsError::Utf8DecodingFailed) .change_context(KmsError::Utf8DecodingFailed)
}) })?;
let time_taken = start.elapsed();
metrics::AWS_KMS_DECRYPT_TIME.record(&metrics::CONTEXT, time_taken.as_secs_f64(), &[]);
Ok(output)
} }
} }

View File

@ -20,11 +20,14 @@ pub mod consts {
/// Metrics for interactions with external systems. /// Metrics for interactions with external systems.
#[cfg(feature = "kms")] #[cfg(feature = "kms")]
pub mod metrics { 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); metrics_context!(CONTEXT);
global_meter!(GLOBAL_METER, "EXTERNAL_SERVICES"); global_meter!(GLOBAL_METER, "EXTERNAL_SERVICES");
#[cfg(feature = "kms")] #[cfg(feature = "kms")]
counter_metric!(AWS_KMS_FAILURES, GLOBAL_METER); // No. of AWS KMS API failures 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)
} }