oxify-engine 0.1.0

Workflow execution engine for OxiFY - DAG orchestration, scheduling, and state management
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! OpenTelemetry Tracing Integration for Workflow Engine
//!
//! Provides distributed tracing for workflow and node execution using OpenTelemetry.
//!
//! ## Features
//!
//! - **Workflow Tracing**: Automatic span creation for entire workflow execution
//! - **Node Tracing**: Individual spans for each node execution
//! - **Metadata Annotations**: Enrich spans with execution parameters, node types, and results
//! - **Performance Metrics**: Capture execution latency and success rates
//! - **Distributed Context**: Propagate trace context across async tasks
//! - **Error Recording**: Capture errors and failures with full context
//!
//! ## Example
//!
//! ```rust,ignore
//! use oxify_engine::otel::{TracingConfig, init_tracing};
//!
//! // Initialize tracing (requires "otel" feature)
//! let config = TracingConfig::default();
//! init_tracing(config)?;
//!
//! // Execute workflow with automatic tracing
//! let engine = Engine::new();
//! let result = engine.execute(&workflow, context).await?;
//! ```

use anyhow::Result;

#[cfg(feature = "otel")]
use opentelemetry::{
    global,
    trace::{Span, SpanKind, Status, Tracer},
    KeyValue,
};

#[cfg(feature = "otel")]
use opentelemetry_sdk::{
    trace::{RandomIdGenerator, Sampler, SdkTracerProvider},
    Resource,
};

/// Configuration for OpenTelemetry tracing
#[derive(Debug, Clone)]
pub struct TracingConfig {
    /// Service name for this application
    pub service_name: String,
    /// Service version
    pub service_version: String,
    /// Sampling ratio (0.0 to 1.0)
    pub sampling_ratio: f64,
    /// Enable detailed node execution tracing
    pub trace_nodes: bool,
    /// Enable detailed template resolution tracing
    pub trace_templates: bool,
}

impl Default for TracingConfig {
    fn default() -> Self {
        Self {
            service_name: "oxify-engine".to_string(),
            service_version: env!("CARGO_PKG_VERSION").to_string(),
            sampling_ratio: 1.0,
            trace_nodes: true,
            trace_templates: false,
        }
    }
}

/// Initialize OpenTelemetry tracing
///
/// This sets up the global tracer provider with the specified configuration.
/// Call this once at application startup.
#[cfg(feature = "otel")]
pub fn init_tracing(config: TracingConfig) -> Result<()> {
    // Create resource with service information using builder
    let resource = Resource::builder()
        .with_attributes([
            KeyValue::new("service.name", config.service_name),
            KeyValue::new("service.version", config.service_version),
        ])
        .build();

    let provider = SdkTracerProvider::builder()
        .with_resource(resource)
        .with_id_generator(RandomIdGenerator::default())
        .with_sampler(Sampler::TraceIdRatioBased(config.sampling_ratio))
        .build();

    global::set_tracer_provider(provider);

    Ok(())
}

/// Shutdown tracing (call before application exit)
#[cfg(feature = "otel")]
pub fn shutdown_tracing() {
    // Note: In OpenTelemetry 0.31.0, shutdown is handled automatically when the provider is dropped
    // This function is kept for backward compatibility but does nothing
}

/// Trace a workflow execution
///
/// Creates a span for the entire workflow execution and records key metrics.
///
/// # Arguments
///
/// * `workflow_name` - Name of the workflow being executed
/// * `node_count` - Total number of nodes in the workflow
/// * `f` - Function that performs the workflow execution
#[cfg(feature = "otel")]
#[allow(dead_code)]
pub fn trace_workflow<F, T>(workflow_name: &str, node_count: usize, f: F) -> Result<T>
where
    F: FnOnce() -> Result<T>,
{
    let tracer = global::tracer("oxify-engine");
    let mut span = tracer
        .span_builder(format!("workflow.execute.{}", workflow_name))
        .with_kind(SpanKind::Internal)
        .start(&tracer);

    // Add attributes
    span.set_attribute(KeyValue::new("workflow.name", workflow_name.to_string()));
    span.set_attribute(KeyValue::new("workflow.node_count", node_count as i64));

    // Execute workflow
    let result = f();

    // Mark span based on result
    match &result {
        Ok(_) => {
            span.set_status(Status::Ok);
            span.set_attribute(KeyValue::new("workflow.status", "success"));
        }
        Err(e) => {
            span.set_status(Status::error(e.to_string()));
            span.set_attribute(KeyValue::new("workflow.status", "failed"));
            span.set_attribute(KeyValue::new("error.message", e.to_string()));
        }
    }

    span.end();

    result
}

