Skip to content

Commit 83a1544

Browse files
authored
minor: tidy up opentelemetry code (#1515)
1 parent 59cab56 commit 83a1544

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

src/client/executor.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use crate::bson::RawDocumentBuf;
33
use crate::bson::{doc, RawBsonRef, RawDocument, Timestamp};
44
#[cfg(feature = "in-use-encryption")]
55
use futures_core::future::BoxFuture;
6-
#[cfg(feature = "opentelemetry")]
7-
use opentelemetry::context::FutureExt;
86
use serde::de::DeserializeOwned;
97
use std::sync::LazyLock;
108

@@ -16,8 +14,6 @@ use std::{
1614
};
1715

1816
use super::{options::ServerAddress, session::TransactionState, Client, ClientSession};
19-
#[cfg(not(feature = "opentelemetry"))]
20-
use crate::otel::OtelFutureStub as _;
2117
use crate::{
2218
bson::Document,
2319
change_stream::{
@@ -105,19 +101,30 @@ impl Client {
105101
.map(|details| details.output)
106102
}
107103

104+
#[cfg(not(feature = "opentelemetry"))]
108105
async fn execute_operation_with_details<T: Operation>(
109106
&self,
110107
op: &mut T,
111108
session: impl Into<Option<&mut ClientSession>>,
112109
) -> Result<ExecutionDetails<T>> {
110+
self.execute_operation_with_details_inner(op, session.into())
111+
.await
112+
}
113+
114+
#[cfg(feature = "opentelemetry")]
115+
async fn execute_operation_with_details<T: Operation>(
116+
&self,
117+
op: &mut T,
118+
session: impl Into<Option<&mut ClientSession>>,
119+
) -> Result<ExecutionDetails<T>> {
120+
use crate::otel::FutureExt as _;
121+
113122
let session = session.into();
114-
#[cfg(feature = "opentelemetry")]
115123
let span = self.start_operation_span(op, session.as_deref());
116-
let inner = self.execute_operation_with_details_inner(op, session);
117-
#[cfg(feature = "opentelemetry")]
118-
let inner = inner.with_context(span.context.clone());
119-
let result = inner.await;
120-
#[cfg(feature = "opentelemetry")]
124+
let result = self
125+
.execute_operation_with_details_inner(op, session)
126+
.with_span(&span)
127+
.await;
121128
span.record_error(&result);
122129

123130
result
@@ -176,13 +183,7 @@ impl Client {
176183
}
177184
}
178185

179-
Box::pin(async {
180-
self.execute_operation_with_retry(op, session)
181-
.with_current_context()
182-
.await
183-
})
184-
.with_current_context()
185-
.await
186+
Box::pin(async { self.execute_operation_with_retry(op, session).await }).await
186187
}
187188

188189
/// Execute the given operation, returning the cursor created by the operation.
@@ -436,7 +437,6 @@ impl Client {
436437
retryability,
437438
effective_criteria,
438439
)
439-
.with_current_context()
440440
.await
441441
{
442442
Ok(output) => ExecutionDetails {

src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ mod index;
4747
mod operation;
4848
#[cfg(feature = "opentelemetry")]
4949
pub mod otel;
50-
#[cfg(not(feature = "opentelemetry"))]
51-
mod otel_stub;
5250
pub mod results;
5351
pub(crate) mod runtime;
5452
mod sdam;
@@ -73,9 +71,6 @@ pub use bson2 as bson;
7371
#[cfg(feature = "bson-3")]
7472
pub use bson3 as bson;
7573

76-
#[cfg(not(feature = "opentelemetry"))]
77-
pub(crate) use otel_stub as otel;
78-
7974
#[cfg(feature = "in-use-encryption")]
8075
pub use crate::client::csfle::client_encryption;
8176
pub use crate::{

src/otel.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Support for OpenTelemetry.
22
3-
use std::sync::{Arc, LazyLock};
3+
use std::{
4+
future::Future,
5+
sync::{Arc, LazyLock},
6+
};
47

58
use derive_where::derive_where;
69

@@ -229,7 +232,7 @@ impl Client {
229232
}
230233

231234
pub(crate) struct OpSpan {
232-
pub(crate) context: Context,
235+
context: Context,
233236
enabled: bool,
234237
}
235238

@@ -443,3 +446,12 @@ impl<'a> From<&'a AggregateTarget> for OperationTarget<'a> {
443446
}
444447
}
445448
}
449+
450+
pub(crate) trait FutureExt: Future + Sized {
451+
fn with_span(self, span: &OpSpan) -> impl Future<Output = Self::Output> {
452+
use opentelemetry::context::FutureExt;
453+
self.with_context(span.context.clone())
454+
}
455+
}
456+
457+
impl<T: Future> FutureExt for T {}

src/otel_stub.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)