pub struct OtelSdkBuilder { /* private fields */ }Expand description
Builder for configuring and initialising the OpenTelemetry SDK.
§Example
use opentelemetry_configuration::{OtelSdkBuilder, SdkError};
let _guard = OtelSdkBuilder::new()
.service_name("my-service")
.build()?;Implementations§
Source§impl OtelSdkBuilder
impl OtelSdkBuilder
Sourcepub fn new() -> OtelSdkBuilder
pub fn new() -> OtelSdkBuilder
Creates a new builder with default configuration.
Sourcepub fn from_figment(figment: Figment) -> OtelSdkBuilder
pub fn from_figment(figment: Figment) -> OtelSdkBuilder
Creates a builder from an existing figment for complex configuration chains.
Sourcepub fn with_file<P>(self, path: P) -> OtelSdkBuilder
pub fn with_file<P>(self, path: P) -> OtelSdkBuilder
Merges configuration from a TOML file. Missing files are silently skipped.
Sourcepub fn with_env(self, prefix: &str) -> OtelSdkBuilder
pub fn with_env(self, prefix: &str) -> OtelSdkBuilder
Merges configuration from environment variables with the given prefix.
Variables are split on underscores: PREFIX_FOO_BAR → foo.bar.
Sourcepub fn with_standard_env(self) -> OtelSdkBuilder
pub fn with_standard_env(self) -> OtelSdkBuilder
Merges standard OTEL_* environment variables per OpenTelemetry spec.
Sourcepub fn endpoint(self, url: impl Into<String>) -> OtelSdkBuilder
pub fn endpoint(self, url: impl Into<String>) -> OtelSdkBuilder
Sets the OTLP endpoint URL. For HTTP, signal paths are appended automatically.
Sourcepub fn protocol(self, protocol: Protocol) -> OtelSdkBuilder
pub fn protocol(self, protocol: Protocol) -> OtelSdkBuilder
Sets the export protocol.
This overrides any configuration from files or environment variables.
The default endpoint changes based on protocol:
Protocol::Grpc→http://localhost:4317Protocol::HttpBinary→http://localhost:4318Protocol::HttpJson→http://localhost:4318
Sourcepub fn service_name(self, name: impl Into<String>) -> OtelSdkBuilder
pub fn service_name(self, name: impl Into<String>) -> OtelSdkBuilder
Sets the service name resource attribute.
This is the most commonly configured resource attribute and identifies your service in the telemetry backend.
Sourcepub fn service_version(self, version: impl Into<String>) -> OtelSdkBuilder
pub fn service_version(self, version: impl Into<String>) -> OtelSdkBuilder
Sets the service version resource attribute.
Sourcepub fn deployment_environment(self, env: impl Into<String>) -> OtelSdkBuilder
pub fn deployment_environment(self, env: impl Into<String>) -> OtelSdkBuilder
Sets the deployment environment resource attribute.
Sourcepub fn resource_attribute(
self,
key: impl Into<String>,
value: impl Into<String>,
) -> OtelSdkBuilder
pub fn resource_attribute( self, key: impl Into<String>, value: impl Into<String>, ) -> OtelSdkBuilder
Adds a custom resource attribute.
Resource attributes describe the entity producing telemetry. Use this for application-specific metadata not covered by standard semantic conventions.
§Example
use opentelemetry_configuration::OtelSdkBuilder;
let _guard = OtelSdkBuilder::new()
.service_name("my-service")
.resource_attribute("git.commit", "abc123")
.resource_attribute("feature.flags", "new-ui,beta-api")
.build()?;Sourcepub fn with_resource(self, resource: Resource) -> OtelSdkBuilder
pub fn with_resource(self, resource: Resource) -> OtelSdkBuilder
Provides a pre-built OpenTelemetry Resource.
This takes precedence over individual resource configuration. Use this when you need fine-grained control over resource construction.
Sourcepub fn resource<F>(self, f: F) -> OtelSdkBuilder
pub fn resource<F>(self, f: F) -> OtelSdkBuilder
Configures the resource using a builder function.
§Example
use opentelemetry_configuration::{OtelSdkBuilder, SdkError};
let _guard = OtelSdkBuilder::new()
.resource(|r| r
.service_name("my-lambda")
.service_version(env!("CARGO_PKG_VERSION"))
.deployment_environment("production"))
.build()?;Sourcepub fn traces(self, enabled: bool) -> OtelSdkBuilder
pub fn traces(self, enabled: bool) -> OtelSdkBuilder
Enables or disables trace collection.
Default: enabled
Sourcepub fn metrics(self, enabled: bool) -> OtelSdkBuilder
pub fn metrics(self, enabled: bool) -> OtelSdkBuilder
Enables or disables metrics collection.
Default: enabled
Sourcepub fn logs(self, enabled: bool) -> OtelSdkBuilder
pub fn logs(self, enabled: bool) -> OtelSdkBuilder
Enables or disables log collection.
Default: enabled
Sourcepub fn without_tracing_subscriber(self) -> OtelSdkBuilder
pub fn without_tracing_subscriber(self) -> OtelSdkBuilder
Disables automatic tracing subscriber initialisation.
By default, the SDK sets up a tracing-subscriber with
tracing-opentelemetry and opentelemetry-appender-tracing integration.
Disable this if you want to configure the subscriber yourself.
Sourcepub fn header(
self,
key: impl Into<String>,
value: impl Into<String>,
) -> OtelSdkBuilder
pub fn header( self, key: impl Into<String>, value: impl Into<String>, ) -> OtelSdkBuilder
Adds an HTTP header to all export requests.
Headers are applied to trace, metric, and log exporters. Common uses include authentication tokens and custom routing metadata.
§Example
use opentelemetry_configuration::OtelSdkBuilder;
let api_token = std::env::var("API_TOKEN").unwrap_or_default();
let _guard = OtelSdkBuilder::new()
.service_name("my-service")
.header("Authorization", format!("Bearer {api_token}"))
.build()?;Sourcepub fn instrumentation_scope_name(
self,
name: impl Into<String>,
) -> OtelSdkBuilder
pub fn instrumentation_scope_name( self, name: impl Into<String>, ) -> OtelSdkBuilder
Sets the instrumentation scope name (otel.library.name).
If not set, defaults to the service name, then “opentelemetry-configuration”.
Sourcepub fn compute_environment(self, env: ComputeEnvironment) -> OtelSdkBuilder
pub fn compute_environment(self, env: ComputeEnvironment) -> OtelSdkBuilder
Sets the compute environment for resource detection.
Controls which resource detectors are run automatically:
Auto(default): Runs generic detectors and probes for Lambda/K8sLambda: Runs generic detectors + Lambda-specific attributesKubernetes: Runs generic detectors + K8s detectorNone: No automatic detection, only explicit configuration
Sourcepub fn with_rust_build_info(self, info: RustBuildInfo) -> OtelSdkBuilder
pub fn with_rust_build_info(self, info: RustBuildInfo) -> OtelSdkBuilder
Adds Rust build-time information as resource attributes.
Use with the capture_rust_build_info! macro
to add rustc version and channel information to telemetry.
Requires emit_rustc_env to be called in your build.rs.
§Example
In build.rs:
opentelemetry_configuration::emit_rustc_env();In main.rs:
use opentelemetry_configuration::{OtelSdkBuilder, capture_rust_build_info};
let _guard = OtelSdkBuilder::new()
.service_name("my-service")
.with_rust_build_info(capture_rust_build_info!())
.build()?;§Attributes Added
process.runtime.version- rustc version (e.g., “1.84.0”)process.runtime.description- full version stringrust.channel- release channel (“stable”, “beta”, or “nightly”)
Sourcepub fn extract_config(&self) -> Result<OtelSdkConfig, SdkError>
pub fn extract_config(&self) -> Result<OtelSdkConfig, SdkError>
Extracts the configuration for inspection or debugging.
§Errors
Returns an error if configuration extraction fails or if the endpoint URL is invalid.
Sourcepub fn build(self) -> Result<OtelGuard, SdkError>
pub fn build(self) -> Result<OtelGuard, SdkError>
Builds and initialises the OpenTelemetry SDK.
Returns an OtelGuard that manages provider lifecycle. When the
guard is dropped, all providers are flushed and shut down.
§Errors
Returns an error if:
- Configuration extraction fails
- Provider initialisation fails
- Tracing subscriber initialisation fails
§Example
use opentelemetry_configuration::{OtelSdkBuilder, SdkError};
fn main() -> Result<(), SdkError> {
let _guard = OtelSdkBuilder::new()
.with_env("OTEL_")
.service_name("my-lambda")
.build()?;
tracing::info!("Application started");
// Guard automatically shuts down providers on drop
Ok(())
}Trait Implementations§
Source§impl Default for OtelSdkBuilder
impl Default for OtelSdkBuilder
Source§fn default() -> OtelSdkBuilder
fn default() -> OtelSdkBuilder
Auto Trait Implementations§
impl Freeze for OtelSdkBuilder
impl !RefUnwindSafe for OtelSdkBuilder
impl Send for OtelSdkBuilder
impl Sync for OtelSdkBuilder
impl Unpin for OtelSdkBuilder
impl !UnwindSafe for OtelSdkBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);