/// Trace a node execution
///
/// Creates a span for a single node execution and records node-specific metrics.
///
/// # Arguments
///
/// * `node_id` - Unique identifier for the node
/// * `node_type` - Type of node (LLM, Retriever, Code, etc.)
/// * `f` - Function that performs the node execution
#[cfg(feature = "otel")]
#[allow(dead_code)]
pub fn trace_node<F, T>(node_id: &str, node_type: &str, f: F) -> Result<T>
where
    F: FnOnce() -> Result<T>,
{
    let tracer = global::tracer("oxify-engine");
    let mut span = tracer
        .span_builder(format!("node.execute.{}", node_type))
        .with_kind(SpanKind::Internal)
        .start(&tracer);

    // Add attributes
    span.set_attribute(KeyValue::new("node.id", node_id.to_string()));
    span.set_attribute(KeyValue::new("node.type", node_type.to_string()));

    // Execute node
    let result = f();

    // Mark span based on result
    match &result {
        Ok(_) => {
            span.set_status(Status::Ok);
            span.set_attribute(KeyValue::new("node.status", "success"));
        }
        Err(e) => {
            span.set_status(Status::error(e.to_string()));
            span.set_attribute(KeyValue::new("node.status", "failed"));
            span.set_attribute(KeyValue::new("error.message", e.to_string()));
        }
    }

    span.end();

    result
}

/// Trace a parallel execution level
///
/// Creates a span for parallel execution of multiple nodes.
///
/// # Arguments
///
/// * `level` - Execution level number
/// * `node_count` - Number of nodes in this level
/// * `f` - Function that performs the parallel execution
#[cfg(feature = "otel")]
#[allow(dead_code)]
pub fn trace_parallel_level<F, T>(level: usize, node_count: usize, f: F) -> Result<T>
where
    F: FnOnce() -> Result<T>,
{
    let tracer = global::tracer("oxify-engine");
    let mut span = tracer
        .span_builder(format!("execution.level.{}", level))
        .with_kind(SpanKind::Internal)
        .start(&tracer);

    // Add attributes
    span.set_attribute(KeyValue::new("level.number", level as i64));
    span.set_attribute(KeyValue::new("level.node_count", node_count as i64));
    span.set_attribute(KeyValue::new("execution.parallel", true));

    // Execute level
    let result = f();

    // Mark span based on result
    match &result {
        Ok(_) => {
            span.set_status(Status::Ok);
        }
        Err(e) => {
            span.set_status(Status::error(e.to_string()));
            span.set_attribute(KeyValue::new("error.message", e.to_string()));
        }
    }

    span.end();

    result
}

/// Trace a retry attempt
///
/// Creates a span for a retry attempt with retry count information.
#[cfg(feature = "otel")]
#[allow(dead_code)]
pub fn trace_retry(node_id: &str, attempt: usize, max_retries: usize) {
    let tracer = global::tracer("oxify-engine");
    let mut span = tracer
        .span_builder(format!("node.retry.{}", node_id))
        .with_kind(SpanKind::Internal)
        .start(&tracer);

    span.set_attribute(KeyValue::new("node.id", node_id.to_string()));
    span.set_attribute(KeyValue::new("retry.attempt", attempt as i64));
    span.set_attribute(KeyValue::new("retry.max", max_retries as i64));

    span.end();
}

