Skip to content

Commit

Permalink
refactor: add OpentelemetryLogBuilder::labels (#61)
Browse files Browse the repository at this point in the history
Co-authored-by: tison <wander4096@gmail.com>
  • Loading branch information
andylokandy and tisonkun authored Oct 8, 2024
1 parent d92d148 commit fd89c8a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/append/opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl OpentelemetryLogBuilder {
/// Default to [`Grpc`].
///
/// [`Grpc`]: OpentelemetryWireProtocol::Grpc
pub fn with_protocol(mut self, protocol: OpentelemetryWireProtocol) -> Self {
pub fn protocol(mut self, protocol: OpentelemetryWireProtocol) -> Self {
self.protocol = match protocol {
OpentelemetryWireProtocol::Grpc => Protocol::Grpc,
OpentelemetryWireProtocol::HttpBinary => Protocol::HttpBinary,
Expand All @@ -79,8 +79,8 @@ impl OpentelemetryLogBuilder {
self
}

/// Add a label to the resource.
pub fn add_label(
/// Append a label to the resource.
pub fn label(
mut self,
key: impl Into<Cow<'static, str>>,
value: impl Into<Cow<'static, str>>,
Expand All @@ -89,6 +89,17 @@ impl OpentelemetryLogBuilder {
self
}

/// Append multiple labels to the resource.
pub fn labels<K, V>(mut self, labels: impl IntoIterator<Item = (K, V)>) -> Self
where
K: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
{
self.labels
.extend(labels.into_iter().map(|(k, v)| (k.into(), v.into())));
self
}

/// Build the [`OpentelemetryLog`] appender.
pub fn build(self) -> Result<OpentelemetryLog, opentelemetry::logs::LogError> {
let OpentelemetryLogBuilder {
Expand Down
4 changes: 4 additions & 0 deletions src/layout/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.

/// A helper struct to format log's key-value pairs.
///
/// This is useful when you want to display log's key-value pairs in a log message.
pub struct KvDisplay<'kvs> {
kv: &'kvs dyn log::kv::Source,
}
Expand Down Expand Up @@ -47,6 +49,8 @@ impl<'a, 'kvs> log::kv::Visitor<'kvs> for KvWriter<'a, 'kvs> {
}

/// A helper to collect log's key-value pairs.
///
/// This is useful when you want to collect log's key-value pairs for further processing.
pub fn collect_kvs(kv: &dyn log::kv::Source) -> Vec<(String, String)> {
let mut collector = KvCollector { kv: Vec::new() };
kv.visit(&mut collector).ok();
Expand Down

0 comments on commit fd89c8a

Please sign in to comment.