Skip to content

Commit

Permalink
update (#1169)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Jul 29, 2023
1 parent 1f59a15 commit 9f28319
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion opentelemetry-user-events-metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rust-version = "1.60"
[dependencies]
opentelemetry_api = { version = "0.19", path = "../opentelemetry-api", features = ["metrics"] }
opentelemetry_sdk = { version = "0.19", path = "../opentelemetry-sdk", features = ["metrics", "rt-tokio"] }
eventheader = { version = "= 0.2.0" }
eventheader = { version = "= 0.3.1" }
opentelemetry-proto = { version = "0.2", path = "../opentelemetry-proto", features = ["gen-tonic", "metrics"] }
async-trait = "0.1"
prost = "0.11"
Expand Down
28 changes: 14 additions & 14 deletions opentelemetry-user-events-metrics/src/tracepoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,39 @@ use std::pin::Pin;
/// For this event:
///
/// - Event is named "otlp_metrics".
/// - Field 1 is named "data" and has type "variable-length array of u8".
/// - Field 1 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[] data\0";
const METRICS_EVENT_DEF: &[u8] = b"otlp_metrics __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 data and call `write()` if `enabled()`
/// check [`enabled()`] and only build the buffer and call `write()` if `enabled()`
/// returns true.
///
/// Requires: data.len() < 65536.
/// Requires: 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.
pub fn write(trace_point: &ehi::TracepointState, data: &[u8]) -> i32 {
pub fn write(trace_point: &ehi::TracepointState, buffer: &[u8]) -> i32 {
// This must stay in sync with the METRICS_EVENT_DEF string.
// Return error -1 if data exceeds max size
if data.len() > u16::MAX as usize {
eprintln!("Data exceeds max length.");
// Return error -1 if buffer exceeds max size
if buffer.len() > u16::MAX as usize {
eprintln!("Buffer exceeds max length.");
return -1;
}

// The rel_loc for the data field stores the size and offset of the data.
// - High 16 bits store the size = data.len()
// - Low 16 bits store the offset of the data from the end of the rel_loc field = 0.
let data_rel_loc: u32 = (data.len() as u32) << 16;
// 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.
let buffer_rel_loc: u32 = (buffer.len() as u32) << 16;

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::from_value(&data_rel_loc), // rel_loc for the data field.
ehi::EventDataDescriptor::from_slice(data), // data field.
ehi::EventDataDescriptor::from_value(&buffer_rel_loc), // rel_loc for the buffer field.
ehi::EventDataDescriptor::from_slice(buffer), // buffer field.
])
}

Expand Down

0 comments on commit 9f28319

Please sign in to comment.