-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathmetrics_cli.rs
More file actions
180 lines (151 loc) · 7.39 KB
/
Copy pathmetrics_cli.rs
File metadata and controls
180 lines (151 loc) · 7.39 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! Interactive observability dashboard for duroxide.
//!
//! This CLI tool demonstrates how to consume and display duroxide metrics
//! and logs in real-time.
//!
//! Run with:
//! ```bash
//! cargo run --example metrics_cli
//! ```
#![allow(clippy::unwrap_used)]
#![allow(clippy::clone_on_ref_ptr)]
#![allow(clippy::expect_used)]
use duroxide::providers::sqlite::SqliteProvider;
use duroxide::runtime::registry::ActivityRegistry;
use duroxide::runtime::{self, LogFormat, ObservabilityConfig, RuntimeOptions};
use duroxide::{ActivityContext, Client, OrchestrationContext, OrchestrationRegistry};
use std::sync::Arc;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("┌─────────────────────────────────────────────┐");
println!("│ Duroxide Observability Dashboard │");
println!("└─────────────────────────────────────────────┘\n");
// Configure observability
// Metrics are always available via the `metrics` facade - install a recorder
// before starting the runtime if you want to export them.
let observability = ObservabilityConfig {
log_format: LogFormat::Compact,
log_level: "info".to_string(),
service_name: "duroxide-dashboard".to_string(),
service_version: Some("1.0.0".to_string()),
..Default::default()
};
let options = RuntimeOptions {
observability,
orchestration_concurrency: 2,
worker_concurrency: 2,
..Default::default()
};
// Create provider
let store = Arc::new(SqliteProvider::new_in_memory().await?);
// Register sample activities with varying characteristics
let activities = ActivityRegistry::builder()
.register("FastTask", |ctx: ActivityContext, _input: String| async move {
ctx.trace_debug("Fast task executing");
tokio::time::sleep(Duration::from_millis(10)).await;
ctx.trace_debug("Fast task complete");
Ok("fast_complete".to_string())
})
.register("SlowTask", |ctx: ActivityContext, _input: String| async move {
ctx.trace_info("Slow task started");
tokio::time::sleep(Duration::from_millis(200)).await;
ctx.trace_info("Slow task finished");
Ok("slow_complete".to_string())
})
.register("FailingTask", |ctx: ActivityContext, input: String| async move {
ctx.trace_info("Failing task invoked");
if input == "fail" {
ctx.trace_error("Failing task returning deliberate failure");
Err("deliberate_failure".to_string())
} else {
ctx.trace_info("Failing task succeeded");
Ok("success".to_string())
}
})
.build();
// Sample orchestrations
let fast_orch = |ctx: OrchestrationContext, _input: String| async move {
ctx.trace_info("Fast orchestration started");
let result = ctx.schedule_activity("FastTask", "data".to_string()).await?;
ctx.trace_info("Fast orchestration completed");
Ok::<_, String>(result)
};
let slow_orch = |ctx: OrchestrationContext, _input: String| async move {
ctx.trace_info("Slow orchestration started");
let r1 = ctx.schedule_activity("SlowTask", "data".to_string());
let r2 = ctx.schedule_activity("SlowTask", "data".to_string());
let _results = ctx.join2(r1, r2).await;
ctx.trace_info("All tasks completed");
Ok::<_, String>("done".to_string())
};
let failing_orch = |ctx: OrchestrationContext, _input: String| async move {
ctx.trace_info("Orchestration with potential failure");
match ctx.schedule_activity("FailingTask", "fail".to_string()).await {
Ok(r) => Ok::<_, String>(r),
Err(e) => {
ctx.trace_error(format!("Activity failed: {e}"));
Err(e)
}
}
};
let orchestrations = OrchestrationRegistry::builder()
.register("FastWorkflow", fast_orch)
.register("SlowWorkflow", slow_orch)
.register("FailingWorkflow", failing_orch)
.build();
// Start runtime
let rt = runtime::Runtime::start_with_options(store.clone(), activities, orchestrations, options).await;
let client = Client::new(store.clone());
println!("Starting sample orchestrations...\n");
println!("══════════════════════════════════════════════\n");
// Run a mix of orchestrations
for i in 1..=3 {
client
.start_orchestration(&format!("fast-{i}"), "FastWorkflow", "data")
.await?;
}
for i in 1..=2 {
client
.start_orchestration(&format!("slow-{i}"), "SlowWorkflow", "data")
.await?;
}
client.start_orchestration("fail-1", "FailingWorkflow", "data").await?;
// Wait for completion
println!("Waiting for orchestrations to complete...\n");
tokio::time::sleep(Duration::from_secs(2)).await;
// Display summary if management capabilities available
if client.has_management_capability() {
println!("\n══════════════════════════════════════════════");
println!(" METRICS SUMMARY");
println!("══════════════════════════════════════════════\n");
let metrics = client.get_system_metrics().await?;
println!("Orchestrations:");
println!(" ✓ Completed: {}", metrics.completed_instances);
println!(" ✗ Failed: {}", metrics.failed_instances);
println!(" ⟳ Running: {}", metrics.running_instances);
println!(" ∑ Total: {}", metrics.total_instances);
let queues = client.get_queue_depths().await?;
println!("\nQueue Depths:");
println!(" Orchestrator: {}", queues.orchestrator_queue);
println!(" Worker: {}", queues.worker_queue);
println!(" Timer: {}", queues.timer_queue);
println!("\nWith full metrics enabled, you would see:");
println!(" • Activity success rates by name");
println!(" • Average history sizes");
println!(" • Turn count distributions");
println!(" • Provider operation latencies");
println!(" • Error breakdowns by type");
println!("\n💡 To export metrics, install a recorder before starting the runtime:");
println!(" metrics_exporter_prometheus::PrometheusBuilder::new().install()?");
println!(" // or: metrics_exporter_opentelemetry::Recorder::builder(\"app\").install_global()?");
} else {
println!("\n📊 Management features not available for this provider");
}
// Shutdown
rt.shutdown(None).await;
println!("\n══════════════════════════════════════════════");
println!("Dashboard demonstration complete!");
println!("══════════════════════════════════════════════\n");
Ok(())
}