llm-optimizer-processor 0.1.1

Data processing and transformation pipeline
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
//! Distributed tracing with OpenTelemetry.
//!
//! This module provides trace provider initialization, span instrumentation,
//! and helpers for distributed tracing.

use crate::telemetry::resource::ResourceBuilder;
use opentelemetry::{
    global,
    trace::{SpanKind, Status, TraceError, Tracer, TracerProvider as _},
    KeyValue,
};
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::{
    propagation::TraceContextPropagator,
    runtime,
    trace::{
        BatchConfig, BatchSpanProcessor, RandomIdGenerator, Sampler, SpanLimits,
        TracerProvider,
    },
    Resource,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;

/// OTLP protocol type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum OtlpProtocol {
    /// gRPC protocol
    Grpc,
    /// HTTP protocol
    Http,
}

impl Default for OtlpProtocol {
    fn default() -> Self {
        Self::Grpc
    }
}

/// Sampling strategy for traces.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum SamplingStrategy {
    /// Always sample (sample all traces).
    AlwaysOn,
    /// Never sample (drop all traces).
    AlwaysOff,
    /// Sample based on trace ID ratio.
    TraceIdRatio { ratio: f64 },
    /// Parent-based sampling (follow parent's sampling decision).
    ParentBased {
        #[serde(flatten)]
        root: Box<SamplingStrategy>,
    },
}

impl Default for SamplingStrategy {
    fn default() -> Self {
        Self::ParentBased {
            root: Box::new(Self::TraceIdRatio { ratio: 1.0 }),
        }
    }
}

impl SamplingStrategy {
    /// Converts the strategy to an OpenTelemetry sampler.
    pub fn to_sampler(&self) -> Sampler {
        match self {
            Self::AlwaysOn => Sampler::AlwaysOn,
            Self::AlwaysOff => Sampler::AlwaysOff,
            Self::TraceIdRatio { ratio } => Sampler::TraceIdRatioBased(*ratio),
            Self::ParentBased { root } => Sampler::ParentBased(Box::new(root.to_sampler())),
        }
    }
}

/// Batch configuration for span export.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchConfiguration {
    /// Maximum queue size for batching.
    #[serde(default = "default_max_queue_size")]
    pub max_queue_size: usize,
    /// Maximum batch size.
    #[serde(default = "default_max_export_batch_size")]
    pub max_export_batch_size: usize,
    /// Maximum time to wait before exporting.
    #[serde(with = "humantime_serde", default = "default_scheduled_delay")]
    pub scheduled_delay: Duration,
    /// Maximum time to wait for export to complete.
    #[serde(with = "humantime_serde", default = "default_export_timeout")]
    pub export_timeout: Duration,
}

fn default_max_queue_size() -> usize {
    2048
}

fn default_max_export_batch_size() -> usize {
    512
}

fn default_scheduled_delay() -> Duration {
    Duration::from_secs(5)
}

fn default_export_timeout() -> Duration {
    Duration::from_secs(30)
}

impl Default for BatchConfiguration {
    fn default() -> Self {
        Self {
            max_queue_size: default_max_queue_size(),
            max_export_batch_size: default_max_export_batch_size(),
            scheduled_delay: default_scheduled_delay(),
            export_timeout: default_export_timeout(),
        }
    }
}

impl From<BatchConfiguration> for BatchConfig {
    fn from(_config: BatchConfiguration) -> Self {
        // Note: BatchConfig API changed in opentelemetry_sdk 0.24
        // TODO: Update to use new BatchConfigBuilder API
        BatchConfig::default()
    }
}

/// Trace provider configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TraceConfig {
    /// Whether tracing is enabled.
    #[serde(default = "default_enabled")]
    pub enabled: bool,

    /// Service name.
    pub service_name: String,

    /// Service version.
    #[serde(default = "default_service_version")]
    pub service_version: String,

    /// OTLP endpoint (e.g., "http://localhost:4317").
    #[serde(default = "default_otlp_endpoint")]
    pub otlp_endpoint: String,

    /// OTLP protocol (grpc or http).
    #[serde(default)]
    pub otlp_protocol: OtlpProtocol,

    /// Sampling strategy.
    #[serde(default)]
    pub sampling: SamplingStrategy,

    /// Batch configuration.
    #[serde(default)]
    pub batch_config: BatchConfiguration,

    /// Additional headers for OTLP export.
    #[serde(default)]
    pub headers: HashMap<String, String>,

    /// Deployment environment.
    #[serde(default)]
    pub environment: Option<String>,
}