/// Trace a checkpoint operation
///
/// Creates a span for checkpoint save/load operations.
#[cfg(feature = "otel")]
#[allow(dead_code)]
pub fn trace_checkpoint<F, T>(operation: &str, checkpoint_id: &str, f: F) -> Result<T>
where
    F: FnOnce() -> Result<T>,
{
    let tracer = global::tracer("oxify-engine");
    let mut span = tracer
        .span_builder(format!("checkpoint.{}", operation))
        .with_kind(SpanKind::Internal)
        .start(&tracer);

    span.set_attribute(KeyValue::new("checkpoint.id", checkpoint_id.to_string()));
    span.set_attribute(KeyValue::new("checkpoint.operation", operation.to_string()));

    let result = f();

    match &result {
        Ok(_) => {
            span.set_status(Status::Ok);
        }
        Err(e) => {
            span.set_status(Status::error(e.to_string()));
            span.set_attribute(KeyValue::new("error.message", e.to_string()));
        }
    }

    span.end();

    result
}

/// Helper to record an error event
#[cfg(feature = "otel")]
#[allow(dead_code)]
pub fn record_error(error_msg: &str, node_id: Option<&str>) {
    let tracer = global::tracer("oxify-engine");
    let mut span = tracer
        .span_builder("error.event")
        .with_kind(SpanKind::Internal)
        .start(&tracer);

    span.set_status(Status::error(error_msg.to_string()));
    span.set_attribute(KeyValue::new("error.message", error_msg.to_string()));

    if let Some(id) = node_id {
        span.set_attribute(KeyValue::new("node.id", id.to_string()));
    }

    span.end();
}

/// Helper to record a custom event
#[cfg(feature = "otel")]
#[allow(dead_code)]
pub fn record_event(event_name: &str, attributes: Vec<KeyValue>) {
    let tracer = global::tracer("oxify-engine");
    let mut span = tracer
        .span_builder(event_name.to_string())
        .with_kind(SpanKind::Internal)
        .start(&tracer);

    for attr in attributes {
        span.set_attribute(attr);
    }

    span.set_status(Status::Ok);
    span.end();
}

// Stub implementations when otel feature is disabled
#[cfg(not(feature = "otel"))]
#[allow(dead_code)]
pub fn init_tracing(_config: TracingConfig) -> Result<()> {
    Ok(())
}

#[cfg(not(feature = "otel"))]
#[allow(dead_code)]
pub fn shutdown_tracing() {}

#[cfg(not(feature = "otel"))]
#[allow(dead_code)]
pub fn trace_workflow<F, T>(_workflow_name: &str, _node_count: usize, f: F) -> Result<T>
where
    F: FnOnce() -> Result<T>,
{
    f()
}

#[cfg(not(feature = "otel"))]
#[allow(dead_code)]
pub fn trace_node<F, T>(_node_id: &str, _node_type: &str, f: F) -> Result<T>
where
    F: FnOnce() -> Result<T>,
{
    f()
}

#[cfg(not(feature = "otel"))]
#[allow(dead_code)]
pub fn trace_parallel_level<F, T>(_level: usize, _node_count: usize, f: F) -> Result<T>
where
    F: FnOnce() -> Result<T>,
{
    f()
}

#[cfg(not(feature = "otel"))]
#[allow(dead_code)]
pub fn trace_retry(_node_id: &str, _attempt: usize, _max_retries: usize) {}

#[cfg(not(feature = "otel"))]
#[allow(dead_code)]
pub fn trace_checkpoint<F, T>(_operation: &str, _checkpoint_id: &str, f: F) -> Result<T>
where
    F: FnOnce() -> Result<T>,
{
    f()
}

#[cfg(not(feature = "otel"))]
#[allow(dead_code)]
pub fn record_error(_error_msg: &str, _node_id: Option<&str>) {}

