From 75ed33a6fd85fd7752a38dd1f9e7fe88fc777d00 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Fri, 11 Aug 2023 10:37:47 -0700 Subject: [PATCH] Update mod.rs --- .../src/tracepoint/mod.rs | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/opentelemetry-user-events-metrics/src/tracepoint/mod.rs b/opentelemetry-user-events-metrics/src/tracepoint/mod.rs index 5a64d73dea..d10940f5a1 100644 --- a/opentelemetry-user-events-metrics/src/tracepoint/mod.rs +++ b/opentelemetry-user-events-metrics/src/tracepoint/mod.rs @@ -2,6 +2,12 @@ use core::ffi; use eventheader::_internal as ehi; use std::pin::Pin; +/// Protocol constant +const PROTOCOL_FIELD_VALUE: u32 = 0; +/// Protobuf definition version +// const PROTOBUF_VERSION: [u8; 8] = [b'v', b'0', b'.', b'1', b'9', b'.', b'0', b'0']; +const PROTOBUF_VERSION: &[u8; 8] = b"v0.19.00"; + /// This is the command string for the event. It needs to follow the /// [Command Format](https://docs.kernel.org/trace/user_events.html#command-format) /// syntax, it needs to end with a "\0", and it needs to stay in sync with the @@ -12,18 +18,20 @@ use std::pin::Pin; /// For this event: /// /// - Event is named "otlp_metrics". -/// - Field 1 is named "buffer" and has type "variable-length array of u8". +/// - Field 1 is named "protocol". Value 0 corresponds to protobuf. +/// - Field 2 is named "version". Corresponds to protocol version (protobuf version). +/// - Field 3 is named "buffer" and has type "variable-length array of u8". /// /// "__rel_loc" is a special type for variable-length fields. It requires /// special handling in the write() method. -const METRICS_EVENT_DEF: &[u8] = b"otlp_metrics __rel_loc u8[] buffer\0"; +const METRICS_EVENT_DEF: &[u8] = b"otlp_metrics u32 protocol;char[8] version;__rel_loc u8[] buffer;\0"; /// If the tracepoint is registered and enabled, writes an event. If the tracepoint /// is unregistered or disabled, this does nothing and returns 0. You should usually /// check [`enabled()`] and only build the buffer and call `write()` if `enabled()` /// returns true. /// -/// Requires: buffer.len() < 65536. +/// Requires: PROTOBUF_VERSION.len() == 8, buffer.len() < 65536. /// /// Return value is 0 for success or an errno code for error. The return value is /// provided to help with debugging and should usually be ignored in release builds. @@ -35,6 +43,11 @@ pub fn write(trace_point: &ehi::TracepointState, buffer: &[u8]) -> i32 { return -1; } + if PROTOBUF_VERSION.len() != 8 { + eprintln!("Version must be char[8]."); + return -1; + } + // The rel_loc for the buffer field stores the size and offset of the buffer. // - High 16 bits store the size = buffer.len() // - Low 16 bits store the offset of the buffer from the end of the rel_loc field = 0. @@ -42,7 +55,9 @@ pub fn write(trace_point: &ehi::TracepointState, buffer: &[u8]) -> i32 { trace_point.write(&mut [ // mut because the write method does some fix-ups. - ehi::EventDataDescriptor::zero(), // First item in array MUST be zero(). + ehi::EventDataDescriptor::zero(), // First item before buffer MUST be zero(). + ehi::EventDataDescriptor::from_value(&PROTOCOL_FIELD_VALUE), // protocol value 0 for protobuf + ehi::EventDataDescriptor::from_slice(PROTOBUF_VERSION), // protobuf definition version ehi::EventDataDescriptor::from_value(&buffer_rel_loc), // rel_loc for the buffer field. ehi::EventDataDescriptor::from_slice(buffer), // buffer field. ])