Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/openshell-core/src/sandbox_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,12 @@ pub const OTEL_EXPORTER_OTLP_PROTOCOL: &str = "OTEL_EXPORTER_OTLP_PROTOCOL";
/// has chosen where traces go, so the relay default is left out.
pub const OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT";

/// Logs-specific endpoint variable. Never injected; handled like the traces one.
pub const OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT";

/// Metrics-specific endpoint variable. Never injected; handled like the traces one.
pub const OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT";

/// Standard OpenTelemetry environment variable for OTLP exporter compression.
///
/// Set to `none` alongside [`OTEL_EXPORTER_OTLP_ENDPOINT`], because the
Expand All @@ -544,6 +550,12 @@ pub const OTEL_EXPORTER_OTLP_COMPRESSION: &str = "OTEL_EXPORTER_OTLP_COMPRESSION
/// every trace export.
pub const OTEL_EXPORTER_OTLP_TRACES_COMPRESSION: &str = "OTEL_EXPORTER_OTLP_TRACES_COMPRESSION";

/// Logs-specific compression variable, set to `none` like the traces one.
pub const OTEL_EXPORTER_OTLP_LOGS_COMPRESSION: &str = "OTEL_EXPORTER_OTLP_LOGS_COMPRESSION";

/// Metrics-specific compression variable, set to `none` like the traces one.
pub const OTEL_EXPORTER_OTLP_METRICS_COMPRESSION: &str = "OTEL_EXPORTER_OTLP_METRICS_COMPRESSION";

/// Reserved destination agent processes export OTLP to.
///
/// This address is never routed. A workload `connect()` to any non-loopback
Expand Down
8 changes: 4 additions & 4 deletions crates/openshell-sandbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ dropped.
When `AgentSpec::otlp_relay_active` is set, `child_env::apply_otlp_relay_env`
gives every workload process (main, exec, and SSH) `OTEL_EXPORTER_OTLP_ENDPOINT`
pointing at the relay address, `OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf`, and
`none` for `OTEL_EXPORTER_OTLP_COMPRESSION` and
`OTEL_EXPORTER_OTLP_TRACES_COMPRESSION`, and removes any inherited
`OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`. These defaults apply after the provider
`none` for `OTEL_EXPORTER_OTLP_COMPRESSION` and the traces, logs, and metrics
compression variables, and removes any inherited traces, logs, or
metrics endpoint variable. These defaults apply after the provider
variables. If the creation request or a per-exec environment names a generic
or traces-specific OTLP endpoint, the runtime injects nothing, and the
or signal-specific OTLP endpoint, the runtime injects nothing, and the
workload exports to that endpoint under the sandbox's network policy. An
endpoint in the image `ENV` does the same for the main process; exec children
start from a cleared environment (`ChildEnvironment::Cleared`), never see the
Expand Down
48 changes: 36 additions & 12 deletions crates/openshell-sandbox/src/child_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ use std::hash::BuildHasher;
use std::path::Path;

