Skip to content

Commit

Permalink
Add on_event
Browse files Browse the repository at this point in the history
  • Loading branch information
aqrln committed Nov 20, 2024
1 parent 4606774 commit cb8bd08
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
38 changes: 37 additions & 1 deletion libs/telemetry/src/capturing/ng/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,47 @@ impl SpanBuilder {
}

#[derive(Debug, Clone)]
pub(crate) struct CollectedEvent {
pub struct CollectedEvent {
name: &'static str,
level: Level,
timestamp: Instant,
attributes: HashMap<&'static str, serde_json::Value>,
}

pub(crate) struct EventBuilder {
name: &'static str,
level: Level,
timestamp: Instant,
attributes: HashMap<&'static str, serde_json::Value>,
}

impl EventBuilder {
pub fn new(name: &'static str, level: Level, timestamp: Instant, attrs_size_hint: usize) -> Self {
Self {
name,
level,
timestamp,
attributes: HashMap::with_capacity(attrs_size_hint),
}
}

pub fn insert_attribute(&mut self, key: &'static str, value: serde_json::Value) {
self.attributes.insert(key, value);
}

pub fn build(self) -> CollectedEvent {
CollectedEvent {
name: self.name,
level: self.level,
timestamp: self.timestamp,
attributes: self.attributes,
}
}
}

pub trait Collector {
fn add_span(&self, trace: SpanId, span: CollectedSpan);
fn add_event(&self, trace: SpanId, event: CollectedEvent);
}

pub struct Exporter {}
Expand All @@ -119,4 +151,8 @@ impl Collector for Exporter {
fn add_span(&self, _trace: SpanId, _span: CollectedSpan) {
todo!()
}

fn add_event(&self, _trace: SpanId, _event: CollectedEvent) {
todo!()
}
}
31 changes: 27 additions & 4 deletions libs/telemetry/src/capturing/ng/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use tracing_subscriber::{
use crate::models::SpanKind;

use super::{
collector::{Collector, Exporter, SpanBuilder},
collector::{CollectedEvent, Collector, EventBuilder, Exporter, SpanBuilder},
traceparent::TraceParent,
};

Expand Down Expand Up @@ -93,11 +93,30 @@ where
}
}

fn on_event(&self, _event: &tracing::Event<'_>, _ctx: Context<'_, S>) {}
fn on_event(&self, event: &tracing::Event<'_>, ctx: Context<'_, S>) {
let Some(parent) = event.parent().cloned().or_else(|| {
event
.is_contextual()
.then(|| ctx.current_span().id().cloned())
.flatten()
}) else {
// Events without a parent span are not collected.
return;
};

let root = Self::root_span(&parent, &ctx).id();

let event_builder = EventBuilder::new(
event.metadata().name(),
*event.metadata().level(),
Instant::now(),
event.metadata().fields().len(),
);

fn on_enter(&self, _id: &Id, _ctx: Context<'_, S>) {}
// TODO: record attributes

fn on_exit(&self, _id: &Id, _ctx: Context<'_, S>) {}
self.collector.add_event(root.into(), event_builder.build());
}

fn on_close(&self, id: Id, ctx: Context<'_, S>) {
let span = Self::require_span(&id, &ctx);
Expand Down Expand Up @@ -194,6 +213,10 @@ mod tests {
let mut spans = self.spans.lock().unwrap();
spans.entry(trace_id).or_default().push(span);
}

fn add_event(&self, trace_id: SpanId, event: CollectedEvent) {

Check failure on line 217 in libs/telemetry/src/capturing/ng/layer.rs

View workflow job for this annotation

GitHub Actions / test

unused variable: `trace_id`

Check failure on line 217 in libs/telemetry/src/capturing/ng/layer.rs

View workflow job for this annotation

GitHub Actions / test

unused variable: `event`

Check failure on line 217 in libs/telemetry/src/capturing/ng/layer.rs

View workflow job for this annotation

GitHub Actions / clippy linting

unused variable: `trace_id`

Check failure on line 217 in libs/telemetry/src/capturing/ng/layer.rs

View workflow job for this annotation

GitHub Actions / clippy linting

unused variable: `event`
todo!()
}
}

fn redact_id() -> Redaction {
Expand Down

0 comments on commit cb8bd08

Please sign in to comment.