Skip to content

Commit 53bf840

Browse files
authored
feat(cli): forward TRACEPARENT/TRACESTATE to gateway for CI trace continuity (NVIDIA#2931)
* feat(cli): forward TRACEPARENT/TRACESTATE to gateway for CI trace continuity The CLI's gateway gRPC channel now passively forwards the W3C trace context (TRACEPARENT/TRACESTATE) from its environment, so a CI pipeline's trace parents the gateway's spans. This needs no OTel collector or provider on the CLI side - short-lived clients on CI runners and jump hosts just pass through what the CI system already exports. Add EnvTraceContextInterceptor to openshell-otel: it reads the trace context once at construction and injects it as gRPC metadata, and is a no-op when the context is unset or invalid. Compose it with EdgeAuthInterceptor via a new GatewayInterceptor, since tonic allows only one interceptor per channel. Closes issue NVIDIA#2639 Signed-off-by: Chris Moore <[email protected]> * fix(cli): normalize trace-context env reading and cover interceptor composition Review follow-ups on the trace-context forwarding feature: - Normalize each TRACEPARENT/TRACESTATE spelling (trim, drop blanks) before the lower-case fallback so a blank upper-case value no longer shadows a valid lower-case one - Share one env-reading helper between EnvTraceContextInterceptor and set_parent_from_environment so both paths agree on what counts as set - Pass the TRACEPARENT_ENV/TRACESTATE_ENV constants instead of duplicated string literals - Log at debug level when a present TRACEPARENT is invalid, keeping the passive no-op behavior but leaving a grep-able signal - Cover EnvTraceContext::from_env (both spellings, padding, blanks) and the GatewayInterceptor auth+trace composition with tests - Drop tracestate when traceparent is invalid, and assert it in tests - Narrow the traces.mdx claim to standard CLI commands, since term and completions build clients without the trace interceptor Signed-off-by: Chris Moore <[email protected]> * fix(otel): address trace-context review findings - gate the OTLP export stack behind the new `export` feature and build the CLI against openshell-otel with default-features = false, so the CLI no longer compiles and links an exporter it never calls - split the env-reading test so the mixed-case steps only run where environment names are case-sensitive; the Windows MSVC lane no longer fails on them - restore mutated env vars through a drop guard so a failed assertion cannot leak them into other tests in the same process - add an SDK test proving auth_interceptor() clones see a refreshed OIDC token through the shared bearer slot - word the trace-forwarding docs around OpenTelemetry integrations instead of implying CI runners export TRACEPARENT natively Signed-off-by: Chris Moore <[email protected]> --------- Signed-off-by: Chris Moore <[email protected]>
1 parent e4dd312 commit 53bf840

9 files changed

Lines changed: 1298 additions & 830 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/openshell-cli/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ path = "src/main.rs"
1818
openshell-crypto = { workspace = true }
1919
openshell-bootstrap = { path = "../openshell-bootstrap" }
2020
openshell-core = { path = "../openshell-core", default-features = false }
21+
openshell-otel = { path = "../openshell-otel", default-features = false }
2122
openshell-policy = { path = "../openshell-policy" }
2223
openshell-providers = { path = "../openshell-providers" }
2324
openshell-sdk = { path = "../openshell-sdk" }

‎crates/openshell-cli/src/tls.rs‎

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use miette::{IntoDiagnostic, Result, WrapErr};
55
pub use openshell_core::replica_routing::{
66
owner_replica, retry_unrouted, routed_to, with_owner_hint,
77
};
8+
use openshell_otel::EnvTraceContextInterceptor;
89
use openshell_sdk::{AuthConfig, ClientConfig, ClientIdentity, OpenShellClient};
910
use rustls::{
1011
RootCertStore,
@@ -16,7 +17,32 @@ use std::path::PathBuf;
1617
use tonic::transport::Channel;
1718

1819
/// Concrete gRPC client type used by all commands.
19-
pub type GrpcClient = openshell_sdk::raw::AuthedGrpcClient;
20+
pub type GrpcClient = openshell_sdk::raw::GrpcClient<
21+
tonic::service::interceptor::InterceptedService<Channel, GatewayInterceptor>,
22+
>;
23+
24+
/// Interceptor stack applied to gateway requests from clients built by
25+
/// [`grpc_client`].
26+
///
27+
/// Runs authentication header injection first, then passive W3C trace-context
28+
/// forwarding so a CI pipeline's `TRACEPARENT` extends into the gateway's
29+
/// spans. tonic allows a single interceptor per channel, so the two concerns
30+
/// are composed here.
31+
#[derive(Clone)]
32+
pub struct GatewayInterceptor {
33+
auth: openshell_sdk::EdgeAuthInterceptor,
34+
trace: EnvTraceContextInterceptor,
35+
}
36+
37+
impl tonic::service::Interceptor for GatewayInterceptor {
38+
fn call(
39+
&mut self,
40+
request: tonic::Request<()>,
41+
) -> std::result::Result<tonic::Request<()>, tonic::Status> {
42+
let request = self.auth.call(request)?;
43+
self.trace.call(request)
44+
}
45+
}
2046

2147
#[derive(Clone, Debug, Default)]
2248
pub struct TlsOptions {
@@ -313,24 +339,82 @@ pub async fn build_channel(server: &str, tls: &TlsOptions) -> Result<Channel> {
313339

314340
/// Build the SDK client and return its raw gRPC escape hatch.
315341
///
316-
/// When `tls.edge_token` is set, the returned client is wrapped with an
317-
/// interceptor that injects authentication headers on every request.
318-
/// Otherwise, standard mTLS is used (interceptor is a no-op).
342+
/// The returned client carries a [`GatewayInterceptor`] that injects
343+
/// authentication headers (when a token is set) and passively forwards the
344+
/// `TRACEPARENT`/`TRACESTATE` trace context from the environment on every
345+
/// request. With no token and no trace context, the interceptor is a no-op.
319346
pub async fn grpc_client(server: &str, tls: &TlsOptions) -> Result<GrpcClient> {
320347
let config = sdk_client_config(server, tls)?;
321348
// Keep the SDK's transport state machine out of every command future.
322349
// Most CLI commands compose this helper into larger async operations.
323350
let client = Box::pin(OpenShellClient::connect(config))
324351
.await
325352
.into_diagnostic()?;
326-
Ok(client.raw_grpc())
353+
// tonic allows a single interceptor per channel: compose the SDK's auth
354+
// interceptor (sharing its live bearer slot) with the CLI's passive
355+
// trace-context forwarding.
356+
let interceptor = GatewayInterceptor {
357+
auth: client.auth_interceptor(),
358+
trace: EnvTraceContextInterceptor::from_env(),
359+
};
360+
Ok(openshell_sdk::raw::GrpcClient::with_interceptor(
361+
client.channel(),
362+
interceptor,
363+
))
327364
}
328365

329366
#[cfg(test)]
330367
mod tests {
331-
use super::{TlsOptions, sdk_client_config};
332-
use openshell_sdk::AuthConfig;
368+
use super::{GatewayInterceptor, TlsOptions, sdk_client_config};
369+
use crate::TEST_ENV_LOCK;
370+
use openshell_otel::EnvTraceContextInterceptor;
371+
use openshell_sdk::{AuthConfig, EdgeAuthInterceptor};
333372
use std::io::Write;
373+
use tonic::service::Interceptor as _;
374+
375+
#[test]
376+
fn gateway_interceptor_composes_auth_and_trace_forwarding() {
377+
let _guard = TEST_ENV_LOCK
378+
.lock()
379+
.unwrap_or_else(std::sync::PoisonError::into_inner);
380+
let traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01";
381+
temp_env::with_vars(
382+
[
383+
("TRACEPARENT", Some(traceparent)),
384+
("TRACESTATE", Some("vendor=value")),
385+
],
386+
|| {
387+
let mut interceptor = GatewayInterceptor {
388+
auth: EdgeAuthInterceptor::new(Some("test-token"), None).unwrap(),
389+
trace: EnvTraceContextInterceptor::from_env(),
390+
};
391+
392+
let request = interceptor.call(tonic::Request::new(())).unwrap();
393+
394+
assert_eq!(
395+
request
396+
.metadata()
397+
.get("authorization")
398+
.and_then(|value| value.to_str().ok()),
399+
Some("Bearer test-token")
400+
);
401+
assert_eq!(
402+
request
403+
.metadata()
404+
.get("traceparent")
405+
.and_then(|value| value.to_str().ok()),
406+
Some(traceparent)
407+
);
408+
assert_eq!(
409+
request
410+
.metadata()
411+
.get("tracestate")
412+
.and_then(|value| value.to_str().ok()),
413+
Some("vendor=value")
414+
);
415+
},
416+
);
417+
}
334418

335419
#[test]
336420
fn sdk_config_carries_cli_mtls_materials() {

‎crates/openshell-otel/Cargo.toml‎

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@ license.workspace = true
1111
repository.workspace = true
1212

1313
[dependencies]
14-
async-trait = "0.1"
15-
bytes = { workspace = true }
16-
futures = { workspace = true }
14+
async-trait = { version = "0.1", optional = true }
15+
bytes = { workspace = true, optional = true }
16+
futures = { workspace = true, optional = true }
1717
http = { workspace = true }
18-
http-body-util = { workspace = true }
19-
hyper-rustls = { version = "0.27", default-features = false, features = ["http1", "logging", "tls12"] }
20-
hyper-util = { workspace = true, features = ["client-legacy", "http1", "tokio"] }
21-
metrics = { workspace = true }
22-
openshell-crypto = { workspace = true }
23-
openshell-ocsf = { path = "../openshell-ocsf" }
18+
http-body-util = { workspace = true, optional = true }
19+
hyper-rustls = { version = "0.27", default-features = false, optional = true, features = ["http1", "logging", "tls12"] }
20+
hyper-util = { workspace = true, optional = true, features = ["client-legacy", "http1", "tokio"] }
21+
metrics = { workspace = true, optional = true }
22+
openshell-crypto = { workspace = true, optional = true }
23+
openshell-ocsf = { path = "../openshell-ocsf", optional = true }
2424
opentelemetry = { workspace = true }
25-
opentelemetry_sdk = { workspace = true, features = ["logs"] }
26-
opentelemetry-http = { workspace = true }
27-
opentelemetry-otlp = { workspace = true, features = ["gzip-http", "http-proto", "logs"] }
28-
rustls = { workspace = true }
29-
rustls-native-certs = { workspace = true }
30-
serde_json = { workspace = true }
31-
thiserror = { workspace = true }
32-
tokio = { workspace = true }
25+
opentelemetry_sdk = { workspace = true }
26+
opentelemetry-http = { workspace = true, optional = true }
27+
opentelemetry-otlp = { workspace = true, optional = true, features = ["gzip-http", "http-proto", "logs"] }
28+
rustls = { workspace = true, optional = true }
29+
rustls-native-certs = { workspace = true, optional = true }
30+
serde_json = { workspace = true, optional = true }
31+
thiserror = { workspace = true, optional = true }
32+
tokio = { workspace = true, optional = true }
3333
tracing = { workspace = true }
3434
tracing-opentelemetry = { workspace = true }
35-
tracing-subscriber = { workspace = true }
35+
tracing-subscriber = { workspace = true, optional = true }
3636
tonic = { workspace = true }
37-
tower-http = { workspace = true }
38-
zeroize = { workspace = true }
37+
tower-http = { workspace = true, optional = true }
38+
zeroize = { workspace = true, optional = true }
3939

4040
[dev-dependencies]
4141
openshell-otel-test-support = { path = "../openshell-otel-test-support" }
@@ -44,6 +44,37 @@ opentelemetry_sdk = { workspace = true, features = ["testing"] }
4444
prost = { workspace = true }
4545
tempfile = "3"
4646
tokio = { workspace = true, features = ["test-util"] }
47+
tracing-subscriber = { workspace = true }
48+
49+
[features]
50+
default = ["export"]
51+
# OTLP/gRPC export: tracer providers, tracing layers, compute-driver and
52+
# gRPC tracing machinery, and the OCSF log bridge. Services that report to
53+
# a collector keep the default; clients that only forward trace context
54+
# (e.g. the CLI) opt out with `default-features = false` so the exporter
55+
# stack stays out of their binaries.
56+
export = [
57+
"dep:async-trait",
58+
"dep:bytes",
59+
"dep:futures",
60+
"dep:http-body-util",
61+
"dep:hyper-rustls",
62+
"dep:hyper-util",
63+
"dep:metrics",
64+
"dep:openshell-crypto",
65+
"dep:openshell-ocsf",
66+
"dep:opentelemetry-http",
67+
"dep:opentelemetry-otlp",
68+
"dep:rustls",
69+
"dep:rustls-native-certs",
70+
"dep:serde_json",
71+
"dep:thiserror",
72+
"dep:tokio",
73+
"dep:tower-http",
74+
"dep:tracing-subscriber",
75+
"dep:zeroize",
76+
"opentelemetry_sdk/logs",
77+
]
4778

4879
[lints]
4980
workspace = true

0 commit comments

Comments
 (0)