#[cfg(not(feature = "otel"))]
#[allow(dead_code)]
pub fn record_event(_event_name: &str, _attributes: Vec<()>) {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tracing_config_default() {
        let config = TracingConfig::default();
        assert_eq!(config.service_name, "oxify-engine");
        assert_eq!(config.sampling_ratio, 1.0);
        assert!(config.trace_nodes);
        assert!(!config.trace_templates);
    }

    #[test]
    fn test_tracing_config_custom() {
        let config = TracingConfig {
            service_name: "my-engine".to_string(),
            service_version: "2.0.0".to_string(),
            sampling_ratio: 0.5,
            trace_nodes: false,
            trace_templates: true,
        };
        assert_eq!(config.service_name, "my-engine");
        assert_eq!(config.service_version, "2.0.0");
        assert_eq!(config.sampling_ratio, 0.5);
        assert!(!config.trace_nodes);
        assert!(config.trace_templates);
    }

    #[test]
    #[cfg(feature = "otel")]
    fn test_init_and_shutdown_tracing() {
        let config = TracingConfig::default();
        let result = init_tracing(config);
        assert!(result.is_ok());
        shutdown_tracing();
    }

    #[test]
    #[cfg(feature = "otel")]
    fn test_trace_workflow() {
        let config = TracingConfig::default();
        init_tracing(config).unwrap();

        let result = trace_workflow("test_workflow", 5, || Ok("success"));

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "success");

        shutdown_tracing();
    }

    #[test]
    #[cfg(feature = "otel")]
    fn test_trace_workflow_failure() {
        let config = TracingConfig::default();
        init_tracing(config).unwrap();

        let result: Result<String> =
            trace_workflow("test_workflow", 5, || Err(anyhow::anyhow!("test error")));

        assert!(result.is_err());

        shutdown_tracing();
    }

    #[test]
    #[cfg(feature = "otel")]
    fn test_trace_node() {
        let config = TracingConfig::default();
        init_tracing(config).unwrap();

        let result = trace_node("node_123", "LLM", || Ok("node result"));

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "node result");

        shutdown_tracing();
    }

    #[test]
    #[cfg(feature = "otel")]
    fn test_trace_parallel_level() {
        let config = TracingConfig::default();
        init_tracing(config).unwrap();

        let result = trace_parallel_level(1, 3, || Ok(vec!["result1", "result2", "result3"]));

        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 3);

        shutdown_tracing();
    }

    #[test]
    #[cfg(feature = "otel")]
    fn test_trace_retry() {
        let config = TracingConfig::default();
        init_tracing(config).unwrap();

        trace_retry("node_456", 2, 3);

        shutdown_tracing();
    }

    #[test]
    #[cfg(feature = "otel")]
    fn test_trace_checkpoint() {
        let config = TracingConfig::default();
        init_tracing(config).unwrap();

        let result = trace_checkpoint("save", "checkpoint_789", || Ok("checkpoint saved"));

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "checkpoint saved");

        shutdown_tracing();
    }

    #[test]
    #[cfg(feature = "otel")]
    fn test_record_error() {
        let config = TracingConfig::default();
        init_tracing(config).unwrap();

        record_error("test error", Some("node_999"));
        record_error("test error without node", None);

        shutdown_tracing();
    }

    #[test]
    #[cfg(feature = "otel")]
    fn test_record_event() {
        let config = TracingConfig::default();
        init_tracing(config).unwrap();

        record_event(
            "custom.event",
            vec![KeyValue::new("key1", "value1"), KeyValue::new("key2", 42)],
        );

        shutdown_tracing();
    }

    #[test]
    #[cfg(not(feature = "otel"))]
    fn test_stub_functions() {
        // These should do nothing when otel feature is disabled
        let config = TracingConfig::default();
        let result = init_tracing(config);
        assert!(result.is_ok());

        shutdown_tracing();

        let result = trace_workflow("test", 5, || Ok("success"));
        assert!(result.is_ok());

        let result = trace_node("node1", "LLM", || Ok("success"));
        assert!(result.is_ok());

        let result = trace_parallel_level(1, 3, || Ok("success"));
        assert!(result.is_ok());

        trace_retry("node1", 1, 3);

        let result = trace_checkpoint("save", "cp1", || Ok("success"));
        assert!(result.is_ok());

        record_error("error", Some("node1"));
        record_event("event", vec![]);
    }
}