|
68 | 68 | //! revision!(), |
69 | 69 | //! "v1", |
70 | 70 | //! Some(config), |
71 | | -//! ); |
| 71 | +//! ).await; |
72 | 72 | //! ``` |
73 | 73 | //! |
74 | 74 | //! When the `opentelemetry` feature is enabled, additional runtime config |
@@ -168,6 +168,34 @@ fn init_zygote_and_logger(debug: bool, config: &Config) { |
168 | 168 | ); |
169 | 169 | } |
170 | 170 |
|
| 171 | +fn otel_traces_guard() -> Box<dyn Send + 'static> { |
| 172 | + #[cfg(feature = "opentelemetry")] |
| 173 | + if otel_traces_enabled() { |
| 174 | + let otlp_config = OtlpConfig::build_from_env().expect("Failed to build OtelConfig."); |
| 175 | + let guard = otlp_config |
| 176 | + .init() |
| 177 | + .expect("Failed to initialize OpenTelemetry."); |
| 178 | + return Box::new(guard); |
| 179 | + } |
| 180 | + Box::new(()) |
| 181 | +} |
| 182 | + |
| 183 | +fn init_traces_context() { |
| 184 | + #[cfg(feature = "opentelemetry")] |
| 185 | + // read TRACECONTEXT env var that's set by the parent process |
| 186 | + if let Ok(ctx) = std::env::var("TRACECONTEXT") { |
| 187 | + OtlpConfig::set_trace_context(&ctx).unwrap(); |
| 188 | + } else { |
| 189 | + let ctx = OtlpConfig::get_trace_context().unwrap(); |
| 190 | + // SAFETY: although it's in a multithreaded context, |
| 191 | + // it's safe to assume that all the other threads are not |
| 192 | + // messing with env vars at this point. |
| 193 | + unsafe { |
| 194 | + std::env::set_var("TRACECONTEXT", ctx); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
| 198 | + |
171 | 199 | /// Main entry point for the shim. |
172 | 200 | /// |
173 | 201 | /// If the `opentelemetry` feature is enabled, this function will start the shim with OpenTelemetry tracing. |
@@ -199,69 +227,24 @@ pub fn shim_main<'a, I>( |
199 | 227 | std::process::exit(0); |
200 | 228 | } |
201 | 229 |
|
| 230 | + let config = config.unwrap_or_default(); |
| 231 | + |
202 | 232 | // Initialize the zygote and logger for the container process |
203 | 233 | #[cfg(unix)] |
204 | | - { |
205 | | - let default_config = Config::default(); |
206 | | - let config = config.as_ref().unwrap_or(&default_config); |
207 | | - init_zygote_and_logger(flags.debug, config); |
208 | | - } |
| 234 | + init_zygote_and_logger(flags.debug, &config); |
209 | 235 |
|
210 | | - #[cfg(feature = "opentelemetry")] |
211 | | - if otel_traces_enabled() { |
212 | | - // opentelemetry uses tokio, so we need to initialize a runtime |
213 | | - async { |
214 | | - let otlp_config = OtlpConfig::build_from_env().expect("Failed to build OtelConfig."); |
215 | | - let _guard = otlp_config |
216 | | - .init() |
217 | | - .expect("Failed to initialize OpenTelemetry."); |
218 | | - tokio::task::block_in_place(move || { |
219 | | - shim_main_inner::<I>(name, version, revision, shim_version, config); |
220 | | - }); |
221 | | - } |
222 | | - .block_on(); |
223 | | - } else { |
224 | | - shim_main_inner::<I>(name, version, revision, shim_version, config); |
225 | | - } |
| 236 | + async { |
| 237 | + let _guard = otel_traces_guard(); |
| 238 | + init_traces_context(); |
226 | 239 |
|
227 | | - #[cfg(not(feature = "opentelemetry"))] |
228 | | - { |
229 | | - shim_main_inner::<I>(name, version, revision, shim_version, config); |
| 240 | + let shim_version = shim_version.into().unwrap_or("v1"); |
| 241 | + let lower_name = name.to_lowercase(); |
| 242 | + let shim_id = format!("io.containerd.{lower_name}.{shim_version}"); |
| 243 | + |
| 244 | + run::<ShimCli<I>>(&shim_id, Some(config)).await; |
230 | 245 | } |
| 246 | + .block_on(); |
231 | 247 |
|
232 | 248 | #[cfg(target_os = "linux")] |
233 | 249 | log_mem(); |
234 | 250 | } |
235 | | - |
236 | | -#[cfg_attr(feature = "tracing", tracing::instrument(level = "Info"))] |
237 | | -fn shim_main_inner<'a, I>( |
238 | | - name: &str, |
239 | | - version: &str, |
240 | | - revision: impl Into<Option<&'a str>> + std::fmt::Debug, |
241 | | - shim_version: impl Into<Option<&'a str>> + std::fmt::Debug, |
242 | | - config: Option<Config>, |
243 | | -) where |
244 | | - I: 'static + Instance + Sync + Send, |
245 | | -{ |
246 | | - #[cfg(feature = "opentelemetry")] |
247 | | - { |
248 | | - // read TRACECONTEXT env var that's set by the parent process |
249 | | - if let Ok(ctx) = std::env::var("TRACECONTEXT") { |
250 | | - OtlpConfig::set_trace_context(&ctx).unwrap(); |
251 | | - } else { |
252 | | - let ctx = OtlpConfig::get_trace_context().unwrap(); |
253 | | - // SAFETY: although it's in a multithreaded context, |
254 | | - // it's safe to assume that all the other threads are not |
255 | | - // messing with env vars at this point. |
256 | | - unsafe { |
257 | | - std::env::set_var("TRACECONTEXT", ctx); |
258 | | - } |
259 | | - } |
260 | | - } |
261 | | - |
262 | | - let shim_version = shim_version.into().unwrap_or("v1"); |
263 | | - let lower_name = name.to_lowercase(); |
264 | | - let shim_id = format!("io.containerd.{lower_name}.{shim_version}"); |
265 | | - |
266 | | - run::<ShimCli<I>>(&shim_id, config); |
267 | | -} |
0 commit comments