use openshell_core::sandbox_env::{
OTEL_EXPORTER_OTLP_COMPRESSION, OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_PROTOCOL,
OTEL_EXPORTER_OTLP_TRACES_COMPRESSION, OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, OTLP_RELAY_ENDPOINT,
OTEL_EXPORTER_OTLP_COMPRESSION, OTEL_EXPORTER_OTLP_ENDPOINT,
OTEL_EXPORTER_OTLP_LOGS_COMPRESSION, OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
OTEL_EXPORTER_OTLP_METRICS_COMPRESSION, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
OTEL_EXPORTER_OTLP_PROTOCOL, OTEL_EXPORTER_OTLP_TRACES_COMPRESSION,
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, OTLP_RELAY_ENDPOINT,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -46,11 +49,13 @@ pub fn tls_env_vars_for_mode<S: BuildHasher>(
.collect()
}

/// Variables that name where an OpenTelemetry SDK exports traces. A layer of
/// the workload environment that sets one has chosen a destination.
const OTLP_ENDPOINT_VARS: [&str; 2] = [
/// Variables that name where an OpenTelemetry SDK exports. A layer of the
/// workload environment that sets one has chosen a destination.
const OTLP_ENDPOINT_VARS: [&str; 4] = [
OTEL_EXPORTER_OTLP_ENDPOINT,
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
];

/// OpenTelemetry exporter variables that point agent SDKs at the
Expand All @@ -59,16 +64,18 @@ const OTLP_ENDPOINT_VARS: [&str; 2] = [
/// Only the generic endpoint and protocol are set. Signal-specific variables
/// take precedence over them in every SDK, so an endpoint the workload sets
/// later at any layer, generic or traces-specific, wins over this default.
/// Compression is disabled for both the generic and the traces-specific
/// Compression is disabled for the generic and every signal-specific
/// variable because the relay refuses compressed bodies, and a `gzip` value
/// from the image or the creation request would otherwise make every export
/// fail.
pub fn otlp_relay_env_vars() -> [(&'static str, &'static str); 4] {
pub fn otlp_relay_env_vars() -> [(&'static str, &'static str); 6] {
[
(OTEL_EXPORTER_OTLP_ENDPOINT, OTLP_RELAY_ENDPOINT),
(OTEL_EXPORTER_OTLP_PROTOCOL, "http/protobuf"),
(OTEL_EXPORTER_OTLP_COMPRESSION, "none"),
(OTEL_EXPORTER_OTLP_TRACES_COMPRESSION, "none"),
(OTEL_EXPORTER_OTLP_LOGS_COMPRESSION, "none"),
(OTEL_EXPORTER_OTLP_METRICS_COMPRESSION, "none"),
]
}

Expand All @@ -84,7 +91,7 @@ pub enum ChildEnvironment {
}

/// Whether one layer of the workload environment names an OTLP endpoint,
/// generic or traces-specific.
/// generic or signal-specific.
pub fn names_otlp_endpoint<S: BuildHasher>(environment: &HashMap<String, String, S>) -> bool {
OTLP_ENDPOINT_VARS
.iter()
Expand Down Expand Up @@ -118,8 +125,8 @@ fn relay_default_applies<S: BuildHasher>(
/// counts for a child that inherits the sandbox environment; a child started
/// from a cleared environment never sees the image's endpoint, so the relay
/// default still applies to it. When the default is injected, a
/// traces-specific endpoint can only have come from a provider, and is
/// removed so a provider cannot redirect traces.
/// signal-specific endpoint can only have come from a provider, and is
/// removed so a provider cannot redirect that signal.
pub fn apply_otlp_relay_env<S: BuildHasher>(
cmd: &mut impl EnvTarget,
relay_active: bool,
Expand All @@ -132,7 +139,13 @@ pub fn apply_otlp_relay_env<S: BuildHasher>(
}) {
return;
}
cmd.remove_var(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT);
for key in [
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
] {
cmd.remove_var(key);
}
for (key, value) in otlp_relay_env_vars() {
cmd.set_var(key, value);
}
Expand Down Expand Up @@ -231,6 +244,8 @@ mod tests {
("OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf"),
("OTEL_EXPORTER_OTLP_COMPRESSION", "none"),
("OTEL_EXPORTER_OTLP_TRACES_COMPRESSION", "none"),
("OTEL_EXPORTER_OTLP_LOGS_COMPRESSION", "none"),
("OTEL_EXPORTER_OTLP_METRICS_COMPRESSION", "none"),
]
);
}
Expand Down Expand Up @@ -265,7 +280,14 @@ mod tests {
.map(String::as_str),
Some("http://192.0.0.8:4318")
);
assert_eq!(env.removed, ["OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"]);
assert_eq!(
env.removed,
[
"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT",
"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT",
"OTEL_EXPORTER_OTLP_METRICS_ENDPOINT"
]
);

let mut env = RecordedEnv::default();
apply_otlp_relay_env(
Expand Down Expand Up @@ -309,6 +331,8 @@ mod tests {
for key in [
"OTEL_EXPORTER_OTLP_ENDPOINT",
"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT",
"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT",
"OTEL_EXPORTER_OTLP_METRICS_ENDPOINT",
] {
let value = "http://collector.example:4318".to_string();
let declared = HashMap::from([(key.to_string(), value.clone())]);
Expand Down
30 changes: 26 additions & 4 deletions crates/openshell-sandbox/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,8 @@ mod tests {
for key in [
openshell_core::sandbox_env::OTEL_EXPORTER_OTLP_ENDPOINT,
openshell_core::sandbox_env::OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
openshell_core::sandbox_env::OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
openshell_core::sandbox_env::OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
] {
command.env_remove(key);
}
Expand Down Expand Up @@ -1389,6 +1391,8 @@ mod tests {
fn canonical_process_sees_relay_endpoint_and_never_the_collector() {
use openshell_core::sandbox_env::{
OTEL_EXPORTER_OTLP_COMPRESSION, OTEL_EXPORTER_OTLP_ENDPOINT,
OTEL_EXPORTER_OTLP_LOGS_COMPRESSION, OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
OTEL_EXPORTER_OTLP_METRICS_COMPRESSION, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
OTEL_EXPORTER_OTLP_PROTOCOL, OTEL_EXPORTER_OTLP_TRACES_COMPRESSION,
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, OTLP_RELAY_ENDPOINT,
};
Expand Down Expand Up @@ -1450,6 +1454,14 @@ mod tests {
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.to_string(),
"http://provider.invalid/v1/traces".to_string(),
),
(
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT.to_string(),
"http://provider.invalid/v1/logs".to_string(),
),
(
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT.to_string(),
"http://provider.invalid/v1/metrics".to_string(),
),
])
};

Expand All @@ -1460,13 +1472,17 @@ mod tests {
HashMap::from([
(OTEL_EXPORTER_OTLP_PROTOCOL.into(), "grpc".into()),
(OTEL_EXPORTER_OTLP_TRACES_COMPRESSION.into(), "gzip".into()),
(OTEL_EXPORTER_OTLP_LOGS_COMPRESSION.into(), "gzip".into()),
(OTEL_EXPORTER_OTLP_METRICS_COMPRESSION.into(), "gzip".into()),
]),
provider_redirect(),
true,
);
for key in [
OTEL_EXPORTER_OTLP_COMPRESSION,
OTEL_EXPORTER_OTLP_TRACES_COMPRESSION,
OTEL_EXPORTER_OTLP_LOGS_COMPRESSION,
OTEL_EXPORTER_OTLP_METRICS_COMPRESSION,
] {
assert_eq!(
get(&variables, key).as_deref(),
Expand All @@ -1484,10 +1500,16 @@ mod tests {
Some("http/protobuf"),
"a declared protocol without an endpoint is replaced: {variables:?}"
);
assert!(
!variables.contains_key(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT),
"no signal-specific endpoint is injected, and a provider's is removed: {variables:?}"
);
for key in [
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
] {
assert!(
!variables.contains_key(key),
"no signal-specific endpoint is injected, and a provider's is removed: {variables:?}"
);
}
assert!(
!variables
.values()
Expand Down
4 changes: 2 additions & 2 deletions crates/openshell-supervisor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ Console JSON is installed independently of optional file appenders and uses an I

## OTLP agent trace relay

The `otlp_relay` module forwards the workload's OTLP/HTTP protobuf trace exports to the collector named by `OPENSHELL_OTLP_AGENT_ENDPOINT`, over the transport named by `OPENSHELL_OTLP_AGENT_PROTOCOL` (`grpc` when unset, or `http/protobuf`). Each export carries the collector headers from the launch bundle's `agent_otlp_headers` and, for an `https` collector, verifies it and authenticates with `agent_otlp_tls`; the supervisor's own span and OCSF exporters use the `traces_` and `ocsf_` fields the same way. The gateway sets those variables only when `[openshell.gateway.otlp.agent]` is enabled, so trace export alone leaves the relay inactive. Metrics and logs exports to the relay are acknowledged and discarded. The workload exports to the reserved address `192.0.0.8:4318`; the sandbox broker stages that connection to the supervisor, and the network proxy hands the stream to the relay's HTTP/1.1 receiver through the generic reserved-destination hook instead of evaluating policy. The receiver replaces the `openshell.sandbox.id` and `openshell.telemetry.source=agent` resource attributes, pushes the re-encoded batch into a bounded buffer, and a single export task forwards the encoded batches to the collector with a lazy connection.
The `otlp_relay` module forwards the workload's OTLP/HTTP protobuf trace, log, and metrics exports to the collector named by `OPENSHELL_OTLP_AGENT_ENDPOINT`, over the transport named by `OPENSHELL_OTLP_AGENT_PROTOCOL` (`grpc` when unset, or `http/protobuf`). Each export carries the collector headers from the launch bundle's `agent_otlp_headers` and, for an `https` collector, verifies it and authenticates with `agent_otlp_tls`; the supervisor's own span and OCSF exporters use the `traces_` and `ocsf_` fields the same way. The gateway sets those variables only when `[openshell.gateway.otlp.agent]` is enabled, so trace export alone leaves the relay inactive. The workload exports to the reserved address `192.0.0.8:4318`; the sandbox broker stages that connection to the supervisor, and the network proxy hands the stream to the relay's HTTP/1.1 receiver through the generic reserved-destination hook instead of evaluating policy. The receiver replaces the `openshell.sandbox.id` and `openshell.telemetry.source=agent` resource attributes, pushes the re-encoded batch into the bounded buffer of its signal, and a single export task forwards the encoded batches to the collector with a lazy connection.
Comment thread
ericcurtin marked this conversation as resolved.
Resolved

Limits are fixed: 2 MiB per request body, at most 512 `ResourceSpans` entries, 256 KiB of encoded `Resource` fields, and 8192 resource attributes per request (each of these answers 413), a 10 second header-read timeout, 16 concurrent connections, 32 buffered batches, and a 5 second deadline per export attempt. Only the resources are decoded; spans are copied byte for byte, at most two requests at a time. Worst-case memory attributable to the agent is about 67 MiB of buffered batches, plus 32 MiB of bodies awaiting enrichment across the 16 connections, plus about 12 MiB for the two enrichments in progress, plus about 4 MiB for the batch being exported and its encoded copy. A full buffer answers 503 with `Retry-After`. Without an agent endpoint nothing starts and the proxy refuses the relay address. After the agent exits, `run_sandbox_with_backend` flushes buffered batches for up to 5 seconds before it reports the exit to the gateway, then logs a final summary of rejected and failed counts. Flushes skip the wait while the most recent export failed, and batches still pending when the relay is dropped without a flush count as failed. The supervisor tells the sandbox runtime through `AgentSpec::otlp_relay_active` whether the relay runs, and the runtime injects the generic `OTEL_EXPORTER_OTLP_*` defaults only then.
Limits are fixed: 2 MiB per request body, at most 512 `ResourceSpans` entries, 256 KiB of encoded `Resource` fields, and 8192 resource attributes per request (each of these answers 413), a 10 second header-read timeout, 16 concurrent connections, 32 buffered trace batches (8 each for logs and metrics), and a 5 second deadline per export attempt. Only the resources are decoded; spans are copied byte for byte, at most two requests at a time. Worst-case memory attributable to the agent is about 100 MiB of buffered batches, plus 32 MiB of bodies awaiting enrichment across the 16 connections, plus about 12 MiB for the two enrichments in progress, plus about 4 MiB for the batch being exported and its encoded copy. A full buffer answers 503 with `Retry-After`. Without an agent endpoint nothing starts and the proxy refuses the relay address. After the agent exits, `run_sandbox_with_backend` flushes buffered batches for up to 5 seconds before it reports the exit to the gateway, then logs a final summary of rejected and failed counts. Flushes skip the wait while the most recent export failed, and batches still pending when the relay is dropped without a flush count as failed. The supervisor tells the sandbox runtime through `AgentSpec::otlp_relay_active` whether the relay runs, and the runtime injects the generic `OTEL_EXPORTER_OTLP_*` defaults only then.

## Safe runtime and parent liveness

Expand Down
Loading