if current Span exist trace_id
,does reqwest-middleware extend Current span trace_id?
E.g:
use reqwest_tracing::{default_on_request_end, reqwest_otel_span, ReqwestOtelSpanBackend, TracingMiddleware};
use opentelemetry::sdk::export::trace::stdout;
use reqwest::{Request, Response};
use reqwest_middleware::{ClientBuilder, Result};
use std::time::Instant;
use task_local_extensions::Extensions;
use tracing::{info_span, Span};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::Registry;
pub struct TimeTrace;
impl ReqwestOtelSpanBackend for TimeTrace {
fn on_request_start(req: &Request, extension: &mut Extensions) -> Span {
extension.insert(Instant::now());
reqwest_otel_span!(req, time_elapsed = tracing::field::Empty)
}
fn on_request_end(span: &Span, outcome: &Result<Response>, extension: &mut Extensions) {
let time_elapsed = extension.get::<Instant>().unwrap().elapsed().as_millis() as i64;
default_on_request_end(span, outcome);
span.record("time_elapsed", &time_elapsed);
}
}
#[tokio::main]
async fn main() {
let tracer = stdout::new_pipeline().install_simple();
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
let subscriber = Registry::default().with(telemetry);
tracing::subscriber::set_global_default(subscriber).unwrap();
+ let span = info_span!("trace_id","1234");
let span = span.enter();
run().await;
}
async fn run() {
let span = Span::current();
+ println!("--->{:?}",span);
let client = ClientBuilder::new(reqwest::Client::new())
.with(TracingMiddleware::default())
.build();
+ client.get("https://truelayer.com").send().await.unwrap();
}
--->Span { name: "trace_id", level: Level(Info), target: "demo", id: Id(1), module_path: "demo", line: 33, file: "src/main.rs" }
SpanData { span_context: SpanContext { trace_id: 9b94f068662cee9c5076363a8896a114, span_id: 49ae88e2b0dcbef2, trace_flags: TraceFlags(1), is_remote: false, trace_state: TraceState(None) }, parent_span_id: 0000000000000000, span_kind: Internal, name: "encode_headers", start_time: SystemTime { tv_sec: 1668667233, tv_nsec: 334094000 }, end_time: SystemTime { tv_sec: 1668667233, tv_nsec: 334356000 },...
SpanData { span_context: SpanContext { trace_id: b318146ecd8c4380ebce06a05d77f0f8, span_id: 0c389105b17853b6, trace_flags: TraceFlags(1), is_remote: false, trace_state: TraceState(None) }, parent_span_id: 0000000000000000, span_kind: Internal, name: "parse_headers", start_time: SystemTime { tv_sec: 1668667233, tv_nsec: 594372000 }, end_time: SystemTime { tv_sec: 1668667233, tv_nsec: 594609000 },...
SpanData { span_context: SpanContext { trace_id: 5d0aaf9994579e8efc422e0fb9016508, span_id: d169884eca2ec6b0, trace_flags: TraceFlags(1), is_remote: false, trace_state: TraceState(None) }, parent_span_id: 0000000000000000, span_kind: Internal, name: "parse_headers", start_time: SystemTime { tv_sec: 1668667233, tv_nsec: 594712000 }, end_time: SystemTime { tv_sec: 1668667233, tv_nsec: 595089000 },...
SpanData { span_context: SpanContext { trace_id: 1234, span_id: 49ae88e2b0dcbef2, trace_flags: TraceFlags(1), is_remote: false, trace_state: TraceState(None) }, parent_span_id: 0000000000000000, span_kind: Internal, name: "encode_headers", start_time: SystemTime { tv_sec: 1668667233, tv_nsec: 334094000 }, end_time: SystemTime { tv_sec: 1668667233, tv_nsec: 334356000 },...
SpanData { span_context: SpanContext { trace_id: 1234, span_id: 0c389105b17853b6, trace_flags: TraceFlags(1), is_remote: false, trace_state: TraceState(None) }, parent_span_id: 0000000000000000, span_kind: Internal, name: "parse_headers", start_time: SystemTime { tv_sec: 1668667233, tv_nsec: 594372000 }, end_time: SystemTime { tv_sec: 1668667233, tv_nsec: 594609000 },...
...
enhancement