Struct tracing_loki::Builder
source · pub struct Builder { /* private fields */ }
Expand description
Builder for constructing a Layer
and its corresponding
BackgroundTask
.
See the crate’s root documentation for an example.
Implementations§
source§impl Builder
impl Builder
sourcepub fn label<S: Into<String>, T: AsRef<str>>(
self,
key: S,
value: T
) -> Result<Builder, Error>
pub fn label<S: Into<String>, T: AsRef<str>>( self, key: S, value: T ) -> Result<Builder, Error>
Add a label to the logs sent to Loki through the built Layer
.
Labels are supposed to be closed categories with few possible values.
For example, "environment"
with values "ci"
, "development"
,
"staging"
or "production"
would work well.
For open categories, extra fields are a better fit. See
Builder::extra_field
.
No two labels can share the same name, and the key "level"
is
reserved for the log level.
Errors
This function will return an error if a key is a duplicate or when the
key is "level"
.
Example
let builder = tracing_loki::builder()
.label("environment", "production")?;
sourcepub fn extra_field<S: Into<String>, T: Into<String>>(
self,
key: S,
value: T
) -> Result<Builder, Error>
pub fn extra_field<S: Into<String>, T: Into<String>>( self, key: S, value: T ) -> Result<Builder, Error>
Set an extra field that is sent with all log records sent to Loki through the built layer.
Fields are meant to be used for open categories or closed categories
with many options. For example, "run_id"
with randomly generated
UUIDv4s
would be a good fit for these extra fields.
Example
let builder = tracing_loki::builder()
.extra_field("run_id", "5b6aedb4-e2c1-4ad9-b8a7-3ef92b5c8120")?;
sourcepub fn http_header<S: AsRef<str>, T: AsRef<str>>(
self,
key: S,
value: T
) -> Result<Builder, Error>
pub fn http_header<S: AsRef<str>, T: AsRef<str>>( self, key: S, value: T ) -> Result<Builder, Error>
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
processes as the tenant ID in a multi-tenant setup.
Example
let builder = tracing_loki::builder()
// Set the tenant ID for Loki.
.http_header("X-Scope-OrgID", "7662a206-fa0f-407f-abe9-261d652c750b")?;
sourcepub fn build_url(self, loki_url: Url) -> Result<(Layer, BackgroundTask), Error>
pub fn build_url(self, loki_url: Url) -> Result<(Layer, BackgroundTask), Error>
Build the tracing Layer
and its corresponding BackgroundTask
.
The loki_url
is the URL of the Loki server, like
https://127.0.0.1:3100
.
The Layer
needs to be registered with a
tracing_subscriber::Registry
, and the BackgroundTask
needs to
be tokio::spawn
ed.
Note that unlike the layer
function, this
function does not strip off the path component of loki_url
before
appending /loki/api/v1/push
.
See the crate’s root documentation for an example.
sourcepub fn build_controller_url(
self,
loki_url: Url
) -> Result<(Layer, BackgroundTaskController, BackgroundTask), Error>
pub fn build_controller_url( self, loki_url: Url ) -> Result<(Layer, BackgroundTaskController, BackgroundTask), Error>
Build the tracing Layer
, BackgroundTask
and its
BackgroundTaskController
.
The BackgroundTaskController
can be used to signal the background
task to shut down.
The loki_url
is the URL of the Loki server, like
https://127.0.0.1:3100
.
The Layer
needs to be registered with a
tracing_subscriber::Registry
, and the BackgroundTask
needs to
be tokio::spawn
ed.
Note that unlike the layer
function, this
function does not strip off the path component of loki_url
before
appending /loki/api/v1/push
.
See the crate’s root documentation for an example.