Skip to content

Commit

Permalink
chore: add OpentelemetryLogBuilder::with_labels
Browse files Browse the repository at this point in the history
  • Loading branch information
andylokandy committed Sep 19, 2024
1 parent 8ebe7f3 commit da81051
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/append/opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,37 @@ impl OpentelemetryLogBuilder {
}

/// Add a label to the resource.
pub fn add_label(mut self, key: impl Into<Cow<'static, str>>, value: impl Into<Cow<'static, str>>) -> Self {
pub fn with_label(
mut self,
key: impl Into<Cow<'static, str>>,
value: impl Into<Cow<'static, str>>,
) -> Self {
self.labels.push((key.into(), value.into()));
self
}

/// Add multiple labels to the resource.
pub fn with_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 { name, endpoint, protocol, labels } = self;
let OpentelemetryLogBuilder {
name,
endpoint,
protocol,
labels,
} = self;

let collector_timeout = Duration::from_secs(opentelemetry_otlp::OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT);
let collector_timeout =
Duration::from_secs(opentelemetry_otlp::OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT);
let exporter = match protocol {
Protocol::Grpc => opentelemetry_otlp::new_exporter()
.tonic()
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 da81051

Please sign in to comment.