rocketmq-observability 1.0.0

Observability support for rocketmq-rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Catalog-backed error surface for observability bootstrap and shutdown.

use std::backtrace::Backtrace;
use std::error::Error as StdError;
use std::fmt;
use std::panic::Location;
use std::sync::Arc;

use rocketmq_error::fields;
use rocketmq_error::CanonicalCondition;
use rocketmq_error::DiagnosticView;
use rocketmq_error::Error as CanonicalError;
use rocketmq_error::ErrorCode;
use rocketmq_error::ErrorContext;
use rocketmq_error::ErrorDescriptor;
use rocketmq_error::PublicErrorView;
use rocketmq_error::RecoveryHint;
use rocketmq_error::SharedError;
use thiserror::Error;

/// Result returned by observability operations.
pub type ObservabilityResult<T> = std::result::Result<T, ObservabilityError>;

/// The closed observability operation retained outside catalog policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ObservabilityOperation {
    /// A requested Cargo feature is disabled.
    FeatureDisabled,
    /// Observability configuration validation.
    ValidateConfiguration,
    /// Metrics initialization.
    InitializeMetrics,
    /// Trace initialization.
    InitializeTraces,
    /// Log initialization.
    InitializeLogs,
    /// Local tracing-subscriber initialization.
    InitializeSubscriber,
    /// Log-filter validation.
    ValidateLogFilter,
    /// Global subscriber installation.
    InstallSubscriber {
        /// Whether installation was attempted.
        attempted: bool,
        /// Whether installation succeeded.
        installed: bool,
    },
    /// Metrics shutdown.
    ShutdownMetrics,
    /// Trace shutdown.
    ShutdownTraces,
    /// Log shutdown.
    ShutdownLogs,
}

/// A string-only diagnostic retained as a typed source.
///
/// The canonical error never renders this value. Code that deliberately walks
/// the source chain may inspect it for local diagnostics.
#[derive(Debug, Error)]
#[error("observability failure detail is available")]
pub struct ObservabilityFailureDetail {
    detail: String,
}

impl ObservabilityFailureDetail {
    /// Returns the original diagnostic detail.
    #[must_use]
    pub fn detail(&self) -> &str {
        &self.detail
    }
}

/// Opaque observability error backed by one canonical shared error.
#[derive(Clone)]
pub struct ObservabilityError {
    error: SharedError,
    operation: ObservabilityOperation,
}

impl ObservabilityError {
    #[track_caller]
    fn source_free(
        descriptor: &'static ErrorDescriptor,
        operation: ObservabilityOperation,
        context: ErrorContext,
    ) -> Self {
        Self {
            error: Arc::new(CanonicalError::new(descriptor).with_context(context)),
            operation,
        }
    }

    #[track_caller]
    fn caused_by(
        descriptor: &'static ErrorDescriptor,
        operation: ObservabilityOperation,
        context: ErrorContext,
        source: impl StdError + Send + Sync + 'static,
    ) -> Self {
        Self {
            error: Arc::new(CanonicalError::caused_by(descriptor, source).with_context(context)),
            operation,
        }
    }

    #[track_caller]
    fn with_detail(
        descriptor: &'static ErrorDescriptor,
        operation: ObservabilityOperation,
        context: ErrorContext,
        detail: impl ToString,
    ) -> Self {
        Self::caused_by(
            descriptor,
            operation,
            context,
            ObservabilityFailureDetail {
                detail: detail.to_string(),
            },
        )
    }

    /// Creates a failure for a requested observability feature that is disabled.
    #[must_use]
    #[track_caller]
    pub fn feature_disabled(feature: &'static str) -> Self {
        Self::source_free(
            &rocketmq_error::OBSERVABILITY_FEATURE_DISABLED,
            ObservabilityOperation::FeatureDisabled,
            ErrorContext::new().with_text(fields::FEATURE, feature),
        )
    }

    /// Creates an invalid-configuration failure.
    #[must_use]
    #[track_caller]
    pub fn invalid_config(detail: impl Into<String>) -> Self {
        Self::with_detail(
            &rocketmq_error::OBSERVABILITY_CONFIGURATION_INVALID,
            ObservabilityOperation::ValidateConfiguration,
            ErrorContext::new().with_secret_presence(fields::REASON_PRESENT),
            detail.into(),
        )
    }

