@@ -29,11 +29,12 @@ use serde_json::Value;
2929use crate :: event:: error:: EventError ;
3030use crate :: event:: format:: EventFormat ;
3131use crate :: event:: { self , format} ;
32- use crate :: handlers:: { PREFIX_META , PREFIX_TAGS , SEPARATOR , STREAM_NAME_HEADER_KEY } ;
32+ use crate :: handlers:: { PREFIX_META , PREFIX_TAGS , SEPARATOR , STREAM_NAME_HEADER_KEY , LOG_SOURCE_KEY } ;
3333use crate :: metadata:: STREAM_INFO ;
3434use crate :: utils:: header_parsing:: { collect_labelled_headers, ParseHeaderError } ;
3535
3636use super :: logstream:: error:: CreateStreamError ;
37+ use super :: kinesis;
3738
3839// Handler for POST /api/v1/ingest
3940// ingests events by extracting stream name from header
@@ -46,13 +47,35 @@ pub async fn ingest(req: HttpRequest, body: Bytes) -> Result<HttpResponse, PostE
4647 {
4748 let stream_name = stream_name. to_str ( ) . unwrap ( ) . to_owned ( ) ;
4849 create_stream_if_not_exists ( & stream_name) . await ?;
50+
51+ //section to flatten ingested log data
52+ let cloned_req = req. clone ( ) ;
53+ flatten_logs ( cloned_req, & body) ;
54+ //section ends
4955 push_logs ( stream_name, req, body) . await ?;
5056 Ok ( HttpResponse :: Ok ( ) . finish ( ) )
5157 } else {
5258 Err ( PostError :: Header ( ParseHeaderError :: MissingStreamName ) )
5359 }
5460}
5561
62+ fn flatten_logs ( req : HttpRequest , body : & Bytes ) {
63+ //flatten logs
64+ if let Some ( ( _, log_source) ) = req
65+ . headers ( )
66+ . iter ( )
67+ . find ( |& ( key, _) | key == LOG_SOURCE_KEY )
68+ {
69+ let log_source: String = log_source. to_str ( ) . unwrap ( ) . to_owned ( ) ;
70+ //println!("log source: {}", log_source);
71+ match log_source. as_str ( ) {
72+ "kinesis_firehose" => kinesis:: flatten_kinesis_logs ( body) ,
73+ _ => { } //do nothing so far
74+ } ;
75+ }
76+ }
77+
78+
5679// Handler for POST /api/v1/logstream/{logstream}
5780// only ingests events into the specified logstream
5881// fails if the logstream does not exist
@@ -70,6 +93,7 @@ async fn push_logs(stream_name: String, req: HttpRequest, body: Bytes) -> Result
7093 . ok_or ( PostError :: StreamNotFound ( stream_name. clone ( ) ) ) ?
7194 . schema
7295 . clone ( ) ;
96+
7397 into_event_batch ( req, body, schema) ?
7498 } ;
7599
@@ -94,12 +118,15 @@ fn into_event_batch(
94118 let tags = collect_labelled_headers ( & req, PREFIX_TAGS , SEPARATOR ) ?;
95119 let metadata = collect_labelled_headers ( & req, PREFIX_META , SEPARATOR ) ?;
96120 let size = body. len ( ) ;
121+ println ! ( "{:?}" , body) ;
97122 let body: Value = serde_json:: from_slice ( & body) ?;
98123 let event = format:: json:: Event {
99124 data : body,
100125 tags,
101126 metadata,
102127 } ;
128+
129+ println ! ( "{:?}" , event) ;
103130 let ( rb, is_first) = event. into_recordbatch ( schema) ?;
104131 Ok ( ( size, rb, is_first) )
105132}
0 commit comments