@@ -72,17 +72,32 @@ pub async fn ingest(req: HttpRequest, Json(json): Json<Value>) -> Result<HttpRes
72
72
return Err ( PostError :: OtelNotSupported ) ;
73
73
}
74
74
75
- let p_custom_fields = get_custom_fields_from_header ( req) ;
76
-
77
75
let log_source_entry = LogSourceEntry :: new ( log_source. clone ( ) , HashSet :: new ( ) ) ;
78
76
PARSEABLE
79
77
. create_stream_if_not_exists (
80
78
& stream_name,
81
79
StreamType :: UserDefined ,
82
- vec ! [ log_source_entry] ,
80
+ vec ! [ log_source_entry. clone ( ) ] ,
83
81
)
84
82
. await ?;
85
83
84
+ //if stream exists, fetch the stream log source
85
+ //return error if the stream log source is otel traces or otel metrics
86
+ if let Ok ( stream) = PARSEABLE . get_stream ( & stream_name) {
87
+ stream
88
+ . get_log_source ( )
89
+ . iter ( )
90
+ . find ( |& stream_log_source_entry| {
91
+ stream_log_source_entry. log_source_format != LogSource :: OtelTraces
92
+ && stream_log_source_entry. log_source_format != LogSource :: OtelMetrics
93
+ } )
94
+ . ok_or ( PostError :: IncorrectLogFormat ( stream_name. clone ( ) ) ) ?;
95
+ }
96
+
97
+ PARSEABLE
98
+ . add_update_log_source ( & stream_name, log_source_entry)
99
+ . await ?;
100
+ let p_custom_fields = get_custom_fields_from_header ( req) ;
86
101
flatten_and_push_logs ( json, & stream_name, & log_source, & p_custom_fields) . await ?;
87
102
88
103
Ok ( HttpResponse :: Ok ( ) . finish ( ) )
@@ -143,9 +158,27 @@ pub async fn handle_otel_logs_ingestion(
143
158
. create_stream_if_not_exists (
144
159
& stream_name,
145
160
StreamType :: UserDefined ,
146
- vec ! [ log_source_entry] ,
161
+ vec ! [ log_source_entry. clone ( ) ] ,
147
162
)
148
163
. await ?;
164
+
165
+ //if stream exists, fetch the stream log source
166
+ //return error if the stream log source is otel traces or otel metrics
167
+ if let Ok ( stream) = PARSEABLE . get_stream ( & stream_name) {
168
+ stream
169
+ . get_log_source ( )
170
+ . iter ( )
171
+ . find ( |& stream_log_source_entry| {
172
+ stream_log_source_entry. log_source_format != LogSource :: OtelTraces
173
+ && stream_log_source_entry. log_source_format != LogSource :: OtelMetrics
174
+ } )
175
+ . ok_or ( PostError :: IncorrectLogFormat ( stream_name. clone ( ) ) ) ?;
176
+ }
177
+
178
+ PARSEABLE
179
+ . add_update_log_source ( & stream_name, log_source_entry)
180
+ . await ?;
181
+
149
182
let p_custom_fields = get_custom_fields_from_header ( req) ;
150
183
151
184
flatten_and_push_logs ( json, & stream_name, & log_source, & p_custom_fields) . await ?;
@@ -172,6 +205,7 @@ pub async fn handle_otel_metrics_ingestion(
172
205
}
173
206
174
207
let stream_name = stream_name. to_str ( ) . unwrap ( ) . to_owned ( ) ;
208
+
175
209
let log_source_entry = LogSourceEntry :: new (
176
210
log_source. clone ( ) ,
177
211
OTEL_METRICS_KNOWN_FIELD_LIST
@@ -183,10 +217,26 @@ pub async fn handle_otel_metrics_ingestion(
183
217
. create_stream_if_not_exists (
184
218
& stream_name,
185
219
StreamType :: UserDefined ,
186
- vec ! [ log_source_entry] ,
220
+ vec ! [ log_source_entry. clone ( ) ] ,
187
221
)
188
222
. await ?;
189
223
224
+ //if stream exists, fetch the stream log source
225
+ //return error if the stream log source is not otel metrics
226
+ if let Ok ( stream) = PARSEABLE . get_stream ( & stream_name) {
227
+ stream
228
+ . get_log_source ( )
229
+ . iter ( )
230
+ . find ( |& stream_log_source_entry| {
231
+ stream_log_source_entry. log_source_format == log_source. clone ( )
232
+ } )
233
+ . ok_or ( PostError :: IncorrectLogFormat ( stream_name. clone ( ) ) ) ?;
234
+ }
235
+
236
+ PARSEABLE
237
+ . add_update_log_source ( & stream_name, log_source_entry)
238
+ . await ?;
239
+
190
240
let p_custom_fields = get_custom_fields_from_header ( req) ;
191
241
192
242
flatten_and_push_logs ( json, & stream_name, & log_source, & p_custom_fields) . await ?;
@@ -213,6 +263,7 @@ pub async fn handle_otel_traces_ingestion(
213
263
return Err ( PostError :: IncorrectLogSource ( LogSource :: OtelTraces ) ) ;
214
264
}
215
265
let stream_name = stream_name. to_str ( ) . unwrap ( ) . to_owned ( ) ;
266
+
216
267
let log_source_entry = LogSourceEntry :: new (
217
268
log_source. clone ( ) ,
218
269
OTEL_TRACES_KNOWN_FIELD_LIST
@@ -225,10 +276,26 @@ pub async fn handle_otel_traces_ingestion(
225
276
. create_stream_if_not_exists (
226
277
& stream_name,
227
278
StreamType :: UserDefined ,
228
- vec ! [ log_source_entry] ,
279
+ vec ! [ log_source_entry. clone ( ) ] ,
229
280
)
230
281
. await ?;
231
282
283
+ //if stream exists, fetch the stream log source
284
+ //return error if the stream log source is not otel traces
285
+ if let Ok ( stream) = PARSEABLE . get_stream ( & stream_name) {
286
+ stream
287
+ . get_log_source ( )
288
+ . iter ( )
289
+ . find ( |& stream_log_source_entry| {
290
+ stream_log_source_entry. log_source_format == log_source. clone ( )
291
+ } )
292
+ . ok_or ( PostError :: IncorrectLogFormat ( stream_name. clone ( ) ) ) ?;
293
+ }
294
+
295
+ PARSEABLE
296
+ . add_update_log_source ( & stream_name, log_source_entry)
297
+ . await ?;
298
+
232
299
let p_custom_fields = get_custom_fields_from_header ( req) ;
233
300
234
301
flatten_and_push_logs ( json, & stream_name, & log_source, & p_custom_fields) . await ?;
@@ -246,6 +313,12 @@ pub async fn post_event(
246
313
) -> Result < HttpResponse , PostError > {
247
314
let stream_name = stream_name. into_inner ( ) ;
248
315
316
+ let log_source = req
317
+ . headers ( )
318
+ . get ( LOG_SOURCE_KEY )
319
+ . and_then ( |h| h. to_str ( ) . ok ( ) )
320
+ . map_or ( LogSource :: default ( ) , LogSource :: from) ;
321
+
249
322
let internal_stream_names = PARSEABLE . streams . list_internal_streams ( ) ;
250
323
if internal_stream_names. contains ( & stream_name) {
251
324
return Err ( PostError :: InternalStream ( stream_name) ) ;
@@ -267,19 +340,25 @@ pub async fn post_event(
267
340
}
268
341
}
269
342
270
- let log_source = req
271
- . headers ( )
272
- . get ( LOG_SOURCE_KEY )
273
- . and_then ( |h| h. to_str ( ) . ok ( ) )
274
- . map_or ( LogSource :: default ( ) , LogSource :: from) ;
275
-
276
343
if matches ! (
277
344
log_source,
278
345
LogSource :: OtelLogs | LogSource :: OtelMetrics | LogSource :: OtelTraces
279
346
) {
280
347
return Err ( PostError :: OtelNotSupported ) ;
281
348
}
282
349
350
+ //if stream exists, fetch the stream log source
351
+ //return error if the stream log source is otel traces or otel metrics
352
+ if let Ok ( stream) = PARSEABLE . get_stream ( & stream_name) {
353
+ stream
354
+ . get_log_source ( )
355
+ . iter ( )
356
+ . find ( |& stream_log_source_entry| {
357
+ stream_log_source_entry. log_source_format != LogSource :: OtelTraces
358
+ && stream_log_source_entry. log_source_format != LogSource :: OtelMetrics
359
+ } )
360
+ . ok_or ( PostError :: IncorrectLogFormat ( stream_name. clone ( ) ) ) ?;
361
+ }
283
362
let p_custom_fields = get_custom_fields_from_header ( req) ;
284
363
flatten_and_push_logs ( json, & stream_name, & log_source, & p_custom_fields) . await ?;
285
364
@@ -347,6 +426,8 @@ pub enum PostError {
347
426
IngestionNotAllowed ,
348
427
#[ error( "Missing field for time partition in json: {0}" ) ]
349
428
MissingTimePartition ( String ) ,
429
+ #[ error( "Ingestion is not allowed to stream {0} as it is already associated with a different OTEL format" ) ]
430
+ IncorrectLogFormat ( String ) ,
350
431
}
351
432
352
433
impl actix_web:: ResponseError for PostError {
@@ -373,6 +454,7 @@ impl actix_web::ResponseError for PostError {
373
454
PostError :: IncorrectLogSource ( _) => StatusCode :: BAD_REQUEST ,
374
455
PostError :: IngestionNotAllowed => StatusCode :: BAD_REQUEST ,
375
456
PostError :: MissingTimePartition ( _) => StatusCode :: BAD_REQUEST ,
457
+ PostError :: IncorrectLogFormat ( _) => StatusCode :: BAD_REQUEST ,
376
458
}
377
459
}
378
460
0 commit comments