    /// Creates a metrics-initialization failure from display-only detail.
    #[must_use]
    #[track_caller]
    pub fn metrics_init(detail: impl ToString) -> Self {
        Self::initialization_detail(ObservabilityOperation::InitializeMetrics, "metrics", detail)
    }

    /// Creates a metrics-initialization failure while retaining its typed source.
    #[must_use]
    #[track_caller]
    pub fn metrics_init_source(source: impl StdError + Send + Sync + 'static) -> Self {
        Self::initialization_source(ObservabilityOperation::InitializeMetrics, "metrics", source)
    }

    /// Creates a trace-initialization failure from display-only detail.
    #[must_use]
    #[track_caller]
    pub fn traces_init(detail: impl ToString) -> Self {
        Self::initialization_detail(ObservabilityOperation::InitializeTraces, "traces", detail)
    }

    /// Creates a trace-initialization failure while retaining its typed source.
    #[must_use]
    #[track_caller]
    pub fn traces_init_source(source: impl StdError + Send + Sync + 'static) -> Self {
        Self::initialization_source(ObservabilityOperation::InitializeTraces, "traces", source)
    }

    /// Creates a log-initialization failure from display-only detail.
    #[must_use]
    #[track_caller]
    pub fn logs_init(detail: impl ToString) -> Self {
        Self::initialization_detail(ObservabilityOperation::InitializeLogs, "logs", detail)
    }

    /// Creates a log-initialization failure while retaining its typed source.
    #[must_use]
    #[track_caller]
    pub fn logs_init_source(source: impl StdError + Send + Sync + 'static) -> Self {
        Self::initialization_source(ObservabilityOperation::InitializeLogs, "logs", source)
    }

    /// Creates a subscriber-initialization failure from display-only detail.
    #[must_use]
    #[track_caller]
    pub fn logging_init(detail: impl ToString) -> Self {
        Self::initialization_detail(ObservabilityOperation::InitializeSubscriber, "subscriber", detail)
    }

    /// Creates a subscriber-initialization failure while retaining its typed source.
    #[must_use]
    #[track_caller]
    pub fn logging_init_source(source: impl StdError + Send + Sync + 'static) -> Self {
        Self::initialization_source(ObservabilityOperation::InitializeSubscriber, "subscriber", source)
    }

    #[track_caller]
    fn initialization_detail(operation: ObservabilityOperation, signal: &'static str, detail: impl ToString) -> Self {
        Self::with_detail(
            &rocketmq_error::OBSERVABILITY_INITIALIZATION_FAILED,
            operation,
            observability_failure_context(signal),
            detail,
        )
    }

    #[track_caller]
    fn initialization_source(
        operation: ObservabilityOperation,
        signal: &'static str,
        source: impl StdError + Send + Sync + 'static,
    ) -> Self {
        Self::caused_by(
            &rocketmq_error::OBSERVABILITY_INITIALIZATION_FAILED,
            operation,
            observability_failure_context(signal),
            source,
        )
    }

    /// Creates an invalid-log-filter failure while retaining the parser source.
    #[must_use]
    #[track_caller]
    pub fn invalid_log_filter(_filter: impl Into<String>, source: impl StdError + Send + Sync + 'static) -> Self {
        Self::caused_by(
            &rocketmq_error::OBSERVABILITY_LOG_FILTER_INVALID,
            ObservabilityOperation::ValidateLogFilter,
            ErrorContext::new()
                .with_secret_presence(fields::FILTER_PRESENT)
                .with_secret_presence(fields::ERROR_PRESENT),
            source,
        )
    }

    /// Creates an invalid-log-filter failure from display-only detail.
    #[must_use]
    #[track_caller]
    pub fn invalid_log_filter_detail(_filter: impl Into<String>, detail: impl ToString) -> Self {
        Self::with_detail(
            &rocketmq_error::OBSERVABILITY_LOG_FILTER_INVALID,
            ObservabilityOperation::ValidateLogFilter,
            ErrorContext::new()
                .with_secret_presence(fields::FILTER_PRESENT)
                .with_secret_presence(fields::ERROR_PRESENT),
            detail,
        )
    }

