Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub fn builder() -> Builder {
labels: FormattedLabels::new(),
extra_fields: HashMap::new(),
http_headers,
include_event_metadata: true,
}
}

Expand All @@ -35,6 +36,7 @@ pub struct Builder {
labels: FormattedLabels,
extra_fields: HashMap<String, String>,
http_headers: reqwest::header::HeaderMap,
include_event_metadata: bool,
}

impl Builder {
Expand Down Expand Up @@ -106,6 +108,26 @@ impl Builder {
}
Ok(self)
}
/// Configure whether tracing event metadata is included in log records.
///
/// When enabled (the default), log records include the `_target`,
/// `_module_path`, `_file`, and `_line` metadata fields. When disabled,
/// those fields are omitted entirely from the JSON payloads sent to Loki.
///
/// # Example
///
/// ```
/// # use tracing_loki::Error;
/// # fn main() -> Result<(), Error> {
/// let builder = tracing_loki::builder()
/// .include_event_metadata(false);
/// # Ok(())
/// # }
/// ```
pub fn include_event_metadata(mut self, include: bool) -> Builder {
self.include_event_metadata = include;
self
}
/// Set an extra HTTP header to be sent with all requests sent to Loki.
///
/// This can be useful to set the `X-Scope-OrgID` header which Loki
Expand Down Expand Up @@ -163,6 +185,7 @@ impl Builder {
Layer {
sender,
extra_fields: self.extra_fields,
include_event_metadata: self.include_event_metadata,
},
BackgroundTask::new(loki_url, self.http_headers, receiver, &self.labels)?,
))
Expand Down Expand Up @@ -194,6 +217,7 @@ impl Builder {
Layer {
sender: sender.clone(),
extra_fields: self.extra_fields,
include_event_metadata: self.include_event_metadata,
},
BackgroundTaskController { sender },
BackgroundTask::new(loki_url, self.http_headers, receiver, &self.labels)?,
Expand Down
22 changes: 18 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ pub fn layer(
pub struct Layer {
extra_fields: HashMap<String, String>,
sender: mpsc::Sender<Option<LokiEvent>>,
include_event_metadata: bool,
}

struct LokiEvent {
Expand All @@ -247,6 +248,12 @@ struct SerializedEvent<'a> {
#[serde(flatten)]
span_fields: serde_json::Map<String, serde_json::Value>,
_spans: &'a [&'a str],
#[serde(flatten)]
metadata: Option<SerializedMetadata<'a>>,
}

#[derive(Serialize)]
struct SerializedMetadata<'a> {
_target: &'a str,
_module_path: Option<&'a str>,
_file: Option<&'a str>,
Expand Down Expand Up @@ -333,6 +340,16 @@ impl<S: Subscriber + for<'a> LookupSpan<'a>> tracing_subscriber::Layer<S> for La
})
})
.unwrap_or(Vec::new());
let metadata = if self.include_event_metadata {
Some(SerializedMetadata {
_target: meta.target(),
_module_path: meta.module_path(),
_file: meta.file(),
_line: meta.line(),
})
} else {
None
};
// TODO: Anything useful to do when the capacity has been reached?
let _ = self.sender.try_send(Some(LokiEvent {
trigger_send: !meta.target().starts_with("tracing_loki"),
Expand All @@ -343,10 +360,7 @@ impl<S: Subscriber + for<'a> LookupSpan<'a>> tracing_subscriber::Layer<S> for La
extra_fields: &self.extra_fields,
span_fields,
_spans: &spans,
_target: meta.target(),
_module_path: meta.module_path(),
_file: meta.file(),
_line: meta.line(),
metadata,
})
.expect("json serialization shouldn't fail"),
}));
Expand Down
Loading