@@ -41,6 +41,10 @@ use arrow_schema::Schema;
41
41
use bytes:: Bytes ;
42
42
use chrono:: Utc ;
43
43
use http:: StatusCode ;
44
+ use nom:: AsBytes ;
45
+ use opentelemetry_proto:: tonic:: logs:: v1:: LogsData ;
46
+ use opentelemetry_proto:: tonic:: metrics:: v1:: MetricsData ;
47
+ use opentelemetry_proto:: tonic:: trace:: v1:: TracesData ;
44
48
use serde_json:: Value ;
45
49
use std:: collections:: HashMap ;
46
50
use std:: sync:: Arc ;
@@ -112,23 +116,20 @@ pub async fn handle_otel_logs_ingestion(
112
116
req : HttpRequest ,
113
117
body : Bytes ,
114
118
) -> Result < HttpResponse , PostError > {
115
- if let Some ( ( _, stream_name) ) = req
116
- . headers ( )
117
- . iter ( )
118
- . find ( |& ( key, _) | key == STREAM_NAME_HEADER_KEY )
119
- {
120
- let stream_name = stream_name. to_str ( ) . unwrap ( ) . to_owned ( ) ;
121
- create_stream_if_not_exists ( & stream_name, & StreamType :: UserDefined . to_string ( ) ) . await ?;
122
-
123
- //custom flattening required for otel logs
124
- let mut json = flatten_otel_logs ( & body) ;
125
- for record in json. iter_mut ( ) {
126
- let body: Bytes = serde_json:: to_vec ( record) . unwrap ( ) . into ( ) ;
127
- push_logs ( & stream_name, & req, & body) . await ?;
128
- }
129
- } else {
119
+ let Some ( stream_name) = req. headers ( ) . get ( STREAM_NAME_HEADER_KEY ) else {
130
120
return Err ( PostError :: Header ( ParseHeaderError :: MissingStreamName ) ) ;
121
+ } ;
122
+ let stream_name = stream_name. to_str ( ) . unwrap ( ) . to_owned ( ) ;
123
+ create_stream_if_not_exists ( & stream_name, & StreamType :: UserDefined . to_string ( ) ) . await ?;
124
+
125
+ //custom flattening required for otel logs
126
+ let logs: LogsData = serde_json:: from_slice ( body. as_bytes ( ) ) ?;
127
+ let mut json = flatten_otel_logs ( & logs) ;
128
+ for record in json. iter_mut ( ) {
129
+ let body: Bytes = serde_json:: to_vec ( record) . unwrap ( ) . into ( ) ;
130
+ push_logs ( & stream_name, & req, & body) . await ?;
131
131
}
132
+
132
133
Ok ( HttpResponse :: Ok ( ) . finish ( ) )
133
134
}
134
135
@@ -139,23 +140,20 @@ pub async fn handle_otel_metrics_ingestion(
139
140
req : HttpRequest ,
140
141
body : Bytes ,
141
142
) -> Result < HttpResponse , PostError > {
142
- if let Some ( ( _, stream_name) ) = req
143
- . headers ( )
144
- . iter ( )
145
- . find ( |& ( key, _) | key == STREAM_NAME_HEADER_KEY )
146
- {
147
- let stream_name = stream_name. to_str ( ) . unwrap ( ) . to_owned ( ) ;
148
- create_stream_if_not_exists ( & stream_name, & StreamType :: UserDefined . to_string ( ) ) . await ?;
149
-
150
- //custom flattening required for otel metrics
151
- let mut json = flatten_otel_metrics ( & body) ;
152
- for record in json. iter_mut ( ) {
153
- let body: Bytes = serde_json:: to_vec ( record) . unwrap ( ) . into ( ) ;
154
- push_logs ( & stream_name, & req, & body) . await ?;
155
- }
156
- } else {
143
+ let Some ( stream_name) = req. headers ( ) . get ( STREAM_NAME_HEADER_KEY ) else {
157
144
return Err ( PostError :: Header ( ParseHeaderError :: MissingStreamName ) ) ;
145
+ } ;
146
+ let stream_name = stream_name. to_str ( ) . unwrap ( ) . to_owned ( ) ;
147
+ create_stream_if_not_exists ( & stream_name, & StreamType :: UserDefined . to_string ( ) ) . await ?;
148
+
149
+ //custom flattening required for otel metrics
150
+ let metrics: MetricsData = serde_json:: from_slice ( body. as_bytes ( ) ) ?;
151
+ let mut json = flatten_otel_metrics ( metrics) ;
152
+ for record in json. iter_mut ( ) {
153
+ let body: Bytes = serde_json:: to_vec ( record) . unwrap ( ) . into ( ) ;
154
+ push_logs ( & stream_name, & req, & body) . await ?;
158
155
}
156
+
159
157
Ok ( HttpResponse :: Ok ( ) . finish ( ) )
160
158
}
161
159
@@ -166,23 +164,20 @@ pub async fn handle_otel_traces_ingestion(
166
164
req : HttpRequest ,
167
165
body : Bytes ,
168
166
) -> Result < HttpResponse , PostError > {
169
- if let Some ( ( _, stream_name) ) = req
170
- . headers ( )
171
- . iter ( )
172
- . find ( |& ( key, _) | key == STREAM_NAME_HEADER_KEY )
173
- {
174
- let stream_name = stream_name. to_str ( ) . unwrap ( ) . to_owned ( ) ;
175
- create_stream_if_not_exists ( & stream_name, & StreamType :: UserDefined . to_string ( ) ) . await ?;
176
-
177
- //custom flattening required for otel traces
178
- let mut json = flatten_otel_traces ( & body) ;
179
- for record in json. iter_mut ( ) {
180
- let body: Bytes = serde_json:: to_vec ( record) . unwrap ( ) . into ( ) ;
181
- push_logs ( & stream_name, & req, & body) . await ?;
182
- }
183
- } else {
167
+ let Some ( stream_name) = req. headers ( ) . get ( STREAM_NAME_HEADER_KEY ) else {
184
168
return Err ( PostError :: Header ( ParseHeaderError :: MissingStreamName ) ) ;
169
+ } ;
170
+ let stream_name = stream_name. to_str ( ) . unwrap ( ) . to_owned ( ) ;
171
+ create_stream_if_not_exists ( & stream_name, & StreamType :: UserDefined . to_string ( ) ) . await ?;
172
+
173
+ //custom flattening required for otel traces
174
+ let traces: TracesData = serde_json:: from_slice ( body. as_bytes ( ) ) ?;
175
+ let mut json = flatten_otel_traces ( & traces) ;
176
+ for record in json. iter_mut ( ) {
177
+ let body: Bytes = serde_json:: to_vec ( record) . unwrap ( ) . into ( ) ;
178
+ push_logs ( & stream_name, & req, & body) . await ?;
185
179
}
180
+
186
181
Ok ( HttpResponse :: Ok ( ) . finish ( ) )
187
182
}
188
183
0 commit comments