pub struct OtelSdkBuilder { /* private fields */ }Expand description
Builder for configuring and initialising the OpenTelemetry SDK.
§Example
use opentelemetry_configuration::{OtelSdkBuilder, SdkError};
fn main() -> Result<(), SdkError> {
// Simple case - uses defaults (localhost:4318 for HTTP)
let _guard = OtelSdkBuilder::new().build()?;
// With environment variables
let _guard = OtelSdkBuilder::new()
.with_env("OTEL_")
.build()?;
// Full configuration
let _guard = OtelSdkBuilder::new()
.with_file("/var/task/otel-config.toml")
.with_env("OTEL_")
.endpoint("http://collector:4318")
.service_name("my-lambda")
.build()?;
Ok(())
}Implementations§
Source§impl OtelSdkBuilder
impl OtelSdkBuilder
Sourcepub fn new() -> OtelSdkBuilder
pub fn new() -> OtelSdkBuilder
Creates a new builder with default configuration.
Defaults include:
- Protocol: HTTP with protobuf encoding
- Endpoint:
http://localhost:4318(or 4317 for gRPC) - All signals enabled (traces, metrics, logs)
- Tracing subscriber initialisation enabled
- Lambda resource detection enabled
Sourcepub fn from_figment(figment: Figment) -> OtelSdkBuilder
pub fn from_figment(figment: Figment) -> OtelSdkBuilder
Creates a builder from an existing figment.
This allows power users to construct complex configuration chains before passing them to the SDK builder.
§Example
use figment::{Figment, providers::{Env, Format, Toml}};
use opentelemetry_configuration::{OtelSdkBuilder, SdkError};
let figment = Figment::new()
.merge(Toml::file("/etc/otel-defaults.toml"))
.merge(Toml::file("/var/task/otel-config.toml"))
.merge(Env::prefixed("OTEL_").split("_"));
let _guard = OtelSdkBuilder::from_figment(figment)
.service_name("my-lambda")
.build()?;Sourcepub fn with_file<P>(self, path: P) -> OtelSdkBuilder
pub fn with_file<P>(self, path: P) -> OtelSdkBuilder
Merges configuration from a TOML file.
If the file doesn’t exist, it’s silently skipped. This allows optional configuration files that may or may not be present.
§Example
use opentelemetry_configuration::{OtelSdkBuilder, SdkError};
let _guard = OtelSdkBuilder::new()
.with_file("/var/task/otel-config.toml") // Optional
.with_file("./otel-local.toml") // For development
.build()?;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.
Environment variables are split on underscores to match nested config.
For example, with prefix OTEL_:
OTEL_ENDPOINT_URL→endpoint.urlOTEL_ENDPOINT_PROTOCOL→endpoint.protocolOTEL_TRACES_ENABLED→traces.enabledOTEL_RESOURCE_SERVICE_NAME→resource.service_name
§Example
export OTEL_ENDPOINT_URL=http://collector:4318
export OTEL_ENDPOINT_PROTOCOL=grpc
export OTEL_TRACES_ENABLED=true
export OTEL_RESOURCE_SERVICE_NAME=my-lambdause opentelemetry_configuration::{OtelSdkBuilder, SdkError};
let _guard = OtelSdkBuilder::new()
.with_env("OTEL_")
.build()?;Sourcepub fn with_standard_env(self) -> OtelSdkBuilder
pub fn with_standard_env(self) -> OtelSdkBuilder
Merges configuration from standard OpenTelemetry environment variables.
This reads the standard OTEL_* environment variables as defined by
the OpenTelemetry specification:
OTEL_EXPORTER_OTLP_ENDPOINT→ endpoint URLOTEL_EXPORTER_OTLP_PROTOCOL→ protocol (grpc, http/protobuf, http/json)OTEL_SERVICE_NAME→ service nameOTEL_TRACES_EXPORTER→ traces exporter (otlp, none)OTEL_METRICS_EXPORTER→ metrics exporter (otlp, none)OTEL_LOGS_EXPORTER→ logs exporter (otlp, none)
Sourcepub fn endpoint(self, url: impl Into<String>) -> OtelSdkBuilder
pub fn endpoint(self, url: impl Into<String>) -> OtelSdkBuilder
Sets the OTLP endpoint URL explicitly.
This overrides any configuration from files or environment variables.
For HTTP protocols, signal-specific paths (/v1/traces, /v1/metrics,
/v1/logs) are appended automatically.
§Example
use opentelemetry_configuration::{OtelSdkBuilder, SdkError};
let _guard = OtelSdkBuilder::new()
.endpoint("http://collector.internal:4318")
.build()?;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 resource attribute.
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 fallback(self, fallback: ExportFallback) -> OtelSdkBuilder
pub fn fallback(self, fallback: ExportFallback) -> OtelSdkBuilder
Sets the fallback strategy for failed exports (planned feature).
Note: The fallback API is defined but not yet wired into the export pipeline. The handler will be stored but not invoked on export failures. Full implementation is planned for a future release.
When implemented, the fallback handler will be called when an export fails after all retry attempts have been exhausted. It will receive the original OTLP request payload, which can be preserved via alternative transport.
§Example
use opentelemetry_configuration::{OtelSdkBuilder, ExportFallback, SdkError};
let _guard = OtelSdkBuilder::new()
.fallback(ExportFallback::Stdout)
.build()?;Sourcepub fn with_fallback<F>(self, f: F) -> OtelSdkBuilder
pub fn with_fallback<F>(self, f: F) -> OtelSdkBuilder
Sets a custom fallback handler using a closure (planned feature).
Note: The fallback API is defined but not yet wired into the export pipeline. The handler will be stored but not invoked on export failures. Full implementation is planned for a future release.
When implemented, the closure will receive the full
ExportFailure including the original OTLP
request payload.
§Example
use opentelemetry_configuration::{OtelSdkBuilder, SdkError};
let _guard = OtelSdkBuilder::new()
.with_fallback(|failure| {
// Write the protobuf payload to S3, a queue, etc.
let bytes = failure.request.to_protobuf();
eprintln!(
"Failed to export {} ({} bytes): {}",
failure.request.signal_type(),
bytes.len(),
failure.error
);
Ok(())
})
.build()?;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.
Useful for authentication or custom routing.
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.
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);