otlp-arrow-library 0.6.4

Cross-platform Rust library for receiving OTLP messages via gRPC and writing to Arrow IPC files
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
//! gRPC server for receiving OTLP messages
//!
//! Implements OTLP TraceService and MetricsService using tonic gRPC framework.

use crate::error::OtlpError;
use crate::otlp::OtlpFileExporter;
use opentelemetry_proto::tonic::collector::metrics::v1::{
    ExportMetricsServiceRequest, ExportMetricsServiceResponse,
    metrics_service_server::{MetricsService, MetricsServiceServer},
};
use opentelemetry_proto::tonic::collector::trace::v1::{
    ExportTraceServiceRequest, ExportTraceServiceResponse,
    trace_service_server::{TraceService, TraceServiceServer},
};
use opentelemetry_sdk::metrics::data::ResourceMetrics;
use opentelemetry_sdk::trace::SpanData;
use std::sync::Arc;
use tonic::Request;
use tonic::Response;
use tonic::Status;
use tonic::transport::Server;
use tracing::{error, info};

/// gRPC server for OTLP messages
#[derive(Debug, Clone)]
pub struct OtlpGrpcServer {
    file_exporter: Arc<OtlpFileExporter>,
}

impl OtlpGrpcServer {
    /// Create a new gRPC server
    pub fn new(file_exporter: Arc<OtlpFileExporter>) -> Self {
        Self { file_exporter }
    }

    /// Start the gRPC server on the specified address
    pub async fn start(&self, addr: std::net::SocketAddr) -> Result<(), OtlpError> {
        info!("Starting OTLP gRPC server on {}", addr);

        let trace_service = TraceServiceImpl {
            file_exporter: self.file_exporter.clone(),
        };

        let metrics_service = MetricsServiceImpl {
            file_exporter: self.file_exporter.clone(),
        };

        Server::builder()
            .add_service(TraceServiceServer::new(trace_service))
            .add_service(MetricsServiceServer::new(metrics_service))
            .serve(addr)
            .await
            .map_err(|e| {
                OtlpError::Server(crate::error::OtlpServerError::StartupError(e.to_string()))
            })?;

        Ok(())
    }
}

/// Trace service implementation
#[derive(Debug, Clone)]
pub struct TraceServiceImpl {
    pub(crate) file_exporter: Arc<OtlpFileExporter>,
}

#[tonic::async_trait]
impl TraceService for TraceServiceImpl {
    async fn export(
        &self,
        request: Request<ExportTraceServiceRequest>,
    ) -> Result<Response<ExportTraceServiceResponse>, Status> {
        let req = request.into_inner();

        // Convert OTLP protobuf to SpanData
        // This is a simplified conversion - full implementation would use opentelemetry-proto conversion utilities
        let spans = convert_trace_request_to_spans(&req)
            .map_err(|e| Status::internal(format!("Failed to convert traces: {}", e)))?;

        if !spans.is_empty() {
            // Export spans using the file exporter directly
            // TODO: Use proper opentelemetry-proto conversion when spans are properly converted
            if let Err(e) = self.file_exporter.export_traces(spans).await {
                error!("Failed to export traces: {}", e);
                return Err(Status::internal(format!("Failed to export traces: {}", e)));
            }
        }

        Ok(Response::new(ExportTraceServiceResponse {
            partial_success: None,
        }))
    }
}