    /// Creates a subscriber-installation failure.
    #[must_use]
    #[track_caller]
    pub fn subscriber_install_failed(attempted: bool, installed: bool) -> Self {
        Self::source_free(
            &rocketmq_error::OBSERVABILITY_SUBSCRIBER_INSTALLATION_FAILED,
            ObservabilityOperation::InstallSubscriber { attempted, installed },
            ErrorContext::new()
                .with_bool(fields::ATTEMPTED, attempted)
                .with_bool(fields::INSTALLED, installed),
        )
    }

    /// Creates a metrics-shutdown failure from display-only detail.
    #[must_use]
    #[track_caller]
    pub fn metrics_shutdown(detail: impl ToString) -> Self {
        Self::shutdown_detail(ObservabilityOperation::ShutdownMetrics, "metrics", detail)
    }

    /// Creates a trace-shutdown failure from display-only detail.
    #[must_use]
    #[track_caller]
    pub fn traces_shutdown(detail: impl ToString) -> Self {
        Self::shutdown_detail(ObservabilityOperation::ShutdownTraces, "traces", detail)
    }

    /// Creates a log-shutdown failure from display-only detail.
    #[must_use]
    #[track_caller]
    pub fn logs_shutdown(detail: impl ToString) -> Self {
        Self::shutdown_detail(ObservabilityOperation::ShutdownLogs, "logs", detail)
    }

    #[track_caller]
    fn shutdown_detail(operation: ObservabilityOperation, signal: &'static str, detail: impl ToString) -> Self {
        Self::with_detail(
            &rocketmq_error::OBSERVABILITY_SHUTDOWN_FAILED,
            operation,
            observability_failure_context(signal),
            detail,
        )
    }

    /// Returns the catalog descriptor that owns identity and policy.
    #[must_use]
    pub fn descriptor(&self) -> &'static ErrorDescriptor {
        self.error.descriptor()
    }

    /// Returns the stable catalog code.
    #[must_use]
    pub fn code(&self) -> ErrorCode {
        self.error.code()
    }

    /// Returns the descriptor-owned canonical condition.
    #[must_use]
    pub fn condition(&self) -> CanonicalCondition {
        self.error.condition()
    }

    /// Returns the descriptor-owned recovery hint.
    #[must_use]
    pub fn recovery_hint(&self) -> RecoveryHint {
        self.error.recovery_hint()
    }

    /// Returns the closed observability operation.
    #[must_use]
    pub const fn operation(&self) -> ObservabilityOperation {
        self.operation
    }

    /// Returns descriptor-validated bounded context.
    #[must_use]
    pub fn context(&self) -> &ErrorContext {
        self.error.context()
    }

    /// Returns the first canonical promotion location.
    #[must_use]
    pub fn location(&self) -> &'static Location<'static> {
        self.error.location()
    }

    /// Returns the catalog-controlled backtrace, when captured.
    #[must_use]
    pub fn backtrace(&self) -> Option<&Backtrace> {
        self.error.backtrace()
    }

    /// Creates a descriptor-validated public projection.
    ///
    /// # Errors
    ///
    /// Returns an error only if the facade built context that violates its
    /// selected descriptor.
    pub fn public_view(&self) -> Result<PublicErrorView<'_>, rocketmq_error::ViewContextViolation> {
        self.error.public_view()
    }

    /// Creates a descriptor-validated diagnostic projection.
    ///
    /// # Errors
    ///
    /// Returns an error only if the facade built context that violates its
    /// selected descriptor.
    pub fn diagnostic_view(&self) -> Result<DiagnosticView<'_>, rocketmq_error::ViewContextViolation> {
        self.error.diagnostic_view()
    }
}

fn observability_failure_context(signal: &'static str) -> ErrorContext {
    ErrorContext::new()
        .with_text(fields::OBSERVABILITY_SIGNAL, signal)
        .with_secret_presence(fields::REASON_PRESENT)
}

impl fmt::Display for ObservabilityError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self.error.as_ref(), formatter)
    }
}

impl fmt::Debug for ObservabilityError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("ObservabilityError")
            .field("code", &self.code())
            .field("condition", &self.condition())
            .field("operation", &self.operation)
            .field("has_source", &self.error.source().is_some())
            .finish()
    }
}

impl StdError for ObservabilityError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        self.error.source()
    }
}