Skip to content

Commit b50f550

Browse files
committed
feat(o11y): add error extraction to DiscoveryOperation
1 parent fd386b3 commit b50f550

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

src/lro/src/internal/discovery.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::{
2727
Poller, PollingBackoffPolicy, PollingErrorPolicy, PollingResult, Result,
2828
sealed::Poller as SealedPoller,
2929
};
30+
use google_cloud_gax::error::rpc::Status;
3031
use google_cloud_gax::polling_state::PollingState;
3132
use google_cloud_gax::retry_result::RetryResult;
3233
use std::sync::Arc;
@@ -54,6 +55,11 @@ pub trait DiscoveryOperation {
5455
///
5556
/// It may be `None` in which case the polling loop stops.
5657
fn name(&self) -> Option<&String>;
58+
59+
/// Returns the error status of the operation, if any.
60+
fn error(&self) -> Option<Status> {
61+
None
62+
}
5763
}
5864

5965
pub fn new_discovery_poller<S, SF, Q, QF, O>(

src/lro/src/internal/tracing.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,30 @@ impl LroRecorder {
6767
pub fn attempt_count(&self) -> Option<u32> {
6868
self.attempt_count
6969
}
70+
}
7071

72+
/// Helper macro to record telemetry for Discovery LROs.
73+
#[macro_export]
74+
#[doc(hidden)]
75+
macro_rules! record_discovery_polling_result {
76+
($span:expr, $op:expr) => {
77+
let done = $crate::internal::DiscoveryOperation::done($op);
78+
$span.record("gcp.longrunning.done", done);
79+
if done {
80+
let error = $crate::internal::DiscoveryOperation::error($op);
81+
let code = error.as_ref().map(|e| e.code as i32).unwrap_or(0);
82+
$span.record("gcp.longrunning.status_code", code);
83+
if let Some(status) = error {
84+
$span.record("otel.status_code", "ERROR");
85+
$span.record("otel.status_description", &status.message);
86+
$span.record("rpc.response.status_code", status.code as i32);
87+
$span.record("error.type", status.code.to_string());
88+
}
89+
}
90+
};
91+
}
92+
93+
impl LroRecorder {
7194
/// Creates a new clone of `LroRecorder` carrying the specified LRO polling attempt count.
7295
///
7396
/// Since `LroRecorder` is immutable to guarantee thread-safety, this updates the context
@@ -105,10 +128,12 @@ macro_rules! record_polling_attributes {
105128
($span:expr) => {
106129
#[cfg(google_cloud_unstable_tracing)]
107130
{
108-
if let Ok(attempt) = $crate::POLL_ATTEMPT_COUNT.try_with(|c| *c) {
109-
let span = &$span;
110-
span.record("gcp.longrunning.poll_attempt_count", attempt);
111-
span.record("gcp.longrunning.done", false);
131+
if let Some(recorder) = $crate::LroRecorder::current() {
132+
if let Some(attempt) = recorder.attempt_count() {
133+
let span = &$span;
134+
span.record("gcp.longrunning.poll_attempt_count", attempt);
135+
span.record("gcp.longrunning.done", false);
136+
}
112137
}
113138
}
114139
#[cfg(not(google_cloud_unstable_tracing))]

0 commit comments

Comments
 (0)