fn default_enabled() -> bool {
    true
}

fn default_service_version() -> String {
    env!("CARGO_PKG_VERSION").to_string()
}

fn default_otlp_endpoint() -> String {
    "http://localhost:4317".to_string()
}

impl Default for TraceConfig {
    fn default() -> Self {
        Self {
            enabled: default_enabled(),
            service_name: "llm-optimizer-processor".to_string(),
            service_version: default_service_version(),
            otlp_endpoint: default_otlp_endpoint(),
            otlp_protocol: OtlpProtocol::default(),
            sampling: SamplingStrategy::default(),
            batch_config: BatchConfiguration::default(),
            headers: HashMap::new(),
            environment: None,
        }
    }
}

/// Initializes the global trace provider with OTLP exporter.
///
/// # Errors
///
/// Returns an error if the trace provider cannot be initialized.
pub fn init_tracer_provider(config: TraceConfig) -> Result<TracerProvider, TraceError> {
    if !config.enabled {
        tracing::info!("OpenTelemetry tracing is disabled");
        return Err(TraceError::Other("tracing disabled".into()));
    }

    // Set up trace context propagator
    global::set_text_map_propagator(TraceContextPropagator::new());

    // Build resource
    let resource = ResourceBuilder::new()
        .with_service_name(config.service_name.clone())
        .with_service_version(config.service_version.clone())
        .with_attribute("service.component", "stream-processor");

    let resource = if let Some(env) = config.environment.clone() {
        resource.with_deployment_environment(env)
    } else {
        resource
    };

    let resource = resource.build();

    // Create OTLP exporter
    let exporter = match config.otlp_protocol {
        OtlpProtocol::Grpc => {
            let mut exporter = opentelemetry_otlp::new_exporter()
                .tonic()
                .with_endpoint(&config.otlp_endpoint);

            // Add custom headers
            if !config.headers.is_empty() {
                let mut metadata = tonic::metadata::MetadataMap::new();
                for (key, value) in &config.headers {
                    if let (Ok(k), Ok(v)) = (
                        key.parse::<tonic::metadata::MetadataKey<_>>(),
                        value.parse::<tonic::metadata::MetadataValue<_>>(),
                    ) {
                        metadata.insert(k, v);
                    }
                }
                exporter = exporter.with_metadata(metadata);
            }

            opentelemetry_otlp::new_pipeline()
                .tracing()
                .with_exporter(exporter)
                .with_trace_config(
                    opentelemetry_sdk::trace::Config::default()
                        .with_sampler(config.sampling.to_sampler())
                        .with_id_generator(RandomIdGenerator::default())
                        .with_resource(resource),
                )
                .with_batch_config(config.batch_config.into())
                .install_batch(runtime::Tokio)?
        }
        OtlpProtocol::Http => {
            // Note: HTTP exporter API changed in opentelemetry-otlp 0.17
            // For now, falling back to gRPC. TODO: Implement HTTP support with new API
            opentelemetry_otlp::new_pipeline()
                .tracing()
                .with_exporter(
                    opentelemetry_otlp::new_exporter()
                        .tonic()
                        .with_endpoint(&config.otlp_endpoint),
                )
                .with_trace_config(
                    opentelemetry_sdk::trace::Config::default()
                        .with_sampler(config.sampling.to_sampler())
                        .with_id_generator(RandomIdGenerator::default())
                        .with_resource(resource),
                )
                .with_batch_config(config.batch_config.into())
                .install_batch(runtime::Tokio)?
        }
    };

    Ok(exporter)
}

/// Shuts down the global trace provider and flushes all pending spans.
pub async fn shutdown_tracer_provider() {
    global::shutdown_tracer_provider();
}

/// Span builder helper for creating instrumented spans.
pub struct SpanBuilder {
    name: String,
    kind: SpanKind,
    attributes: Vec<KeyValue>,
}

