Skip to content

Commit

Permalink
Add tests for layer
Browse files Browse the repository at this point in the history
  • Loading branch information
aqrln committed Nov 19, 2024
1 parent 44f6da0 commit 9d905be
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 38 deletions.
27 changes: 15 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions libs/telemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ crosstarget-utils = { path = "../crosstarget-utils" }
lru = "0.7.7"
enumflags2.workspace = true
derive_more = "0.99.17"

[dev-dependencies]
insta = { version = "1.41", features = ["redactions", "ron"] }
51 changes: 35 additions & 16 deletions libs/telemetry/src/capturing/ng/collector.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,55 @@
use std::{borrow::Cow, collections::HashMap};
use std::{borrow::Cow, collections::HashMap, num::NonZeroU64};

use serde::Serialize;
use tokio::time::Instant;
use tracing::{span::Id, Level};
use tracing::Level;

use crate::models::{HrTime, SpanKind, TraceSpan};

#[derive(Debug)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
pub struct SpanId(NonZeroU64);

impl From<&tracing::span::Id> for SpanId {
fn from(id: &tracing::span::Id) -> Self {
Self(id.into_non_zero_u64())
}
}

impl From<tracing::span::Id> for SpanId {
fn from(id: tracing::span::Id) -> Self {
Self::from(&id)
}
}

#[derive(Debug, Clone)]
#[cfg_attr(test, derive(serde::Serialize))]
pub struct CollectedSpan {
id: Id,
parent_id: Option<Id>,
id: SpanId,
parent_id: Option<SpanId>,
name: Cow<'static, str>,
#[cfg_attr(test, serde(skip_serializing))]
start_time: Instant,
#[cfg_attr(test, serde(skip_serializing))]
end_time: Instant,
attributes: HashMap<&'static str, serde_json::Value>,
kind: SpanKind,
links: Vec<Id>,
links: Vec<SpanId>,
}

pub(crate) struct SpanBuilder {
id: Id,
id: SpanId,
name: Cow<'static, str>,
start_time: Instant,
end_time: Option<Instant>,
attributes: HashMap<&'static str, serde_json::Value>,
kind: Option<SpanKind>,
links: Vec<Id>,
links: Vec<SpanId>,
}

impl SpanBuilder {
pub fn new(name: &'static str, id: Id, start_time: Instant, attrs_size_hint: usize) -> Self {
pub fn new(name: &'static str, id: impl Into<SpanId>, start_time: Instant, attrs_size_hint: usize) -> Self {
Self {
id,
id: id.into(),
name: name.into(),
start_time,
end_time: None,
Expand All @@ -52,14 +71,14 @@ impl SpanBuilder {
self.attributes.insert(key, value);
}

pub fn add_link(&mut self, link: Id) {
pub fn add_link(&mut self, link: SpanId) {
self.links.push(link);
}

pub fn end(self, parent_id: Option<Id>, end_time: Instant) -> CollectedSpan {
pub fn end(self, parent_id: Option<impl Into<SpanId>>, end_time: Instant) -> CollectedSpan {
CollectedSpan {
id: self.id,
parent_id,
parent_id: parent_id.map(Into::into),
name: self.name,
start_time: self.start_time,
end_time,
Expand All @@ -70,7 +89,7 @@ impl SpanBuilder {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub(crate) struct CollectedEvent {
name: &'static str,
level: Level,
Expand All @@ -79,7 +98,7 @@ pub(crate) struct CollectedEvent {
}

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

pub struct Exporter {}
Expand All @@ -97,7 +116,7 @@ impl Default for Exporter {
}

impl Collector for Exporter {
fn add_span(&self, _trace: Id, _span: CollectedSpan) {
fn add_span(&self, _trace: SpanId, _span: CollectedSpan) {
todo!()
}
}
Loading

0 comments on commit 9d905be

Please sign in to comment.