Skip to content

Commit

Permalink
feat(otel): modify the span naming logic
Browse files Browse the repository at this point in the history
- Modify the span name from funcion name to the module path + function name

Signed-off-by: Jiaxiao (mossaka) Zhou <duibao55328@gmail.com>
  • Loading branch information
Mossaka committed Jan 29, 2025
1 parent 5088658 commit ca8e9f8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
37 changes: 33 additions & 4 deletions crates/containerd-shim-wasm/src/sandbox/shim/otel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ pub use opentelemetry_otlp::{
};
use opentelemetry_sdk::propagation::TraceContextPropagator;
use opentelemetry_sdk::{runtime, trace as sdktrace};
use tracing::Span;
use tracing_opentelemetry::OpenTelemetrySpanExt as _;
use tracing::span::{Attributes, Id};
use tracing::{Span, Subscriber};
use tracing_opentelemetry::{OpenTelemetrySpanExt as _, OtelData};
use tracing_subscriber::layer::SubscriberExt as _;
use tracing_subscriber::{EnvFilter, Registry};
use tracing_subscriber::{EnvFilter, Layer, Registry};

const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON: &str = "http/json";
const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF: &str = "http/protobuf";
Expand Down Expand Up @@ -91,7 +92,10 @@ impl Config {

let filter = EnvFilter::try_new("info,h2=off")?;

let subscriber = Registry::default().with(telemetry).with(filter);
let subscriber = Registry::default()
.with(telemetry)
.with(filter)
.with(SpanNamingLayer);

tracing::subscriber::set_global_default(subscriber)?;
Ok(ShutdownGuard)
Expand Down Expand Up @@ -179,6 +183,31 @@ fn traces_protocol_from_env() -> anyhow::Result<Protocol> {
Ok(protocol)
}

/// A layer that renames spans to include the target in the span name.
struct SpanNamingLayer;

impl<S> Layer<S> for SpanNamingLayer
where
S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
{
fn on_new_span(
&self,
attrs: &Attributes<'_>,
id: &Id,
ctx: tracing_subscriber::layer::Context<'_, S>,
) {
let span = ctx.span(id).expect("Span not found");
let mut extensions = span.extensions_mut();

if let Some(otel_data) = extensions.get_mut::<OtelData>() {
let target = attrs.metadata().target();
let original_name = attrs.metadata().name();
let new_name = format!("{}::{}", target, original_name);
otel_data.builder.name = new_name.into();
}
}
}

#[cfg(test)]
mod tests {
use temp_env::with_vars;
Expand Down
12 changes: 11 additions & 1 deletion scripts/verify-jaeger-traces.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ set -euo pipefail
TRACE_DATA=$(curl -s "http://localhost:16686/api/traces?service=containerd&limit=0" \
| jq '[ .data[].spans[].operationName ]')

REQUIRED_OPS=("task_create" "task_wait" "task_start" "task_delete" "task_state" "wait" "shutdown" "shim_main_inner")
REQUIRED_OPS=("task_create" "task_wait" "task_start" "task_delete" "task_state")

for op in "${REQUIRED_OPS[@]}"; do
COUNT=$(echo "$TRACE_DATA" | jq --arg op "$op" '[ .[] | select(. == "containerd_shim_wasm::sandbox::shim::local::" + $op) ] | length')
if [ "$COUNT" -eq 0 ]; then
echo "Operation '$op' not found in Jaeger!"
exit 1
fi
done

REQUIRED_OPS=( "containerd_shim_wasm::sandbox::shim::cli::wait" "containerd_shim_wasm::sandbox::shim::local::shutdown" "containerd_shim_wasm::sandbox::cli::shim_main_inner")

for op in "${REQUIRED_OPS[@]}"; do
COUNT=$(echo "$TRACE_DATA" | jq --arg op "$op" '[ .[] | select(. == $op) ] | length')
Expand Down

0 comments on commit ca8e9f8

Please sign in to comment.