From 55ea18e7e1883e7299f538ff61a3ec71c50b4328 Mon Sep 17 00:00:00 2001 From: liuyuancheng Date: Mon, 27 Feb 2023 21:43:12 +0800 Subject: [PATCH 1/7] Add: common/logger Log configuration unification, with future support for integration with tower and OpenTelemetry, and allowing configuration via configuration files --- Cargo.toml | 8 +++-- common/{ => logger}/Cargo.toml | 7 ++--- common/{ => logger}/LICENSE | 0 common/logger/README.md | 3 ++ common/{ => logger}/src/lib.rs | 17 ++++++++-- common/logger/src/tracing_configurer.rs | 41 +++++++++++++++++++++++++ examples/greeter/Cargo.toml | 3 +- examples/greeter/src/greeter/client.rs | 13 +------- examples/greeter/src/greeter/server.rs | 5 +-- 9 files changed, 72 insertions(+), 25 deletions(-) rename common/{ => logger}/Cargo.toml (55%) rename common/{ => logger}/LICENSE (100%) create mode 100644 common/logger/README.md rename common/{ => logger}/src/lib.rs (78%) create mode 100644 common/logger/src/tracing_configurer.rs diff --git a/Cargo.toml b/Cargo.toml index d4404f23..84e57178 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,10 +5,14 @@ members = [ "registry-zookeeper", "registry-nacos", "metadata", - "common", "config", "dubbo", "examples/echo", "examples/greeter", - "dubbo-build" + "dubbo-build", + "common/logger" ] + +[workspace.dependencies] +tracing = "0.1" +tracing-subscriber = "0.3" diff --git a/common/Cargo.toml b/common/logger/Cargo.toml similarity index 55% rename from common/Cargo.toml rename to common/logger/Cargo.toml index 19282c2e..a589d70b 100644 --- a/common/Cargo.toml +++ b/common/logger/Cargo.toml @@ -1,11 +1,10 @@ [package] -name = "common" +name = "logger" version = "0.3.0" edition = "2021" -license = "Apache-2.0" -description = "dubbo-rust-common" -repository = "https://github.com/apache/dubbo-rust.git" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +tracing.workspace=true +tracing-subscriber.workspace=true \ No newline at end of file diff --git a/common/LICENSE b/common/logger/LICENSE similarity index 100% rename from common/LICENSE rename to common/logger/LICENSE diff --git a/common/logger/README.md b/common/logger/README.md new file mode 100644 index 00000000..cdf70128 --- /dev/null +++ b/common/logger/README.md @@ -0,0 +1,3 @@ +ToDo: +1. 日志配置器与tower集成 https://github.com/tokio-rs/tracing/tree/master/tracing-tower +2. 与OpenTelemetry集成 https://github.com/tokio-rs/tracing/tree/master/tracing-opentelemetry \ No newline at end of file diff --git a/common/src/lib.rs b/common/logger/src/lib.rs similarity index 78% rename from common/src/lib.rs rename to common/logger/src/lib.rs index 3e01853c..4ff082f2 100644 --- a/common/src/lib.rs +++ b/common/logger/src/lib.rs @@ -15,11 +15,22 @@ * limitations under the License. */ +pub use tracing; +pub use tracing::Level; + +mod tracing_configurer; + +pub fn init() { + tracing_configurer::default() +} + #[cfg(test)] mod tests { + use tracing::debug; + #[test] - fn it_works() { - let result = 2 + 2; - assert_eq!(result, 4); + fn test_print_info_log() { + super::init(); + debug!("hello rust"); } } diff --git a/common/logger/src/tracing_configurer.rs b/common/logger/src/tracing_configurer.rs new file mode 100644 index 00000000..5cf2f09c --- /dev/null +++ b/common/logger/src/tracing_configurer.rs @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use crate::Level; + +pub(crate) fn default() { + if let Some(true) = configured() { + parse_from_config() + } else { + tracing_subscriber::fmt() + .compact() + // enable everything + .with_max_level(Level::TRACE) + .with_thread_names(false) + .with_line_number(true) + // sets this to be the default, global collector for this application. + .init(); + } +} + +pub(crate) fn parse_from_config() { + todo!() +} + +pub(crate) fn configured() -> Option { + None +} diff --git a/examples/greeter/Cargo.toml b/examples/greeter/Cargo.toml index 4e56f4e4..491f4054 100644 --- a/examples/greeter/Cargo.toml +++ b/examples/greeter/Cargo.toml @@ -28,8 +28,7 @@ prost-derive = {version = "0.10", optional = true} prost = "0.10.4" async-trait = "0.1.56" tokio-stream = "0.1" -tracing = "0.1" -tracing-subscriber = "0.2.0" +logger = {path="../../common/logger"} dubbo = {path = "../../dubbo", version = "0.3.0" } dubbo-config = {path = "../../config", version = "0.3.0" } dubbo-registry-zookeeper = {path = "../../registry-zookeeper", version = "0.3.0" } diff --git a/examples/greeter/src/greeter/client.rs b/examples/greeter/src/greeter/client.rs index d6208434..e01bf929 100644 --- a/examples/greeter/src/greeter/client.rs +++ b/examples/greeter/src/greeter/client.rs @@ -24,21 +24,10 @@ use dubbo::codegen::*; use dubbo_registry_zookeeper::zookeeper_registry::ZookeeperRegistry; use futures_util::StreamExt; use protos::{greeter_client::GreeterClient, GreeterRequest}; -use tracing::Level; -use tracing_subscriber::FmtSubscriber; #[tokio::main] async fn main() { - // a builder for `FmtSubscriber`. - let subscriber = FmtSubscriber::builder() - // all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.) - // will be written to stdout. - .with_max_level(Level::INFO) - // completes the builder. - .finish(); - - tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed"); - + logger::init(); // let mut cli = GreeterClient::new(ClientBuilder::from_static(&"http://127.0.0.1:8888")); // Here is example for zk diff --git a/examples/greeter/src/greeter/server.rs b/examples/greeter/src/greeter/server.rs index 271a54b3..07df5700 100644 --- a/examples/greeter/src/greeter/server.rs +++ b/examples/greeter/src/greeter/server.rs @@ -21,11 +21,12 @@ use async_trait::async_trait; use futures_util::{Stream, StreamExt}; use tokio::sync::mpsc; use tokio_stream::wrappers::ReceiverStream; -use tracing::info; use dubbo::{codegen::*, Dubbo}; use dubbo_config::RootConfig; use dubbo_registry_zookeeper::zookeeper_registry::ZookeeperRegistry; +use logger::tracing::{info, span}; +use logger::Level; use protos::{ greeter_server::{register_server, Greeter}, GreeterReply, GreeterRequest, @@ -41,7 +42,7 @@ type ResponseStream = #[tokio::main] async fn main() { - use tracing::{span, Level}; + logger::init(); let span = span!(Level::DEBUG, "greeter.server"); let _enter = span.enter(); register_server(GreeterServerImpl { From 81677af9a5178ee0757a749e86a21826f8f4cd2e Mon Sep 17 00:00:00 2001 From: liuyuancheng Date: Mon, 27 Feb 2023 21:49:20 +0800 Subject: [PATCH 2/7] Add: common/logger Log configuration unification, with future support for integration with tower and OpenTelemetry, and allowing configuration via configuration files --- common/logger/src/lib.rs | 3 +-- examples/greeter/src/greeter/server.rs | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/common/logger/src/lib.rs b/common/logger/src/lib.rs index 4ff082f2..73f958d7 100644 --- a/common/logger/src/lib.rs +++ b/common/logger/src/lib.rs @@ -15,8 +15,7 @@ * limitations under the License. */ -pub use tracing; -pub use tracing::Level; +pub use tracing::{self, Level}; mod tracing_configurer; diff --git a/examples/greeter/src/greeter/server.rs b/examples/greeter/src/greeter/server.rs index 07df5700..a275d6dc 100644 --- a/examples/greeter/src/greeter/server.rs +++ b/examples/greeter/src/greeter/server.rs @@ -25,8 +25,10 @@ use tokio_stream::wrappers::ReceiverStream; use dubbo::{codegen::*, Dubbo}; use dubbo_config::RootConfig; use dubbo_registry_zookeeper::zookeeper_registry::ZookeeperRegistry; -use logger::tracing::{info, span}; -use logger::Level; +use logger::{ + tracing::{info, span}, + Level, +}; use protos::{ greeter_server::{register_server, Greeter}, GreeterReply, GreeterRequest, From efc370182a2c36174d61fbfa93f12dc1fd90c3cc Mon Sep 17 00:00:00 2001 From: liuyuancheng Date: Mon, 27 Feb 2023 23:31:20 +0800 Subject: [PATCH 3/7] Add: common/logger Log configuration unification, with future support for integration with tower and OpenTelemetry, and allowing configuration via configuration files --- common/logger/src/lib.rs | 2 ++ common/logger/src/tracing_configurer.rs | 11 ++++++----- dubbo/src/framework.rs | 1 - examples/echo/Cargo.toml | 1 + examples/echo/src/echo/client.rs | 1 + examples/echo/src/echo/server.rs | 1 + 6 files changed, 11 insertions(+), 6 deletions(-) diff --git a/common/logger/src/lib.rs b/common/logger/src/lib.rs index 73f958d7..7f7713b3 100644 --- a/common/logger/src/lib.rs +++ b/common/logger/src/lib.rs @@ -1,3 +1,4 @@ +#![feature(once_cell)] /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -19,6 +20,7 @@ pub use tracing::{self, Level}; mod tracing_configurer; +// put on main method pub fn init() { tracing_configurer::default() } diff --git a/common/logger/src/tracing_configurer.rs b/common/logger/src/tracing_configurer.rs index 5cf2f09c..4bdf9d23 100644 --- a/common/logger/src/tracing_configurer.rs +++ b/common/logger/src/tracing_configurer.rs @@ -15,7 +15,9 @@ * limitations under the License. */ -use crate::Level; +// https://github.com/tokio-rs/tracing/issues/971 + +use tracing::Level; pub(crate) fn default() { if let Some(true) = configured() { @@ -23,12 +25,11 @@ pub(crate) fn default() { } else { tracing_subscriber::fmt() .compact() - // enable everything - .with_max_level(Level::TRACE) - .with_thread_names(false) .with_line_number(true) + .with_max_level(Level::DEBUG) // sets this to be the default, global collector for this application. - .init(); + .try_init() + .expect("init err."); } } diff --git a/dubbo/src/framework.rs b/dubbo/src/framework.rs index 342ef6c2..71e11d2e 100644 --- a/dubbo/src/framework.rs +++ b/dubbo/src/framework.rs @@ -48,7 +48,6 @@ pub struct Dubbo { impl Dubbo { pub fn new() -> Dubbo { - tracing_subscriber::fmt::init(); Self { protocols: HashMap::new(), registries: None, diff --git a/examples/echo/Cargo.toml b/examples/echo/Cargo.toml index 1a842d2e..82d1554c 100644 --- a/examples/echo/Cargo.toml +++ b/examples/echo/Cargo.toml @@ -28,6 +28,7 @@ prost-derive = {version = "0.10", optional = true} prost = "0.10.4" async-trait = "0.1.56" tokio-stream = "0.1" +logger = {path="../../common/logger"} hyper = { version = "0.14.19", features = ["full"]} diff --git a/examples/echo/src/echo/client.rs b/examples/echo/src/echo/client.rs index 4d5bb3ad..b107e305 100644 --- a/examples/echo/src/echo/client.rs +++ b/examples/echo/src/echo/client.rs @@ -30,6 +30,7 @@ impl Filter for FakeFilter { #[tokio::main] async fn main() { + logger::init(); // let builder = ClientBuilder::new() // .with_connector("unix") // .with_host("unix://127.0.0.1:8888"); diff --git a/examples/echo/src/echo/server.rs b/examples/echo/src/echo/server.rs index 4a40d66b..1e10a2e8 100644 --- a/examples/echo/src/echo/server.rs +++ b/examples/echo/src/echo/server.rs @@ -46,6 +46,7 @@ impl Filter for FakeFilter { #[tokio::main] async fn main() { + logger::init(); register_server(EchoServerImpl { name: "echo".to_string(), }); From a8727032c30c109e4433c231a55326d01c61d1ea Mon Sep 17 00:00:00 2001 From: liuyuancheng Date: Mon, 27 Feb 2023 23:42:01 +0800 Subject: [PATCH 4/7] Add: common/logger Log configuration unification, with future support for integration with tower and OpenTelemetry, and allowing configuration via configuration files --- common/logger/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/common/logger/src/lib.rs b/common/logger/src/lib.rs index 7f7713b3..27372747 100644 --- a/common/logger/src/lib.rs +++ b/common/logger/src/lib.rs @@ -1,4 +1,3 @@ -#![feature(once_cell)] /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with From c7c6043b6175e96c3b66aa05a82929dc3dc8ac0c Mon Sep 17 00:00:00 2001 From: liuyuancheng Date: Tue, 28 Feb 2023 08:21:25 +0800 Subject: [PATCH 5/7] Add: common/logger Log configuration unification, with future support for integration with tower and OpenTelemetry, and allowing configuration via configuration files --- Cargo.toml | 3 --- common/logger/Cargo.toml | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 84e57178..7bb948d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,3 @@ members = [ "common/logger" ] -[workspace.dependencies] -tracing = "0.1" -tracing-subscriber = "0.3" diff --git a/common/logger/Cargo.toml b/common/logger/Cargo.toml index a589d70b..14157bb1 100644 --- a/common/logger/Cargo.toml +++ b/common/logger/Cargo.toml @@ -6,5 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -tracing.workspace=true -tracing-subscriber.workspace=true \ No newline at end of file +tracing = "0.1" +tracing-subscriber = "0.3" \ No newline at end of file From 6f82d48f8755a10797614de32a744069c5f0cfdf Mon Sep 17 00:00:00 2001 From: liuyuancheng Date: Tue, 28 Feb 2023 08:28:28 +0800 Subject: [PATCH 6/7] Add: common/logger Log configuration unification, with future support for integration with tower and OpenTelemetry, and allowing configuration via configuration files --- common/logger/src/tracing_configurer.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/logger/src/tracing_configurer.rs b/common/logger/src/tracing_configurer.rs index 4bdf9d23..40b1e844 100644 --- a/common/logger/src/tracing_configurer.rs +++ b/common/logger/src/tracing_configurer.rs @@ -17,7 +17,7 @@ // https://github.com/tokio-rs/tracing/issues/971 -use tracing::Level; +use tracing::{debug, Level}; pub(crate) fn default() { if let Some(true) = configured() { @@ -31,6 +31,7 @@ pub(crate) fn default() { .try_init() .expect("init err."); } + debug!("Tracing configured.") } pub(crate) fn parse_from_config() { From ca5267ae00cd150661d761f6d9d0038f512c7e2e Mon Sep 17 00:00:00 2001 From: liuyuancheng Date: Tue, 28 Feb 2023 08:39:09 +0800 Subject: [PATCH 7/7] Fix: nacos sdk deprecated --- registry-nacos/src/nacos_registry.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/registry-nacos/src/nacos_registry.rs b/registry-nacos/src/nacos_registry.rs index 888382f0..2d0d6695 100644 --- a/registry-nacos/src/nacos_registry.rs +++ b/registry-nacos/src/nacos_registry.rs @@ -163,8 +163,7 @@ impl Registry for NacosRegistry { let nacos_service_instance = Self::create_nacos_service_instance(url); info!("register service: {}", nacos_service_name); - - let ret = self.nacos_naming_service.register_service( + let ret = self.nacos_naming_service.register_instance( nacos_service_name, group_name, nacos_service_instance,