1#![doc = include_str!("../README.md")]
19#![cfg_attr(docsrs, feature(doc_cfg))]
20#![cfg_attr(docsrs, doc(auto_cfg))]
21#![deny(missing_docs)]
22use std::sync::Arc;
23
24use opendal_core::raw::*;
25use opendal_core::*;
26use opentelemetry::Context as TraceContext;
27use opentelemetry::KeyValue;
28use opentelemetry::global;
29use opentelemetry::trace::FutureExt as TraceFutureExt;
30use opentelemetry::trace::Span;
31use opentelemetry::trace::TraceContextExt;
32use opentelemetry::trace::Tracer;
33
34#[derive(Clone, Debug, Default)]
63#[non_exhaustive]
64pub struct OtelTraceLayer {}
65
66impl OtelTraceLayer {
67 pub fn new() -> Self {
69 Self::default()
70 }
71}
72
73impl Layer for OtelTraceLayer {
74 fn apply_service(&self, inner: Servicer) -> Servicer {
75 Arc::new(self.layer(inner))
76 }
77}
78
79impl OtelTraceLayer {
80 fn layer(&self, inner: Servicer) -> OtelTraceService {
81 OtelTraceService { inner }
82 }
83}
84
85#[doc(hidden)]
86#[derive(Debug)]
87pub struct OtelTraceService {
88 inner: Servicer,
89}
90
91impl Service for OtelTraceService {
92 type Reader = OtelTraceWrapper<oio::Reader>;
93 type Writer = OtelTraceWrapper<oio::Writer>;
94 type Lister = OtelTraceWrapper<oio::Lister>;
95 type Deleter = oio::Deleter;
96 type Copier = oio::Copier;
97
98 fn info(&self) -> ServiceInfo {
99 let tracer = global::tracer("opendal");
100 tracer.in_span("info", |_cx| self.inner.info())
101 }
102
103 fn capability(&self) -> Capability {
104 self.inner.capability()
105 }
106
107 async fn create_dir(
108 &self,
109 ctx: &OperationContext,
110 path: &str,
111 args: OpCreateDir,
112 ) -> Result<RpCreateDir> {
113 let tracer = global::tracer("opendal");
114 let mut span = tracer.start("create");
115 span.set_attribute(KeyValue::new("path", path.to_string()));
116 span.set_attribute(KeyValue::new("args", format!("{args:?}")));
117 let cx = TraceContext::current_with_span(span);
118 self.inner
119 .create_dir(ctx, path, args)
120 .with_context(cx)
121 .await
122 }
123
124 fn read(&self, ctx: &OperationContext, path: &str, args: OpRead) -> Result<Self::Reader> {
125 let tracer = global::tracer("opendal");
126 let mut span = tracer.start("read");
127 span.set_attribute(KeyValue::new("path", path.to_string()));
128 span.set_attribute(KeyValue::new("args", format!("{args:?}")));
129 let cx = TraceContext::current_with_span(span);
130 self.inner
131 .read(ctx, path, args)
132 .map(|r| OtelTraceWrapper::new(cx, r))
133 }
134
135 fn write(&self, ctx: &OperationContext, path: &str, args: OpWrite) -> Result<Self::Writer> {
136 let tracer = global::tracer("opendal");
137 let mut span = tracer.start("write");
138 span.set_attribute(KeyValue::new("path", path.to_string()));
139 span.set_attribute(KeyValue::new("args", format!("{args:?}")));
140 let cx = TraceContext::current_with_span(span);
141 self.inner
142 .write(ctx, path, args)
143 .map(|r| OtelTraceWrapper::new(cx, r))
144 }
145
146 fn copy(
147 &self,
148 ctx: &OperationContext,
149 from: &str,
150 to: &str,
151 args: OpCopy,
152 opts: OpCopier,
153 ) -> Result<Self::Copier> {
154 let tracer = global::tracer("opendal");
155 let mut span = tracer.start("copy");
156 span.set_attribute(KeyValue::new("from", from.to_string()));
157 span.set_attribute(KeyValue::new("to", to.to_string()));
158 span.set_attribute(KeyValue::new("args", format!("{args:?}")));
159 let cx = TraceContext::current_with_span(span);
160 let _guard = cx.attach();
161 self.inner.copy(ctx, from, to, args, opts)
162 }
163
164 async fn rename(
165 &self,
166 ctx: &OperationContext,
167 from: &str,
168 to: &str,
169 args: OpRename,
170 ) -> Result<RpRename> {
171 let tracer = global::tracer("opendal");
172 let mut span = tracer.start("rename");
173 span.set_attribute(KeyValue::new("from", from.to_string()));
174 span.set_attribute(KeyValue::new("to", to.to_string()));
175 span.set_attribute(KeyValue::new("args", format!("{args:?}")));
176 let cx = TraceContext::current_with_span(span);
177 self.inner
178 .rename(ctx, from, to, args)
179 .with_context(cx)
180 .await
181 }
182
183 async fn stat(&self, ctx: &OperationContext, path: &str, args: OpStat) -> Result<RpStat> {
184 let tracer = global::tracer("opendal");
185 let mut span = tracer.start("stat");
186 span.set_attribute(KeyValue::new("path", path.to_string()));
187 span.set_attribute(KeyValue::new("args", format!("{args:?}")));
188 let cx = TraceContext::current_with_span(span);
189 self.inner.stat(ctx, path, args).with_context(cx).await
190 }
191
192 fn delete(&self, ctx: &OperationContext) -> Result<Self::Deleter> {
193 self.inner.delete(ctx)
194 }
195
196 fn list(&self, ctx: &OperationContext, path: &str, args: OpList) -> Result<Self::Lister> {
197 let tracer = global::tracer("opendal");
198 let mut span = tracer.start("list");
199 span.set_attribute(KeyValue::new("path", path.to_string()));
200 span.set_attribute(KeyValue::new("args", format!("{args:?}")));
201 let cx = TraceContext::current_with_span(span);
202 self.inner
203 .list(ctx, path, args)
204 .map(|s| OtelTraceWrapper::new(cx, s))
205 }
206
207 async fn presign(
208 &self,
209 ctx: &OperationContext,
210 path: &str,
211 args: OpPresign,
212 ) -> Result<RpPresign> {
213 let tracer = global::tracer("opendal");
214 let mut span = tracer.start("presign");
215 span.set_attribute(KeyValue::new("path", path.to_string()));
216 span.set_attribute(KeyValue::new("args", format!("{args:?}")));
217 let cx = TraceContext::current_with_span(span);
218 self.inner.presign(ctx, path, args).with_context(cx).await
219 }
220}
221
222#[doc(hidden)]
223pub struct OtelTraceWrapper<R> {
224 cx: TraceContext,
225 inner: R,
226}
227
228impl<R> OtelTraceWrapper<R> {
229 fn new(cx: TraceContext, inner: R) -> Self {
230 Self { cx, inner }
231 }
232
233 fn child_context(&self, name: &'static str, range: BytesRange) -> TraceContext {
234 let tracer = global::tracer("opendal");
235 let mut span = tracer.start_with_context(name, &self.cx);
236 span.set_attribute(KeyValue::new("range", range.to_string()));
237 self.cx.with_span(span)
238 }
239}
240
241impl<R: oio::ReadStream> oio::ReadStream for OtelTraceWrapper<R> {
242 async fn read(&mut self) -> Result<Buffer> {
243 self.inner.read().with_context(self.cx.clone()).await
244 }
245}
246
247impl<R: oio::Read> oio::Read for OtelTraceWrapper<R> {
248 async fn open(&self, range: BytesRange) -> Result<(RpRead, Box<dyn oio::ReadStreamDyn>)> {
249 let cx = self.child_context("reader.open", range);
250 let (rp, stream) = self.inner.open(range).with_context(cx.clone()).await?;
251 Ok((
252 rp,
253 Box::new(OtelTraceWrapper::new(cx, stream)) as Box<dyn oio::ReadStreamDyn>,
254 ))
255 }
256
257 async fn read(&self, range: BytesRange) -> Result<(RpRead, Buffer)> {
258 let cx = self.child_context("reader.read", range);
259 self.inner.read(range).with_context(cx).await
260 }
261}
262
263impl<R: oio::Write> oio::Write for OtelTraceWrapper<R> {
264 async fn write(&mut self, bs: Buffer) -> Result<()> {
265 self.inner.write(bs).with_context(self.cx.clone()).await
266 }
267
268 async fn abort(&mut self) -> Result<()> {
269 self.inner.abort().with_context(self.cx.clone()).await
270 }
271
272 async fn close(&mut self) -> Result<Metadata> {
273 self.inner.close().with_context(self.cx.clone()).await
274 }
275}
276
277impl<R: oio::List> oio::List for OtelTraceWrapper<R> {
278 async fn next(&mut self) -> Result<Option<oio::Entry>> {
279 self.inner.next().with_context(self.cx.clone()).await
280 }
281}