Skip to content

Commit

Permalink
Update user_events metrics exporter to reflect new schema (#1224)
Browse files Browse the repository at this point in the history
* cli

* update

* Update mod.rs

* lint
  • Loading branch information
lzchen authored Aug 24, 2023
1 parent 759556d commit b22f7d1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions opentelemetry-user-events-metrics/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Changed

- Add version, protocol to schema
[#1224](https://github.com/open-telemetry/opentelemetry-rust/pull/1224).

## v0.1.0

### Added
Expand Down
25 changes: 20 additions & 5 deletions opentelemetry-user-events-metrics/src/tracepoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ 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"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
Expand All @@ -12,18 +17,21 @@ 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.
Expand All @@ -35,16 +43,23 @@ 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.
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::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.
ehi::EventDataDescriptor::from_slice(buffer), // buffer field.
])
}

Expand Down

0 comments on commit b22f7d1

Please sign in to comment.