1use std::fmt;
2use std::sync::Arc;
3
4use regex::{Regex, RegexBuilder};
5use serde::{Deserialize, Serialize};
6use shard_stream_core::{LogicalOffset, ShardId, TopicPartition};
7
8use crate::{
9 CompressionCohortId, ResourceContext, ScopeContext, SpanId, TelemetryAttribute, TelemetryError,
10 TelemetryResult, TelemetrySignal, TelemetryValue, TraceId,
11};
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
15pub struct TelemetryRecordRef {
16 pub signal: TelemetrySignal,
18 pub topic_partition: TopicPartition,
20 pub offset: LogicalOffset,
22}
23
24impl TelemetryRecordRef {
25 #[must_use]
27 pub const fn new(topic_partition: TopicPartition, offset: LogicalOffset) -> Self {
28 Self {
29 signal: TelemetrySignal::Logs,
30 topic_partition,
31 offset,
32 }
33 }
34
35 #[must_use]
37 pub const fn for_signal(
38 signal: TelemetrySignal,
39 topic_partition: TopicPartition,
40 offset: LogicalOffset,
41 ) -> Self {
42 Self {
43 signal,
44 topic_partition,
45 offset,
46 }
47 }
48}
49
50#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
52pub struct MetadataField {
53 pub key: Arc<str>,
55 pub value: Arc<str>,
57}
58
59impl MetadataField {
60 #[must_use]
62 pub fn new(key: impl Into<Arc<str>>, value: impl Into<Arc<str>>) -> Self {
63 Self {
64 key: key.into(),
65 value: value.into(),
66 }
67 }
68}
69
70#[derive(Debug, Clone, PartialEq, Eq)]
72pub struct DurableLog {
73 pub stream_shard_id: ShardId,
75 pub record_ref: TelemetryRecordRef,
77 pub timestamp_unix_nanos: u64,
79 pub observed_timestamp_unix_nanos: u64,
81 pub body: Option<TelemetryValue>,
83 pub message: Arc<str>,
85 pub fields: Arc<Vec<MetadataField>>,
87 pub attributes: Arc<Vec<TelemetryAttribute>>,
89 pub resource: Arc<ResourceContext>,
91 pub scope: Arc<ScopeContext>,
93 pub severity_number: i32,
95 pub severity_text: Arc<str>,
97 pub dropped_attributes_count: u32,
99 pub flags: u32,
101 pub trace_id: Option<TraceId>,
103 pub span_id: Option<SpanId>,
105 pub event_name: Arc<str>,
107 pub compression_cohort: CompressionCohortId,
109}
110
111impl DurableLog {
112 #[must_use]
114 pub fn resource_id(&self) -> crate::ResourceContextId {
115 self.resource.id()
116 }
117
118 #[must_use]
120 pub fn scope_id(&self) -> crate::ScopeContextId {
121 self.scope.id()
122 }
123
124 #[must_use]
126 pub fn new(
127 stream_shard_id: ShardId,
128 topic_partition: TopicPartition,
129 offset: LogicalOffset,
130 timestamp_unix_nanos: u64,
131 message: impl Into<Arc<str>>,
132 compression_cohort: CompressionCohortId,
133 ) -> Self {
134 Self {
135 stream_shard_id,
136 record_ref: TelemetryRecordRef::new(topic_partition, offset),
137 timestamp_unix_nanos,
138 observed_timestamp_unix_nanos: 0,
139 body: None,
140 message: message.into(),
141 fields: Arc::new(Vec::new()),
142 attributes: Arc::new(Vec::new()),
143 resource: Arc::new(ResourceContext::default()),
144 scope: Arc::new(ScopeContext::default()),
145 severity_number: 0,
146 severity_text: Arc::from(""),
147 dropped_attributes_count: 0,
148 flags: 0,
149 trace_id: None,
150 span_id: None,
151 event_name: Arc::from(""),
152 compression_cohort,
153 }
154 }
155
156 #[must_use]
158 pub fn with_field(mut self, key: impl Into<Arc<str>>, value: impl Into<Arc<str>>) -> Self {
159 Arc::make_mut(&mut self.fields).push(MetadataField::new(key, value));
160 self
161 }
162
163 #[must_use]
165 pub fn with_attribute(mut self, attribute: TelemetryAttribute) -> Self {
166 Arc::make_mut(&mut self.attributes).push(attribute);
167 self
168 }
169}
170
171#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
173pub enum QueryOrder {
174 #[default]
176 OldestFirst,
177 NewestFirst,
179}
180
181#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
183pub enum CaseSensitivity {
184 Sensitive,
186 #[default]
188 Insensitive,
189}
190
191#[derive(Debug, Clone, Copy, PartialEq, Eq)]
193pub enum TextMatchKind {
194 Exact,
196 Contains,
198 Prefix,
200 Suffix,
202}
203
204#[derive(Debug, Clone, PartialEq, Eq)]
206pub struct TextMatcher {
207 pub value: Arc<str>,
209 pub kind: TextMatchKind,
211 pub case_sensitivity: CaseSensitivity,
213}
214
215impl TextMatcher {
216 #[must_use]
218 pub fn new(
219 value: impl Into<Arc<str>>,
220 kind: TextMatchKind,
221 case_sensitivity: CaseSensitivity,
222 ) -> Self {
223 Self {
224 value: value.into(),
225 kind,
226 case_sensitivity,
227 }
228 }
229}
230
231#[derive(Clone)]
233pub struct LogRegex {
234 pattern: Arc<str>,
235 case_sensitivity: CaseSensitivity,
236 compiled: Arc<Regex>,
237}
238
239impl LogRegex {
240 pub fn new(
242 pattern: impl Into<Arc<str>>,
243 case_sensitivity: CaseSensitivity,
244 ) -> TelemetryResult<Self> {
245 let pattern = pattern.into();
246 let compiled = RegexBuilder::new(&pattern)
247 .case_insensitive(case_sensitivity == CaseSensitivity::Insensitive)
248 .build()
249 .map_err(|error| TelemetryError::InvalidQuery(error.to_string()))?;
250 Ok(Self {
251 pattern,
252 case_sensitivity,
253 compiled: Arc::new(compiled),
254 })
255 }
256
257 #[must_use]
259 pub fn pattern(&self) -> &str {
260 &self.pattern
261 }
262
263 #[must_use]
265 pub const fn case_sensitivity(&self) -> CaseSensitivity {
266 self.case_sensitivity
267 }
268
269 pub(crate) fn is_match(&self, observed: &str) -> bool {
270 self.compiled.is_match(observed)
271 }
272}
273
274impl fmt::Debug for LogRegex {
275 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
276 formatter
277 .debug_struct("LogRegex")
278 .field("pattern", &self.pattern)
279 .field("case_sensitivity", &self.case_sensitivity)
280 .finish_non_exhaustive()
281 }
282}
283
284impl PartialEq for LogRegex {
285 fn eq(&self, other: &Self) -> bool {
286 self.pattern == other.pattern && self.case_sensitivity == other.case_sensitivity
287 }
288}
289
290impl Eq for LogRegex {}
291
292#[derive(Debug, Clone, Copy, PartialEq, Eq)]
294pub enum NumericComparison {
295 Equal,
297 NotEqual,
299 LessThan,
301 LessThanOrEqual,
303 GreaterThan,
305 GreaterThanOrEqual,
307}
308
309#[derive(Debug, Clone, Default, PartialEq, Eq)]
311pub enum LogPredicate {
312 #[default]
314 MatchAll,
315 MatchNone,
317 Term(Arc<str>),
319 MessageToken {
322 value: Arc<str>,
324 case_sensitivity: CaseSensitivity,
326 },
327 Message(TextMatcher),
329 MessageRegex(LogRegex),
331 FieldExists(Arc<str>),
333 Field {
335 key: Arc<str>,
337 matcher: TextMatcher,
339 },
340 FieldIn {
342 key: Arc<str>,
344 values: Vec<Arc<str>>,
346 },
347 FieldRegex {
349 key: Arc<str>,
351 regex: LogRegex,
353 },
354 FieldNumeric {
356 key: Arc<str>,
358 comparison: NumericComparison,
360 value: i128,
362 },
363 And(Vec<LogPredicate>),
365 Or(Vec<LogPredicate>),
367 Not(Box<LogPredicate>),
369}
370
371impl LogPredicate {
372 #[must_use]
374 pub fn term(term: impl Into<Arc<str>>) -> Self {
375 Self::Term(term.into())
376 }
377
378 #[must_use]
381 pub fn message_token(value: impl Into<Arc<str>>, case_sensitivity: CaseSensitivity) -> Self {
382 Self::MessageToken {
383 value: value.into(),
384 case_sensitivity,
385 }
386 }
387
388 #[must_use]
390 pub fn message(matcher: TextMatcher) -> Self {
391 Self::Message(matcher)
392 }
393
394 #[must_use]
396 pub fn message_contains(value: impl Into<Arc<str>>) -> Self {
397 Self::Message(TextMatcher::new(
398 value,
399 TextMatchKind::Contains,
400 CaseSensitivity::Insensitive,
401 ))
402 }
403
404 pub fn message_regex(
406 pattern: impl Into<Arc<str>>,
407 case_sensitivity: CaseSensitivity,
408 ) -> TelemetryResult<Self> {
409 Ok(Self::MessageRegex(LogRegex::new(
410 pattern,
411 case_sensitivity,
412 )?))
413 }
414
415 #[must_use]
417 pub fn field_exists(key: impl Into<Arc<str>>) -> Self {
418 Self::FieldExists(key.into())
419 }
420
421 #[must_use]
423 pub fn field_equals(key: impl Into<Arc<str>>, value: impl Into<Arc<str>>) -> Self {
424 Self::Field {
425 key: key.into(),
426 matcher: TextMatcher::new(value, TextMatchKind::Exact, CaseSensitivity::Sensitive),
427 }
428 }
429
430 #[must_use]
432 pub fn field(key: impl Into<Arc<str>>, matcher: TextMatcher) -> Self {
433 Self::Field {
434 key: key.into(),
435 matcher,
436 }
437 }
438
439 #[must_use]
441 pub fn field_in(
442 key: impl Into<Arc<str>>,
443 values: impl IntoIterator<Item = impl Into<Arc<str>>>,
444 ) -> Self {
445 Self::FieldIn {
446 key: key.into(),
447 values: values.into_iter().map(Into::into).collect(),
448 }
449 }
450
451 pub fn field_regex(
453 key: impl Into<Arc<str>>,
454 pattern: impl Into<Arc<str>>,
455 case_sensitivity: CaseSensitivity,
456 ) -> TelemetryResult<Self> {
457 Ok(Self::FieldRegex {
458 key: key.into(),
459 regex: LogRegex::new(pattern, case_sensitivity)?,
460 })
461 }
462
463 #[must_use]
465 pub fn field_numeric(
466 key: impl Into<Arc<str>>,
467 comparison: NumericComparison,
468 value: i128,
469 ) -> Self {
470 Self::FieldNumeric {
471 key: key.into(),
472 comparison,
473 value,
474 }
475 }
476
477 #[must_use]
479 pub fn and(predicates: Vec<Self>) -> Self {
480 if predicates.is_empty() {
481 Self::MatchAll
482 } else {
483 Self::And(predicates)
484 }
485 }
486
487 #[must_use]
489 pub fn or(predicates: Vec<Self>) -> Self {
490 if predicates.is_empty() {
491 Self::MatchNone
492 } else {
493 Self::Or(predicates)
494 }
495 }
496
497 #[must_use]
499 pub fn negate(predicate: Self) -> Self {
500 Self::Not(Box::new(predicate))
501 }
502}
503
504#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
506pub enum QuerySort {
507 #[default]
509 Offset,
510 Timestamp,
512}
513
514#[derive(Debug, Clone, Copy, PartialEq, Eq)]
516pub struct QueryCursor {
517 pub timestamp_unix_nanos: u64,
519 pub offset: LogicalOffset,
521}
522
523impl QueryCursor {
524 #[must_use]
526 pub const fn new(timestamp_unix_nanos: u64, offset: LogicalOffset) -> Self {
527 Self {
528 timestamp_unix_nanos,
529 offset,
530 }
531 }
532}
533
534#[derive(Debug, Clone, PartialEq, Eq)]
536pub struct LogQuery {
537 pub topic_partition: TopicPartition,
539 pub terms: Vec<Arc<str>>,
541 pub exact_fields: Vec<MetadataField>,
543 pub predicate: LogPredicate,
545 pub start_offset: Option<LogicalOffset>,
547 pub end_offset: Option<LogicalOffset>,
549 pub start_timestamp_unix_nanos: Option<u64>,
551 pub end_timestamp_unix_nanos: Option<u64>,
553 pub limit: Option<usize>,
555 pub order: QueryOrder,
557 pub sort: QuerySort,
559 pub after: Option<QueryCursor>,
561}
562
563impl LogQuery {
564 #[must_use]
566 pub fn new(topic_partition: TopicPartition) -> Self {
567 Self {
568 topic_partition,
569 terms: Vec::new(),
570 exact_fields: Vec::new(),
571 predicate: LogPredicate::MatchAll,
572 start_offset: None,
573 end_offset: None,
574 start_timestamp_unix_nanos: None,
575 end_timestamp_unix_nanos: None,
576 limit: None,
577 order: QueryOrder::OldestFirst,
578 sort: QuerySort::Offset,
579 after: None,
580 }
581 }
582
583 #[must_use]
585 pub fn with_term(mut self, term: impl Into<Arc<str>>) -> Self {
586 self.terms.push(term.into());
587 self
588 }
589
590 #[must_use]
592 pub fn with_field(mut self, key: impl Into<Arc<str>>, value: impl Into<Arc<str>>) -> Self {
593 self.exact_fields.push(MetadataField::new(key, value));
594 self
595 }
596
597 #[must_use]
599 pub fn with_predicate(mut self, predicate: LogPredicate) -> Self {
600 self.predicate = match self.predicate {
601 LogPredicate::MatchAll => predicate,
602 current => LogPredicate::And(vec![current, predicate]),
603 };
604 self
605 }
606
607 #[must_use]
609 pub fn where_predicate(mut self, predicate: LogPredicate) -> Self {
610 self.predicate = predicate;
611 self
612 }
613
614 #[must_use]
616 pub fn with_offset_range(mut self, start: LogicalOffset, end: LogicalOffset) -> Self {
617 self.start_offset = Some(start);
618 self.end_offset = Some(end);
619 self
620 }
621
622 #[must_use]
624 pub fn with_timestamp_range(mut self, start: u64, end: u64) -> Self {
625 self.start_timestamp_unix_nanos = Some(start);
626 self.end_timestamp_unix_nanos = Some(end);
627 self
628 }
629
630 #[must_use]
632 pub fn with_limit(mut self, limit: usize) -> Self {
633 self.limit = Some(limit);
634 self
635 }
636
637 #[must_use]
639 pub fn newest_first(mut self) -> Self {
640 self.order = QueryOrder::NewestFirst;
641 self
642 }
643
644 #[must_use]
646 pub fn oldest_first(mut self) -> Self {
647 self.order = QueryOrder::OldestFirst;
648 self
649 }
650
651 #[must_use]
653 pub fn sort_by_timestamp(mut self) -> Self {
654 self.sort = QuerySort::Timestamp;
655 self
656 }
657
658 #[must_use]
660 pub fn sort_by_offset(mut self) -> Self {
661 self.sort = QuerySort::Offset;
662 self
663 }
664
665 #[must_use]
667 pub fn after(mut self, cursor: QueryCursor) -> Self {
668 self.after = Some(cursor);
669 self
670 }
671}
672
673#[derive(Debug, Clone, PartialEq, Eq)]
675pub struct LogMatch {
676 pub record: DurableLog,
678}