From 2845a5fdaf4cd00dc07930d2a9744153cb8fb8df Mon Sep 17 00:00:00 2001 From: Juliano Costa Date: Tue, 5 Mar 2024 16:04:32 +0100 Subject: [PATCH] Add metrics conf --- src/shippingservice/Cargo.toml | 4 +- src/shippingservice/src/main.rs | 3 + src/shippingservice/src/shipping_service.rs | 78 +------------------ src/shippingservice/src/telemetry.rs | 3 + .../src/telemetry/logs_conf.rs | 6 +- .../src/telemetry/metrics_conf.rs | 15 ++++ 6 files changed, 29 insertions(+), 80 deletions(-) create mode 100644 src/shippingservice/src/telemetry/metrics_conf.rs diff --git a/src/shippingservice/Cargo.toml b/src/shippingservice/Cargo.toml index 4ac8b3238a..145cf851d8 100644 --- a/src/shippingservice/Cargo.toml +++ b/src/shippingservice/Cargo.toml @@ -19,9 +19,9 @@ reqwest = { version = "0.11", default-features = false, features = ["json"] } prost = "0.12" prost-types = "0.12" tokio = { version = "1.36", features = ["macros", "rt-multi-thread"] } -opentelemetry = { version = "0.21", features = ["trace", "logs"] } +opentelemetry = { version = "0.21", features = ["trace", "metrics", "logs"] } opentelemetry-appender-log = "0.2.0" -opentelemetry_sdk = { version = "0.21", features = ["rt-tokio", "logs_level_enabled"]} +opentelemetry_sdk = { version = "0.21", features = ["rt-tokio", "metrics", "logs_level_enabled"]} opentelemetry-semantic-conventions = "0.13.0" opentelemetry-http = "0.10.0" opentelemetry-otlp = { version = "0.14.0", features = ["tonic", "metrics", "logs"] } diff --git a/src/shippingservice/src/main.rs b/src/shippingservice/src/main.rs index 06a8b5f7f6..b6ecaf3873 100644 --- a/src/shippingservice/src/main.rs +++ b/src/shippingservice/src/main.rs @@ -15,6 +15,7 @@ mod telemetry; use telemetry::init_logger; use telemetry::init_reqwest_tracing; use telemetry::init_tracer; +use telemetry::init_metrics; #[tokio::main] async fn main() -> Result<(), Box> { @@ -25,6 +26,8 @@ async fn main() -> Result<(), Box> { init_logger(); init_reqwest_tracing(init_tracer()?)?; + let _ = init_metrics()?; + info!("OTel pipeline created"); let port = env::var("SHIPPING_SERVICE_PORT").expect("$SHIPPING_SERVICE_PORT is not set"); let addr = format!("0.0.0.0:{}", port).parse()?; diff --git a/src/shippingservice/src/shipping_service.rs b/src/shippingservice/src/shipping_service.rs index be4b123a31..ae33b6a9cf 100644 --- a/src/shippingservice/src/shipping_service.rs +++ b/src/shippingservice/src/shipping_service.rs @@ -55,7 +55,7 @@ impl ShippingService for ShippingServer { &self, request: Request, ) -> Result, Status> { - info!("GetQuoteRequest: {:?}", request); + debug!("GetQuoteRequest: {:?}", request); let parent_cx = global::get_text_map_propagator(|prop| prop.extract(&MetadataMap(request.metadata()))); @@ -113,7 +113,7 @@ impl ShippingService for ShippingServer { &self, request: Request, ) -> Result, Status> { - info!("ShipOrderRequest: {:?}", request); + debug!("ShipOrderRequest: {:?}", request); let parent_cx = global::get_text_map_propagator(|prop| prop.extract(&MetadataMap(request.metadata()))); @@ -146,82 +146,12 @@ impl ShippingService for ShippingServer { mod tests { use super::{ shop::shipping_service_server::ShippingService, - shop::{Address, GetQuoteRequest}, - shop::{CartItem, ShipOrderRequest}, - ShippingServer, NANOS_MULTIPLE, + shop::ShipOrderRequest, + ShippingServer, }; use tonic::Request; use uuid::Uuid; - fn make_quote_request_with_items(items: Vec) -> Request { - let cart_items: Vec = items.into_iter().fold(Vec::new(), |mut accum, count| { - accum.push(CartItem { - product_id: "fake-item".to_string(), - quantity: count, - }); - accum - }); - - Request::new(GetQuoteRequest { - address: Some(Address::default()), - items: cart_items, - }) - } - - fn make_empty_quote_request() -> Request { - Request::new(GetQuoteRequest::default()) - } - #[tokio::test] - async fn empty_quote() { - let server = ShippingServer::default(); - - // when we provide no items, the quote should be empty - match server.get_quote(make_empty_quote_request()).await { - Ok(resp) => { - let money = resp.into_inner().cost_usd.unwrap(); - assert_eq!(money.units, 0); - assert_eq!(money.nanos, 0); - } - Err(e) => panic!("error when making empty quote request: {}", e), - } - } - - #[tokio::test] - async fn quote_for_one_value() { - let server = ShippingServer::default(); - - match server - .get_quote(make_quote_request_with_items(vec![1_i32])) - .await - { - Ok(resp) => { - // items are fixed at 8.99, so we should see that price reflected. - let money = resp.into_inner().cost_usd.unwrap(); - assert_eq!(money.units, 8); - assert_eq!(money.nanos, 99 * NANOS_MULTIPLE); - } - Err(e) => panic!("error when making quote request for one value: {}", e), - } - } - - #[tokio::test] - async fn quote_for_many_values() { - let server = ShippingServer::default(); - - match server - .get_quote(make_quote_request_with_items(vec![1_i32, 2_i32])) - .await - { - Ok(resp) => { - // items are fixed at 8.99, so we should see that price reflected for 3 items - let money = resp.into_inner().cost_usd.unwrap(); - assert_eq!(money.units, 26); - assert_eq!(money.nanos, 97 * NANOS_MULTIPLE); - } - Err(e) => panic!("error when making quote request for many values: {}", e), - } - } - #[tokio::test] async fn can_get_tracking_id() { let server = ShippingServer::default(); diff --git a/src/shippingservice/src/telemetry.rs b/src/shippingservice/src/telemetry.rs index ed3f66806c..64c438b6e6 100644 --- a/src/shippingservice/src/telemetry.rs +++ b/src/shippingservice/src/telemetry.rs @@ -10,3 +10,6 @@ pub use traces_conf::init_tracer; mod logs_conf; pub use logs_conf::init_logger; + +mod metrics_conf; +pub use metrics_conf::init_metrics; diff --git a/src/shippingservice/src/telemetry/logs_conf.rs b/src/shippingservice/src/telemetry/logs_conf.rs index 75010a4509..6d2b11f1da 100644 --- a/src/shippingservice/src/telemetry/logs_conf.rs +++ b/src/shippingservice/src/telemetry/logs_conf.rs @@ -17,14 +17,12 @@ pub fn init_logger() { .with_exporter(opentelemetry_otlp::new_exporter().tonic()) .install_batch(runtime::Tokio); - bla(); + set_log_bridge(); } -fn bla() { - // Retrieve the global LoggerProvider. +fn set_log_bridge() { let logger_provider = logger_provider(); - // Create a new OpenTelemetryLogBridge using the above LoggerProvider. let otel_log_appender = OpenTelemetryLogBridge::new(&logger_provider); log::set_boxed_logger(Box::new(otel_log_appender)).unwrap(); log::set_max_level(Level::Info.to_level_filter()); diff --git a/src/shippingservice/src/telemetry/metrics_conf.rs b/src/shippingservice/src/telemetry/metrics_conf.rs new file mode 100644 index 0000000000..d6efc7b225 --- /dev/null +++ b/src/shippingservice/src/telemetry/metrics_conf.rs @@ -0,0 +1,15 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +use opentelemetry::metrics::MetricsError; +use opentelemetry_sdk::{metrics::MeterProvider, runtime}; + +use super::get_resource_attr; + +pub fn init_metrics() -> Result { + opentelemetry_otlp::new_pipeline() + .metrics(runtime::Tokio) + .with_exporter(opentelemetry_otlp::new_exporter().tonic()) + .with_resource(get_resource_attr()) + .build() +}