Skip to content

Commit df2551b

Browse files
authored
Include diagnosing error messages for userevents metrics exporter (#1273)
1 parent 6713143 commit df2551b

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

opentelemetry-user-events-metrics/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Changed
66

7+
- Include error diagnosing messages for registering tracepoint
8+
[#1273](https://github.com/open-telemetry/opentelemetry-rust/pull/1273).
79
- Add version, protocol to schema
810
[#1224](https://github.com/open-telemetry/opentelemetry-rust/pull/1224).
911

opentelemetry-user-events-metrics/src/tracepoint/mod.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use core::ffi;
22
use eventheader::_internal as ehi;
3+
use opentelemetry::{global, metrics::MetricsError};
4+
use std::panic;
35
use std::pin::Pin;
46

57
/// Protocol constant
@@ -68,8 +70,7 @@ pub fn write(trace_point: &ehi::TracepointState, buffer: &[u8]) -> i32 {
6870
/// Requires: this tracepoint is not currently registered.
6971
/// The tracepoint must be in a Pin<&TracepointState> because we must ensure it will never be moved
7072
///
71-
/// Return value is 0 for success or an errno code for error. The return value is
72-
/// provided to help with debugging and should usually be ignored in release builds.
73+
/// Return value is 0 for success or -1 for failed register.
7374
///
7475
/// # Safety
7576
///
@@ -79,5 +80,38 @@ pub unsafe fn register(trace_point: Pin<&ehi::TracepointState>) -> i32 {
7980
debug_assert!(METRICS_EVENT_DEF[METRICS_EVENT_DEF.len() - 1] == b'\0');
8081

8182
// CStr::from_bytes_with_nul_unchecked is ok because METRICS_EVENT_DEF ends with "\0".
82-
trace_point.register(ffi::CStr::from_bytes_with_nul_unchecked(METRICS_EVENT_DEF))
83+
// Returns errno code 95 if trace/debug file systems are not mounted
84+
// Returns errno code 13 if insufficient permissions
85+
// If tracepoint doesn't exist, it will create one automatically
86+
let result = panic::catch_unwind(|| {
87+
// CStr::from_bytes_with_nul_unchecked is ok because METRICS_EVENT_DEF ends with "\0".
88+
trace_point.register(ffi::CStr::from_bytes_with_nul_unchecked(METRICS_EVENT_DEF))
89+
});
90+
91+
match result {
92+
Ok(value) => {
93+
if value == 0 {
94+
// Temporary print as a measure for quick testing
95+
// will be replaced with proper logging mechanism
96+
println!("Tracepoint registered successfully.")
97+
} else if value == 95 {
98+
global::handle_error(MetricsError::Other(
99+
"Trace/debug file systems are not mounted.".into(),
100+
));
101+
} else if value == 13 {
102+
global::handle_error(MetricsError::Other(
103+
"Insufficient permissions. You need read/write/execute permissions to user_events tracing directory.".into(),
104+
));
105+
}
106+
value
107+
}
108+
// We don't want to ever panic so we catch the error and return a unique code for retry
109+
Err(err) => {
110+
global::handle_error(MetricsError::Other(format!(
111+
"Tracepoint failed to register: {:?}.",
112+
err,
113+
)));
114+
-1
115+
}
116+
}
83117
}

0 commit comments

Comments
 (0)