Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
add some data model examples to the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Jan 2, 2024
1 parent 336ce0e commit d7b5e3c
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 18 deletions.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,57 @@ For some idea of what it can do, see the `tests/smoke-test` example.
An event is a point of change in a system surfaced to an observer along with rationale describing it.
Events are a model of your unique domain through the lens of significant interactions with it.

You can represent a lot of interesting observability signals using events.

```
emit::info!(extent: now, "A log record");
```

```
13:18:58.657 A log record
```

-----

```
emit::info!(extent: now..later, "A span");
```

```
13:19:03.657 5s A span
```

-----

```
emit::info!(extent: now, "An independent metric {metric_value: 1.0}");
```

```
13:18:58.657 An independent metric 1
```

-----

```
emit::info!(extent: now..later, "A cumulative metric {metric_value: 4.0}");
```

```
13:19:03.657 5s A cumulative metric 4
```

-----

```
emit::info!(extent: now..later, "A histogram metric {#[emit::as_serde] metric_value: [1.0, 3.0, 2.0, 5.0, 1.0]}");
```

```
13:19:03.657 5s A histogram metric (1, 3, 2, 5, 1)
▁▄▃▇▁
```

## How is this different?

`emit` takes a different path from `log` or `tracing` by abandoning `format_args!` as the basis of its instrumentation.
Expand Down
66 changes: 48 additions & 18 deletions core/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,21 +290,19 @@ pub use self::alloc_support::*;

#[cfg(all(feature = "sval", not(feature = "serde")))]
mod seq {
use core::marker::PhantomData;

use super::*;

impl<'v> Value<'v> {
pub(super) fn to_sequence<E: Default + for<'a> Extend<Option<Value<'a>>>>(
&self,
) -> Option<E> {
if let Ok(seq) = sval_nested::stream_ref(Root(Default::default()), &self.0) {
Some(seq)
} else {
None
}
sval_nested::stream_ref(Root(Default::default()), &self.0).ok()
}
}

struct Root<C>(C);
struct Root<C>(PhantomData<C>);

struct Seq<C>(C);

Expand All @@ -315,7 +313,7 @@ mod seq {

type Map = sval_nested::Unsupported<C>;

type Tuple = sval_nested::Unsupported<C>;
type Tuple = Seq<C>;

type Record = sval_nested::Unsupported<C>;

Expand Down Expand Up @@ -354,9 +352,9 @@ mod seq {
_: Option<sval::Tag>,
_: Option<sval::Label>,
_: Option<sval::Index>,
_: Option<usize>,
num_entries: Option<usize>,
) -> sval_nested::Result<Self::Tuple> {
Err(sval_nested::Error::invalid_value("not a sequence"))
self.seq_begin(num_entries)
}

fn record_begin(
Expand Down Expand Up @@ -392,25 +390,40 @@ mod seq {
Ok(self.0)
}
}

impl<'sval, C: for<'a> Extend<Option<Value<'a>>>> sval_nested::StreamTuple<'sval> for Seq<C> {
type Ok = C;

fn value_computed<V: sval::Value>(
&mut self,
tag: Option<sval::Tag>,
_: sval::Index,
value: V,
) -> sval_nested::Result {
sval_nested::StreamSeq::value_computed(self, value)
}

fn end(self) -> sval_nested::Result<Self::Ok> {
sval_nested::StreamSeq::end(self)
}
}
}

#[cfg(feature = "serde")]
mod seq {
use core::marker::PhantomData;

use super::*;

impl<'v> Value<'v> {
pub(super) fn to_sequence<E: Default + for<'a> Extend<Option<Value<'a>>>>(
&self,
) -> Option<E> {
if let Ok(seq) = serde::Serialize::serialize(&self.0, Root(Default::default())) {
Some(seq)
} else {
None
}
serde::Serialize::serialize(&self.0, Root(Default::default())).ok()
}
}

struct Root<C>(C);
struct Root<C>(PhantomData<C>);

struct Seq<C>(C);

Expand Down Expand Up @@ -441,7 +454,7 @@ mod seq {

type SerializeSeq = Seq<C>;

type SerializeTuple = serde::ser::Impossible<C, Unsupported>;
type SerializeTuple = Seq<C>;

type SerializeTupleStruct = serde::ser::Impossible<C, Unsupported>;

Expand Down Expand Up @@ -565,8 +578,8 @@ mod seq {
Ok(Seq(C::default()))
}

fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> {
Err(Unsupported)
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
self.serialize_seq(Some(len))
}

fn serialize_tuple_struct(
Expand Down Expand Up @@ -628,4 +641,21 @@ mod seq {
Ok(self.0)
}
}

impl<C: for<'a> Extend<Option<Value<'a>>>> serde::ser::SerializeTuple for Seq<C> {
type Ok = C;

type Error = Unsupported;

fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: serde::Serialize,
{
serde::ser::SerializeSeq::serialize_element(self, value)
}

fn end(self) -> Result<Self::Ok, Self::Error> {
serde::ser::SerializeSeq::end(self)
}
}
}

0 comments on commit d7b5e3c

Please sign in to comment.