Skip to content

Commit

Permalink
add OTEL_SDK_DISABLED support
Browse files Browse the repository at this point in the history
Signed-off-by: jiaxiao zhou <[email protected]>
  • Loading branch information
Mossaka committed Jul 15, 2024
1 parent 493249d commit f3a280e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 28 deletions.
4 changes: 2 additions & 2 deletions crates/containerd-shim-wasm/src/sandbox/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ttrpc::Server;
use crate::sandbox::manager::Shim;
use crate::sandbox::shim::Local;
#[cfg(feature = "opentelemetry")]
use crate::sandbox::shim::OTLPConfig;
use crate::sandbox::shim::{otel_traces_enabled, OTLPConfig};
use crate::sandbox::{Instance, ManagerService, ShimCli};
use crate::services::sandbox_ttrpc::{create_manager, Manager};

Expand Down Expand Up @@ -54,7 +54,7 @@ pub fn shim_main<'a, I>(
I::Engine: Default,
{
#[cfg(feature = "opentelemetry")]
if OTLPConfig::traces_enabled() {
if otel_traces_enabled() {
// opentelemetry uses tokio, so we need to initialize a runtime
use tokio::runtime::Runtime;
let rt = Runtime::new().unwrap();
Expand Down
4 changes: 3 additions & 1 deletion crates/containerd-shim-wasm/src/sandbox/shim/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ where
.and_then(|a| a.get("io.kubernetes.cri.sandbox-id"))
.unwrap_or(&id);

let (_child, address) = shim::spawn(opts, grouping, vec![])?;
setup_namespaces(&spec)
.map_err(|e| shim::Error::Other(format!("failed to setup namespaces: {}", e)))?;

let (_child, address) = shim::spawn(opts, grouping, vec![])?;

write_address(&address)?;

Ok(address)
Expand Down
4 changes: 2 additions & 2 deletions crates/containerd-shim-wasm/src/sandbox/shim/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ pub use cli::Cli;
pub(crate) use local::Local;
#[cfg(feature = "opentelemetry")]
pub use otel::{
Config as OTLPConfig, ShutdownGuard as OTLPShutdownGuard, OTEL_EXPORTER_OTLP_ENDPOINT,
OTEL_EXPORTER_OTLP_PROTOCOL,
traces_enabled as otel_traces_enabled, Config as OTLPConfig,
ShutdownGuard as OTLPShutdownGuard, OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_PROTOCOL,
};
69 changes: 46 additions & 23 deletions crates/containerd-shim-wasm/src/sandbox/shim/otel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
//! use containerd_shim_wasm::sandbox::shim::otel::Config;
//!
//! fn main() -> anyhow::Result<()> {
//! let otel_config = Config::build_from_env()?;
//!
//! let _guard = otel_config.init()?;
//!
//! // Your application code here
//!
//! if traces_enabled() {
//! let otel_config = Config::build_from_env()?;
//!
//! let _guard = otel_config.init()?;
//!
//! // Your application code here
//! }
//! Ok(())
//! }
//! ```
Expand All @@ -41,13 +42,28 @@ use tracing_subscriber::{EnvFilter, Registry};
const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF: &str = "http/protobuf";
const OTEL_EXPORTER_OTLP_PROTOCOL_GRPC: &str = "grpc";
const OTEL_EXPORTER_OTLP_TRACES_PROTOCOL: &str = "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL";
const OTEL_SDK_DISABLED: &str = "OTEL_SDK_DISABLED";

/// Configuration struct for OpenTelemetry setup.
pub struct Config {
traces_endpoint: String,
traces_protocol: Protocol,
}

/// Returns `true` if traces are enabled, `false` otherwise.
///
/// Traces are enabled if either `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` or `OTEL_EXPORTER_OTLP_ENDPOINT` is set and not empty.
/// `OTEL_SDK_DISABLED` can be set to `true` to disable traces.
pub fn traces_enabled() -> bool {
let check_env_var = |var: &str| env::var_os(var).is_some_and(|val| !val.is_empty());
let traces_endpoint = check_env_var(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT);
let otlp_endpoint = check_env_var(OTEL_EXPORTER_OTLP_ENDPOINT);

// https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#general-sdk-configuration
let sdk_disabled = env::var_os(OTEL_SDK_DISABLED).is_some_and(|val| val == "true");
(traces_endpoint || otlp_endpoint) && !sdk_disabled
}

/// Initializes a new OpenTelemetry tracer with the OTLP exporter.
///
/// Returns a `Result` containing the initialized tracer or a `TraceError` if initialization fails.
Expand All @@ -63,17 +79,6 @@ impl Config {
})
}

/// Returns `true` if traces are enabled, `false` otherwise.
///
/// Traces are enabled if either `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` or `OTEL_EXPORTER_OTLP_ENDPOINT` is set and not empty.
pub fn traces_enabled() -> bool {
let traces_endpoint = env::var(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT).ok();
let otlp_endpoint = env::var(OTEL_EXPORTER_OTLP_ENDPOINT).ok();

traces_endpoint.map_or(false, |v| !v.is_empty())
|| otlp_endpoint.map_or(false, |v| !v.is_empty())
}

/// Initializes the tracer, sets up the telemetry and subscriber layers, and sets the global subscriber.
///
/// Note: this function should be called only once and be called by the binary entry point.
Expand Down Expand Up @@ -183,59 +188,77 @@ mod tests {
[
(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, Some("trace_endpoint")),
(OTEL_EXPORTER_OTLP_ENDPOINT, Some("general_endpoint")),
(OTEL_SDK_DISABLED, None::<&str>),
],
|| {
assert!(Config::traces_enabled());
assert!(traces_enabled());
},
);

with_vars(
[
(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, Some("")),
(OTEL_EXPORTER_OTLP_ENDPOINT, Some("general_endpoint")),
(OTEL_SDK_DISABLED, Some("t")),
],
|| {
assert!(Config::traces_enabled());
assert!(traces_enabled());
},
);

with_vars(
[
(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, None),
(OTEL_EXPORTER_OTLP_ENDPOINT, Some("general_endpoint")),
(OTEL_SDK_DISABLED, Some("false")),
],
|| {
assert!(Config::traces_enabled());
assert!(traces_enabled());
},
);

with_vars(
[
(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, Some("trace_endpoint")),
(OTEL_EXPORTER_OTLP_ENDPOINT, Some("")),
(OTEL_SDK_DISABLED, Some("1")),
],
|| {
assert!(Config::traces_enabled());
assert!(traces_enabled());
},
);

with_vars(
[
(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, Some("")),
(OTEL_EXPORTER_OTLP_ENDPOINT, Some("")),
(OTEL_SDK_DISABLED, None::<&str>),
],
|| {
assert!(!Config::traces_enabled());
assert!(!traces_enabled());
},
);

with_vars(
[
(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, None::<&str>),
(OTEL_EXPORTER_OTLP_ENDPOINT, None::<&str>),
(OTEL_SDK_DISABLED, None::<&str>),
],
|| {
assert!(!traces_enabled());
},
);

// Test when traces are disabled due to OTEL_SDK_DISABLED
with_vars(
[
(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, Some("trace_endpoint")),
(OTEL_EXPORTER_OTLP_ENDPOINT, Some("general_endpoint")),
(OTEL_SDK_DISABLED, Some("true")),
],
|| {
assert!(!Config::traces_enabled());
assert!(!traces_enabled());
},
);
}
Expand Down
2 changes: 2 additions & 0 deletions docs/opentelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ sudo ctr run --net-host --rm --runtime=io.containerd.wasmtime.v1 ghcr.io/contai
`OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` - The endpoint to send trace data to. Overrides `OTEL_EXPORTER_OTLP_ENDPOINT`.
`OTEL_EXPORTER_OTLP_PROTOCOL` - A base protocol to use when sending trace data. Default is `http/protobuf`. Valid values are `http/protobuf`, `grpc`.
`OTEL_EXPORTER_OTLP_TRACES_PROTOCOL` - The protocol to use when sending trace data. Overrides `OTEL_EXPORTER_OTLP_PROTOCOL`.
`OTEL_SDK_DISABLED` - Disables the SDK if set to `true`.
`OTEL_SERVICE_NAME` - The name of the service.

## Context Propagation

Expand Down

0 comments on commit f3a280e

Please sign in to comment.