|
3 | 3 |
|
4 | 4 | //! OpenTelemetry trace exporting for the Docker compute driver. |
5 | 5 |
|
6 | | -use openshell_otel::{OtlpTraceConfig, SdkTracerProvider, ServiceName, SetupError}; |
7 | | -use tracing::Subscriber; |
| 6 | +use http::Request; |
| 7 | +use openshell_otel::{ |
| 8 | + HeaderMapExtractor, OtlpTraceConfig, RecordGrpcFailure, RecordGrpcStatus, SdkTracerProvider, |
| 9 | + ServiceName, SetupError, |
| 10 | +}; |
| 11 | +use opentelemetry::propagation::TextMapPropagator as _; |
| 12 | +use opentelemetry::trace::TraceContextExt as _; |
| 13 | +use opentelemetry_sdk::propagation::TraceContextPropagator; |
| 14 | +use tower_http::trace::{GrpcMakeClassifier, MakeSpan, TraceLayer}; |
| 15 | +use tracing::{Span, Subscriber}; |
| 16 | +use tracing_opentelemetry::OpenTelemetrySpanExt as _; |
8 | 17 | use tracing_subscriber::registry::LookupSpan; |
9 | 18 |
|
10 | 19 | const SERVICE_NAME: &str = "openshell-driver-docker"; |
11 | 20 | const INSTRUMENTATION_SCOPE: &str = "openshell-driver-docker"; |
| 21 | +const COMPUTE_DRIVER_SERVICE: &str = "openshell.compute.v1.ComputeDriver"; |
12 | 22 | pub const IN_PROCESS_TARGET_PREFIX: &str = "openshell_driver_docker"; |
13 | 23 |
|
| 24 | +/// Trace inbound standalone compute-driver RPCs and continue gateway context. |
| 25 | +pub fn compute_driver_rpc_layer() -> TraceLayer< |
| 26 | + GrpcMakeClassifier, |
| 27 | + ComputeDriverRpcSpan, |
| 28 | + (), |
| 29 | + RecordGrpcStatus, |
| 30 | + (), |
| 31 | + RecordGrpcStatus, |
| 32 | + RecordGrpcFailure, |
| 33 | +> { |
| 34 | + TraceLayer::new_for_grpc() |
| 35 | + .make_span_with(ComputeDriverRpcSpan) |
| 36 | + .on_request(()) |
| 37 | + .on_response(RecordGrpcStatus) |
| 38 | + .on_body_chunk(()) |
| 39 | + .on_eos(RecordGrpcStatus) |
| 40 | + .on_failure(RecordGrpcFailure) |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(Debug, Clone, Copy)] |
| 44 | +pub struct ComputeDriverRpcSpan; |
| 45 | + |
| 46 | +impl<B> MakeSpan<B> for ComputeDriverRpcSpan { |
| 47 | + fn make_span(&mut self, request: &Request<B>) -> Span { |
| 48 | + let (operation, method) = compute_driver_rpc_operation(request.uri().path()); |
| 49 | + let span = tracing::info_span!( |
| 50 | + "driver_rpc", |
| 51 | + otel.name = operation, |
| 52 | + otel.kind = "server", |
| 53 | + otel.status_code = tracing::field::Empty, |
| 54 | + rpc.system = "grpc", |
| 55 | + rpc.service = COMPUTE_DRIVER_SERVICE, |
| 56 | + rpc.method = method, |
| 57 | + rpc.grpc.status_code = tracing::field::Empty, |
| 58 | + ); |
| 59 | + let parent = TraceContextPropagator::new().extract_with_context( |
| 60 | + &opentelemetry::Context::new(), |
| 61 | + &HeaderMapExtractor::new(request.headers()), |
| 62 | + ); |
| 63 | + if parent.span().span_context().is_valid() { |
| 64 | + let _ = span.set_parent(parent); |
| 65 | + } |
| 66 | + span |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +pub(crate) fn compute_driver_rpc_operation(path: &str) -> (&'static str, &'static str) { |
| 71 | + match path.rsplit('/').next() { |
| 72 | + Some("GetCapabilities") => ("driver.get_capabilities", "get_capabilities"), |
| 73 | + Some("GetGatewayListenerRequirements") => ( |
| 74 | + "driver.get_gateway_listener_requirements", |
| 75 | + "get_gateway_listener_requirements", |
| 76 | + ), |
| 77 | + Some("ValidateSandboxCreate") => { |
| 78 | + ("driver.validate_sandbox_create", "validate_sandbox_create") |
| 79 | + } |
| 80 | + Some("CreateSandbox") => ("driver.create_sandbox", "create_sandbox"), |
| 81 | + Some("GetSandbox") => ("driver.get_sandbox", "get_sandbox"), |
| 82 | + Some("ListSandboxes") => ("driver.list_sandboxes", "list_sandboxes"), |
| 83 | + Some("StopSandbox") => ("driver.stop_sandbox", "stop_sandbox"), |
| 84 | + Some("StartSandbox") => ("driver.start_sandbox", "start_sandbox"), |
| 85 | + Some("DeleteSandbox") => ("driver.delete_sandbox", "delete_sandbox"), |
| 86 | + Some("WatchSandboxes") => ("driver.watch_sandboxes", "watch_sandboxes"), |
| 87 | + Some("EnsureWorkspace") => ("driver.ensure_workspace", "ensure_workspace"), |
| 88 | + Some("DeleteWorkspace") => ("driver.delete_workspace", "delete_workspace"), |
| 89 | + _ => ("driver.unknown", "unknown"), |
| 90 | + } |
| 91 | +} |
| 92 | + |
14 | 93 | #[must_use] |
15 | 94 | pub fn provider_for(endpoint: Option<&str>) -> (Option<SdkTracerProvider>, Option<SetupError>) { |
16 | 95 | openshell_otel::provider_for(endpoint.map(|endpoint| OtlpTraceConfig { |
@@ -57,6 +136,20 @@ mod tests { |
57 | 136 | use openshell_otel_test_support::OtlpTestServer; |
58 | 137 | use tracing_subscriber::layer::SubscriberExt as _; |
59 | 138 |
|
| 139 | + #[test] |
| 140 | + fn compute_driver_rpc_names_are_explicitly_mapped_and_schema_bounded() { |
| 141 | + assert_eq!( |
| 142 | + super::compute_driver_rpc_operation( |
| 143 | + "/openshell.compute.v1.ComputeDriver/CreateSandbox" |
| 144 | + ), |
| 145 | + ("driver.create_sandbox", "create_sandbox") |
| 146 | + ); |
| 147 | + assert_eq!( |
| 148 | + super::compute_driver_rpc_operation("/openshell.compute.v1.ComputeDriver/FutureMethod"), |
| 149 | + ("driver.unknown", "unknown") |
| 150 | + ); |
| 151 | + } |
| 152 | + |
60 | 153 | #[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
61 | 154 | async fn tracing_docker_driver_spans_reach_otlp_collector_with_distinct_service_name() { |
62 | 155 | let _tracing_lock = super::test_lock().await; |
|
0 commit comments