use super::either::Either;
use super::tracing::Tracing;
use crate::Poller;
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct TracingDetails {
pub method_name: &'static str,
}
#[derive(Default)]
#[non_exhaustive]
pub struct PollerOptions {
pub tracing: Option<TracingDetails>,
}
pub trait PollerExt<ResponseType, MetadataType> {
fn with_options(self, options: PollerOptions) -> impl Poller<ResponseType, MetadataType>;
}
impl<ResponseType, MetadataType, T> PollerExt<ResponseType, MetadataType> for T
where
T: Poller<ResponseType, MetadataType>,
ResponseType: Send,
MetadataType: Send,
{
fn with_options(self, options: PollerOptions) -> impl Poller<ResponseType, MetadataType> {
if let Some(t) = options.tracing {
let method_name = if t.method_name.is_empty() {
"google_longrunning::Operations/Wait"
} else {
t.method_name
};
let span = tracing::info_span!(
"LRO Wait",
"otel.name" = method_name,
"gcp.rpc.method" = method_name,
"gcp.resource.destination.id" = tracing::field::Empty,
"otel.status_code" = tracing::field::Empty,
"otel.status_description" = tracing::field::Empty
);
let traced = Tracing::new(self, span);
return Either::Right(traced);
}
Either::Left(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{PollingResult, sealed};
use google_cloud_gax::polling_state::PollingState;
use google_cloud_wkt::{Duration, Timestamp};
use mockall::mock;
type ResponseType = Duration;
type MetadataType = Timestamp;
mock! {
PollerA {}
impl sealed::Poller for PollerA {
async fn backoff(&mut self, state: &PollingState);
}
impl Poller<ResponseType, MetadataType> for PollerA {
async fn poll(&mut self) -> Option<PollingResult<ResponseType, MetadataType>>;
async fn until_done(self) -> google_cloud_gax::Result<ResponseType>;
#[cfg(feature = "unstable-stream")]
fn into_stream(
self,
) -> impl futures::Stream<Item = PollingResult<ResponseType, MetadataType>> + Unpin;
}
}
#[cfg(google_cloud_unstable_tracing)]
#[test]
fn test_poller_initialization_with_tracing() {
let mock = MockPollerA::new();
let _poller = mock.with_options(PollerOptions {
tracing: Some(TracingDetails {
method_name: "test_method",
}),
});
}
}