Skip to content

Commit ae7deb5

Browse files
add p_format to event, add tests
1 parent e9d4ae1 commit ae7deb5

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

src/event/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use std::collections::HashMap;
3737
pub const DEFAULT_TIMESTAMP_KEY: &str = "p_timestamp";
3838
pub const USER_AGENT_KEY: &str = "p_user_agent";
3939
pub const SOURCE_IP_KEY: &str = "p_src_ip";
40+
pub const FORMAT_KEY: &str = "p_format";
4041

4142
#[derive(Clone)]
4243
pub struct Event {

src/handlers/http/modal/utils/ingest_utils.rs

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::collections::HashMap;
2020

2121
use actix_web::HttpRequest;
2222
use chrono::Utc;
23+
use http::header::USER_AGENT;
2324
use opentelemetry_proto::tonic::{
2425
logs::v1::LogsData, metrics::v1::MetricsData, trace::v1::TracesData,
2526
};
@@ -28,7 +29,7 @@ use serde_json::Value;
2829
use crate::{
2930
event::{
3031
format::{json, EventFormat, LogSource},
31-
SOURCE_IP_KEY, USER_AGENT_KEY,
32+
FORMAT_KEY, SOURCE_IP_KEY, USER_AGENT_KEY,
3233
},
3334
handlers::{
3435
http::{
@@ -43,7 +44,7 @@ use crate::{
4344
utils::json::{convert_array_to_object, flatten::convert_to_array},
4445
};
4546

46-
const IGNORE_HEADERS: [&str; 2] = [STREAM_NAME_HEADER_KEY, LOG_SOURCE_KEY];
47+
const IGNORE_HEADERS: [&str; 1] = [STREAM_NAME_HEADER_KEY];
4748

4849
pub async fn flatten_and_push_logs(
4950
json: Value,
@@ -146,7 +147,7 @@ async fn push_logs(
146147
pub fn get_custom_fields_from_header(req: HttpRequest) -> HashMap<String, String> {
147148
let user_agent = req
148149
.headers()
149-
.get("User-Agent")
150+
.get(USER_AGENT)
150151
.and_then(|a| a.to_str().ok())
151152
.unwrap_or_default();
152153

@@ -166,6 +167,58 @@ pub fn get_custom_fields_from_header(req: HttpRequest) -> HashMap<String, String
166167
p_custom_fields.insert(key.to_string(), value.to_string());
167168
}
168169
}
170+
171+
if header_name == LOG_SOURCE_KEY {
172+
if let Ok(value) = header_value.to_str() {
173+
p_custom_fields.insert(FORMAT_KEY.to_string(), value.to_string());
174+
}
175+
}
169176
}
177+
170178
p_custom_fields
171179
}
180+
181+
#[cfg(test)]
182+
mod tests {
183+
use super::*;
184+
use actix_web::test::TestRequest;
185+
186+
#[test]
187+
fn test_get_custom_fields_from_header_with_custom_fields() {
188+
let req = TestRequest::default()
189+
.insert_header((USER_AGENT, "TestUserAgent"))
190+
.insert_header(("x-p-environment", "dev"))
191+
.to_http_request();
192+
193+
let custom_fields = get_custom_fields_from_header(req);
194+
195+
assert_eq!(custom_fields.get(USER_AGENT_KEY).unwrap(), "TestUserAgent");
196+
assert_eq!(custom_fields.get("environment").unwrap(), "dev");
197+
}
198+
199+
#[test]
200+
fn test_get_custom_fields_from_header_with_ignored_headers() {
201+
let req = TestRequest::default()
202+
.insert_header((USER_AGENT, "TestUserAgent"))
203+
.insert_header((STREAM_NAME_HEADER_KEY, "teststream"))
204+
.to_http_request();
205+
206+
let custom_fields = get_custom_fields_from_header(req);
207+
208+
assert_eq!(custom_fields.get(USER_AGENT_KEY).unwrap(), "TestUserAgent");
209+
assert!(!custom_fields.contains_key(STREAM_NAME_HEADER_KEY));
210+
}
211+
212+
#[test]
213+
fn test_get_custom_fields_from_header_with_format_key() {
214+
let req = TestRequest::default()
215+
.insert_header((USER_AGENT, "TestUserAgent"))
216+
.insert_header((LOG_SOURCE_KEY, "otel-logs"))
217+
.to_http_request();
218+
219+
let custom_fields = get_custom_fields_from_header(req);
220+
221+
assert_eq!(custom_fields.get(USER_AGENT_KEY).unwrap(), "TestUserAgent");
222+
assert_eq!(custom_fields.get(FORMAT_KEY).unwrap(), "otel-logs");
223+
}
224+
}

0 commit comments

Comments
 (0)