eeyf 0.1.0

Eric Evans' Yahoo Finance API - A rate-limited, reliable Rust adapter for Yahoo Finance API
Documentation
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
//! Distributed tracing implementation for EEYF
//!
//! This module provides comprehensive tracing capabilities using the `tracing`
//! crate, enabling distributed request tracking and span correlation across
//! the EEYF library components.

use crate::yahoo_error::YahooError;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
use uuid::Uuid;

#[cfg(feature = "tracing")]
use tracing::{Level, Span, debug, error, field::Empty, info, instrument, span, warn};

#[cfg(feature = "tracing")]
use tracing_subscriber::{
    EnvFilter, Registry,
    fmt::{self, format::FmtSpan},
    layer::SubscriberExt,
};

/// Tracing configuration
#[derive(Debug, Clone)]
pub struct TracingConfig {
    /// Enable tracing
    pub enabled: bool,
    /// Trace level (error, warn, info, debug, trace)
    pub level: TraceLevel,
    /// Service name for tracing
    pub service_name: String,
    /// Service version
    pub service_version: String,
    /// Environment (production, staging, development)
    pub environment: String,
    /// Export traces to Jaeger
    pub jaeger_endpoint: Option<String>,
    /// Export traces to Zipkin
    pub zipkin_endpoint: Option<String>,
    /// Sample rate (0.0 to 1.0)
    pub sample_rate: f64,
    /// Maximum spans per trace
    pub max_spans_per_trace: usize,
}

impl Default for TracingConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            level: TraceLevel::Info,
            service_name: "eeyf".to_string(),
            service_version: env!("CARGO_PKG_VERSION").to_string(),
            environment: "development".to_string(),
            jaeger_endpoint: None,
            zipkin_endpoint: None,
            sample_rate: 1.0,
            max_spans_per_trace: 100,
        }
    }
}

/// Trace levels
#[derive(Debug, Clone)]
pub enum TraceLevel {
    Error,
    Warn,
    Info,
    Debug,
    Trace,
}

#[cfg(feature = "tracing")]
impl From<TraceLevel> for Level {
    fn from(level: TraceLevel) -> Self {
        match level {
            TraceLevel::Error => Level::ERROR,
            TraceLevel::Warn => Level::WARN,
            TraceLevel::Info => Level::INFO,
            TraceLevel::Debug => Level::DEBUG,
            TraceLevel::Trace => Level::TRACE,
        }
    }
}

/// Request context for tracing
#[derive(Debug, Clone)]
pub struct RequestContext {
    /// Unique request ID
    pub request_id: String,
    /// Parent trace ID (if part of larger trace)
    pub trace_id: Option<String>,
    /// Symbol being requested
    pub symbol: String,
    /// Endpoint being called
    pub endpoint: String,
    /// Start time
    pub start_time: Instant,
    /// Additional metadata
    pub metadata: HashMap<String, String>,
}

impl RequestContext {
    /// Create a new request context
    pub fn new(symbol: &str, endpoint: &str) -> Self {
        Self {
            request_id: Uuid::new_v4().to_string(),
            trace_id: None,
            symbol: symbol.to_string(),
            endpoint: endpoint.to_string(),
            start_time: Instant::now(),
            metadata: HashMap::new(),
        }
    }

    /// Create a child context from parent trace
    pub fn with_parent_trace(symbol: &str, endpoint: &str, parent_trace_id: &str) -> Self {
        let mut ctx = Self::new(symbol, endpoint);
        ctx.trace_id = Some(parent_trace_id.to_string());
        ctx
    }

    /// Add metadata to the context
    pub fn with_metadata(mut self, key: &str, value: &str) -> Self {
        self.metadata.insert(key.to_string(), value.to_string());
        self
    }

    /// Get elapsed time since request start
    pub fn elapsed(&self) -> std::time::Duration {
        self.start_time.elapsed()
    }
}

/// Tracing manager
#[derive(Debug)]
pub struct TracingManager {
    #[allow(dead_code)]
    config: TracingConfig,
    active_spans: Arc<std::sync::RwLock<HashMap<String, RequestContext>>>,
}

impl TracingManager {
    /// Create a new tracing manager
    pub fn new(config: TracingConfig) -> Self {
        Self {
            config,
            active_spans: Arc::new(std::sync::RwLock::new(HashMap::new())),
        }
    }

    /// Initialize tracing subscriber
    #[cfg(feature = "tracing")]
    pub fn init_tracing(&self) -> Result<(), YahooError> {
        let filter = EnvFilter::try_from_default_env()
            .or_else(|_| {
                let level = match self.config.level {
                    TraceLevel::Error => "error",
                    TraceLevel::Warn => "warn",
                    TraceLevel::Info => "info",
                    TraceLevel::Debug => "debug",
                    TraceLevel::Trace => "trace",
                };
                EnvFilter::try_new(format!("eeyf={}", level))
            })
            .map_err(|e| YahooError::InvalidStatusCode(format!("Invalid tracing filter: {}", e)))?;

        let fmt_layer = fmt::layer()
            .with_target(true)
            .with_level(true)
            .with_thread_ids(true)
            .with_span_events(FmtSpan::CLOSE);

        let subscriber = Registry::default().with(filter).with(fmt_layer);

        // OpenTelemetry integration disabled due to version conflicts
        // TODO: Re-enable with compatible versions
        let subscriber = subscriber;

        tracing::subscriber::set_global_default(subscriber).map_err(|e| {
            YahooError::InvalidStatusCode(format!("Failed to set tracing subscriber: {}", e))
        })?;

        info!(
            "🔍 Tracing initialized for service: {}",
            self.config.service_name
        );
        Ok(())
    }

