mfyuce commented on code in PR #3516: URL: https://github.com/apache/iggy/pull/3516#discussion_r3711246700
########## core/connectors/sources/otlp_source/src/convert.rs: ########## @@ -0,0 +1,331 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use iggy_connector_sdk::ProducedMessage; +use opentelemetry_proto::tonic::collector::logs::v1::ExportLogsServiceRequest; +use opentelemetry_proto::tonic::collector::metrics::v1::ExportMetricsServiceRequest; +use opentelemetry_proto::tonic::collector::trace::v1::ExportTraceServiceRequest; +use opentelemetry_proto::tonic::common::v1::{AnyValue, KeyValue, any_value}; +use opentelemetry_proto::tonic::metrics::v1::{Metric, metric, number_data_point}; +use opentelemetry_proto::tonic::resource::v1::Resource; +use serde_json::{Map, Value, json}; +use std::fmt::Write as _; +use tracing::warn; + +pub fn export_logs_to_messages(req: ExportLogsServiceRequest) -> Vec<ProducedMessage> { + let mut messages = Vec::new(); + for resource_logs in req.resource_logs { + let resource_attrs = extract_resource_attrs(resource_logs.resource.as_ref()); + let service_name = resource_attrs + .get("service.name") + .and_then(Value::as_str) + .unwrap_or("") + .to_owned(); + + let resource_value = Value::Object(resource_attrs); + for scope_logs in resource_logs.scope_logs { + for record in scope_logs.log_records { + let body = record.body.as_ref().map(any_value_to_json); + let mut doc = json!({ + "signal": "log", + "timestamp_ns": record.time_unix_nano, + "observed_timestamp_ns": record.observed_time_unix_nano, + "severity": severity_number_to_text(record.severity_number), + "severity_text": record.severity_text, + "body": body, + "trace_id": bytes_to_hex(&record.trace_id), + "span_id": bytes_to_hex(&record.span_id), + "service_name": service_name, + "resource": resource_value.clone(), + "attributes": extract_attrs(&record.attributes), + }); + if let Some(obj) = doc.as_object_mut() { + obj.retain(|_, v| !v.is_null() && v != &Value::String(String::new())); + } + match serde_json::to_vec(&doc) { + Ok(payload) => messages.push(ProducedMessage { + id: None, + checksum: None, + timestamp: (record.time_unix_nano != 0).then_some(record.time_unix_nano), + origin_timestamp: None, + headers: None, + payload, + }), + Err(err) => warn!("Failed to serialize log record: {err}"), + } + } + } + } + messages +} + +pub fn export_metrics_to_messages(req: ExportMetricsServiceRequest) -> Vec<ProducedMessage> { + let mut messages = Vec::new(); + for resource_metrics in req.resource_metrics { + let resource_attrs = extract_resource_attrs(resource_metrics.resource.as_ref()); + let service_name = resource_attrs + .get("service.name") + .and_then(Value::as_str) + .unwrap_or("") + .to_owned(); + + for scope_metrics in resource_metrics.scope_metrics { + for metric in scope_metrics.metrics { + let data_points = metric_to_data_points(&metric, &resource_attrs, &service_name); + messages.extend(data_points); + } + } + } + messages +} + +pub fn export_traces_to_messages(req: ExportTraceServiceRequest) -> Vec<ProducedMessage> { + let mut messages = Vec::new(); + for resource_spans in req.resource_spans { + let resource_attrs = extract_resource_attrs(resource_spans.resource.as_ref()); + let service_name = resource_attrs + .get("service.name") + .and_then(Value::as_str) + .unwrap_or("") + .to_owned(); + + let resource_value = Value::Object(resource_attrs); + for scope_spans in resource_spans.scope_spans { + for span in scope_spans.spans { + let status_code = span + .status + .as_ref() + .map(|s| status_code_to_text(s.code)) + .unwrap_or("unset"); + let status_message = span + .status + .as_ref() + .map(|s| s.message.as_str()) + .unwrap_or_default(); + + let mut doc = json!({ + "signal": "trace", + "trace_id": bytes_to_hex(&span.trace_id), + "span_id": bytes_to_hex(&span.span_id), + "parent_span_id": bytes_to_hex(&span.parent_span_id), + "name": span.name, + "kind": span_kind_to_text(span.kind), + "start_time_ns": span.start_time_unix_nano, + "end_time_ns": span.end_time_unix_nano, + "status": status_code, + "status_message": status_message, + "service_name": service_name, + "resource": resource_value.clone(), + "attributes": extract_attrs(&span.attributes), + }); + if let Some(obj) = doc.as_object_mut() { + obj.retain(|_, v| !v.is_null() && v != &Value::String(String::new())); + } + match serde_json::to_vec(&doc) { + Ok(payload) => messages.push(ProducedMessage { + id: None, + checksum: None, + timestamp: (span.start_time_unix_nano != 0) + .then_some(span.start_time_unix_nano), + origin_timestamp: None, + headers: None, + payload, + }), + Err(err) => warn!("Failed to serialize span: {err}"), + } + } + } + } + messages +} + Review Comment: `MetricDoc` borrows the map, so the per-data-point `to_value` walk is gone. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
