-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic.rs
More file actions
82 lines (69 loc) · 2.41 KB
/
basic.rs
File metadata and controls
82 lines (69 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Basic example demonstrating the charon-tracing functionality.
//!
//! Run the following command to start the test infrastructure:
//! ```bash
//! docker compose -f test-infra/docker-compose.yml up -d
//! ```
//!
//! Run the following command to start the example:
//! ```bash
//! cargo run --example basic
//! ```
//!
//! You can see the logs in Grafana at http://localhost:3000.
use std::{collections::HashMap, net::SocketAddr};
use pluto_tracing::{LokiConfig, config::TracingConfig, init::init};
use tracing::{debug, error, info, instrument, trace, warn};
use vise_exporter::MetricsExporter;
#[tokio::main]
async fn main() {
// Initialize tracing with default console config
let config = TracingConfig::builder()
.with_default_console()
.with_metrics(true)
.loki(LokiConfig {
loki_url: "http://localhost:3100".to_string(),
labels: HashMap::new(),
extra_fields: HashMap::new(),
})
.override_env_filter("debug")
.build();
let background_task = init(&config)
.expect("Failed to initialize tracing")
.expect("Background task should be Some");
tokio::spawn(background_task);
let bind_address = SocketAddr::from(([0, 0, 0, 0], 9464));
let exporter = MetricsExporter::default()
.bind(bind_address)
.await
.expect("Failed to bind metrics exporter");
tokio::spawn(async move {
exporter
.start()
.await
.expect("Failed to start metrics exporter");
});
// Test various log levels
trace!("This is a trace message");
debug!("This is a debug message");
info!("This is an info message");
warn!("This is a warning message");
error!("This is an error message");
// Test structured logging with fields
info!(user_id = 42, action = "login", "User performed an action");
instrumented_function();
// Test spans
let span = tracing::info_span!("processing", request_id = "abc-123");
let _guard = span.enter();
info!("Processing started");
debug!("Debug info inside span");
info!("Processing completed");
// Wait for 10 seconds to see the logs in Loki
std::thread::sleep(std::time::Duration::from_secs(10));
}
#[instrument]
fn instrumented_function() {
info!("Instrumented function started");
debug!("Debug info inside instrumented function");
info!("Instrumented function completed");
}