    /// Start a new trace for a request
    #[cfg(feature = "tracing")]
    #[instrument(
        skip(self),
        fields(
            request_id = %context.request_id,
            symbol = %context.symbol,
            endpoint = %context.endpoint,
            trace_id = context.trace_id.as_deref().unwrap_or("none")
        )
    )]
    pub async fn start_request_trace(&self, context: RequestContext) -> RequestSpan {
        let span = span!(
            Level::INFO,
            "eeyf_request",
            request_id = %context.request_id,
            symbol = %context.symbol,
            endpoint = %context.endpoint,
            trace_id = context.trace_id.as_deref().unwrap_or("none"),
            duration_ms = Empty,
            status = Empty,
            error_type = Empty
        );

        // Store active span
        {
            let mut active_spans = self.active_spans.write().unwrap();
            active_spans.insert(context.request_id.clone(), context.clone());
        }

        info!(
            request_id = %context.request_id,
            "Starting request for symbol: {} on endpoint: {}",
            context.symbol,
            context.endpoint
        );

        RequestSpan::new(span, context, Arc::clone(&self.active_spans))
    }

    /// Log enterprise flow span
    #[cfg(feature = "tracing")]
    #[instrument(skip(self))]
    pub fn trace_enterprise_flow(&self, request_id: &str, component: &str, operation: &str) {
        debug!(
            request_id = %request_id,
            component = %component,
            operation = %operation,
            "Enterprise flow step"
        );
    }

    /// Log rate limiter activity
    #[cfg(feature = "tracing")]
    #[instrument(skip(self))]
    pub fn trace_rate_limiter(&self, request_id: &str, wait_time_ms: u64, tokens_remaining: f64) {
        if wait_time_ms > 0 {
            warn!(
                request_id = %request_id,
                wait_time_ms = %wait_time_ms,
                tokens_remaining = %tokens_remaining,
                "Rate limit triggered, waiting"
            );
        } else {
            debug!(
                request_id = %request_id,
                tokens_remaining = %tokens_remaining,
                "Rate limit check passed"
            );
        }
    }

    /// Log circuit breaker activity  
    #[cfg(feature = "tracing")]
    #[instrument(skip(self))]
    pub fn trace_circuit_breaker(&self, request_id: &str, state: &str, action: &str) {
        match state {
            "open" => error!(
                request_id = %request_id,
                state = %state,
                action = %action,
                "Circuit breaker is open, rejecting request"
            ),
            "half-open" => warn!(
                request_id = %request_id,
                state = %state,
                action = %action,
                "Circuit breaker is half-open, allowing probe request"
            ),
            _ => debug!(
                request_id = %request_id,
                state = %state,
                action = %action,
                "Circuit breaker state change"
            ),
        }
    }

    /// Log cache activity
    #[cfg(feature = "tracing")]
    #[instrument(skip(self))]
    pub fn trace_cache_activity(&self, request_id: &str, operation: &str, hit: bool, key: &str) {
        if hit {
            debug!(
                request_id = %request_id,
                operation = %operation,
                key = %key,
                "Cache hit"
            );
        } else {
            debug!(
                request_id = %request_id,
                operation = %operation,
                key = %key,
                "Cache miss"
            );
        }
    }

    /// Log retry attempt
    #[cfg(feature = "tracing")]
    #[instrument(skip(self))]
    pub fn trace_retry_attempt(&self, request_id: &str, attempt: u32, delay_ms: u64, error: &str) {
        warn!(
            request_id = %request_id,
            attempt = %attempt,
            delay_ms = %delay_ms,
            error = %error,
            "Retrying request after failure"
        );
    }

    /// Get active span count
    pub fn active_span_count(&self) -> usize {
        self.active_spans.read().unwrap().len()
    }

    /// Cleanup old spans (should be called periodically)
    pub fn cleanup_old_spans(&self, max_age_secs: u64) {
        let mut active_spans = self.active_spans.write().unwrap();
        let cutoff = std::time::Duration::from_secs(max_age_secs);

        active_spans.retain(|_, context| context.start_time.elapsed() < cutoff);
    }
}

/// Request span wrapper
pub struct RequestSpan {
    #[cfg(feature = "tracing")]
    span: Span,
    context: RequestContext,
    active_spans: Arc<std::sync::RwLock<HashMap<String, RequestContext>>>,
}

impl RequestSpan {
    #[cfg(feature = "tracing")]
    fn new(
        span: Span,
        context: RequestContext,
        active_spans: Arc<std::sync::RwLock<HashMap<String, RequestContext>>>,
    ) -> Self {
        Self {
            span,
            context,
            active_spans,
        }
    }

