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;
30/// State machine for individual steps.
31pub mod step_machine;
32/// OpenTelemetry tracing and metrics initialization.
33pub mod telemetry;
34/// WebAssembly module execution utilities.
35pub mod wasm;
36/// State machine for full workflow runs.
37pub mod workflow_machine;
38
39use once_cell::sync::Lazy;
40use opentelemetry::{
41    global,
42    metrics::{Counter, Histogram},
43};
44use std::time::Duration;
45
46/// Counter metric for the total number of started workflow runs.
47pub static RUNS_STARTED: Lazy<Counter<u64>> = Lazy::new(|| {
48    global::meter("stormchaser-engine")
49        .u64_counter("stormchaser.runs_started")
50        .with_description("Total number of runs started")
51        .build()
52});
53
54/// Counter metric for the total number of successfully completed workflow runs.
55pub static RUNS_COMPLETED: Lazy<Counter<u64>> = Lazy::new(|| {
56    global::meter("stormchaser-engine")
57        .u64_counter("stormchaser.runs_completed")
58        .with_description("Total number of runs completed successfully")
59        .build()
60});
61
62/// Counter metric for the total number of failed workflow runs.
63pub static RUNS_FAILED: Lazy<Counter<u64>> = Lazy::new(|| {
64    global::meter("stormchaser-engine")
65        .u64_counter("stormchaser.runs_failed")
66        .with_description("Total number of runs failed")
67        .build()
68});
69
70/// Counter metric for the total number of started workflow steps.
71pub static STEPS_STARTED: Lazy<Counter<u64>> = Lazy::new(|| {
72    global::meter("stormchaser-engine")
73        .u64_counter("stormchaser.steps_started")
74        .with_description("Total number of steps started")
75        .build()
76});
77
78/// Counter metric for the total number of successfully completed workflow steps.
79pub static STEPS_COMPLETED: Lazy<Counter<u64>> = Lazy::new(|| {
80    global::meter("stormchaser-engine")
81        .u64_counter("stormchaser.steps_completed")
82        .with_description("Total number of steps completed successfully")
83        .build()
84});
85
86/// Counter metric for the total number of failed workflow steps.
87pub static STEPS_FAILED: Lazy<Counter<u64>> = Lazy::new(|| {
88    global::meter("stormchaser-engine")
89        .u64_counter("stormchaser.steps_failed")
90        .with_description("Total number of steps failed")
91        .build()
92});
93
94/// Histogram metric for the duration of step executions in seconds.
95pub static STEP_DURATION: Lazy<Histogram<f64>> = Lazy::new(|| {
96    global::meter("stormchaser-engine")
97        .f64_histogram("stormchaser.step_duration_seconds")
98        .with_description("Duration of step execution in seconds")
99        .build()
100});
101
102/// Parse duration.
103pub fn parse_duration(s: &str) -> anyhow::Result<Duration> {
104    Ok(humantime::parse_duration(s)?)
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110
111    #[test]
112    fn test_parse_duration() {
113        assert_eq!(parse_duration("10s").unwrap(), Duration::from_secs(10));
114        assert_eq!(parse_duration("5m").unwrap(), Duration::from_secs(300));
115        assert_eq!(parse_duration("1h").unwrap(), Duration::from_secs(3600));
116        assert_eq!(parse_duration("1d").unwrap(), Duration::from_secs(86400));
117        assert_eq!(parse_duration("2h 30m").unwrap(), Duration::from_secs(9000));
118    }
119}