/// Helper function to create a temporary metrics server for extraction
///
/// This creates a temporary gRPC server that can capture ExportMetricsServiceRequest.
/// Used by the metrics extraction function to convert ResourceMetrics to protobuf.
pub(crate) async fn create_temporary_metrics_server(
    capture_tx: tokio::sync::oneshot::Sender<ExportMetricsServiceRequest>,
) -> Result<(tokio::task::JoinHandle<()>, String), anyhow::Error> {
    use opentelemetry_proto::tonic::collector::metrics::v1::metrics_service_server::{
        MetricsService, MetricsServiceServer,
    };
    use std::sync::Arc;
    use tonic::{Request, Response, Status};

    // Create a capture service that reuses the same pattern as MetricsServiceImpl
    struct CaptureMetricsService {
        tx: Arc<
            tokio::sync::Mutex<Option<tokio::sync::oneshot::Sender<ExportMetricsServiceRequest>>>,
        >,
    }

    #[tonic::async_trait]
    impl MetricsService for CaptureMetricsService {
        async fn export(
            &self,
            request: Request<ExportMetricsServiceRequest>,
        ) -> Result<Response<ExportMetricsServiceResponse>, Status> {
            let req = request.into_inner();

            // Send the captured request
            if let Some(sender) = self.tx.lock().await.take() {
                let _ = sender.send(req.clone());
            }

            Ok(Response::new(ExportMetricsServiceResponse {
                partial_success: None,
            }))
        }
    }

    // Find an available port using tokio::net::TcpListener
    // This must be called from within a Tokio runtime context
    // If we're not in a runtime, we need to spawn the server creation in a task
    // that has access to a runtime (the library's main runtime)
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .map_err(|e| anyhow::anyhow!("Failed to bind to port: {}", e))?;
    let addr = listener
        .local_addr()
        .map_err(|e| anyhow::anyhow!("Failed to get local address: {}", e))?;
    let addr_str = format!("http://{}", addr);

    // Create the capture service
    let capture_service = CaptureMetricsService {
        tx: Arc::new(tokio::sync::Mutex::new(Some(capture_tx))),
    };

    // Start the temporary server in a background task
    let server_handle = tokio::spawn(async move {
        let server = Server::builder()
            .add_service(MetricsServiceServer::new(capture_service))
            .serve_with_incoming(tokio_stream::wrappers::TcpListenerStream::new(listener))
            .await;

        if let Err(e) = server {
            tracing::error!("Temporary gRPC server error: {}", e);
        }
    });

    Ok((server_handle, addr_str))
}

/// Metrics service implementation
#[derive(Debug, Clone)]
pub struct MetricsServiceImpl {
    pub(crate) file_exporter: Arc<OtlpFileExporter>,
}

#[tonic::async_trait]
impl MetricsService for MetricsServiceImpl {
    async fn export(
        &self,
        request: Request<ExportMetricsServiceRequest>,
    ) -> Result<Response<ExportMetricsServiceResponse>, Status> {
        let req = request.into_inner();

        // As an OpenTelemetry endpoint, we receive Protobuf (ExportMetricsServiceRequest) directly via gRPC.
        // We don't receive ResourceMetrics - that's an SDK-internal structure that only exists in the client.
        // So we can convert Protobuf → InternalResourceMetrics → Arrow directly (NO PROXY NEEDED!)
        if let Err(e) = self.file_exporter.export_metrics_from_protobuf(&req).await {
            error!("Failed to export metrics: {}", e);
            return Err(Status::internal(format!("Failed to export metrics: {}", e)));
        }

        Ok(Response::new(ExportMetricsServiceResponse {
            partial_success: None,
        }))
    }
}