    #[cfg(not(feature = "tracing"))]
    fn new(
        _span: (),
        context: RequestContext,
        active_spans: Arc<std::sync::RwLock<HashMap<String, RequestContext>>>,
    ) -> Self {
        Self {
            context,
            active_spans,
        }
    }

    /// Get the request context
    pub fn context(&self) -> &RequestContext {
        &self.context
    }

    /// Record successful completion
    #[cfg(feature = "tracing")]
    pub fn record_success(self, response_size: usize) {
        let duration_ms = self.context.elapsed().as_millis() as u64;

        self.span.record("duration_ms", duration_ms);
        self.span.record("status", "success");

        info!(
            request_id = %self.context.request_id,
            duration_ms = %duration_ms,
            response_size = %response_size,
            "Request completed successfully"
        );

        // Remove from active spans
        self.active_spans
            .write()
            .unwrap()
            .remove(&self.context.request_id);
    }

    /// Record error completion  
    #[cfg(feature = "tracing")]
    pub fn record_error(self, error: &YahooError) {
        let duration_ms = self.context.elapsed().as_millis() as u64;
        let error_type = crate::metrics::categorize_error(error);

        self.span.record("duration_ms", duration_ms);
        self.span.record("status", "error");
        self.span.record("error_type", error_type);

        error!(
            request_id = %self.context.request_id,
            duration_ms = %duration_ms,
            error_type = %error_type,
            error = %error,
            "Request failed"
        );

        // Remove from active spans
        self.active_spans
            .write()
            .unwrap()
            .remove(&self.context.request_id);
    }

    /// No-op implementations when tracing is disabled
    #[cfg(not(feature = "tracing"))]
    pub fn record_success(self, _response_size: usize) {
        self.active_spans
            .write()
            .unwrap()
            .remove(&self.context.request_id);
    }

    #[cfg(not(feature = "tracing"))]
    pub fn record_error(self, _error: &YahooError) {
        self.active_spans
            .write()
            .unwrap()
            .remove(&self.context.request_id);
    }
}

// No-op implementations when tracing feature is disabled
#[cfg(not(feature = "tracing"))]
impl TracingManager {
    pub fn init_tracing(&self) -> Result<(), YahooError> {
        Ok(())
    }

    pub async fn start_request_trace(&self, context: RequestContext) -> RequestSpan {
        // Store active span even in no-op mode to maintain consistent span counting
        {
            let mut active_spans = self.active_spans.write().unwrap();
            active_spans.insert(context.request_id.clone(), context.clone());
        }
        RequestSpan::new((), context, Arc::clone(&self.active_spans))
    }

    pub fn trace_enterprise_flow(&self, _request_id: &str, _component: &str, _operation: &str) {}
    pub fn trace_rate_limiter(
        &self,
        _request_id: &str,
        _wait_time_ms: u64,
        _tokens_remaining: f64,
    ) {
    }
    pub fn trace_circuit_breaker(&self, _request_id: &str, _state: &str, _action: &str) {}
    pub fn trace_cache_activity(
        &self,
        _request_id: &str,
        _operation: &str,
        _hit: bool,
        _key: &str,
    ) {
    }
    pub fn trace_retry_attempt(
        &self,
        _request_id: &str,
        _attempt: u32,
        _delay_ms: u64,
        _error: &str,
    ) {
    }
}

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

    #[test]
    fn test_request_context_creation() {
        let ctx = RequestContext::new("AAPL", "quotes");
        assert_eq!(ctx.symbol, "AAPL");
        assert_eq!(ctx.endpoint, "quotes");
        assert!(ctx.trace_id.is_none());
        assert!(!ctx.request_id.is_empty());
    }

    #[test]
    fn test_request_context_with_parent() {
        let ctx = RequestContext::with_parent_trace("AAPL", "quotes", "parent-123");
        assert_eq!(ctx.trace_id, Some("parent-123".to_string()));
    }

    #[test]
    fn test_request_context_metadata() {
        let ctx = RequestContext::new("AAPL", "quotes")
            .with_metadata("user_id", "123")
            .with_metadata("session", "abc");

        assert_eq!(ctx.metadata.get("user_id"), Some(&"123".to_string()));
        assert_eq!(ctx.metadata.get("session"), Some(&"abc".to_string()));
    }

    #[test]
    fn test_tracing_manager_creation() {
        let config = TracingConfig::default();
        let manager = TracingManager::new(config);
        assert_eq!(manager.active_span_count(), 0);
    }

    #[tokio::test]
    async fn test_span_lifecycle() {
        let config = TracingConfig::default();
        let manager = TracingManager::new(config);

        let context = RequestContext::new("AAPL", "quotes");
        let _request_id = context.request_id.clone();

        // Start span
        let span = manager.start_request_trace(context).await;
        assert_eq!(manager.active_span_count(), 1);

        // Complete span
        span.record_success(1024);
        assert_eq!(manager.active_span_count(), 0);
    }
}