1
1
use core:: ffi;
2
2
use eventheader:: _internal as ehi;
3
+ use opentelemetry:: { global, metrics:: MetricsError } ;
4
+ use std:: panic;
3
5
use std:: pin:: Pin ;
4
6
5
7
/// Protocol constant
@@ -68,8 +70,7 @@ pub fn write(trace_point: &ehi::TracepointState, buffer: &[u8]) -> i32 {
68
70
/// Requires: this tracepoint is not currently registered.
69
71
/// The tracepoint must be in a Pin<&TracepointState> because we must ensure it will never be moved
70
72
///
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.
73
74
///
74
75
/// # Safety
75
76
///
@@ -79,5 +80,38 @@ pub unsafe fn register(trace_point: Pin<&ehi::TracepointState>) -> i32 {
79
80
debug_assert ! ( METRICS_EVENT_DEF [ METRICS_EVENT_DEF . len( ) - 1 ] == b'\0' ) ;
80
81
81
82
// 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
+ }
83
117
}
0 commit comments