|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use chrono::{DateTime, Utc}; |
| 4 | +use prost_types::Timestamp; |
| 5 | +use relay_event_schema::protocol::{Attributes, OurLog}; |
| 6 | +use relay_protocol::{Annotated, Value}; |
| 7 | +use relay_quotas::Scoping; |
| 8 | +use sentry_protos::snuba::v1::{AnyValue, TraceItem, TraceItemType, any_value}; |
| 9 | +use uuid::Uuid; |
| 10 | + |
| 11 | +use crate::constants::DEFAULT_EVENT_RETENTION; |
| 12 | +use crate::processing::Counted; |
| 13 | +use crate::processing::logs::{Error, Result}; |
| 14 | +use crate::services::outcome::DiscardReason; |
| 15 | +use crate::services::store::StoreLog; |
| 16 | + |
| 17 | +macro_rules! required { |
| 18 | + ($value:expr) => {{ |
| 19 | + match $value { |
| 20 | + Annotated(Some(value), _) => value, |
| 21 | + Annotated(None, meta) => { |
| 22 | + relay_log::debug!( |
| 23 | + "dropping log because of missing required field {} with meta {meta:?}", |
| 24 | + stringify!($value), |
| 25 | + ); |
| 26 | + return Err(Error::Invalid(DiscardReason::InvalidLog)); |
| 27 | + } |
| 28 | + } |
| 29 | + }}; |
| 30 | +} |
| 31 | + |
| 32 | +/// Context parameters for [`convert`]. |
| 33 | +#[derive(Debug, Clone, Copy)] |
| 34 | +pub struct Context { |
| 35 | + /// Received time. |
| 36 | + pub received_at: DateTime<Utc>, |
| 37 | + /// Item scoping. |
| 38 | + pub scoping: Scoping, |
| 39 | + /// Storage retention in days. |
| 40 | + pub retention: Option<u16>, |
| 41 | +} |
| 42 | + |
| 43 | +pub fn convert(log: Annotated<OurLog>, ctx: &Context) -> Result<StoreLog> { |
| 44 | + let quantities = log.quantities(); |
| 45 | + |
| 46 | + let log = required!(log); |
| 47 | + let timestamp = required!(log.timestamp); |
| 48 | + let attrs = log.attributes.0.unwrap_or_default(); |
| 49 | + |
| 50 | + let trace_item = TraceItem { |
| 51 | + item_type: TraceItemType::Log.into(), |
| 52 | + organization_id: ctx.scoping.organization_id.value(), |
| 53 | + project_id: ctx.scoping.project_id.value(), |
| 54 | + received: Some(ts(ctx.received_at)), |
| 55 | + retention_days: ctx.retention.unwrap_or(DEFAULT_EVENT_RETENTION).into(), |
| 56 | + timestamp: Some(ts(timestamp.0)), |
| 57 | + trace_id: required!(log.trace_id).to_string(), |
| 58 | + item_id: Uuid::new_v7(timestamp.into()).as_bytes().to_vec(), |
| 59 | + attributes: attributes(attrs), |
| 60 | + client_sample_rate: 1.0, |
| 61 | + server_sample_rate: 1.0, |
| 62 | + }; |
| 63 | + |
| 64 | + Ok(StoreLog { |
| 65 | + trace_item, |
| 66 | + quantities, |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +fn ts(dt: DateTime<Utc>) -> Timestamp { |
| 71 | + Timestamp { |
| 72 | + seconds: dt.timestamp(), |
| 73 | + nanos: i32::try_from(dt.timestamp_subsec_nanos()).unwrap_or(0), |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +fn attributes(attributes: Attributes) -> HashMap<String, AnyValue> { |
| 78 | + let mut result = HashMap::with_capacity(attributes.0.len()); |
| 79 | + |
| 80 | + for (name, attribute) in attributes { |
| 81 | + let value = attribute |
| 82 | + .into_value() |
| 83 | + .and_then(|v| v.value.value.into_value()); |
| 84 | + |
| 85 | + let Some(value) = value else { |
| 86 | + // Emit `_meta` attributes here with #4804. |
| 87 | + continue; |
| 88 | + }; |
| 89 | + |
| 90 | + let Some(value) = (match value { |
| 91 | + Value::Bool(v) => Some(any_value::Value::BoolValue(v)), |
| 92 | + Value::I64(v) => Some(any_value::Value::IntValue(v)), |
| 93 | + Value::U64(v) => i64::try_from(v).ok().map(any_value::Value::IntValue), |
| 94 | + Value::F64(v) => Some(any_value::Value::DoubleValue(v)), |
| 95 | + Value::String(v) => Some(any_value::Value::StringValue(v)), |
| 96 | + // These cases do not happen, as they are not valid attributes |
| 97 | + // and they should have been filtered out before already. |
| 98 | + Value::Array(_) | Value::Object(_) => { |
| 99 | + debug_assert!(false, "unsupported log value"); |
| 100 | + None |
| 101 | + } |
| 102 | + }) else { |
| 103 | + continue; |
| 104 | + }; |
| 105 | + |
| 106 | + result.insert(name, AnyValue { value: Some(value) }); |
| 107 | + } |
| 108 | + |
| 109 | + result |
| 110 | +} |
0 commit comments