/// Convert OTLP trace request to SpanData
/// Converts protobuf ResourceSpans to SDK SpanData format
pub(crate) fn convert_trace_request_to_spans(
    req: &ExportTraceServiceRequest,
) -> Result<Vec<SpanData>, anyhow::Error> {
    use opentelemetry::KeyValue;
    use opentelemetry::trace::{
        SpanContext, SpanId, SpanKind, Status, TraceFlags, TraceId, TraceState,
    };
    use std::time::{Duration, UNIX_EPOCH};

    let mut spans = Vec::new();

    for resource_span in &req.resource_spans {
        // Extract resource attributes
        let resource_attrs = if let Some(ref resource) = resource_span.resource {
            resource
                .attributes
                .iter()
                .map(|kv| {
                    let value = kv.value.as_ref().and_then(|v| match &v.value {
                        Some(
                            opentelemetry_proto::tonic::common::v1::any_value::Value::StringValue(
                                s,
                            ),
                        ) => Some(opentelemetry::Value::String(s.clone().into())),
                        Some(
                            opentelemetry_proto::tonic::common::v1::any_value::Value::IntValue(i),
                        ) => Some(opentelemetry::Value::I64(*i)),
                        Some(
                            opentelemetry_proto::tonic::common::v1::any_value::Value::DoubleValue(
                                d,
                            ),
                        ) => Some(opentelemetry::Value::F64(*d)),
                        Some(
                            opentelemetry_proto::tonic::common::v1::any_value::Value::BoolValue(b),
                        ) => Some(opentelemetry::Value::Bool(*b)),
                        _ => None,
                    });
                    KeyValue::new(
                        kv.key.clone(),
                        value.unwrap_or(opentelemetry::Value::String("".to_string().into())),
                    )
                })
                .collect()
        } else {
            vec![]
        };

        for scope_span in &resource_span.scope_spans {
            for span in &scope_span.spans {
                // Extract trace and span IDs
                if span.trace_id.len() != 16 || span.span_id.len() != 8 {
                    continue; // Skip invalid spans
                }

                let trace_id = TraceId::from_bytes([
                    span.trace_id[0],
                    span.trace_id[1],
                    span.trace_id[2],
                    span.trace_id[3],
                    span.trace_id[4],
                    span.trace_id[5],
                    span.trace_id[6],
                    span.trace_id[7],
                    span.trace_id[8],
                    span.trace_id[9],
                    span.trace_id[10],
                    span.trace_id[11],
                    span.trace_id[12],
                    span.trace_id[13],
                    span.trace_id[14],
                    span.trace_id[15],
                ]);

                let span_id = SpanId::from_bytes([
                    span.span_id[0],
                    span.span_id[1],
                    span.span_id[2],
                    span.span_id[3],
                    span.span_id[4],
                    span.span_id[5],
                    span.span_id[6],
                    span.span_id[7],
                ]);

                let parent_span_id = if span.parent_span_id.len() == 8 {
                    SpanId::from_bytes([
                        span.parent_span_id[0],
                        span.parent_span_id[1],
                        span.parent_span_id[2],
                        span.parent_span_id[3],
                        span.parent_span_id[4],
                        span.parent_span_id[5],
                        span.parent_span_id[6],
                        span.parent_span_id[7],
                    ])
                } else {
                    SpanId::INVALID
                };

                let span_context = SpanContext::new(
                    trace_id,
                    span_id,
                    TraceFlags::default(),
                    false,
                    TraceState::default(),
                );

                // Convert span kind
                let span_kind = match span.kind {
                    0 => SpanKind::Internal,
                    1 => SpanKind::Server,
                    2 => SpanKind::Client,
                    3 => SpanKind::Producer,
                    4 => SpanKind::Consumer,
                    _ => SpanKind::Internal,
                };

                // Convert timestamps
                let start_time = UNIX_EPOCH + Duration::from_nanos(span.start_time_unix_nano);
                let end_time = UNIX_EPOCH + Duration::from_nanos(span.end_time_unix_nano);

                // Convert attributes
                let mut attributes = resource_attrs.clone();
                for attr in &span.attributes {
                    let value = attr.value.as_ref().and_then(|v| match &v.value {
                        Some(
                            opentelemetry_proto::tonic::common::v1::any_value::Value::StringValue(
                                s,
                            ),
                        ) => Some(opentelemetry::Value::String(s.clone().into())),
                        Some(
                            opentelemetry_proto::tonic::common::v1::any_value::Value::IntValue(i),
                        ) => Some(opentelemetry::Value::I64(*i)),
                        Some(
                            opentelemetry_proto::tonic::common::v1::any_value::Value::DoubleValue(
                                d,
                            ),
                        ) => Some(opentelemetry::Value::F64(*d)),
                        Some(
                            opentelemetry_proto::tonic::common::v1::any_value::Value::BoolValue(b),
                        ) => Some(opentelemetry::Value::Bool(*b)),
                        _ => None,
                    });
                    if let Some(val) = value {
                        attributes.push(KeyValue::new(attr.key.clone(), val));
                    }
                }

                // Convert status
                let status = if let Some(ref s) = span.status {
                    match s.code {
                        1 => Status::Ok,
                        2 => Status::Error {
                            description: s.message.clone().into(),
                        },
                        _ => Status::Unset,
                    }
                } else {
                    Status::Unset
                };

                // Get instrumentation scope
                let instrumentation_scope = if let Some(ref scope) = scope_span.scope {
                    opentelemetry::InstrumentationScope::builder(scope.name.clone())
                        .with_version(scope.version.clone())
                        .build()
                } else {
                    opentelemetry::InstrumentationScope::builder("unknown").build()
                };

                let span_data = SpanData {
                    span_context,
                    parent_span_id,
                    span_kind,
                    name: std::borrow::Cow::Owned(span.name.clone()),
                    start_time,
                    end_time,
                    attributes,
                    events: opentelemetry_sdk::trace::SpanEvents::default(),
                    links: opentelemetry_sdk::trace::SpanLinks::default(),
                    status,
                    dropped_attributes_count: span.dropped_attributes_count,
                    parent_span_is_remote: false,
                    instrumentation_scope,
                };

                spans.push(span_data);
            }
        }
    }

    Ok(spans)
}

