Skip to main content

stormchaser_engine/
lib.rs

1//! Core execution and orchestration engine for Stormchaser workflows.
2//!
3//! This crate contains the logic to execute steps, evaluate HCL expressions,
4//! handle state transitions, and manage telemetry.
5
6/// Artifact management and backend integrations.
7pub mod artifact;
8/// Database interaction layer.
9pub mod db;
10/// Git caching and cloning utilities.
11pub mod git_cache;
12/// Event handling and dispatching.
13pub mod handler;
14/// HCL expression evaluation and context building.
15pub mod hcl_eval;
16/// Human-In-The-Loop (HITL) manual approval handling.
17pub mod hitl;
18/// JUnit test report parsing and aggregation.
19pub mod junit;
20/// NATS JetStream integration for pub/sub events.
21pub mod nats;
22/// Database persistence for workflow and step models.
23pub mod persistence;
24/// Utilities for parsing resources like CPU and Memory.
25pub mod resource_utils;
26/// S3 backend storage implementation.
27pub mod s3;
28/// Secrets management interfaces and backends.
29pub mod secrets;
30pub mod stdlib;
31/// State machine for individual steps.
32pub mod step_machine;
33/// OpenTelemetry tracing and metrics initialization.
34pub mod telemetry;
35/// WebAssembly module execution utilities.
36pub mod wasm;
37/// State machine for full workflow runs.
38pub mod workflow_machine;
39
40use once_cell::sync::Lazy;
41use opentelemetry::{
42    global,
43    metrics::{Counter, Histogram},
44};
45use std::time::Duration;
46
47/// Counter metric for the total number of started workflow runs.
48pub static RUNS_STARTED: Lazy<Counter<u64>> = Lazy::new(|| {
49    global::meter("stormchaser-engine")
50        .u64_counter("stormchaser.v1.runs_started")
51        .with_description("Total number of runs started")
52        .build()
53});
54
55/// Counter metric for the total number of successfully completed workflow runs.
56pub static RUNS_COMPLETED: Lazy<Counter<u64>> = Lazy::new(|| {
57    global::meter("stormchaser-engine")
58        .u64_counter("stormchaser.v1.runs_completed")
59        .with_description("Total number of runs completed successfully")
60        .build()
61});
62
63/// Counter metric for the total number of failed workflow runs.
64pub static RUNS_FAILED: Lazy<Counter<u64>> = Lazy::new(|| {
65    global::meter("stormchaser-engine")
66        .u64_counter("stormchaser.v1.runs_failed")
67        .with_description("Total number of runs failed")
68        .build()
69});
70
71/// Counter metric for the total number of started workflow steps.
72pub static STEPS_STARTED: Lazy<Counter<u64>> = Lazy::new(|| {
73    global::meter("stormchaser-engine")
74        .u64_counter("stormchaser.v1.steps_started")
75        .with_description("Total number of steps started")
76        .build()
77});
78
79/// Counter metric for the total number of successfully completed workflow steps.
80pub static STEPS_COMPLETED: Lazy<Counter<u64>> = Lazy::new(|| {
81    global::meter("stormchaser-engine")
82        .u64_counter("stormchaser.v1.steps_completed")
83        .with_description("Total number of steps completed successfully")
84        .build()
85});
86
87/// Counter metric for the total number of failed workflow steps.
88pub static STEPS_FAILED: Lazy<Counter<u64>> = Lazy::new(|| {
89    global::meter("stormchaser-engine")
90        .u64_counter("stormchaser.v1.steps_failed")
91        .with_description("Total number of steps failed")
92        .build()
93});
94
95/// Histogram metric for the duration of step executions in seconds.
96pub static STEP_DURATION: Lazy<Histogram<f64>> = Lazy::new(|| {
97    global::meter("stormchaser-engine")
98        .f64_histogram("stormchaser.v1.step_duration_seconds")
99        .with_description("Duration of step execution in seconds")
100        .build()
101});
102
103/// Parse duration.
104pub fn parse_duration(s: &str) -> anyhow::Result<Duration> {
105    Ok(humantime::parse_duration(s)?)
106}
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111
112    #[test]
113    fn test_parse_duration() {
114        assert_eq!(parse_duration("10s").unwrap(), Duration::from_secs(10));
115        assert_eq!(parse_duration("5m").unwrap(), Duration::from_secs(300));
116        assert_eq!(parse_duration("1h").unwrap(), Duration::from_secs(3600));
117        assert_eq!(parse_duration("1d").unwrap(), Duration::from_secs(86400));
118        assert_eq!(parse_duration("2h 30m").unwrap(), Duration::from_secs(9000));
119    }
120}