Repository navigation
fix(relay): Limit maximum number of otel logs deserialized from protos #6274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: christopherklochek/ingest-1099-add-bounded-json-serializer
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,23 @@ | ||
| use opentelemetry_proto::tonic::logs::v1::LogsData; | ||
| use prost::Message as _; | ||
| use relay_event_schema::protocol::OurLog; | ||
|
|
||
| use crate::integrations::OtelFormat; | ||
| use crate::processing::logs::{Error, Result, Settings}; | ||
| use crate::processing::logs::{ | ||
| Error, Result, Settings, | ||
| integrations::{otel_json_deserializer, otel_proto_deserializer}, | ||
| }; | ||
| use crate::services::outcome::DiscardReason; | ||
| use opentelemetry_proto::tonic::logs::v1::LogsData; | ||
| use relay_event_schema::protocol::OurLog; | ||
|
|
||
| /// Expands OTeL logs into the [`OurLog`] format. | ||
| pub fn expand<F>(format: OtelFormat, payload: &[u8], mut produce: F) -> Result<Settings> | ||
| pub fn expand<F>( | ||
| format: OtelFormat, | ||
| payload: &[u8], | ||
| max_logs: usize, | ||
| mut produce: F, | ||
| ) -> Result<Settings> | ||
| where | ||
| F: FnMut(OurLog), | ||
| { | ||
| let logs = parse_logs_data(format, payload)?; | ||
| let logs = parse_logs_data(format, payload, max_logs)?; | ||
|
|
||
| for resource_logs in logs.resource_logs { | ||
| let resource = resource_logs.resource.as_ref(); | ||
|
|
@@ -27,21 +33,129 @@ where | |
| Ok(Settings::default()) | ||
| } | ||
|
|
||
| fn parse_logs_data(format: OtelFormat, payload: &[u8]) -> Result<LogsData, Error> { | ||
| fn parse_logs_data(format: OtelFormat, payload: &[u8], max_logs: usize) -> Result<LogsData, Error> { | ||
| match format { | ||
| OtelFormat::Json => serde_json::from_slice(payload).map_err(|e| { | ||
| OtelFormat::Json => otel_json_deserializer::deserialize(payload, max_logs).map_err(|e| { | ||
| relay_log::debug!( | ||
| error = &e as &dyn std::error::Error, | ||
| "Failed to parse logs data as JSON" | ||
| ); | ||
| Error::Invalid(DiscardReason::InvalidJson) | ||
| }), | ||
| OtelFormat::Protobuf => LogsData::decode(payload).map_err(|e| { | ||
| relay_log::debug!( | ||
| error = &e as &dyn std::error::Error, | ||
| "Failed to parse logs data as protobuf" | ||
| ); | ||
| Error::Invalid(DiscardReason::InvalidProtobuf) | ||
| }), | ||
| OtelFormat::Protobuf => { | ||
| otel_proto_deserializer::deserialize(payload, max_logs).map_err(|e| { | ||
| relay_log::debug!( | ||
| error = &e as &dyn std::error::Error, | ||
| "Failed to parse logs data as protobuf" | ||
| ); | ||
| Error::Invalid(DiscardReason::InvalidProtobuf) | ||
| }) | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Limit error remapped incorrectlyMedium Severity
Reviewed by Cursor Bugbot for commit da82bf9. Configure here.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be worth looking at. |
||
| } | ||
| } | ||
| #[cfg(test)] | ||
| mod tests { | ||
|
|
||
| use opentelemetry_proto::tonic::common::v1::any_value::Value; | ||
| use opentelemetry_proto::tonic::common::v1::{ | ||
| AnyValue, ArrayValue, InstrumentationScope, KeyValue, | ||
| }; | ||
| use opentelemetry_proto::tonic::resource::v1::Resource; | ||
| use relay_ourlogs::otel_logs::{LogRecord, LogsData, ResourceLogs, ScopeLogs}; | ||
|
|
||
| use crate::processing::logs::integrations::otel::parse_logs_data; | ||
|
|
||
| #[test] | ||
| fn test_basic_json() { | ||
| let log_data = LogsData { | ||
| resource_logs: vec![ResourceLogs { | ||
| resource: Some(Resource { | ||
| attributes: vec![KeyValue { | ||
| key: "service.name".to_owned(), | ||
| value: Some(AnyValue { | ||
| value: Some(Value::StringValue("test-service".to_owned())), | ||
| }), | ||
| }], | ||
| dropped_attributes_count: 0, | ||
| entity_refs: vec![], | ||
| }), | ||
| scope_logs: vec![ScopeLogs { | ||
| scope: Some(InstrumentationScope { | ||
| name: "test-library".to_owned(), | ||
| version: "".to_owned(), | ||
| attributes: vec![], | ||
| dropped_attributes_count: 0, | ||
| }), | ||
| log_records: vec![LogRecord { | ||
| time_unix_nano: 123, | ||
| observed_time_unix_nano: 123, | ||
| severity_number: 2, | ||
| severity_text: "Information".to_owned(), | ||
| body: Some(AnyValue { | ||
| value: Some(Value::StringValue("a body".to_owned())), | ||
| }), | ||
| attributes: vec![ | ||
| KeyValue { | ||
| key: "attribute".to_owned(), | ||
| value: Some(AnyValue { | ||
| value: Some(Value::StringValue("value".to_owned())), | ||
| }), | ||
| }, | ||
| KeyValue { | ||
| key: "nested attribute".to_owned(), | ||
| value: Some(AnyValue { | ||
| value: Some(Value::ArrayValue(ArrayValue { | ||
| values: vec![AnyValue { | ||
| value: Some(Value::StringValue("value".to_owned())), | ||
| }], | ||
| })), | ||
| }), | ||
| }, | ||
| ], | ||
| dropped_attributes_count: 0, | ||
| flags: 0, | ||
| trace_id: "5B8EFFF798038103D269B633813FC60C".into(), | ||
| span_id: "EEE19B7EC3C1B174".into(), | ||
| event_name: "".to_owned(), | ||
| }], | ||
| schema_url: "".to_owned(), | ||
| }], | ||
| schema_url: "http://example.com".to_owned(), | ||
| }], | ||
| }; | ||
|
|
||
| let json = serde_json::to_string(&log_data).unwrap(); | ||
|
|
||
| let unjson: LogsData = serde_json::from_str(&json).unwrap(); | ||
|
|
||
| assert_eq!(unjson, log_data); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_abusive_json() { | ||
| let mut abusive_log = "{},".repeat(1_001); | ||
| abusive_log.pop(); | ||
|
|
||
| let json = r#"{ | ||
| "resourceLogs": [ | ||
| { | ||
| "resource": { | ||
| "attributes": [ | ||
| { | ||
| "key": "service.name", | ||
| "value": {"stringValue": "test-service"} | ||
| } | ||
| ] | ||
| }, | ||
| "scopeLogs": [ | ||
| { | ||
| "scope": {"name": "test-library"}, | ||
| "logRecords": ["# | ||
| .to_owned(); | ||
| let json = json + &abusive_log + "]}]}]}"; | ||
|
|
||
| assert!( | ||
| parse_logs_data(crate::integrations::OtelFormat::Json, json.as_bytes(), 1000).is_err() | ||
| ); | ||
| } | ||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What constitutes an "expansion" might be confusing to users, should this just be
max_log_count, and then also apply to non-integration logs? Or is there a good reason why integrations should have a different limit than our own log format?If this only applies to Otel logs, I would name the option
max_otel_log_count.