/// Convert OTLP metrics request to ResourceMetrics
/// Converts protobuf ResourceMetrics to SDK ResourceMetrics format
pub(crate) fn convert_metrics_request_to_resource_metrics(
    req: &ExportMetricsServiceRequest,
) -> Result<Option<ResourceMetrics>, anyhow::Error> {
    use opentelemetry::KeyValue;

    if req.resource_metrics.is_empty() {
        return Ok(None);
    }

    // Convert the first ResourceMetrics (simplified - full implementation would handle all)
    let resource_metric = &req.resource_metrics[0];

    // Extract resource attributes (preserved for future use when ResourceMetrics construction is available)
    let _resource_attrs = if let Some(ref resource) = resource_metric.resource {
        resource
            .attributes
            .iter()
            .filter_map(|kv| {
                let value = kv.value.as_ref().and_then(|v| match &v.value {
                    Some(
                        opentelemetry_proto::tonic::common::v1::any_value::Value::StringValue(s),
                    ) => Some(opentelemetry::Value::String(s.clone().into())),
                    Some(opentelemetry_proto::tonic::common::v1::any_value::Value::IntValue(i)) => {
                        Some(opentelemetry::Value::I64(*i))
                    }
                    Some(
                        opentelemetry_proto::tonic::common::v1::any_value::Value::DoubleValue(d),
                    ) => Some(opentelemetry::Value::F64(*d)),
                    Some(opentelemetry_proto::tonic::common::v1::any_value::Value::BoolValue(
                        b,
                    )) => Some(opentelemetry::Value::Bool(*b)),
                    _ => None,
                });
                value.map(|val| KeyValue::new(kv.key.clone(), val))
            })
            .collect()
    } else {
        vec![]
    };

    // Create ResourceMetrics
    // Note: Full implementation would convert ScopeMetrics and Metric data structures
    // For now, we create a minimal ResourceMetrics using Default
    // ResourceMetrics fields are private in opentelemetry-sdk 0.31, so we can't construct it directly
    // The resource attributes are preserved in the Arrow format when written to file
    // TODO: Use opentelemetry-proto conversion utilities for full ResourceMetrics construction
    let resource_metrics = ResourceMetrics::default();

    Ok(Some(resource_metrics))
}