@@ -434,6 +434,11 @@ async fn handle_create_sandbox_inner(
434434 * policy = validate_and_canonicalize_policy ( policy. clone ( ) ) ?;
435435 }
436436
437+ // Point the workload's OTel SDK at the supervisor relay when this gateway
438+ // can accept relayed telemetry. The values persist in the stored spec, so
439+ // restarts inherit them and `sandbox get` shows where traces go.
440+ inject_otel_relay_environment ( & mut spec. environment , state. otel_relay_exporter . is_some ( ) ) ;
441+
437442 // Process identity and MCP default materialization can increase the
438443 // protobuf size. Recheck the exact canonical spec before any middleware or
439444 // compute boundary can observe or persist it. The initial check remains
@@ -580,6 +585,31 @@ fn validate_create_sandbox_request_pre_io(
580585 Ok ( ( ) )
581586}
582587
588+ /// Set `OTEL_EXPORTER_OTLP_ENDPOINT` and `OTEL_EXPORTER_OTLP_PROTOCOL` to the
589+ /// supervisor relay when `otel_relay_enabled`, unless the caller already chose
590+ /// an endpoint. A caller-supplied endpoint is left untouched together with its
591+ /// protocol, since both describe their collector rather than ours. Returns
592+ /// whether anything was injected.
593+ fn inject_otel_relay_environment (
594+ environment : & mut HashMap < String , String > ,
595+ otel_relay_enabled : bool ,
596+ ) -> bool {
597+ use openshell_core:: sandbox_env:: {
598+ OTEL_EXPORTER_OTLP_ENDPOINT , OTEL_EXPORTER_OTLP_PROTOCOL , OTLP_RELAY_ENDPOINT ,
599+ } ;
600+ if !otel_relay_enabled || environment. contains_key ( OTEL_EXPORTER_OTLP_ENDPOINT ) {
601+ return false ;
602+ }
603+ environment. insert (
604+ OTEL_EXPORTER_OTLP_ENDPOINT . to_string ( ) ,
605+ OTLP_RELAY_ENDPOINT . to_string ( ) ,
606+ ) ;
607+ environment
608+ . entry ( OTEL_EXPORTER_OTLP_PROTOCOL . to_string ( ) )
609+ . or_insert_with ( || "http/protobuf" . to_string ( ) ) ;
610+ true
611+ }
612+
583613fn validate_template_create_governance_spec ( spec : & SandboxSpec ) -> Result < ( ) , Status > {
584614 if !spec. log_level . is_empty ( ) {
585615 return Err ( Status :: invalid_argument (
@@ -3246,6 +3276,121 @@ mod tests {
32463276 state
32473277 }
32483278
3279+ // ---- OTLP relay environment ----
3280+
3281+ async fn test_server_state_with_otel_relay ( ) -> Arc < ServerState > {
3282+ let mut state = test_server_state ( ) . await ;
3283+ Arc :: get_mut ( & mut state)
3284+ . expect ( "test server state should be uniquely owned" )
3285+ . otel_relay_exporter = Some ( Arc :: new (
3286+ crate :: otel_relay:: OtelRelayExporter :: lazy_for_test ( ) ,
3287+ ) ) ;
3288+ state
3289+ }
3290+
3291+ fn otel_create_request (
3292+ name : & str ,
3293+ environment : HashMap < String , String > ,
3294+ ) -> CreateSandboxRequest {
3295+ CreateSandboxRequest {
3296+ name : name. to_string ( ) ,
3297+ spec : Some ( SandboxSpec {
3298+ environment,
3299+ ..Default :: default ( )
3300+ } ) ,
3301+ labels : HashMap :: new ( ) ,
3302+ annotations : HashMap :: new ( ) ,
3303+ workspace_scope : Some ( openshell_core:: proto:: workspace_selector ( "default" ) ) ,
3304+ await_main_process_attachment : false ,
3305+ workload_template_name : String :: new ( ) ,
3306+ }
3307+ }
3308+
3309+ #[ test]
3310+ fn inject_otel_relay_environment_sets_endpoint_and_protocol_when_enabled ( ) {
3311+ let mut env = HashMap :: from ( [ ( "HOME" . to_string ( ) , "/home/user" . to_string ( ) ) ] ) ;
3312+ assert ! ( inject_otel_relay_environment( & mut env, true ) ) ;
3313+ assert_eq ! (
3314+ env. get( "OTEL_EXPORTER_OTLP_ENDPOINT" ) . map( String :: as_str) ,
3315+ Some ( openshell_core:: sandbox_env:: OTLP_RELAY_ENDPOINT )
3316+ ) ;
3317+ assert_eq ! (
3318+ env. get( "OTEL_EXPORTER_OTLP_PROTOCOL" ) . map( String :: as_str) ,
3319+ Some ( "http/protobuf" )
3320+ ) ;
3321+ assert_eq ! ( env. get( "HOME" ) . map( String :: as_str) , Some ( "/home/user" ) ) ;
3322+ }
3323+
3324+ #[ test]
3325+ fn inject_otel_relay_environment_is_a_noop_without_a_relay ( ) {
3326+ let mut env = HashMap :: new ( ) ;
3327+ assert ! ( !inject_otel_relay_environment( & mut env, false ) ) ;
3328+ assert ! ( env. is_empty( ) ) ;
3329+ }
3330+
3331+ #[ test]
3332+ fn inject_otel_relay_environment_keeps_a_caller_supplied_endpoint ( ) {
3333+ let mut env = HashMap :: from ( [ (
3334+ "OTEL_EXPORTER_OTLP_ENDPOINT" . to_string ( ) ,
3335+ "https://collector.example:4317" . to_string ( ) ,
3336+ ) ] ) ;
3337+ assert ! ( !inject_otel_relay_environment( & mut env, true ) ) ;
3338+ assert_eq ! (
3339+ env. get( "OTEL_EXPORTER_OTLP_ENDPOINT" ) . map( String :: as_str) ,
3340+ Some ( "https://collector.example:4317" )
3341+ ) ;
3342+ assert ! (
3343+ !env. contains_key( "OTEL_EXPORTER_OTLP_PROTOCOL" ) ,
3344+ "the caller's collector keeps the caller's protocol choice"
3345+ ) ;
3346+ }
3347+
3348+ #[ tokio:: test]
3349+ async fn create_sandbox_injects_relay_endpoint_when_gateway_has_an_exporter ( ) {
3350+ let state = test_server_state_with_otel_relay ( ) . await ;
3351+ let created = handle_create_sandbox (
3352+ & state,
3353+ authed_request ( otel_create_request ( "otel-relay" , HashMap :: new ( ) ) ) ,
3354+ )
3355+ . await
3356+ . expect ( "create should succeed" )
3357+ . into_inner ( )
3358+ . sandbox
3359+ . expect ( "created sandbox" ) ;
3360+
3361+ let env = created. spec . expect ( "resolved sandbox spec" ) . environment ;
3362+ assert_eq ! (
3363+ env. get( "OTEL_EXPORTER_OTLP_ENDPOINT" ) . map( String :: as_str) ,
3364+ Some ( openshell_core:: sandbox_env:: OTLP_RELAY_ENDPOINT )
3365+ ) ;
3366+ assert_eq ! (
3367+ env. get( "OTEL_EXPORTER_OTLP_PROTOCOL" ) . map( String :: as_str) ,
3368+ Some ( "http/protobuf" )
3369+ ) ;
3370+ }
3371+
3372+ #[ tokio:: test]
3373+ async fn create_sandbox_leaves_environment_alone_without_an_exporter ( ) {
3374+ let state = test_server_state ( ) . await ;
3375+ let created = handle_create_sandbox (
3376+ & state,
3377+ authed_request ( otel_create_request (
3378+ "no-relay" ,
3379+ HashMap :: from ( [ ( "HOME" . to_string ( ) , "/home/user" . to_string ( ) ) ] ) ,
3380+ ) ) ,
3381+ )
3382+ . await
3383+ . expect ( "create should succeed" )
3384+ . into_inner ( )
3385+ . sandbox
3386+ . expect ( "created sandbox" ) ;
3387+
3388+ let env = created. spec . expect ( "resolved sandbox spec" ) . environment ;
3389+ assert_eq ! ( env. get( "HOME" ) . map( String :: as_str) , Some ( "/home/user" ) ) ;
3390+ assert ! ( !env. contains_key( "OTEL_EXPORTER_OTLP_ENDPOINT" ) ) ;
3391+ assert ! ( !env. contains_key( "OTEL_EXPORTER_OTLP_PROTOCOL" ) ) ;
3392+ }
3393+
32493394 // ---- shell_escape ----
32503395
32513396 #[ test]
0 commit comments