Skip to content

Commit

Permalink
add a method to add multiple dynamic fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mladedav committed Sep 2, 2024
1 parent 750d915 commit d9f1153
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,27 @@ where
);
}

pub fn add_multiple_dynamic_fields<Fun, Res>(&mut self, key: impl Into<String>, mapper: Fun)
where
for<'a> Fun: Fn(&'a Event<'_>, &Context<'_, S>) -> Res + Send + Sync + 'a,
Res: IntoIterator<Item = (String, serde_json::Value)>,
{
self.schema.insert(
SchemaKey::from(key.into()),
JsonValue::DynamicFromEvent(Box::new(move |event| {
Some(DynamicJsonValue {
flatten: true,
value: serde_json::to_value(
mapper(event.event(), event.context())
.into_iter()
.collect::<Vec<_>>(),
)
.ok()?,
})
})),
);
}

pub fn add_from_span<Fun, Res>(&mut self, key: impl Into<String>, mapper: Fun)
where
for<'a> Fun: Fn(&'a SpanRef<'_, S>) -> Option<Res> + Send + Sync + 'a,
Expand Down

0 comments on commit d9f1153

Please sign in to comment.