Skip to main content

basic_metrics_demo/
basic_metrics_demo.rs

1use observability_core::domain::TraceContext;
2use observability_core::{
3    BasicMetricType, MetricsEntry, MetricsPort, ObservabilityResult, UnifiedWasmStdoutAdapter,
4    WasmStdoutMetricsAdapter, create_counter_metric, create_histogram_metric,
5};
6
7fn main() -> ObservabilityResult<()> {
8    println!("šŸŽÆ Basic MetricsPort Demo");
9
10    // Create simple metrics adapter
11    let metrics_adapter = WasmStdoutMetricsAdapter::new();
12
13    println!("\nšŸ“Š Testing simple metrics emission:");
14
15    // Test counter
16    metrics_adapter.emit_counter_simple("requests_total", 1.0)?;
17
18    // Test histogram
19    metrics_adapter.emit_histogram_simple("request_duration_ms", 150.0)?;
20
21    // Test gauge
22    metrics_adapter.emit_gauge_simple("active_connections", 5.0)?;
23
24    println!("\nšŸ”„ Testing unified adapter (logs + metrics correlation):");
25
26    // Create unified adapter for correlation
27    let unified_adapter = UnifiedWasmStdoutAdapter::new();
28
29    // Test correlated metrics
30    unified_adapter.emit_counter_simple("llm_requests", 1.0)?;
31    unified_adapter.emit_histogram_simple("llm_response_time_ms", 2500.0)?;
32
33    println!("\nšŸ“ˆ Testing MetricsEntry with trace context:");
34
35    // Create metrics with trace context for correlation
36    let trace_context = TraceContext {
37        trace_id: "trace-123456".to_string(),
38        span_id: "span-789".to_string(),
39        parent_span_id: Some("parent-456".to_string()),
40    };
41
42    let counter_metric = create_counter_metric("a2a_messages_sent", 3.0)
43        .with_trace_context(trace_context.clone())
44        .with_source(
45            Some("a2a_jsonrpc_core".to_string()),
46            Some("message_handler".to_string()),
47            Some("send_request".to_string()),
48        );
49
50    println!("Metric with context: {}", counter_metric.to_json());
51
52    let histogram_metric = create_histogram_metric("template_render_duration_us", 1250.0)
53        .with_trace_context(trace_context)
54        .with_source(
55            Some("template_engines".to_string()),
56            Some("sailfish_renderer".to_string()),
57            Some("render_template".to_string()),
58        );
59
60    println!("Metric with context: {}", histogram_metric.to_json());
61
62    println!("\nāœ… Basic metrics demo completed!");
63
64    Ok(())
65}