impl SpanBuilder {
    /// Creates a new span builder with the given name.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            kind: SpanKind::Internal,
            attributes: Vec::new(),
        }
    }

    /// Sets the span kind.
    pub fn with_kind(mut self, kind: SpanKind) -> Self {
        self.kind = kind;
        self
    }

    /// Adds an attribute to the span.
    pub fn with_attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.attributes
            .push(KeyValue::new(key.into(), value.into()));
        self
    }

    /// Adds multiple attributes to the span.
    pub fn with_attributes(mut self, attributes: Vec<KeyValue>) -> Self {
        self.attributes.extend(attributes);
        self
    }

    /// Starts the span using the global tracer.
    pub fn start(self) -> impl opentelemetry::trace::Span {
        let tracer = global::tracer("llm-optimizer-processor");
        let span = tracer
            .span_builder(self.name)
            .with_kind(self.kind)
            .with_attributes(self.attributes)
            .start(&tracer);
        span
    }

    /// Starts the span with a custom tracer.
    pub fn start_with_tracer<T: Tracer>(
        self,
        tracer: &T,
    ) -> T::Span {
        tracer
            .span_builder(self.name)
            .with_kind(self.kind)
            .with_attributes(self.attributes)
            .start(tracer)
    }
}

/// Helper macro for creating instrumented spans.
///
/// # Example
///
/// ```ignore
/// use crate::telemetry::span;
///
/// let span = span!("process_event", kind = SpanKind::Internal, "event.type" => "metric");
/// ```
#[macro_export]
macro_rules! span {
    ($name:expr) => {
        $crate::telemetry::SpanBuilder::new($name).start()
    };
    ($name:expr, kind = $kind:expr) => {
        $crate::telemetry::SpanBuilder::new($name)
            .with_kind($kind)
            .start()
    };
    ($name:expr, $($key:expr => $value:expr),+ $(,)?) => {
        {
            let mut builder = $crate::telemetry::SpanBuilder::new($name);
            $(
                builder = builder.with_attribute($key, $value);
            )+
            builder.start()
        }
    };
    ($name:expr, kind = $kind:expr, $($key:expr => $value:expr),+ $(,)?) => {
        {
            let mut builder = $crate::telemetry::SpanBuilder::new($name)
                .with_kind($kind);
            $(
                builder = builder.with_attribute($key, $value);
            )+
            builder.start()
        }
    };
}

/// Records an exception on a span.
pub fn record_exception<T: opentelemetry::trace::Span>(
    span: &mut T,
    error: &dyn std::error::Error,
) {
    span.record_error(error);
    span.set_status(Status::error(error.to_string()));
}

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

    #[test]
    fn test_sampling_strategy_default() {
        let strategy = SamplingStrategy::default();
        let sampler = strategy.to_sampler();
        // Default should be parent-based with always-on root
        assert!(matches!(sampler, Sampler::ParentBased(_)));
    }

    #[test]
    fn test_sampling_strategy_always_on() {
        let strategy = SamplingStrategy::AlwaysOn;
        let sampler = strategy.to_sampler();
        assert!(matches!(sampler, Sampler::AlwaysOn));
    }

    #[test]
    fn test_sampling_strategy_trace_id_ratio() {
        let strategy = SamplingStrategy::TraceIdRatio { ratio: 0.5 };
        let sampler = strategy.to_sampler();
        assert!(matches!(sampler, Sampler::TraceIdRatioBased(_)));
    }

    #[test]
    fn test_batch_config_default() {
        let config = BatchConfiguration::default();
        assert_eq!(config.max_queue_size, 2048);
        assert_eq!(config.max_export_batch_size, 512);
    }

    #[test]
    fn test_trace_config_default() {
        let config = TraceConfig::default();
        assert!(config.enabled);
        assert_eq!(config.service_name, "llm-optimizer-processor");
        assert_eq!(config.otlp_endpoint, "http://localhost:4317");
        assert_eq!(config.otlp_protocol, OtlpProtocol::Grpc);
    }

    #[test]
    fn test_span_builder() {
        let builder = SpanBuilder::new("test-span")
            .with_kind(SpanKind::Server)
            .with_attribute("test.key", "test.value");

        assert_eq!(builder.name, "test-span");
        assert_eq!(builder.kind, SpanKind::Server);
        assert_eq!(builder.attributes.len(), 1);
    }

    #[test]
    fn test_otlp_protocol_serialization() {
        let grpc = OtlpProtocol::Grpc;
        let http = OtlpProtocol::Http;

        let grpc_json = serde_json::to_string(&grpc).unwrap();
        let http_json = serde_json::to_string(&http).unwrap();

        assert_eq!(grpc_json, "\"grpc\"");
        assert_eq!(http_json, "\"http\"");
    }
}