improve code

This commit is contained in:
houseme
2025-03-16 16:33:52 +08:00
parent c3ca7960e9
commit ef162396cf
15 changed files with 724 additions and 206 deletions
+2 -2
View File
@@ -3,7 +3,7 @@ endpoint = "http://localhost:4317"
use_stdout = true
sample_ratio = 0.5
meter_interval = 30
service_name = "logging_service"
service_name = "rustfs_obs_service"
service_version = "0.1.0"
deployment_environment = "develop"
@@ -23,7 +23,7 @@ batch_timeout_ms = 1000 # Default is 100ms if not specified
[sinks.file]
enabled = true
path = "app.log"
path = "logs/app.log"
batch_size = 100
batch_timeout_ms = 1000 # Default is 8192 bytes if not specified
+39 -30
View File
@@ -1,47 +1,53 @@
use opentelemetry::global;
use opentelemetry::trace::TraceContextExt;
use rustfs_obs::{init_logging, load_config, LogEntry};
use rustfs_obs::{get_logger, init_obs, load_config, log_info, LogEntry};
use std::time::{Duration, SystemTime};
use tracing::{info, instrument, Span};
use tracing::{info, instrument};
use tracing_core::Level;
use tracing_opentelemetry::OpenTelemetrySpanExt;
#[tokio::main]
async fn main() {
let start_time = SystemTime::now();
let config = load_config(Some("packages/obs/examples/config".to_string()));
info!("Configuration file loading is complete {:?}", config.clone());
let (logger, _guard) = init_logging(config);
info!("Log module initialization is completed");
let (_logger, _guard) = init_obs(config.clone()).await;
// Simulate the operation
tokio::time::sleep(Duration::from_millis(100)).await;
run(
"service-demo".to_string(),
"object-demo".to_string(),
"user-demo".to_string(),
"service-demo".to_string(),
)
.await;
info!("Program ends");
}
#[instrument(fields(bucket, object, user))]
async fn run(bucket: String, object: String, user: String, service_name: String) {
let start_time = SystemTime::now();
info!("Log module initialization is completed service_name: {:?}", service_name);
// Record Metrics
let meter = global::meter("rustfs.rs");
let request_duration = meter.f64_histogram("s3_request_duration_seconds").build();
request_duration.record(
start_time.elapsed().unwrap().as_secs_f64(),
&[opentelemetry::KeyValue::new("operation", "put_object")],
&[opentelemetry::KeyValue::new("operation", "run")],
);
// Gets the current span
let span = Span::current();
// Use 'OpenTelemetrySpanExt' to get 'SpanContext'
let span_context = span.context(); // Get context via OpenTelemetrySpanExt
let span_id = span_context.span().span_context().span_id().to_string(); // Get the SpanId
let trace_id = span_context.span().span_context().trace_id().to_string(); // Get the TraceId
let result = logger
let result = get_logger()
.lock()
.await
.log(LogEntry::new(
Level::INFO,
"Process user requests".to_string(),
"api_handler".to_string(),
Some("demo-audit".to_string()),
Some("req-12345".to_string()),
Some("user-6789".to_string()),
Some(user),
vec![
("endpoint".to_string(), "/api/v1/data".to_string()),
("method".to_string(), "GET".to_string()),
("span_id".to_string(), span_id),
("trace_id".to_string(), trace_id),
("bucket".to_string(), bucket),
("object-length".to_string(), object.len().to_string()),
],
))
.await;
@@ -49,28 +55,31 @@ async fn main() {
put_object("bucket".to_string(), "object".to_string(), "user".to_string()).await;
info!("Logging is completed");
tokio::time::sleep(Duration::from_secs(2)).await;
info!("Program ends");
info!("Program run ends");
}
#[instrument(fields(bucket, object, user))]
async fn put_object(bucket: String, object: String, user: String) {
let start_time = SystemTime::now();
info!("Starting PUT operation");
// Gets the current span
let span = Span::current();
// Use 'OpenTelemetrySpanExt' to get 'SpanContext'
let span_context = span.context(); // Get context via OpenTelemetrySpanExt
let span_id = span_context.span().span_context().span_id().to_string(); // Get the SpanId
let trace_id = span_context.span().span_context().trace_id().to_string(); // Get the TraceId
info!("Starting put_object operation");
let meter = global::meter("rustfs.rs");
let request_duration = meter.f64_histogram("s3_request_duration_seconds").build();
request_duration.record(
start_time.elapsed().unwrap().as_secs_f64(),
&[opentelemetry::KeyValue::new("operation", "put_object")],
);
info!(
"Starting PUT operation content: bucket = {}, object = {}, user = {},span_id = {},trace_id = {},start_time = {}",
"Starting PUT operation content: bucket = {}, object = {}, user = {},start_time = {}",
bucket,
object,
user,
span_id,
trace_id,
start_time.elapsed().unwrap().as_secs_f64()
);
let result = log_info("put_object logger info", "put_object").await;
info!("put_object is completed {:?}", result);
// Simulate the operation
tokio::time::sleep(Duration::from_millis(100)).await;